Skip to content

Commit

Permalink
use centers from the texture in the shader (#257)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Valigursky <[email protected]>
  • Loading branch information
mvaligursky and Martin Valigursky authored Oct 20, 2023
1 parent 82c411c commit 6748515
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/splat/splat-material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import {
GraphicsDevice,
Material,
SEMANTIC_POSITION,
SEMANTIC_ATTR11,
SEMANTIC_ATTR13
} from "playcanvas";

const splatVS = `
attribute vec3 vertex_position;
attribute vec3 splat_center;
uniform mat4 matrix_model;
uniform mat4 matrix_view;
Expand Down Expand Up @@ -50,6 +48,7 @@ const splatVS = `
uniform sampler2D splatColor;
uniform highp sampler2D splatScale;
uniform highp sampler2D splatRotation;
uniform highp sampler2D splatCenter;
#ifdef WEBGPU
Expand Down Expand Up @@ -78,6 +77,10 @@ const splatVS = `
return texelFetch(splatRotation, dataUV, 0).xyz;
}
vec3 getCenter() {
return texelFetch(splatCenter, dataUV, 0).xyz;
}
#else
// TODO: use texture2DLodEXT on WebGL
Expand Down Expand Up @@ -108,6 +111,10 @@ const splatVS = `
return texture(splatRotation, dataUV).xyz;
}
vec3 getCenter() {
return texture(splatCenter, dataUV).xyz;
}
#endif
void computeCov3d(in vec3 rot, in vec3 scale, out vec3 covA, out vec3 covB)
Expand Down Expand Up @@ -144,7 +151,8 @@ const splatVS = `
{
evalDataUV();
vec4 splat_cam = matrix_view * matrix_model * vec4(splat_center, 1.0);
vec3 center = getCenter();
vec4 splat_cam = matrix_view * matrix_model * vec4(center, 1.0);
vec4 splat_proj = matrix_projection * splat_cam;
// cull behind camera
Expand Down Expand Up @@ -199,7 +207,7 @@ const splatVS = `
#ifdef DEBUG_RENDER
vec3 local = quatToMat3(rotation) * (vertex_position * scale * 2.0) + splat_center;
vec3 local = quatToMat3(rotation) * (vertex_position * scale * 2.0) + center;
gl_Position = matrix_viewProjection * matrix_model * vec4(local, 1.0);
color = getColor();
Expand Down Expand Up @@ -243,7 +251,6 @@ const createSplatMaterial = (device: GraphicsDevice, debugRender = false) => {

result.shader = createShaderFromCode(device, vs, fs, `splatShader-${debugRender}`, {
vertex_position: SEMANTIC_POSITION,
splat_center: SEMANTIC_ATTR11,
vertex_id: SEMANTIC_ATTR13
});

Expand Down
33 changes: 32 additions & 1 deletion src/splat/splat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,36 @@ class Splat {
return texture;
}

createCenterTexture(splatData: SplatData, size: Vec2, format: object) {

// texture format based vars
const { numComponents, isHalf } = format;

const x = splatData.getProp('x');
const y = splatData.getProp('y');
const z = splatData.getProp('z');

const texture = this.createTexture('splatCenter', format.format, size);
const data = texture.lock();

for (let i = 0; i < splatData.numSplats; i++) {

if (isHalf) {
data[i * numComponents + 0] = float2Half(x[i]);
data[i * numComponents + 1] = float2Half(y[i]);
data[i * numComponents + 2] = float2Half(z[i]);
} else {
data[i * numComponents + 0] = x[i];
data[i * numComponents + 1] = y[i];
data[i * numComponents + 2] = z[i];
}
}

texture.unlock();
return texture;
}


create(splatData: SplatData, options: any) {
const x = splatData.getProp('x');
const y = splatData.getProp('y');
Expand All @@ -280,12 +310,12 @@ class Splat {
const colorTexture = this.createColorTexture(splatData, textureSize);
const scaleTexture = this.createScaleTexture(splatData, textureSize, this.getTextureFormat(false));
const rotationTexture = this.createRotationTexture(splatData, textureSize, this.getTextureFormat(false));
const centerTexture = this.createCenterTexture(splatData, textureSize, this.getTextureFormat(false));

// position.xyz, rotation.xyz
const floatData = new Float32Array(splatData.numSplats * stride);
const uint32Data = new Uint32Array(floatData.buffer);

// const quat = new Quat();
const isWebGPU = this.device.isWebGPU;

for (let i = 0; i < splatData.numSplats; ++i) {
Expand All @@ -307,6 +337,7 @@ class Splat {
this.material.setParameter('splatColor', colorTexture);
this.material.setParameter('splatScale', scaleTexture);
this.material.setParameter('splatRotation', rotationTexture);
this.material.setParameter('splatCenter', centerTexture);
this.material.setParameter('tex_params', new Float32Array([textureSize.x, textureSize.y, 1 / textureSize.x, 1 / textureSize.y]));

// create instance data
Expand Down

0 comments on commit 6748515

Please sign in to comment.