Skip to content

Commit

Permalink
build from texture
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanebert committed Dec 10, 2023
1 parent aaf474c commit 9d45ca3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/core/Object3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { Vector3 } from "../math/Vector3";
import { Quaternion } from "../math/Quaternion";
import { EventDispatcher } from "../events/EventDispatcher";
import { Matrix4 } from "../math/Matrix4";
import { ObjectChangedEvent } from "../events/Events";

abstract class Object3D extends EventDispatcher {
protected _position: Vector3 = new Vector3();
protected _rotation: Quaternion = new Quaternion();
protected _scale: Vector3 = new Vector3(1, 1, 1);
protected _matrix: Matrix4 = new Matrix4();

private _changeEvent = new ObjectChangedEvent(this);

update: () => void;
applyPosition: () => void;
applyRotation: () => void;
Expand Down Expand Up @@ -44,6 +47,7 @@ abstract class Object3D extends EventDispatcher {
if (!this._position.equals(position)) {
this._position = position;
this._updateMatrix();
this.dispatchEvent(this._changeEvent);
}
}

Expand All @@ -55,6 +59,7 @@ abstract class Object3D extends EventDispatcher {
if (!this._rotation.equals(rotation)) {
this._rotation = rotation;
this._updateMatrix();
this.dispatchEvent(this._changeEvent);
}
}

Expand All @@ -66,6 +71,7 @@ abstract class Object3D extends EventDispatcher {
if (!this._scale.equals(scale)) {
this._scale = scale;
this._updateMatrix();
this.dispatchEvent(this._changeEvent);
}
}

Expand Down
10 changes: 2 additions & 8 deletions src/renderers/WebGLRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,8 @@ export class WebGLRenderer {
gl.uniformMatrix4fv(u_view, false, activeCamera.data.viewMatrix.buffer);
gl.uniform2fv(u_viewport, new Float32Array([canvas.width, canvas.height]));
gl.uniform2fv(u_focal, new Float32Array([activeCamera.data.fx, activeCamera.data.fy]));
let matrix = new Matrix4();
for (const object of activeScene.objects) {
if (object instanceof Splat) {
matrix = object.matrix;
break;
}
}
gl.uniformMatrix4fv(u_transform, false, matrix.buffer);
const matrix = renderData.transforms.slice(0, 16);
gl.uniformMatrix4fv(u_transform, false, matrix);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.vertexAttribPointer(positionAttribute, 2, gl.FLOAT, false, 0, 0);
gl.bindTexture(gl.TEXTURE_2D, texture);
Expand Down
35 changes: 31 additions & 4 deletions src/renderers/webgl/utils/RenderData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Quaternion } from "../../../math/Quaternion";
import { Vector3 } from "../../../math/Vector3";

class RenderData {
private _indices: Map<Splat, number> = new Map<Splat, number>();
private _transforms: Float32Array;
private _buffer: Uint32Array;
private _width: number;
private _height: number;
Expand Down Expand Up @@ -53,15 +55,24 @@ class RenderData {
};

let vertexCount = 0;
const offsets = new Map<Splat, number>();
for (const object of scene.objects) {
if (object instanceof Splat) {
offsets.set(object, vertexCount);
this._indices.set(object, vertexCount);
vertexCount += object.data.vertexCount;
this._dirty.add(object);
}
}

const transformsTextureWidth = 4;
const transformsTextureHeight = this._indices.size * 4;
this._transforms = new Float32Array(transformsTextureWidth * transformsTextureHeight);
for (const [splat, index] of this._indices) {
const transform = splat.matrix.buffer;
for (let j = 0; j < 16; j++) {
this._transforms[16 * index + j] = transform[j];
}
}

this._vertexCount = vertexCount;
this._width = 2048;
this._height = Math.ceil((2 * this.vertexCount) / this.width);
Expand All @@ -78,7 +89,7 @@ class RenderData {
const scales = splat.worldScales;
const colors = data.colors;
for (let i = 0; i < splat.data.vertexCount; i++) {
const offset = offsets.get(splat) as number;
const offset = this._indices.get(splat) as number;
const index = offset + i;

data_f[8 * index + 0] = positions[3 * i + 0];
Expand Down Expand Up @@ -123,7 +134,15 @@ class RenderData {
};

this.markDirty = (splat: Splat) => {
this._dirty.add(splat);
const index = this._indices.get(splat) as number;
const transform = splat.matrix.buffer;
for (let j = 0; j < 16; j++) {
this._transforms[16 * index + j] = transform[j];
}

if (splat.dirty) {
this._dirty.add(splat);
}
};

this.rebuild = () => {
Expand All @@ -137,6 +156,14 @@ class RenderData {
this.rebuild();
}

get indices() {
return this._indices;
}

get transforms() {
return this._transforms;
}

get buffer() {
return this._buffer;
}
Expand Down
5 changes: 5 additions & 0 deletions src/splats/Splat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Matrix3 } from "../math/Matrix3";

class Splat extends Object3D {
private _data: SplatData;
private _dirty = false;

constructor(splat: SplatData | undefined = undefined) {
super();
Expand All @@ -32,6 +33,10 @@ class Splat extends Object3D {
return this._data;
}

get dirty() {
return this._dirty;
}

get worldPositions() {
const result = new Float32Array(this.data.vertexCount * 3);
const R = Matrix3.RotationFromQuaternion(this.rotation).buffer;
Expand Down

0 comments on commit 9d45ca3

Please sign in to comment.