diff --git a/src/renderers/WebGLRenderer.ts b/src/renderers/WebGLRenderer.ts index 483fe81..7f325aa 100644 --- a/src/renderers/WebGLRenderer.ts +++ b/src/renderers/WebGLRenderer.ts @@ -93,15 +93,13 @@ export class WebGLRenderer { }; const initWebGL = () => { - if (activeScene.objects.length === 0) return; - - renderData = new RenderData(activeScene.objects[0]); + renderData = new RenderData(activeScene); worker = new SortWorker(); worker.postMessage({ sortData: { - positions: activeScene.objects[0].positions, - vertexCount: activeScene.objects[0].vertexCount, + positions: renderData.positions, + vertexCount: renderData.vertexCount, }, }); @@ -236,13 +234,13 @@ export class WebGLRenderer { activeCamera.update(canvas.width, canvas.height); worker.postMessage({ viewProj: activeCamera.viewProj }); - if (activeScene.objects.length > 0 && activeScene.objects[0].vertexCount > 0) { + if (renderData.vertexCount > 0) { for (const shaderPass of shaderPasses) { shaderPass.render(); } gl.uniformMatrix4fv(u_view, false, activeCamera.viewMatrix.buffer); gl.clear(gl.COLOR_BUFFER_BIT); - gl.drawArraysInstanced(gl.TRIANGLE_FAN, 0, 4, activeScene.objects[0].vertexCount); + gl.drawArraysInstanced(gl.TRIANGLE_FAN, 0, 4, renderData.vertexCount); } else { gl.clear(gl.COLOR_BUFFER_BIT); } diff --git a/src/renderers/webgl/utils/RenderData.ts b/src/renderers/webgl/utils/RenderData.ts index abe56fd..d74f72e 100644 --- a/src/renderers/webgl/utils/RenderData.ts +++ b/src/renderers/webgl/utils/RenderData.ts @@ -1,4 +1,4 @@ -import { Object3DGS } from "../../../core/Object3DGS"; +import { Scene } from "../../../core/Scene"; import { Matrix3 } from "../../../math/Matrix3"; import { Quaternion } from "../../../math/Quaternion"; import { Vector3 } from "../../../math/Vector3"; @@ -7,10 +7,10 @@ class RenderData { private _buffer: Uint32Array; private _width: number; private _height: number; + private _positions: Float32Array; + private _vertexCount: number; - update: (i: number) => void; - - constructor(object: Object3DGS) { + constructor(scene: Scene) { const _floatView: Float32Array = new Float32Array(1); const _int32View: Int32Array = new Int32Array(_floatView.buffer); @@ -47,54 +47,67 @@ class RenderData { return (floatToHalf(x) | (floatToHalf(y) << 16)) >>> 0; }; + this._vertexCount = 0; + for (const object of scene.objects) { + this._vertexCount += object.vertexCount; + } + this._width = 2048; - this._height = Math.ceil((2 * object.vertexCount) / this._width); + this._height = Math.ceil((2 * this._vertexCount) / this._width); this._buffer = new Uint32Array(this._width * this._height * 4); + this._positions = new Float32Array(this._vertexCount * 3); const data_f = new Float32Array(this._buffer.buffer); const data_c = new Uint8Array(this._buffer.buffer); - this.update = (i: number) => { - data_f[8 * i + 0] = object.positions[3 * i + 0]; - data_f[8 * i + 1] = object.positions[3 * i + 1]; - data_f[8 * i + 2] = object.positions[3 * i + 2]; - - data_c[4 * (8 * i + 7) + 0] = object.colors[4 * i + 0]; - data_c[4 * (8 * i + 7) + 1] = object.colors[4 * i + 1]; - data_c[4 * (8 * i + 7) + 2] = object.colors[4 * i + 2]; - data_c[4 * (8 * i + 7) + 3] = object.colors[4 * i + 3]; - - const rot = Matrix3.RotationFromQuaternion( - new Quaternion( - object.rotations[4 * i + 1], - object.rotations[4 * i + 2], - object.rotations[4 * i + 3], - -object.rotations[4 * i + 0], - ), - ); - - const scale = Matrix3.Diagonal( - new Vector3(object.scales[3 * i + 0], object.scales[3 * i + 1], object.scales[3 * i + 2]), - ); - - const M = scale.multiply(rot).buffer; - - const sigma = [ - M[0] * M[0] + M[3] * M[3] + M[6] * M[6], - M[0] * M[1] + M[3] * M[4] + M[6] * M[7], - M[0] * M[2] + M[3] * M[5] + M[6] * M[8], - M[1] * M[1] + M[4] * M[4] + M[7] * M[7], - M[1] * M[2] + M[4] * M[5] + M[7] * M[8], - M[2] * M[2] + M[5] * M[5] + M[8] * M[8], - ]; - - this._buffer[8 * i + 4] = packHalf2x16(4 * sigma[0], 4 * sigma[1]); - this._buffer[8 * i + 5] = packHalf2x16(4 * sigma[2], 4 * sigma[3]); - this._buffer[8 * i + 6] = packHalf2x16(4 * sigma[4], 4 * sigma[5]); - }; + let offset = 0; + for (const object of scene.objects) { + for (let i = 0; i < object.vertexCount; i++) { + const index = offset + i; + + data_f[8 * index + 0] = object.positions[3 * i + 0]; + data_f[8 * index + 1] = object.positions[3 * i + 1]; + data_f[8 * index + 2] = object.positions[3 * i + 2]; + + this._positions[3 * index + 0] = object.positions[3 * i + 0]; + this._positions[3 * index + 1] = object.positions[3 * i + 1]; + this._positions[3 * index + 2] = object.positions[3 * i + 2]; + + data_c[4 * (8 * index + 7) + 0] = object.colors[4 * i + 0]; + data_c[4 * (8 * index + 7) + 1] = object.colors[4 * i + 1]; + data_c[4 * (8 * index + 7) + 2] = object.colors[4 * i + 2]; + data_c[4 * (8 * index + 7) + 3] = object.colors[4 * i + 3]; + + const rot = Matrix3.RotationFromQuaternion( + new Quaternion( + object.rotations[4 * i + 1], + object.rotations[4 * i + 2], + object.rotations[4 * i + 3], + -object.rotations[4 * i + 0], + ), + ); + + const scale = Matrix3.Diagonal( + new Vector3(object.scales[3 * i + 0], object.scales[3 * i + 1], object.scales[3 * i + 2]), + ); + + const M = scale.multiply(rot).buffer; + + const sigma = [ + M[0] * M[0] + M[3] * M[3] + M[6] * M[6], + M[0] * M[1] + M[3] * M[4] + M[6] * M[7], + M[0] * M[2] + M[3] * M[5] + M[6] * M[8], + M[1] * M[1] + M[4] * M[4] + M[7] * M[7], + M[1] * M[2] + M[4] * M[5] + M[7] * M[8], + M[2] * M[2] + M[5] * M[5] + M[8] * M[8], + ]; + + this._buffer[8 * index + 4] = packHalf2x16(4 * sigma[0], 4 * sigma[1]); + this._buffer[8 * index + 5] = packHalf2x16(4 * sigma[2], 4 * sigma[3]); + this._buffer[8 * index + 6] = packHalf2x16(4 * sigma[4], 4 * sigma[5]); + } - for (let i = 0; i < object.vertexCount; i++) { - this.update(i); + offset += object.vertexCount; } } @@ -109,6 +122,14 @@ class RenderData { get height() { return this._height; } + + get positions() { + return this._positions; + } + + get vertexCount() { + return this._vertexCount; + } } export { RenderData };