@@ -52,23 +52,23 @@ void GetNormalsAndTexCoord(inout State state, inout Ray r)
52
52
state.normal = normal;
53
53
state.ffnormal = dot (normal, r.direction) <= 0.0 ? normal : normal * - 1.0 ;
54
54
55
- Onb(state.ffnormal , state.tangent, state.bitangent);
55
+ Onb(state.normal , state.tangent, state.bitangent);
56
56
}
57
57
58
58
// -----------------------------------------------------------------------
59
59
void GetMaterialsAndTextures(inout State state, in Ray r)
60
60
// -----------------------------------------------------------------------
61
61
{
62
- int index = state.matID;
62
+ int index = state.matID * 7 ;
63
63
Material mat;
64
64
65
- vec4 param1 = texelFetch(materialsTex, ivec2 (index * 7 + 0 , 0 ), 0 );
66
- vec4 param2 = texelFetch(materialsTex, ivec2 (index * 7 + 1 , 0 ), 0 );
67
- vec4 param3 = texelFetch(materialsTex, ivec2 (index * 7 + 2 , 0 ), 0 );
68
- vec4 param4 = texelFetch(materialsTex, ivec2 (index * 7 + 3 , 0 ), 0 );
69
- vec4 param5 = texelFetch(materialsTex, ivec2 (index * 7 + 4 , 0 ), 0 );
70
- vec4 param6 = texelFetch(materialsTex, ivec2 (index * 7 + 5 , 0 ), 0 );
71
- vec4 param7 = texelFetch(materialsTex, ivec2 (index * 7 + 6 , 0 ), 0 );
65
+ vec4 param1 = texelFetch(materialsTex, ivec2 (index + 0 , 0 ), 0 );
66
+ vec4 param2 = texelFetch(materialsTex, ivec2 (index + 1 , 0 ), 0 );
67
+ vec4 param3 = texelFetch(materialsTex, ivec2 (index + 2 , 0 ), 0 );
68
+ vec4 param4 = texelFetch(materialsTex, ivec2 (index + 3 , 0 ), 0 );
69
+ vec4 param5 = texelFetch(materialsTex, ivec2 (index + 4 , 0 ), 0 );
70
+ vec4 param6 = texelFetch(materialsTex, ivec2 (index + 5 , 0 ), 0 );
71
+ vec4 param7 = texelFetch(materialsTex, ivec2 (index + 6 , 0 ), 0 );
72
72
73
73
mat.albedo = param1.xyz;
74
74
mat.specular = param1.w;
@@ -106,9 +106,10 @@ void GetMaterialsAndTextures(inout State state, in Ray r)
106
106
if (int (mat.texIDs.y) >= 0 )
107
107
{
108
108
vec2 matRgh;
109
+ // TODO: Change metallic roughness maps in repo to linear space and remove gamma correction
109
110
matRgh = pow (texture(textureMapsArrayTex, vec3 (texUV, int (mat.texIDs.y))).zy, vec2 (2.2 ));
110
111
mat.metallic = matRgh.x;
111
- mat.roughness = matRgh.y;
112
+ mat.roughness = max ( matRgh.y, 0.001 ) ;
112
113
}
113
114
114
115
// Normal Map
@@ -117,21 +118,21 @@ void GetMaterialsAndTextures(inout State state, in Ray r)
117
118
vec3 nrm = texture(textureMapsArrayTex, vec3 (texUV, int (mat.texIDs.z))).xyz;
118
119
nrm = normalize (nrm * 2.0 - 1.0 );
119
120
120
- // Orthonormal Basis
121
121
vec3 T, B;
122
- Onb(state.ffnormal , T, B);
122
+ Onb(state.normal , T, B);
123
123
124
- nrm = T * nrm.x + B * nrm.y + state.ffnormal * nrm.z;
124
+ nrm = T * nrm.x + B * nrm.y + state.normal * nrm.z;
125
125
state.normal = normalize (nrm);
126
126
state.ffnormal = dot (state.normal, r.direction) <= 0.0 ? state.normal : state.normal * - 1.0 ;
127
127
128
- Onb(state.ffnormal , state.tangent, state.bitangent);
128
+ Onb(state.normal , state.tangent, state.bitangent);
129
129
}
130
130
131
+ // Commented out the following as anisotropic param is temporarily unused.
131
132
// Calculate anisotropic roughness along the tangent and bitangent directions
132
- float aspect = sqrt (1.0 - mat.anisotropic * 0.9 );
133
- mat.ax = max (0.001 , mat.roughness / aspect);
134
- mat.ay = max (0.001 , mat.roughness * aspect);
133
+ // float aspect = sqrt(1.0 - mat.anisotropic * 0.9);
134
+ // mat.ax = max(0.001, mat.roughness / aspect);
135
+ // mat.ay = max(0.001, mat.roughness * aspect);
135
136
136
137
state.mat = mat;
137
138
state.eta = dot (state.normal, state.ffnormal) > 0.0 ? (1.0 / mat.ior) : mat.ior;
@@ -180,38 +181,36 @@ vec3 DirectLight(in Ray r, in State state)
180
181
Light light;
181
182
182
183
// Pick a light to sample
183
- int index = int (rand() * float (numOfLights));
184
+ int index = int (rand() * float (numOfLights)) * 5 ;
184
185
185
186
// Fetch light Data
186
- vec3 position = texelFetch(lightsTex, ivec2 (index * 5 + 0 , 0 ), 0 ).xyz;
187
- vec3 emission = texelFetch(lightsTex, ivec2 (index * 5 + 1 , 0 ), 0 ).xyz;
188
- vec3 u = texelFetch(lightsTex, ivec2 (index * 5 + 2 , 0 ), 0 ).xyz; // u vector for rect
189
- vec3 v = texelFetch(lightsTex, ivec2 (index * 5 + 3 , 0 ), 0 ).xyz; // v vector for rect
190
- vec3 params = texelFetch(lightsTex, ivec2 (index * 5 + 4 , 0 ), 0 ).xyz;
187
+ vec3 position = texelFetch(lightsTex, ivec2 (index + 0 , 0 ), 0 ).xyz;
188
+ vec3 emission = texelFetch(lightsTex, ivec2 (index + 1 , 0 ), 0 ).xyz;
189
+ vec3 u = texelFetch(lightsTex, ivec2 (index + 2 , 0 ), 0 ).xyz; // u vector for rect
190
+ vec3 v = texelFetch(lightsTex, ivec2 (index + 3 , 0 ), 0 ).xyz; // v vector for rect
191
+ vec3 params = texelFetch(lightsTex, ivec2 (index + 4 , 0 ), 0 ).xyz;
191
192
float radius = params.x;
192
193
float area = params.y;
193
- float type = params.z; // 0->rect , 1->sphere
194
+ float type = params.z; // 0->Rect , 1->Sphere, 2->Distant
194
195
195
196
light = Light(position, emission, u, v, radius, area, type);
196
- sampleLight (light, lightSampleRec);
197
+ sampleOneLight (light, surfacePos , lightSampleRec);
197
198
198
- vec3 lightDir = lightSampleRec.surfacePos - surfacePos;
199
- float lightDist = length (lightDir);
200
- float lightDistSq = lightDist * lightDist;
201
- lightDir /= lightDist;
202
-
203
- if (dot (lightDir, lightSampleRec.normal) < 0.0 )
199
+ if (dot (lightSampleRec.direction, lightSampleRec.normal) < 0.0 )
204
200
{
205
- Ray shadowRay = Ray(surfacePos, lightDir );
206
- bool inShadow = AnyHit(shadowRay, lightDist - EPS);
201
+ Ray shadowRay = Ray(surfacePos, lightSampleRec.direction );
202
+ bool inShadow = AnyHit(shadowRay, lightSampleRec.dist - EPS);
207
203
208
204
if (! inShadow)
209
205
{
210
- bsdfSampleRec.f = DisneyEval(state, - r.direction, state.ffnormal, lightDir, bsdfSampleRec.pdf);
211
- float lightPdf = lightDistSq / (light.area * abs (dot (lightSampleRec.normal, lightDir)));
206
+ bsdfSampleRec.f = DisneyEval(state, - r.direction, state.ffnormal, lightSampleRec.direction, bsdfSampleRec.pdf);
207
+
208
+ float weight = 1.0 ;
209
+ if (light.area > 0.0 )
210
+ weight = powerHeuristic(lightSampleRec.pdf, bsdfSampleRec.pdf);
212
211
213
212
if (bsdfSampleRec.pdf > 0.0 )
214
- Li += powerHeuristic(lightPdf, bsdfSampleRec.pdf) * bsdfSampleRec.f * abs (dot (state.ffnormal, lightDir )) * lightSampleRec.emission / lightPdf ;
213
+ Li += weight * bsdfSampleRec.f * abs (dot (state.ffnormal, lightSampleRec.direction )) * lightSampleRec.emission / lightSampleRec.pdf ;
215
214
}
216
215
}
217
216
}
0 commit comments