33
44#include camera.glsl
55#include object.glsl
6+ #include light.glsl
7+ #include scene.glsl
8+ #include utils.glsl
69
710// Input
811layout (location = 0 ) in vec3 pos;
912layout (location = 1 ) in vec3 normal;
1013layout (location = 2 ) in vec2 uv;
11- layout (location = 3 ) in vec3 tangent;
1214
1315// Output
1416layout (location = 0 ) out vec3 v_pos;
1517layout (location = 1 ) out vec3 v_normal;
1618layout (location = 2 ) out vec2 v_uv;
17- layout (location = 3 ) out mat3 v_TBN;
1819
1920layout (set = 1 , binding = 1 ) uniform MaterialUniforms {
2021 vec4 slot1;
@@ -29,39 +30,71 @@ layout(set = 1, binding = 1) uniform MaterialUniforms {
2930
3031
3132
32- void main() {
33-
34- gl_Position = camera.viewProj * object.model * vec4 (pos, 1.0 );
3533
36- v_uv = vec2 (uv.x * material.slot2.x, ( 1 - uv.y) * material.slot2.y); // Tiling
34+ void main() {
3735
38- mat4 mv = camera.view * object.model;
39- v_pos = (mv * vec4 (pos, 1.0 )).xyz;
36+ v_pos = (object.model * vec4 (pos, 1.0 )).xyz;
37+ v_uv = vec2 (uv.x * material.slot2.x, (1 - uv.y) * material.slot2.y);
38+ v_normal = normalize (mat3 (transpose (inverse(object.model))) * normal);
4039
41- v_normal = normalize (mat3 (transpose (inverse(mv))) * normal);
40+ vec3 ndc = mapToZeroOne(v_pos, scene.minCoord.xyz, scene.maxCoord.xyz) * 2.0 - 1.0 ;
41+ gl_Position = vec4 (ndc, 1.0 );
42+ // gl_Position = camera.viewProj * vec4(v_pos, 1.0);
43+
44+ }
45+ #shader geometry
46+ #version 460
4247
43- if (int (material.slot5.x) == 1 ) { // If has normal texture
44- vec3 T = normalize (vec3 (mv * vec4 (tangent, 0.0 )));
45- vec3 N = normalize (vec3 (mv * vec4 (normal, 0.0 )));
46- vec3 B = cross (N, T);
47- v_TBN = mat3 (T, B, N);
48- }
48+ layout (triangles) in ;
49+ layout (triangle_strip, max_vertices = 3 ) out ;
50+
51+ layout (location = 0 ) in vec3 v_pos[];
52+ layout (location = 1 ) in vec3 v_normal[];
53+ layout (location = 2 ) in vec2 v_uv[];
54+
55+ layout (location = 0 ) out vec3 _pos;
56+ layout (location = 1 ) out vec3 _normal;
57+ layout (location = 2 ) out vec2 _uv;
58+
59+ void main(){
60+
61+ const vec3 p1 = gl_in[1 ].gl_Position .xyz - gl_in[0 ].gl_Position .xyz;
62+ const vec3 p2 = gl_in[2 ].gl_Position .xyz - gl_in[0 ].gl_Position .xyz;
63+ const vec3 p = abs (cross (p1, p2));
64+
65+ for (uint i = 0 ; i < 3 ; ++ i){
66+ _pos = v_pos[i];
67+ _normal = v_normal[i];
68+ _uv = v_uv[i];
69+ if (p.z > p.x && p.z > p.y){
70+ gl_Position = vec4 (gl_in[i].gl_Position .x, gl_in[i].gl_Position .y, 0 , 1 );
71+ } else if (p.x > p.y && p.x > p.z){
72+ gl_Position = vec4 (gl_in[i].gl_Position .y, gl_in[i].gl_Position .z, 0 , 1 );
73+ } else {
74+ gl_Position = vec4 (gl_in[i].gl_Position .x, gl_in[i].gl_Position .z, 0 , 1 );
75+ }
76+ EmitVertex();
77+ }
78+ EndPrimitive();
4979}
5080
81+
5182#shader fragment
5283#version 460
84+ #include light.glsl
85+ #include scene.glsl
86+ #include utils.glsl
5387#include material_defines.glsl
5488
55- layout (location = 0 ) in vec3 v_pos;
56- layout (location = 1 ) in vec3 v_normal;
57- layout (location = 2 ) in vec2 v_uv;
58- layout (location = 3 ) in mat3 v_TBN;
89+ layout (location = 0 ) in vec3 _pos;
90+ layout (location = 1 ) in vec3 _normal;
91+ layout (location = 2 ) in vec2 _uv;
5992
6093layout (set = 0 , binding = 2 ) uniform sampler2DArray shadowMap;
6194layout (set = 0 , binding = 3 ) uniform samplerCube irradianceMap;
6295// layout(set = 0, binding = 4) uniform accelerationStructureEXT TLAS;
6396layout (set = 0 , binding = 5 ) uniform sampler2D blueNoiseMap;
64- layout (set = 0 , binding = 6 , r32f) uniform image3D voxelImage;
97+ layout (set = 0 , binding = 6 , r32f) uniform image3D voxelImage;
6598
6699layout (set = 1 , binding = 1 ) uniform MaterialUniforms {
67100 vec4 slot1;
@@ -82,13 +115,6 @@ layout(set = 2, binding = 3) uniform sampler2D materialText2;
82115layout (set = 2 , binding = 4 ) uniform sampler2D materialText3;
83116layout (set = 2 , binding = 5 ) uniform sampler2D materialText4;
84117
85- // Output
86- // layout(location = 0) out vec4 outPos;
87- // layout(location = 1) out vec4 outNormal;
88- // layout(location = 2) out vec4 outAlbedo;
89- // layout(location = 3) out vec4 outMaterial; //U8
90- // layout(location = 4) out vec4 outEmissionF; //F32
91- // layout(location = 5) out vec4 outTemporal;
92118
93119#define EPSILON 0.1
94120
@@ -108,12 +134,11 @@ void setupSurfaceProperties(){
108134 if (material.slot8.w == PHYSICAL_MATERIAL){
109135
110136 // Setting input surface properties
111- g_albedo = int (material.slot4.w)== 1 ? mix (material.slot1.rgb, texture(albedoTex, v_uv).rgb, material.slot3.x) : material.slot1.rgb;
112- g_opacity = int (material.slot4.w)== 1 ? mix (material.slot1.w, texture(albedoTex, v_uv).a, material.slot6.z) : material.slot1.w;
113- g_normal = int (material.slot5.x)== 1 ? normalize ((v_TBN * (texture(normalTex, v_uv).rgb * 2.0 - 1.0 ))) : normalize ( v_normal );
137+ g_albedo = int (material.slot4.w)== 1 ? mix (material.slot1.rgb, texture(albedoTex, _uv).rgb, material.slot3.x) : material.slot1.rgb;
138+ g_opacity = int (material.slot4.w)== 1 ? mix (material.slot1.w, texture(albedoTex, _uv).a, material.slot6.z) : material.slot1.w;
114139
115140 if (int (material.slot6.x)== 1 ) {
116- vec4 mask = texture(materialText1, v_uv ).rgba; // Correction linearize color
141+ vec4 mask = texture(materialText1, _uv ).rgba; // Correction linearize color
117142 if (int (material.slot6.y) == 0 ) { // HDRP UNITY
118143 // Unity HDRP uses glossiness not roughness pipeline, so it has to be inversed
119144 g_material.r = 1.0 - mix (material.slot3.w,mask.a, material.slot4.x);
@@ -127,12 +152,12 @@ void setupSurfaceProperties(){
127152 // TO DO ...
128153 }
129154 } else {
130- g_material.r = material.slot5.y== 1 ? mix (material.slot3.w, texture(materialText1, v_uv ).r, material.slot4.x) : material.slot3.w; // Roughness
131- g_material.g = material.slot5.z== 1 ? mix (material.slot3.y, texture(materialText2, v_uv ).r, material.slot3.z) : material.slot3.y; // Metalness
132- g_material.b = material.slot5.w== 1 ? mix (material.slot4.y, texture(materialText3, v_uv ).r, material.slot4.z) : material.slot4.y; // AO
155+ g_material.r = material.slot5.y== 1 ? mix (material.slot3.w, texture(materialText1, _uv ).r, material.slot4.x) : material.slot3.w; // Roughness
156+ g_material.g = material.slot5.z== 1 ? mix (material.slot3.y, texture(materialText2, _uv ).r, material.slot3.z) : material.slot3.y; // Metalness
157+ g_material.b = material.slot5.w== 1 ? mix (material.slot4.y, texture(materialText3, _uv ).r, material.slot4.z) : material.slot4.y; // AO
133158 }
134159
135- g_emisison = material.slot6.w == 1 ? mix (material.slot7.rgb, texture(materialText4, v_uv ).rgb, material.slot7.w) : material.slot7.rgb;
160+ g_emisison = material.slot6.w == 1 ? mix (material.slot7.rgb, texture(materialText4, _uv ).rgb, material.slot7.w) : material.slot7.rgb;
136161 g_emisison *= material.slot8.x;
137162
138163 g_fresnelThreshold = material.slot8.y;
@@ -141,7 +166,7 @@ void setupSurfaceProperties(){
141166
142167 }
143168 if (material.slot8.w == UNLIT_MATERIAL){
144- g_albedo = int (material.slot2.w) == 1 ? texture(albedoTex, v_uv ).rgb : material.slot1.rgb;
169+ g_albedo = int (material.slot2.w) == 1 ? texture(albedoTex, _uv ).rgb : material.slot1.rgb;
145170 g_material.w = UNLIT_MATERIAL;
146171 }
147172 if (material.slot8.w == HAIR_STRAND_MATERIAL){
@@ -155,12 +180,59 @@ void setupSurfaceProperties(){
155180
156181
157182}
183+ ivec3 worldSpaceToVoxelSpace(vec3 worldPos)
184+ {
185+ vec3 uvw = mapToZeroOne(worldPos, scene.minCoord.xyz, scene.maxCoord.xyz);
186+ ivec3 voxelPos = ivec3 (uvw * imageSize(voxelImage));
187+ return voxelPos;
188+ }
189+ // vec3 evaluateDiffuseLighting(Light light)
190+ // {
191+
192+ // float cosTheta = dot(normalize(inData.Normal), lightDir);
193+ // if (cosTheta > 0.0)
194+ // {
195+ // vec3 diffuse = light.Color * cosTheta * albedo;
196+ // float attenuation = GetAttenuationFactor(dist * dist, light.Radius);
197+
198+ // return diffuse * attenuation;
199+ // }
200+
201+ // return vec3(0.0);
202+ // }
203+
158204
159205
160206void main() {
161207
162- // setupSurfaceProperties();
208+
209+ setupSurfaceProperties();
210+
211+ vec3 color = vec3 (0.0 );
212+
213+ for (int i = 0 ; i < scene.numLights; i++ ) {
214+ if (isInAreaOfInfluence(scene.lights[i], _pos)){
215+
216+ // Diffuse Component ________________________
217+
218+ // vec3 lighting = vec3(0.0);
219+ // lighting = evalSchlickSmithBRDF(
220+ // scene.lights[i].type != DIRECTIONAL_LIGHT ? normalize(scene.lights[i].position - g_pos) : normalize(scene.lights[i].position.xyz), //wi
221+ // normalize(-g_pos), //wo
222+ // scene.lights[i].color * computeAttenuation( scene.lights[i], g_pos) * scene.lights[i].intensity, //radiance
223+ // brdf
224+ // );
225+
226+ // color += lighting;
227+ }
228+ }
229+
230+ color = g_albedo;
231+ color += g_emisison;
232+
163233
164- imageStore(voxelImage, ivec3 (gl_FragCoord .x,gl_FragCoord .y ,0 ), vec4 (1.0 ));
234+ vec4 result = g_opacity * vec4 (vec3 (color), 1 );
235+ ivec3 voxelPos = worldSpaceToVoxelSpace(_pos);
236+ imageStore(voxelImage, voxelPos, result);
165237
166238}
0 commit comments