Refactor shapes

This commit is contained in:
schmelczerandras 2020-08-16 10:24:12 +02:00
parent eb39846b75
commit 006ab3c4e6
24 changed files with 203 additions and 256 deletions

View file

@ -0,0 +1,26 @@
import { IDrawable } from './i-drawable';
import { TunnelShape } from '../../shapes/types/tunnel-shape';
import { IDrawableDescriptor } from './i-drawable-descriptor';
import { settings } from '../settings';
export class DrawableTunnel extends TunnelShape implements IDrawable {
public static descriptor: IDrawableDescriptor = {
uniformName: 'lines',
countMacroName: 'lineCount',
shaderCombinationSteps: settings.shaderCombinations.lineSteps,
};
public serializeToUniforms(uniforms: any): void {
const uniformName = DrawableTunnel.descriptor.uniformName;
if (!uniforms.hasOwnProperty(uniformName)) {
uniforms[uniformName] = [];
}
uniforms[uniformName].push({
from: this.from,
toFromDelta: this.toFromDelta,
fromRadius: this.fromRadius,
toRadius: this.toRadius,
});
}
}

View file

@ -1,11 +1,6 @@
import { vec2 } from 'gl-matrix';
import { ImmutableBoundingBox } from '../../physics/containers/immutable-bounding-box';
import { GameObject } from '../../objects/game-object';
export interface IDrawable {
serializeToUniforms(uniforms: any): void;
distance(target: vec2): number;
minimumDistance(target: vec2): number;
readonly owner: GameObject;
readonly boundingBox: ImmutableBoundingBox;
serializeToUniforms(uniforms: any): void;
}

View file

