Tidy up object count handling

This commit is contained in:
schmelczerandras 2020-10-12 14:21:43 +02:00
parent 63a1772afa
commit c158f8655b
7 changed files with 30 additions and 31 deletions

View file

@ -47,6 +47,15 @@ export interface DrawableDescriptor {
isInverted?: boolean; isInverted?: boolean;
}; };
/**
* Calculating the number of objects to be drawn is done using the following pseudo-formula:
*
* objectCountScaler * propertyUniformMapping[0].length
*
* By default, its value is 1
*/
objectCountScaler?: number;
/** /**
* Number of possible drawables around each tile. * Number of possible drawables around each tile.
* *

View file

@ -14,7 +14,7 @@ export class CircleLight extends LightDrawable {
}, },
uniformCountMacroName: 'CIRCLE_LIGHT_COUNT', uniformCountMacroName: 'CIRCLE_LIGHT_COUNT',
shaderCombinationSteps: [0, 1, 2, 4, 8, 16], shaderCombinationSteps: [0, 1, 2, 4, 8, 16],
empty: new CircleLight(vec2.fromValues(0, 0), vec3.fromValues(0, 0, 0), 0), empty: new CircleLight(vec2.create(), vec3.create(), 0),
}; };
constructor(center: vec2, color: vec3, intensity: number) { constructor(center: vec2, color: vec3, intensity: number) {

View file

@ -16,12 +16,7 @@ export class Flashlight extends LightDrawable {
}, },
uniformCountMacroName: 'FLASHLIGHT_COUNT', uniformCountMacroName: 'FLASHLIGHT_COUNT',
shaderCombinationSteps: [0, 1, 2, 4], shaderCombinationSteps: [0, 1, 2, 4],
empty: new Flashlight( empty: new Flashlight(vec2.create(), vec3.create(), 0, vec2.create()),
vec2.fromValues(0, 0),
vec3.fromValues(0, 0, 0),
0,
vec2.fromValues(1, 0)
),
}; };
public constructor( public constructor(

View file

@ -51,12 +51,7 @@ export class Droplet extends Drawable {
}, },
uniformCountMacroName: 'DROPLET_COUNT', uniformCountMacroName: 'DROPLET_COUNT',
shaderCombinationSteps: [0, 1, 4, 16, 32], shaderCombinationSteps: [0, 1, 4, 16, 32],
empty: new Droplet( empty: new Droplet(vec2.create(), vec2.create(), 0, 0),
vec2.fromValues(-100000, -100000),
vec2.fromValues(-100000, -100000),
0,
0
),
}; };
constructor( constructor(

View file

@ -21,14 +21,14 @@ export class InvertedTunnel extends Drawable {
uniform sampler2D noiseTexture; uniform sampler2D noiseTexture;
#ifdef WEBGL2_IS_AVAILABLE #ifdef WEBGL2_IS_AVAILABLE
float myTerrain(float h) { float invertedTunnelTerrain(float h) {
return texture( return texture(
noiseTexture, noiseTexture,
vec2(h, 0.5) vec2(h, 0.5)
)[0] - 0.5; )[0] - 0.5;
} }
#else #else
float myTerrain(float h) { float invertedTunnelTerrain(float h) {
return texture2D( return texture2D(
noiseTexture, noiseTexture,
vec2(h, 0.5) vec2(h, 0.5)
@ -39,7 +39,7 @@ export class InvertedTunnel extends Drawable {
float invertedTunnelMinDistance(vec2 target, out float colorIndex) { float invertedTunnelMinDistance(vec2 target, out float colorIndex) {
colorIndex = 3.0; colorIndex = 3.0;
float minDistance = 1000.0; float minDistance = -1000.0;
for (int i = 0; i < INVERTED_TUNNEL_COUNT; i++) { for (int i = 0; i < INVERTED_TUNNEL_COUNT; i++) {
vec2 targetFromDelta = target - froms[i]; vec2 targetFromDelta = target - froms[i];
@ -53,12 +53,12 @@ export class InvertedTunnel extends Drawable {
fromRadii[i], toRadii[i], clampedH fromRadii[i], toRadii[i], clampedH
) + distance( ) + distance(
targetFromDelta, toFromDeltas[i] * clampedH targetFromDelta, toFromDeltas[i] * clampedH
) - myTerrain(h) / 12.0; ) - invertedTunnelTerrain(h) / 12.0;
minDistance = min(minDistance, currentDistance); minDistance = max(minDistance, -currentDistance);
} }
return -minDistance; return minDistance;
} }
`, `,
isInverted: true, isInverted: true,
@ -72,12 +72,7 @@ export class InvertedTunnel extends Drawable {
}, },
uniformCountMacroName: 'INVERTED_TUNNEL_COUNT', uniformCountMacroName: 'INVERTED_TUNNEL_COUNT',
shaderCombinationSteps: [0, 1, 4, 16, 32], shaderCombinationSteps: [0, 1, 4, 16, 32],
empty: new InvertedTunnel( empty: new InvertedTunnel(vec2.create(), vec2.create(), 0, 0),
vec2.fromValues(10000, 10000),
vec2.fromValues(10000, 10000),
0,
0
),
}; };
constructor( constructor(

View file

@ -32,7 +32,7 @@ export class MetaCircle extends Drawable {
}, },
uniformCountMacroName: 'META_CIRCLE_COUNT', uniformCountMacroName: 'META_CIRCLE_COUNT',
shaderCombinationSteps: [0, 1, 2, 3, 8, 16], shaderCombinationSteps: [0, 1, 2, 3, 8, 16],
empty: new MetaCircle(vec2.fromValues(10000, 10000), -1000), empty: new MetaCircle(vec2.create(), 0),
}; };
constructor(public center: vec2, public radius: number) { constructor(public center: vec2, public radius: number) {

View file

@ -75,7 +75,8 @@ export class UniformArrayAutoScalingProgram implements IProgram {
return 0; return 0;
} }
const uniformName = uniformNames[1]; const uniformName = uniformNames[1];
return uniforms[uniformName] ? uniforms[uniformName].length : 0; const scaler = d.objectCountScaler === undefined ? 1 : d.objectCountScaler;
return uniforms[uniformName] ? scaler * uniforms[uniformName].length : 0;
}); });
const closest = this.programs.find((p) => p.values.every((v, i) => v >= values[i])); const closest = this.programs.find((p) => p.values.every((v, i) => v >= values[i]));
@ -91,7 +92,11 @@ export class UniformArrayAutoScalingProgram implements IProgram {
this.descriptors!.map((d, i) => { this.descriptors!.map((d, i) => {
const difference = closest.values[i] - values[i]; const difference = closest.values[i] - values[i];
for (let i = 0; i < difference; i++) { for (let i = 0; i < difference; i++) {
d.empty.serializeToUniforms(uniforms, mat2d.create(), 0); d.empty.serializeToUniforms(
uniforms,
mat2d.fromTranslation(mat2d.create(), vec2.fromValues(-10000, -10000)),
0
);
} }
}); });
} }
@ -171,12 +176,12 @@ export class UniformArrayAutoScalingProgram implements IProgram {
}(position, objectColor); }(position, objectColor);
color = mix( color = mix(
objectColor / {paletteSize}, objectColor / {paletteSize},
color, color,
${ ${
descriptors[i].sdf?.isInverted descriptors[i].sdf?.isInverted
? `step(-distanceNdcPixelSize, -objectMinDistance)` ? `step(-distanceNdcPixelSize, -objectMinDistance)`
: `step(1.0 * distanceNdcPixelSize, objectMinDistance)` : `step(distanceNdcPixelSize, objectMinDistance)`
} }
); );