Refactor rendering
This commit is contained in:
parent
9b47d56d8f
commit
c892ca2d01
9 changed files with 505 additions and 423 deletions
|
|
@ -0,0 +1,5 @@
|
|||
export interface IDrawableDescriptor {
|
||||
uniformName: string;
|
||||
countMacroName: string;
|
||||
shaderCombinationSteps: Array<number>;
|
||||
}
|
||||
7
frontend/src/scripts/drawing/drawables/i-drawable.ts
Normal file
7
frontend/src/scripts/drawing/drawables/i-drawable.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
|
||||
export interface IDrawable {
|
||||
serializeToUniforms(uniforms: any): void;
|
||||
distance(target: vec2): number;
|
||||
minimumDistance(target: vec2): number;
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
import { ILight } from './i-light';
|
||||
import { vec2, vec3 } from 'gl-matrix';
|
||||
import { settings } from '../../settings';
|
||||
import { IDrawableDescriptor } from '../i-drawable-descriptor';
|
||||
|
||||
export class CircleLight implements ILight {
|
||||
public static descriptor: IDrawableDescriptor = {
|
||||
uniformName: 'lights',
|
||||
countMacroName: 'lightCount',
|
||||
shaderCombinationSteps: settings.shaderCombinations.circleLightSteps,
|
||||
};
|
||||
|
||||
constructor(
|
||||
public center: vec2,
|
||||
public radius: number,
|
||||
public color: vec3,
|
||||
public lightness: number
|
||||
) {}
|
||||
|
||||
distance(target: vec2): number {
|
||||
return 0;
|
||||
}
|
||||
|
||||
minimumDistance(target: vec2): number {
|
||||
return 0;
|
||||
}
|
||||
|
||||
serializeToUniforms(uniforms: any): void {
|
||||
const listName = CircleLight.descriptor.uniformName;
|
||||
|
||||
if (!uniforms.hasOwnProperty(listName)) {
|
||||
uniforms[listName] = [];
|
||||
}
|
||||
|
||||
uniforms[listName].push({
|
||||
center: this.center,
|
||||
radius: this.radius,
|
||||
value: this.value,
|
||||
});
|
||||
}
|
||||
|
||||
get value(): vec3 {
|
||||
return vec3.scale(
|
||||
vec3.create(),
|
||||
vec3.normalize(this.color, this.color),
|
||||
this.lightness
|
||||
);
|
||||
}
|
||||
}
|
||||
3
frontend/src/scripts/drawing/drawables/lights/i-light.ts
Normal file
3
frontend/src/scripts/drawing/drawables/lights/i-light.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import { IDrawable } from '../i-drawable';
|
||||
|
||||
export interface ILight extends IDrawable {}
|
||||
36
frontend/src/scripts/drawing/drawables/lights/point-light.ts
Normal file
36
frontend/src/scripts/drawing/drawables/lights/point-light.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { ILight } from './i-light';
|
||||
import { vec2, vec3 } from 'gl-matrix';
|
||||
|
||||
export class PointLight implements ILight {
|
||||
public static uniformName = 'lights';
|
||||
|
||||
constructor(
|
||||
public center: vec2,
|
||||
public color: vec3,
|
||||
public lightness: number
|
||||
) {}
|
||||
distance(target: vec2): number {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
minimumDistance(target: vec2): number {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
serializeToUniforms(uniforms: any): void {
|
||||
const listName = PointLight.uniformName;
|
||||
|
||||
if (!uniforms.hasOwnProperty(listName)) {
|
||||
uniforms[listName] = [];
|
||||
}
|
||||
|
||||
uniforms[listName].push({
|
||||
center: this.center,
|
||||
radius: 0,
|
||||
value: this.value,
|
||||
});
|
||||
}
|
||||
|
||||
get value(): vec3 {
|
||||
return vec3.scale(vec3.create(), this.color, this.lightness);
|
||||
}
|
||||
}
|
||||
27
frontend/src/scripts/drawing/drawables/primitives/circle.ts
Normal file
27
frontend/src/scripts/drawing/drawables/primitives/circle.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { IPrimitive } from './i-primitive';
|
||||
|
||||
export class Circle implements IPrimitive {
|
||||
public constructor(public center = vec2.create(), public radius = 0) {}
|
||||
|
||||
public serializeToUniforms(uniforms: any): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
public distance(target: vec2): number {
|
||||
return vec2.distance(this.center, target) - this.radius;
|
||||
}
|
||||
|
||||
public minimumDistance(target: vec2): number {
|
||||
return vec2.distance(this.center, target) - this.radius;
|
||||
}
|
||||
|
||||
public isInside(target: vec2): boolean {
|
||||
return this.distance(target) < 0;
|
||||
}
|
||||
|
||||
public areIntersecting(other: Circle): boolean {
|
||||
const distance = vec2.distance(this.center, other.center);
|
||||
return distance < this.radius + other.radius;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
import { IDrawable } from '../i-drawable';
|
||||
|
||||
export interface IPrimitive extends IDrawable {}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
|
||||
export class Rectangle {
|
||||
public constructor(
|
||||
public topLeft: vec2 = vec2.create(),
|
||||
public size: vec2 = vec2.create()
|
||||
) {}
|
||||
|
||||
public isInside(position: vec2): boolean {
|
||||
const translated = vec2.subtract(vec2.create(), position, this.topLeft);
|
||||
return (
|
||||
0 <= translated.x &&
|
||||
translated.x < this.size.x &&
|
||||
0 <= translated.y &&
|
||||
translated.y < this.size.y
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { clamp01 } from '../../../helper/clamp';
|
||||
import { mix } from '../../../helper/mix';
|
||||
import { Circle } from './circle';
|
||||
import { IPrimitive } from './i-primitive';
|
||||
import { IDrawableDescriptor } from '../i-drawable-descriptor';
|
||||
import { settings } from '../../settings';
|
||||
|
||||
export class TunnelShape implements IPrimitive {
|
||||
public static descriptor: IDrawableDescriptor = {
|
||||
uniformName: 'lines',
|
||||
countMacroName: 'lineCount',
|
||||
shaderCombinationSteps: settings.shaderCombinations.lineSteps,
|
||||
};
|
||||
|
||||
public readonly toFromDelta: vec2;
|
||||
private toFromDeltaLength: number;
|
||||
|
||||
private boundingCircle: Circle;
|
||||
|
||||
constructor(
|
||||
public readonly from: vec2,
|
||||
public readonly to: vec2,
|
||||
public readonly fromRadius: number,
|
||||
public readonly toRadius: number
|
||||
) {
|
||||
this.toFromDelta = vec2.subtract(vec2.create(), to, from);
|
||||
this.toFromDeltaLength = vec2.length(this.toFromDelta);
|
||||
|
||||
this.boundingCircle = new Circle(
|
||||
vec2.fromValues(from.x / 2 + to.x / 2, from.y / 2 + to.y / 2),
|
||||
Math.max(fromRadius, toRadius) + vec2.distance(from, to)
|
||||
);
|
||||
}
|
||||
|
||||
serializeToUniforms(uniforms: any): void {
|
||||
if (!uniforms.hasOwnProperty(TunnelShape.descriptor.uniformName)) {
|
||||
uniforms[TunnelShape.descriptor.uniformName] = [];
|
||||
}
|
||||
|
||||
uniforms[TunnelShape.descriptor.uniformName].push({
|
||||
from: this.from,
|
||||
toFromDelta: this.toFromDelta,
|
||||
fromRadius: this.fromRadius,
|
||||
toRadius: this.toRadius,
|
||||
});
|
||||
}
|
||||
|
||||
public distance(target: vec2): number {
|
||||
const targetFromDelta = vec2.subtract(vec2.create(), target, this.from);
|
||||
const h = clamp01(
|
||||
vec2.dot(targetFromDelta, this.toFromDelta) /
|
||||
this.toFromDeltaLength /
|
||||
this.toFromDeltaLength
|
||||
);
|
||||
return (
|
||||
vec2.distance(
|
||||
targetFromDelta,
|
||||
vec2.scale(vec2.create(), this.toFromDelta, h)
|
||||
) - mix(this.fromRadius, this.toRadius, h)
|
||||
);
|
||||
}
|
||||
|
||||
public minimumDistance(target: vec2): number {
|
||||
return this.boundingCircle.distance(target);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue