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+ }
0 commit comments