Skip to content

Commit 6c4bec3

Browse files
authored
add gaussianSplat mat file for shader (#7192)
* add gaussianSplat mat file for shader * calculate color from f_rest * add test cases * add color space test case in gaussianSplat.mat --------- Co-authored-by: hanyin-intel <[email protected]> Co-authored-by: Zhang Yang <[email protected]>
1 parent 8893079 commit 6c4bec3

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

Diff for: cpp/open3d/visualization/gui/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ open3d_add_compiled_materials(materials
9292
Materials/infiniteGroundPlane.mat
9393
Materials/normals.mat
9494
Materials/pointcloud.mat
95+
Materials/gaussianSplat.mat
9596
Materials/ui_blit.mat
9697
Materials/unlitBackground.mat
9798
Materials/unlitGradient.mat
+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
material {
2+
name : defaultLit,
3+
shadingModel : lit,
4+
doubleSided : true,
5+
parameters : [
6+
{ type : float3, name : baseColor },
7+
{ type : float, name : baseMetallic },
8+
{ type : float, name : baseRoughness },
9+
{ type : float, name : reflectance },
10+
{ type : float, name : pointSize },
11+
{ type : float, name : clearCoat },
12+
{ type : float, name : clearCoatRoughness },
13+
{ type : float, name : anisotropy },
14+
{ type : sampler2d, name : albedo },
15+
{ type : sampler2d, name : ao_rough_metalMap },
16+
{ type : sampler2d, name : normalMap },
17+
{ type : sampler2d, name : reflectanceMap },
18+
{ type : sampler2d, name : anisotropyMap },
19+
{ type : int, name : shDegree }
20+
],
21+
variables : [
22+
colorFromSh,
23+
test1,
24+
test2,
25+
test3
26+
],
27+
requires : [
28+
color, // scale
29+
tangents,
30+
custom0, // f_dc and opacity
31+
custom1, // custom1-custom6 store f_rest_0-f_rest_23 SH coeffs
32+
custom2,
33+
custom3,
34+
custom4,
35+
custom5,
36+
custom6,
37+
custom7 // rot
38+
]
39+
}
40+
41+
vertex {
42+
#include "../../shader/glsl/ShCoeffsToColorFast.glsl"
43+
44+
void materialVertex(inout MaterialVertexInputs material) {
45+
gl_PointSize = materialParams.pointSize;
46+
47+
// refering to https://google.github.io/filament/Materials.html#materialdefinitions/shaderpublicapis/vertexonly,
48+
// the worldPosition coordinate in the vertex shader is shifted by the camera position.
49+
vec3 dir = material.worldPosition.xyz;
50+
float inorm = inversesqrt(dot(dir, dir));
51+
dir = dir * inorm;
52+
53+
// According to "../../shader/glsl/ShCoeffsToColorFast.glsl", the coeffs should be in the following order:
54+
// [Y0_R, Y0_G, Y0_B, Y1m1_R, Y1m1_G, Y1m1_B, Y10_R, Y10_G, Y10_B, Y11_R, Y11_G, Y11_B, Y2m2_R, Y2m2_G, Y2m2_B, ...]
55+
vec3 colors;
56+
57+
float4 f_dc_and_opacity = getCustom0();
58+
colors = sh0_coeffs_to_color_fast(f_dc_and_opacity.xyz);
59+
60+
if (materialParams.shDegree >= 1) {
61+
float coeffs_sh1[3 * 3];
62+
float4 buffer1 = getCustom1();
63+
float4 buffer2 = getCustom2();
64+
65+
for (int i = 0; i < 4; i++) {
66+
coeffs_sh1[i] = buffer1[i];
67+
coeffs_sh1[4 + i] = buffer2[i];
68+
}
69+
70+
float4 buffer3 = getCustom3();
71+
coeffs_sh1[8] = buffer3[0];
72+
73+
colors += sh1_coeffs_to_color_fast(dir, coeffs_sh1);
74+
}
75+
76+
if (materialParams.shDegree >= 2) {
77+
float coeffs_sh2[5 * 3];
78+
79+
float4 buffer3 = getCustom3();
80+
coeffs_sh2[0] = buffer3[1];
81+
coeffs_sh2[1] = buffer3[2];
82+
coeffs_sh2[2] = buffer3[3];
83+
84+
float4 buffer4 = getCustom4();
85+
float4 buffer5 = getCustom5();
86+
float4 buffer6 = getCustom6();
87+
88+
for (int i = 0; i < 4; i++) {
89+
coeffs_sh2[3 + i] = buffer4[i];
90+
coeffs_sh2[3 + 4 + i] = buffer5[i];
91+
coeffs_sh2[3 + 2*4 + i] = buffer6[i];
92+
}
93+
94+
colors += sh2_coeffs_to_color_fast(dir, coeffs_sh2);
95+
}
96+
97+
material.colorFromSh.r = colors[0];
98+
material.colorFromSh.g = colors[1];
99+
material.colorFromSh.b = colors[2];
100+
material.colorFromSh.a = 0.0;
101+
102+
// The following code is just used for verifing we can get data from different buffer.
103+
material.test1 = getCustom7();
104+
material.test2 = getCustom1();
105+
material.test3 = getCustom6();
106+
}
107+
}
108+
109+
fragment {
110+
float sRGB_to_linear(float color) {
111+
return color <= 0.04045 ? color / 12.92 : pow((color + 0.055) / 1.055, 2.4);
112+
}
113+
114+
float linear_to_sRGB(float color) {
115+
return color <= 0.0031308 ? color * 12.92 : 1.055 * pow(color, 1.0 / 2.4) - 0.055;
116+
}
117+
118+
void material(inout MaterialInputs material) {
119+
prepareMaterial(material);
120+
121+
// The following code is only used to test we can get colorFromSh from vertex shader.
122+
float r = 0.0;
123+
float g = 0.0;
124+
float b = 0.0;
125+
material.baseColor.rgb = vec3(r, g, b);
126+
127+
// test case1: verify the effect of 'f_dc'
128+
material.baseColor.rgb = variable_colorFromSh.rgb;
129+
130+
// test case2: Check we can get data 'rot'.
131+
// material.baseColor.rgb = variable_test1.rgb;
132+
133+
// test case3: Check we can get data 'f_rest' of degree 1
134+
// material.baseColor.rgb = variable_test2.rgb * 10.0;
135+
136+
// test case4: Check we can get data 'f_rest' of degree 2
137+
// material.baseColor.rgb = variable_test3.rgb * 10.0;
138+
139+
// test case5: Check we can get data 'scale'
140+
// material.baseColor.rgb = getColor().rgb * (-0.05);
141+
142+
// test case6: test the color space(linear RGB or sRGB)
143+
// r = linear_to_sRGB(variable_colorFromSh.r);
144+
// g = linear_to_sRGB(variable_colorFromSh.g);
145+
// b = linear_to_sRGB(variable_colorFromSh.b);
146+
// r = sRGB_to_linear(variable_colorFromSh.r);
147+
// g = sRGB_to_linear(variable_colorFromSh.g);
148+
// b = sRGB_to_linear(variable_colorFromSh.b);
149+
// material.baseColor.rgb = vec3(r, g, b);
150+
}
151+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Refering to [this link](https://github.com/nerfstudio-project/gsplat/blob/bd64a47414e182dc105c1a2fdb6691068518d060/gsplat/cuda/include/spherical_harmonics.cuh)
2+
3+
vec3 sh0_coeffs_to_color_fast(vec3 coeffs_sh0) {
4+
return 0.2820947917738781 * coeffs_sh0;
5+
}
6+
7+
// vec3 dir must be normalized vector
8+
vec3 sh1_coeffs_to_color_fast(vec3 dir, float coeffs[3 * 3]) {
9+
vec3 result;
10+
for (int i = 0; i < 3; i++) {
11+
result[i] = 0.48860251190292 * (-dir.y * coeffs[0 + i] + dir.z * coeffs[1 * 3 + i] - dir.x * coeffs[2 * 3 + i]);
12+
}
13+
return result;
14+
}
15+
16+
// vec3 dir must be normalized vector
17+
vec3 sh2_coeffs_to_color_fast(vec3 dir, float coeffs[5 * 3]) {
18+
float inorm = inversesqrt(dot(dir, dir));
19+
float x = dir.x * inorm;
20+
float y = dir.y * inorm;
21+
float z = dir.z * inorm;
22+
23+
float z2 = dir.z * dir.z;
24+
float fTmp0B = -1.092548430592079 * dir.z;
25+
float fC1 = dir.x * dir.x - dir.y * dir.y;
26+
float fS1 = 2.0 * dir.x * dir.y;
27+
28+
float pSH6 = (0.9461746957575601 * z2 - 0.3153915652525201);
29+
float pSH7 = fTmp0B * dir.x;
30+
float pSH5 = fTmp0B * dir.y;
31+
float pSH8 = 0.5462742152960395 * fC1;
32+
float pSH4 = 0.5462742152960395 * fS1;
33+
34+
vec3 result;
35+
for (int i = 0; i < 3; i++) {
36+
result[i] = pSH4 * coeffs[0 + i] + pSH5 * coeffs[1 * 3 + i] +
37+
pSH6 * coeffs[2 * 3 + i] + pSH7 * coeffs[3 * 3 + i] +
38+
pSH8 * coeffs[4 * 3 + i];
39+
}
40+
41+
return result;
42+
}

0 commit comments

Comments
 (0)