Skip to content

Commit

Permalink
shader updates improves performance and visual quality
Browse files Browse the repository at this point in the history
removing legacy _opacity attribute support
  • Loading branch information
keyboardspecialist committed Oct 25, 2024
1 parent 52b3d61 commit f207786
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
19 changes: 11 additions & 8 deletions packages/engine/Source/Scene/Model/GaussianSplatPipelineStage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ GaussianSplatPipelineStage.process = function (
shaderBuilder.addAttribute("vec2", "a_screenQuadPosition");
shaderBuilder.addAttribute("vec3", "a_splatPosition");
shaderBuilder.addAttribute("vec4", "a_splatColor");
shaderBuilder.addAttribute("float", "a_splatOpacity");
//shaderBuilder.addAttribute("float", "a_splatOpacity");

shaderBuilder.addVarying("vec4", "v_splatColor");
shaderBuilder.addVarying("vec2", "v_vertPos");
shaderBuilder.addVarying("float", "v_splatOpacity");
shaderBuilder.addVarying("vec4", "v_splatScale");
shaderBuilder.addVarying("vec4", "v_splatRot");

shaderBuilder.addUniform("float", "u_tan_fovX", ShaderDestination.VERTEX);
shaderBuilder.addUniform("float", "u_tan_fovY", ShaderDestination.VERTEX);
Expand Down Expand Up @@ -86,19 +88,19 @@ GaussianSplatPipelineStage.process = function (
const scaleAttr = attributes.find((a) => a.name === "_SCALE");
const rotAttr = attributes.find((a) => a.name === "_ROTATION");
const clrAttr = attributes.find((a) => a.name === "COLOR_0");
// const opAttr = attributes.find((a) => a.name === "_OPACITY");
// const opAttr = attributes.find((a) => a.name === "_OPACITY");

const posArray = posAttr.typedArray;
const scaleArray = scaleAttr.typedArray;
const rotArray = rotAttr.typedArray;
const clrArray = clrAttr.typedArray;
// const opArray = opAttr.typedArray;
// const opArray = opAttr.typedArray;

const newPosArray = new posArray.constructor(posArray.length);
const newScaleArray = new scaleArray.constructor(scaleArray.length);
const newRotArray = new rotArray.constructor(rotArray.length);
const newClrArray = new clrArray.constructor(clrArray.length);
// const newOpArray = new opArray.constructor(opArray.length);
// const newOpArray = new opArray.constructor(opArray.length);

const calcDepth = (i) =>
posArray[i * 3] * modelView[2] +
Expand Down Expand Up @@ -149,11 +151,12 @@ GaussianSplatPipelineStage.process = function (
newRotArray[i * 4 + 2] = rotArray[j * 4 + 2];
newRotArray[i * 4 + 3] = rotArray[j * 4 + 3];

newClrArray[i * 3] = clrArray[j * 3];
newClrArray[i * 3 + 1] = clrArray[j * 3 + 1];
newClrArray[i * 3 + 2] = clrArray[j * 3 + 2];
newClrArray[i * 4] = clrArray[j * 4];
newClrArray[i * 4 + 1] = clrArray[j * 4 + 1];
newClrArray[i * 4 + 2] = clrArray[j * 4 + 2];
newClrArray[i * 4 + 3] = clrArray[j * 4 + 3];

// newOpArray[i] = opArray[j];
// newOpArray[i] = opArray[j];
}

posAttr.typedArray = newPosArray;
Expand Down
3 changes: 0 additions & 3 deletions packages/engine/Source/Scene/Model/GeometryPipelineStage.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ GeometryPipelineStage.process = function (
showSplats ? 1 : 0;
primitive.attributes.find((a) => a.name === "COLOR_0").instanceDivisor =
showSplats ? 1 : 0;
// primitive.attributes.find(
// (a) => a.name === "_OPACITY"
// ).instanceDivisor = showSplats ? 1 : 0;

if (!showSplats) {
shaderBuilder.addDefine("PRIMITIVE_TYPE_POINTS");
Expand Down
18 changes: 17 additions & 1 deletion packages/engine/Source/Scene/Model/ModelDrawCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ function buildDrawCommandForModel(
screenQuadPosition: 0,
splatPosition: 6,
splatColor: 7,
splatOpacity: 8,
// splatScale:8,
// splatRot:9
// splatOpacity: 8,
};
const geometry = new Geometry({
attributes: {
Expand All @@ -151,6 +153,20 @@ function buildDrawCommandForModel(
name: "_SPLAT_COLOR",
variableName: "splatColor",
},
// splatScale: {
// ...primitiveRenderResources.runtimePrimitive.primitive.attributes.find(
// (a) => a.name === "_SCALE",
// ),
// name: "_SPLAT_SCALE",
// variableName: "splatScale"
// },
// splatRot: {
// ...primitiveRenderResources.runtimePrimitive.primitive.attributes.find(
// (a) => a.name === "_ROTATION",
// ),
// name: "_SPLAT_ROTATION",
// variableName: "splatRot"
// }
// splatOpacity: {
// ...primitiveRenderResources.runtimePrimitive.primitive.attributes.find(
// (a) => a.name === "_OPACITY"
Expand Down
9 changes: 5 additions & 4 deletions packages/engine/Source/Shaders/Model/GaussianSplatFS.glsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
void gaussianSplatStage(inout vec4 color, in ProcessedAttributes attributes) {
float A = -dot(v_vertPos, v_vertPos);
if(A < -4.0)
float A = dot(v_vertPos, v_vertPos);
if(A > 2.0)
discard;
float B = exp(A) * v_splatColor.a;
color = vec4(v_splatColor.rgb * B , B);

float B = exp(-A * 2.5) * v_splatColor.a;
color = vec4(v_splatColor.rgb * B, B);
}

10 changes: 4 additions & 6 deletions packages/engine/Source/Shaders/Model/GaussianSplatVS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ vec3 calcCov2D(vec3 worldPos, float focal_x, float focal_y, float tan_fovx, floa
void gaussianSplatStage(ProcessedAttributes attributes, inout vec4 positionClip) {
mat4 viewMatrix = czm_modelView;

vec4 clipPosition = czm_modelViewProjection * vec4(a_splatPosition,1.0);
vec4 clipPosition = czm_modelViewProjection * vec4(a_splatPosition ,1.0);
positionClip = clipPosition;

float[6] cov3D;
Expand All @@ -85,12 +85,10 @@ void gaussianSplatStage(ProcessedAttributes attributes, inout vec4 positionClip)
vec2 minorAxis = min(sqrt(2.0 * lambda2), 1024.0) * vec2(diagonalVector.y, -diagonalVector.x);

vec2 corner = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2) - 1.;
corner *= 2.0;

vec2 deltaScreenPos = (corner.x * majorAxis + corner.y * minorAxis) * 2.0 / czm_viewport.zw;
positionClip.xy += deltaScreenPos * positionClip.w;
v_vertPos = corner;
positionClip += vec4((corner.x * majorAxis + corner.y * minorAxis) * 4.0 / czm_viewport.zw * positionClip.w, 0, 0);
positionClip.z = clamp(positionClip.z, -abs(positionClip.w), abs(positionClip.w));
v_vertPos = corner ;
v_splatColor = a_splatColor;
v_splatOpacity = a_splatOpacity;
}

0 comments on commit f207786

Please sign in to comment.