Refactor rendering

This commit is contained in:
schmelczerandras 2020-07-30 13:28:10 +02:00
commit c892ca2d01

View file

@ -1,8 +1,105 @@
import { IProgram } from '../graphics-library/program/i-program';
import { FrameBuffer } from '../graphics-library/frame-buffer/frame-buffer';
import { IDrawable } from '../drawables/i-drawable';
import { settings } from '../settings';
import { vec2 } from 'gl-matrix';
import { Circle } from '../drawables/primitives/circle';
import { InfoText } from '../../objects/types/info-text';
import { UniformArrayAutoScalingProgram } from '../graphics-library/program/uniform-array-autoscaling-program';
import { IDrawableDescriptor } from '../drawables/i-drawable-descriptor';
export class RenderingPass {
constructor(private program: IProgram, private frame: FrameBuffer) {}
private drawables: Array<IDrawable> = [];
private program: UniformArrayAutoScalingProgram;
public render(uniforms: any, inputTexture?: WebGLTexture) {}
constructor(
gl: WebGL2RenderingContext,
shaderSources: [string, string],
private drawableDescriptors: Array<IDrawableDescriptor>,
private frame: FrameBuffer
) {
this.program = new UniformArrayAutoScalingProgram(
gl,
shaderSources,
drawableDescriptors
);
}
public addDrawable(drawable: IDrawable) {
this.drawables.push(drawable);
}
public render(
commonUniforms: any,
viewCircle: Circle,
inputTexture?: WebGLTexture
) {
this.frame.bindAndClear(inputTexture);
const q = 1 / settings.tileMultiplier;
const tileUvSize = vec2.fromValues(q, q);
const possiblyOnScreenDrawables = this.drawables.filter(
(p) => p.minimumDistance(viewCircle.center) < viewCircle.radius
);
const origin = vec2.transformMat2d(
vec2.create(),
vec2.fromValues(0, 0),
commonUniforms.uvToWorld
);
const firstCenter = vec2.transformMat2d(
vec2.create(),
vec2.fromValues(q, q),
commonUniforms.uvToWorld
);
vec2.subtract(firstCenter, firstCenter, origin);
const worldR = vec2.length(firstCenter);
let sumLineCount = 0;
for (let x = 0; x < 1; x += q) {
for (let y = 0; y < 1; y += q) {
const uniforms = { ...commonUniforms };
uniforms.maxMinDistance = 2 * worldR;
const uvBottomLeft = vec2.fromValues(x, y);
this.program.setDrawingRectangle(uvBottomLeft, tileUvSize);
const tileCenterWorldCoordinates = vec2.transformMat2d(
vec2.create(),
vec2.add(vec2.create(), uvBottomLeft, vec2.fromValues(q, q)),
uniforms.uvToWorld
);
const primitivesNearTile = possiblyOnScreenDrawables.filter(
(p) => p.distance(tileCenterWorldCoordinates) < 2 * worldR
);
sumLineCount += primitivesNearTile.length;
primitivesNearTile.forEach((p) => p.serializeToUniforms(uniforms));
this.program.bindAndSetUniforms(uniforms);
this.program.draw();
}
}
this.drawables = [];
InfoText.modifyRecord(
'nearby ' + this.drawableDescriptors[0].countMacroName,
possiblyOnScreenDrawables.length.toFixed(2)
);
InfoText.modifyRecord(
'drawn ' + this.drawableDescriptors[0].countMacroName,
(
sumLineCount /
settings.tileMultiplier /
settings.tileMultiplier
).toFixed(2)
);
}
}