Improve tunnel

This commit is contained in:
schmelczerandras 2020-09-16 16:04:55 +02:00
parent bc16bdd62e
commit 935d3796b0

View file

@ -6,7 +6,6 @@ import { DrawableDescriptor } from '../drawable-descriptor';
export class Tunnel extends Drawable { export class Tunnel extends Drawable {
public readonly isInverted = false; public readonly isInverted = false;
public readonly toFromDelta: vec2;
public static get descriptor(): DrawableDescriptor { public static get descriptor(): DrawableDescriptor {
return { return {
@ -21,7 +20,7 @@ export class Tunnel extends Drawable {
}[TUNNEL_COUNT] tunnels; }[TUNNEL_COUNT] tunnels;
void tunnelMinDistance(inout float minDistance, inout float color) { void tunnelMinDistance(inout float minDistance, inout float color) {
float tunnelMinDistance = minDistance; float myMinDistance = minDistance;
for (int i = 0; i < TUNNEL_COUNT; i++) { for (int i = 0; i < TUNNEL_COUNT; i++) {
Tunnel tunnel = tunnels[i]; Tunnel tunnel = tunnels[i];
vec2 targetFromDelta = position - tunnel.from; vec2 targetFromDelta = position - tunnel.from;
@ -34,25 +33,25 @@ export class Tunnel extends Drawable {
float lineDistance = -mix( float lineDistance = -mix(
tunnel.fromRadius, tunnel.toRadius, h tunnel.fromRadius, tunnel.toRadius, h
) + tunnel.inverted * distance( ) + distance(
targetFromDelta, tunnel.toFromDelta * h targetFromDelta, tunnel.toFromDelta * h
); );
tunnelMinDistance = min(tunnelMinDistance, lineDistance); myMinDistance = min(myMinDistance, lineDistance);
} }
color = mix(2.0, color, step( color = mix(2.0, color, step(
distanceNdcPixelSize + SURFACE_OFFSET, distanceNdcPixelSize + SURFACE_OFFSET,
-tunnelMinDistance -myMinDistance
)); ));
minDistance = -tunnelMinDistance; minDistance = -myMinDistance;
} }
`, `,
distanceFunctionName: 'tunnelMinDistance', distanceFunctionName: 'tunnelMinDistance',
}, },
uniformName: 'tunnels', uniformName: 'tunnels',
uniformCountMacroName: 'TUNNEL_COUNT', uniformCountMacroName: 'TUNNEL_COUNT',
shaderCombinationSteps: [0, 1, 2, 8, 32], shaderCombinationSteps: [0, 1, 4, 16, 32],
empty: new Tunnel(vec2.fromValues(0, 0), vec2.fromValues(0, 0), 0, 0), empty: new Tunnel(vec2.fromValues(0, 0), vec2.fromValues(0, 0), 0, 0),
}; };
} }
@ -64,27 +63,28 @@ export class Tunnel extends Drawable {
public readonly toRadius: number public readonly toRadius: number
) { ) {
super(); super();
this.toFromDelta = vec2.subtract(vec2.create(), to, from);
} }
public distance(target: vec2): number { public distance(target: vec2): number {
const toFromDelta = vec2.subtract(vec2.create(), this.to, this.from);
const targetFromDelta = vec2.subtract(vec2.create(), target, this.from); const targetFromDelta = vec2.subtract(vec2.create(), target, this.from);
const h = clamp01( const h = clamp01(
vec2.dot(targetFromDelta, this.toFromDelta) / vec2.dot(targetFromDelta, toFromDelta) / vec2.dot(toFromDelta, toFromDelta)
vec2.dot(this.toFromDelta, this.toFromDelta)
); );
return ( return (
vec2.distance(targetFromDelta, vec2.scale(vec2.create(), this.toFromDelta, h)) - vec2.distance(targetFromDelta, vec2.scale(vec2.create(), toFromDelta, h)) -
mix(this.fromRadius, this.toRadius, h) mix(this.fromRadius, this.toRadius, h)
); );
} }
protected getObjectToSerialize(transform2d: mat2d, transform1d: number): any { protected getObjectToSerialize(transform2d: mat2d, transform1d: number): any {
const toFromDelta = vec2.subtract(vec2.create(), this.to, this.from);
return { return {
from: vec2.transformMat2d(vec2.create(), this.from, transform2d), from: vec2.transformMat2d(vec2.create(), this.from, transform2d),
toFromDelta: vec2.scale(vec2.create(), this.toFromDelta, transform1d), toFromDelta: vec2.scale(vec2.create(), toFromDelta, transform1d),
fromRadius: this.fromRadius * transform1d, fromRadius: this.fromRadius * transform1d,
toRadius: this.toRadius * transform1d, toRadius: this.toRadius * transform1d,
inverted: this.isInverted ? -1 : 1, inverted: this.isInverted ? -1 : 1,