@ -1,6 +1,5 @@
import { vec2, vec3 } from 'gl-matrix';
import { GameObject } from '../../../objects/game-object';
import { ImmutableBoundingBox } from '../../../physics/containers/immutable-bounding-box';
import { settings } from '../../settings';
import { IDrawableDescriptor } from '../i-drawable-descriptor';
import { ILight } from './i-light';
@ -20,24 +19,18 @@ export class CircleLight implements ILight {
public lightness: number
) {}
boundingBox: ImmutableBoundingBox;
public distance(target: vec2): number {
return 0;
}
public minimumDistance(target: vec2): number {
return 0;
}
public serializeToUniforms(uniforms: any): void {
const listName = CircleLight.descriptor.uniformName;
const uniformName = CircleLight.descriptor.uniformName;
if (!uniforms.hasOwnProperty(listName)) {
uniforms[listName] = [];
if (!uniforms.hasOwnProperty(uniformName)) {
uniforms[uniformName] = [];
}
uniforms[listName].push({
uniforms[uniformName].push({
center: this.center,
radius: this.radius,
value: this.value,

View file

@ -1,3 +1,4 @@
import { vec2 } from 'gl-matrix';
import { IDrawable } from '../i-drawable';
export interface ILight extends IDrawable {}

View file

@ -2,7 +2,6 @@ import { ILight } from './i-light';
import { vec2, vec3 } from 'gl-matrix';
import { IDrawableDescriptor } from '../i-drawable-descriptor';
import { settings } from '../../settings';
import { ImmutableBoundingBox } from '../../../physics/containers/immutable-bounding-box';
import { GameObject } from '../../../objects/game-object';
export class PointLight implements ILight {
@ -20,16 +19,10 @@ export class PointLight implements ILight {
public lightness: number
) {}
boundingBox: ImmutableBoundingBox;
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 serializeToUniforms(uniforms: any): void {
const listName = PointLight.descriptor.uniformName;

View file

@ -1,47 +0,0 @@
import { vec2 } from 'gl-matrix';
import { GameObject } from '../../../objects/game-object';
import { ImmutableBoundingBox } from '../../../physics/containers/immutable-bounding-box';
import { IPrimitive } from './i-primitive';
export class Circle implements IPrimitive {
public constructor(
public readonly owner: GameObject,
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 get boundingBox(): ImmutableBoundingBox {
return new ImmutableBoundingBox(
this,
this.center.x - this.radius,
this.center.x + this.radius,
this.center.y - this.radius,
this.center.y + 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;
}
public clone(): Circle {
return new Circle(this.owner, this.center, this.radius);
}
}

View file

@ -1,5 +0,0 @@
import { IDrawable } from '../i-drawable';
export interface IPrimitive extends IDrawable {
clone(): IPrimitive
}

View file

@ -1,99 +0,0 @@
import { vec2 } from 'gl-matrix';
import { clamp01 } from '../../../helper/clamp';
import { mix } from '../../../helper/mix';
import { GameObject } from '../../../objects/game-object';
import { ImmutableBoundingBox } from '../../../physics/containers/immutable-bounding-box';
import { settings } from '../../settings';
import { IDrawableDescriptor } from '../i-drawable-descriptor';
import { Circle } from './circle';
import { IPrimitive } from './i-primitive';
export class TunnelShape implements IPrimitive {
public static descriptor: IDrawableDescriptor = {
uniformName: 'lines',
countMacroName: 'lineCount',
shaderCombinationSteps: settings.shaderCombinations.lineSteps,
};
public readonly toFromDelta: vec2;
private boundingCircle: Circle;
constructor(
public readonly owner: GameObject,
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.boundingCircle = new Circle(
this.owner,
vec2.fromValues(from.x / 2 + to.x / 2, from.y / 2 + to.y / 2),
Math.max(fromRadius, toRadius) + vec2.distance(from, to)
);
}
public 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 get boundingBox(): ImmutableBoundingBox {
const xMin = Math.min(
this.from.x - this.fromRadius,
this.to.x - this.toRadius
);
const yMin = Math.min(
this.from.y - this.fromRadius,
this.to.y - this.toRadius
);
const xMax = Math.max(
this.from.x + this.fromRadius,
this.to.x + this.toRadius
);
const yMax = Math.max(
this.from.y + this.fromRadius,
this.to.y + this.toRadius
);
return new ImmutableBoundingBox(this, xMin, xMax, yMin, yMax);
}
public distance(target: vec2): number {
const targetFromDelta = vec2.subtract(vec2.create(), target, this.from);
const h = clamp01(
vec2.dot(targetFromDelta, this.toFromDelta) /
vec2.dot(this.toFromDelta, this.toFromDelta)
);
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);
}
public clone(): TunnelShape {
return new TunnelShape(
this.owner,
this.from,
this.to,
this.fromRadius,
this.toRadius
);
}
}

View file

@ -1,12 +1,12 @@
import { vec2 } from 'gl-matrix';
import { ILight } from './drawables/lights/i-light';
import { IPrimitive } from './drawables/primitives/i-primitive';
import { IDrawable } from './drawables/i-drawable';
export interface IRenderer {
startFrame(deltaTime: DOMHighResTimeStamp): void;
finishFrame(): void;
drawPrimitive(primitive: IPrimitive): void;
drawShape(drawable: IDrawable): void;
drawLight(light: ILight): void;
drawInfoText(text: string): void;

View file

@ -38,7 +38,7 @@ export class RenderingPass {
const tileUvSize = vec2.fromValues(q, q);
const possiblyOnScreenDrawables = this.drawables.filter(
(p) => p.minimumDistance(viewBoxCenter) < viewBoxRadius
(p) => p.distance(viewBoxCenter) < viewBoxRadius
);
const origin = vec2.transformMat2d(

View file

@ -2,8 +2,6 @@ import { mat2d, vec2 } from 'gl-matrix';
import { CircleLight } from '../drawables/lights/circle-light';
import { ILight } from '../drawables/lights/i-light';
import { PointLight } from '../drawables/lights/point-light';
import { IPrimitive } from '../drawables/primitives/i-primitive';
import { TunnelShape } from '../drawables/primitives/tunnel-shape';
// import lightsShader from '../shaders/rainbow-shading-fs.glsl';
import { DefaultFrameBuffer } from '../graphics-library/frame-buffer/default-frame-buffer';
import { IntermediateFrameBuffer } from '../graphics-library/frame-buffer/intermediate-frame-buffer';
@ -16,6 +14,8 @@ import caveVertexShader from '../shaders/passthrough-distance-vs.glsl';
import lightsVertexShader from '../shaders/passthrough-shading-vs.glsl';
import { FpsAutoscaler } from './fps-autoscaler';
import { RenderingPass } from './rendering-pass';
import { IDrawable } from '../drawables/i-drawable';
import { DrawableTunnel } from '../drawables/drawable-tunnel';
export class WebGl2Renderer implements IRenderer {
private gl: WebGL2RenderingContext;
@ -50,7 +50,7 @@ export class WebGl2Renderer implements IRenderer {
this.distancePass = new RenderingPass(
this.gl,
[caveVertexShader, caveFragmentShader],
[TunnelShape.descriptor],
[DrawableTunnel.descriptor],
this.distanceFieldFrameBuffer
);
@ -68,11 +68,11 @@ export class WebGl2Renderer implements IRenderer {
try {
this.stopwatch = new WebGlStopwatch(this.gl);
} catch { }
} catch {}
}
public drawPrimitive(primitive: IPrimitive) {
this.distancePass.addDrawable(primitive);
public drawShape(shape: IDrawable) {
this.distancePass.addDrawable(shape);
}
public drawLight(light: ILight) {
@ -94,14 +94,11 @@ export class WebGl2Renderer implements IRenderer {
vec2.scale(vec2.create(), this.viewBoxSize, 0.5)
);
this.distancePass.render(
this.uniforms,
this.cameraPosition,
viewBoxRadius
);
this.distancePass.render(this.uniforms, this.cameraPosition, viewBoxRadius);
this.lightingPass.render(
this.uniforms, this.cameraPosition,
this.uniforms,
this.cameraPosition,
viewBoxRadius,
this.distanceFieldFrameBuffer.colorTexture
);
@ -111,7 +108,7 @@ export class WebGl2Renderer implements IRenderer {
private get uniforms(): any {
const cursorPosition = this.screenUvToWorldCoordinate(this.cursorPosition);
return { ...this.matrices, cursorPosition, viewBoxSize: this.viewBoxSize }
return { ...this.matrices, cursorPosition, viewBoxSize: this.viewBoxSize };
}
private calculateMatrices() {