Improve API
This commit is contained in:
parent
b2c2cfcb41
commit
78444d8cc6
24 changed files with 193 additions and 325 deletions
|
|
@ -2,7 +2,11 @@ import { Drawable } from './drawable';
|
|||
|
||||
export interface DrawableDescriptor {
|
||||
uniformName: string;
|
||||
countMacroName: string;
|
||||
uniformCountMacroName: string;
|
||||
sdf?: {
|
||||
shader: string;
|
||||
distanceFunctionName: string;
|
||||
};
|
||||
shaderCombinationSteps: Array<number>;
|
||||
readonly empty: Drawable;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,9 +7,20 @@ export abstract class Drawable {
|
|||
}
|
||||
|
||||
public abstract distance(target: vec2): number;
|
||||
public abstract serializeToUniforms(
|
||||
|
||||
protected abstract getObjectToSerialize(transform2d: mat2d, transform1d: number): any;
|
||||
|
||||
public serializeToUniforms(
|
||||
uniforms: any,
|
||||
scale: number,
|
||||
transform: mat2d
|
||||
): void;
|
||||
transform2d: mat2d,
|
||||
transform1d: number
|
||||
): void {
|
||||
const { uniformName } = (this.constructor as typeof Drawable).descriptor;
|
||||
|
||||
if (!Object.prototype.hasOwnProperty.call(uniforms, uniformName)) {
|
||||
uniforms[uniformName] = [];
|
||||
}
|
||||
|
||||
uniforms[uniformName].push(this.getObjectToSerialize(transform2d, transform1d));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ export class CircleLight extends Drawable {
|
|||
public static get descriptor(): DrawableDescriptor {
|
||||
return {
|
||||
uniformName: 'circleLights',
|
||||
countMacroName: 'circleLightCount',
|
||||
uniformCountMacroName: 'CIRCLE_LIGHT_COUNT',
|
||||
shaderCombinationSteps: settings.shaderCombinations.circleLightSteps,
|
||||
empty: new CircleLight(vec2.fromValues(0, 0), 0, vec3.fromValues(0, 0, 0), 0),
|
||||
};
|
||||
|
|
@ -26,18 +26,12 @@ export class CircleLight extends Drawable {
|
|||
return 0;
|
||||
}
|
||||
|
||||
public serializeToUniforms(uniforms: any, scale: number, transform: mat2d): void {
|
||||
const { uniformName } = CircleLight.descriptor;
|
||||
|
||||
if (!Object.prototype.hasOwnProperty.call(uniforms, uniformName)) {
|
||||
uniforms[uniformName] = [];
|
||||
}
|
||||
|
||||
uniforms[uniformName].push({
|
||||
center: vec2.transformMat2d(vec2.create(), this.center, transform),
|
||||
protected getObjectToSerialize(transform2d: mat2d, transform1d: number): any {
|
||||
return {
|
||||
center: vec2.transformMat2d(vec2.create(), this.center, transform2d),
|
||||
lightDrop: this.lightDrop,
|
||||
value: this.value,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
public get value(): vec3 {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ export class Flashlight extends Drawable {
|
|||
public static get descriptor(): DrawableDescriptor {
|
||||
return {
|
||||
uniformName: 'flashlights',
|
||||
countMacroName: 'flashlightCount',
|
||||
uniformCountMacroName: 'FLASHLIGHT_COUNT',
|
||||
shaderCombinationSteps: settings.shaderCombinations.flashlightSteps,
|
||||
empty: new Flashlight(
|
||||
vec2.fromValues(0, 0),
|
||||
|
|
@ -33,19 +33,13 @@ export class Flashlight extends Drawable {
|
|||
return 0;
|
||||
}
|
||||
|
||||
public serializeToUniforms(uniforms: any, scale: number, transform: mat2d): void {
|
||||
const listName = Flashlight.descriptor.uniformName;
|
||||
|
||||
if (!Object.prototype.hasOwnProperty.call(uniforms, listName)) {
|
||||
uniforms[listName] = [];
|
||||
}
|
||||
|
||||
uniforms[listName].push({
|
||||
center: vec2.transformMat2d(vec2.create(), this.center, transform),
|
||||
protected getObjectToSerialize(transform2d: mat2d, transform1d: number): any {
|
||||
return {
|
||||
center: vec2.transformMat2d(vec2.create(), this.center, transform2d),
|
||||
direction: this.direction,
|
||||
lightDrop: this.lightDrop,
|
||||
value: this.value,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
public get value(): vec3 {
|
||||
|
|
|
|||
|
|
@ -1,14 +1,29 @@
|
|||
import { mat2d, vec2 } from 'gl-matrix';
|
||||
import { settings } from '../../graphics/settings';
|
||||
import { Drawable } from '../drawable';
|
||||
import { DrawableDescriptor } from '../drawable-descriptor';
|
||||
|
||||
export class Circle extends Drawable {
|
||||
public static get descriptor(): DrawableDescriptor {
|
||||
return {
|
||||
sdf: {
|
||||
shader: `
|
||||
uniform struct {
|
||||
vec2 center;
|
||||
float radius;
|
||||
}[CIRCLE_COUNT] circles;
|
||||
|
||||
void circleMinDistance(inout float minDistance, inout float color) {
|
||||
for (int i = 0; i < CIRCLE_COUNT; i++) {
|
||||
float dist = distance(circles[i].center, position) - circles[i].radius;
|
||||
minDistance = min(minDistance, dist);
|
||||
}
|
||||
}
|
||||
`,
|
||||
distanceFunctionName: 'circleMinDistance',
|
||||
},
|
||||
uniformName: 'circles',
|
||||
countMacroName: 'circleCount',
|
||||
shaderCombinationSteps: settings.shaderCombinations.circleSteps,
|
||||
uniformCountMacroName: 'CIRCLE_COUNT',
|
||||
shaderCombinationSteps: [0, 1, 2, 3, 16, 32],
|
||||
empty: new Circle(vec2.fromValues(0, 0), 0),
|
||||
};
|
||||
}
|
||||
|
|
@ -18,19 +33,13 @@ export class Circle extends Drawable {
|
|||
}
|
||||
|
||||
public distance(position: vec2): number {
|
||||
return 0;
|
||||
return vec2.dist(this.center, position) - this.radius;
|
||||
}
|
||||
|
||||
public serializeToUniforms(uniforms: any, scale: number, transform: mat2d): void {
|
||||
const { uniformName } = Circle.descriptor;
|
||||
|
||||
if (!Object.prototype.hasOwnProperty.call(uniforms, uniformName)) {
|
||||
uniforms[uniformName] = [];
|
||||
}
|
||||
|
||||
uniforms[uniformName].push({
|
||||
center: vec2.transformMat2d(vec2.create(), this.center, transform),
|
||||
radius: this.radius * scale,
|
||||
});
|
||||
protected getObjectToSerialize(transform2d: mat2d, transform1d: number): any {
|
||||
return {
|
||||
center: vec2.transformMat2d(vec2.create(), this.center, transform2d),
|
||||
radius: this.radius * transform1d,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue