浏览代码

Finalize before testing

Panagiotis Christopoulos Charitos 7 年之前
父节点
当前提交
934752ceb3
共有 4 个文件被更改,包括 119 次插入55 次删除
  1. 74 2
      samples/sponza/assets/scene.lua
  2. 20 19
      shaders/LightFunctions.glsl
  3. 24 33
      shaders/Reflections.ankiprog
  4. 1 1
      tools/scene/Exporter.cpp

+ 74 - 2
samples/sponza/assets/scene.lua

@@ -77,9 +77,81 @@ trf:setRotation(rot)
 trf:setScale(0.259678)
 trf:setScale(0.259678)
 node:getSceneNodeBase():getMoveComponent():setLocalTransform(trf)
 node:getSceneNodeBase():getMoveComponent():setLocalTransform(trf)
 
 
-node = scene:newReflectionProbeNode("reflprobe0", Vec4.new(-0.5, -0.5, -0.5, 0), Vec4.new(0.5, 0.5, 0.5, 0))
+node = scene:newReflectionProbeNode("reflprobe0", Vec4.new(-24.8339, -3.89695, -4.12719, 0), Vec4.new(24.8339, 3.89695, 4.12719, 0))
 trf = Transform.new()
 trf = Transform.new()
-trf:setOrigin(Vec4.new(0.510497, 8.00824, -10.5717, 0))
+trf:setOrigin(Vec4.new(-1.00211, 3.65293, -7.63218, 0))
+rot = Mat3x4.new()
+rot:setAll(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0)
+trf:setRotation(rot)
+trf:setScale(1)
+node:getSceneNodeBase():getMoveComponent():setLocalTransform(trf)
+
+node = scene:newReflectionProbeNode("reflprobe1", Vec4.new(-24.8339, -4.2659, -4.12719, 0), Vec4.new(24.8339, 4.2659, 4.12719, 0))
+trf = Transform.new()
+trf:setOrigin(Vec4.new(-1.00211, 11.4761, -7.63218, 0))
+rot = Mat3x4.new()
+rot:setAll(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0)
+trf:setRotation(rot)
+trf:setScale(1)
+node:getSceneNodeBase():getMoveComponent():setLocalTransform(trf)
+
+node = scene:newReflectionProbeNode("reflprobe2", Vec4.new(-24.8339, -4.2659, -4.12719, 0), Vec4.new(24.8339, 4.2659, 4.12719, 0))
+trf = Transform.new()
+trf:setOrigin(Vec4.new(-1.00211, 19.8704, -7.63218, 0))
+rot = Mat3x4.new()
+rot:setAll(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0)
+trf:setRotation(rot)
+trf:setScale(1)
+node:getSceneNodeBase():getMoveComponent():setLocalTransform(trf)
+
+node = scene:newReflectionProbeNode("reflprobe3", Vec4.new(-24.8339, -4.2659, -4.12719, 0), Vec4.new(24.8339, 4.2659, 4.12719, 0))
+trf = Transform.new()
+trf:setOrigin(Vec4.new(-1.00211, 19.8704, -0.0445416, 0))
+rot = Mat3x4.new()
+rot:setAll(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0)
+trf:setRotation(rot)
+trf:setScale(1)
+node:getSceneNodeBase():getMoveComponent():setLocalTransform(trf)
+
+node = scene:newReflectionProbeNode("reflprobe4", Vec4.new(-24.8339, -4.2659, -4.12719, 0), Vec4.new(24.8339, 4.2659, 4.12719, 0))
+trf = Transform.new()
+trf:setOrigin(Vec4.new(-1.00211, 11.4761, -0.0445416, 0))
+rot = Mat3x4.new()
+rot:setAll(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0)
+trf:setRotation(rot)
+trf:setScale(1)
+node:getSceneNodeBase():getMoveComponent():setLocalTransform(trf)
+
+node = scene:newReflectionProbeNode("reflprobe5", Vec4.new(-24.8339, -3.89695, -4.12719, 0), Vec4.new(24.8339, 3.89695, 4.12719, 0))
+trf = Transform.new()
+trf:setOrigin(Vec4.new(-1.00211, 3.65293, -0.0445416, 0))
+rot = Mat3x4.new()
+rot:setAll(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0)
+trf:setRotation(rot)
+trf:setScale(1)
+node:getSceneNodeBase():getMoveComponent():setLocalTransform(trf)
+
+node = scene:newReflectionProbeNode("reflprobe6", Vec4.new(-24.8339, -3.89695, -4.12719, 0), Vec4.new(24.8339, 3.89695, 4.12719, 0))
+trf = Transform.new()
+trf:setOrigin(Vec4.new(-1.00211, 3.65293, 6.48553, 0))
+rot = Mat3x4.new()
+rot:setAll(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0)
+trf:setRotation(rot)
+trf:setScale(1)
+node:getSceneNodeBase():getMoveComponent():setLocalTransform(trf)
+
+node = scene:newReflectionProbeNode("reflprobe7", Vec4.new(-24.8339, -4.2659, -4.12719, 0), Vec4.new(24.8339, 4.2659, 4.12719, 0))
+trf = Transform.new()
+trf:setOrigin(Vec4.new(-1.00211, 11.4761, 6.48553, 0))
+rot = Mat3x4.new()
+rot:setAll(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0)
+trf:setRotation(rot)
+trf:setScale(1)
+node:getSceneNodeBase():getMoveComponent():setLocalTransform(trf)
+
+node = scene:newReflectionProbeNode("reflprobe8", Vec4.new(-24.8339, -4.2659, -4.12719, 0), Vec4.new(24.8339, 4.2659, 4.12719, 0))
+trf = Transform.new()
+trf:setOrigin(Vec4.new(-1.00211, 19.8704, 6.48553, 0))
 rot = Mat3x4.new()
 rot = Mat3x4.new()
 rot:setAll(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0)
 rot:setAll(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0)
 trf:setRotation(rot)
 trf:setRotation(rot)

+ 20 - 19
shaders/LightFunctions.glsl

@@ -191,16 +191,12 @@ F32 computeAttenuationFactor(F32 lightRadius, Vec3 frag2Light)
 	return att * att;
 	return att * att;
 }
 }
 
 
-// Given the probe properties trace a ray inside the probe and find the cube tex coordinates to sample and the color
-// blend weight
-void intersectProbe(Vec3 fragPos, // Ray origin
+// Given the probe properties trace a ray inside the probe and find the cube tex coordinates to sample
+Vec3 intersectProbe(Vec3 fragPos, // Ray origin
 	Vec3 rayDir, // Ray direction
 	Vec3 rayDir, // Ray direction
 	Vec3 probeAabbMin,
 	Vec3 probeAabbMin,
 	Vec3 probeAabbMax,
 	Vec3 probeAabbMax,
-	Vec3 probeOrigin, // Cubemap origin
-	F32 fadeDistance,
-	out Vec3 cubeTexCoord, // Cubemap tex coord
-	out F32 blendWeight // Blend weight
+	Vec3 probeOrigin // Cubemap origin
 )
 )
 {
 {
 	// Compute the intersection point
 	// Compute the intersection point
@@ -208,19 +204,24 @@ void intersectProbe(Vec3 fragPos, // Ray origin
 	Vec3 intersectionPoint = fragPos + intresectionDist * rayDir;
 	Vec3 intersectionPoint = fragPos + intresectionDist * rayDir;
 
 
 	// Compute the cubemap vector
 	// Compute the cubemap vector
-	cubeTexCoord = intersectionPoint - probeOrigin;
-
-	// Calculate the weight
-	{
-		// Distance between the frag and the AABB bounds
-		F32 fragBoxDist = length(fragPos - intersectionPoint);
-
-		// Cap the distance
-		fragBoxDist = min(fragBoxDist, fadeDistance);
+	return intersectionPoint - probeOrigin;
+}
 
 
-		// The weight will be zero close to AABB bounds and 1.0 at fadeDistance and less
-		blendWeight = fragBoxDist / fadeDistance;
-	}
+// Compute a weight (factor) of fragPos against some probe's bounds. The weight will be zero when fragPos is close to
+// AABB bounds and 1.0 at fadeDistance and less.
+F32 computeProbeBlendWeight(Vec3 fragPos, // Doesn't need to be inside the AABB
+	Vec3 probeAabbMin,
+	Vec3 probeAabbMax,
+	F32 fadeDistance)
+{
+	// Compute the min distance of fragPos from the edges of the AABB
+	Vec3 distFromMin = fragPos - probeAabbMin;
+	Vec3 distFromMax = probeAabbMax - fragPos;
+	Vec3 minDistVec = min(distFromMin, distFromMax);
+	F32 minDist = min(minDistVec.x, min(minDistVec.y, minDistVec.z));
+
+	// Use saturate because minDist might be negative.
+	return saturate(minDist / fadeDistance);
 }
 }
 
 
 #endif
 #endif

+ 24 - 33
shaders/Reflections.ankiprog

@@ -174,45 +174,36 @@ void readReflectionsAndIrradianceFromProbes(
 
 
 	F32 reflLod = F32(IR_MIPMAP_COUNT - 1u) * roughness;
 	F32 reflLod = F32(IR_MIPMAP_COUNT - 1u) * roughness;
 
 
+	F32 totalBlendWeight = EPSILON;
+
 	// Check proxy
 	// Check proxy
 	U32 count = u_lightIndices[idxOffset++];
 	U32 count = u_lightIndices[idxOffset++];
 	ANKI_LOOP while(count-- != 0)
 	ANKI_LOOP while(count-- != 0)
 	{
 	{
 		ReflectionProbe probe = u_reflectionProbes[u_lightIndices[idxOffset++]];
 		ReflectionProbe probe = u_reflectionProbes[u_lightIndices[idxOffset++]];
-
-		Vec3 cubeCoord;
-		F32 blendWeight;
-		intersectProbe(worldPos, reflDir, probe.m_aabbMinPad1.xyz, probe.m_aabbMinPad1.xyz, 
-			probe.m_positionCubemapIndex.xyz, 0.2, cubeCoord, blendWeight);
-
-		diffIndirect = Vec3(blendWeight);
-
-#if 0
-		F32 R2 = probe.m_positionRadiusSq.w;
-		Vec3 center = probe.m_positionRadiusSq.xyz;
-
-		// Get distance from the center of the probe
-		Vec3 f = worldPos - center;
-
-		// Cubemap UV in view space
-		Vec3 uv = computeCubemapVecAccurate(reflDir, R2, f);
-
-		// Read!
-		F32 cubemapIndex = probe.m_cubemapIndexPad3.x;
-		Vec3 c = textureLod(u_reflectionsTex, Vec4(uv, cubemapIndex), reflLod).rgb;
-
-		// Combine (lerp) with previous color
-		F32 d = dot(f, f);
-		F32 factor = d / R2;
-		factor = min(factor, 1.0);
-		specIndirect = mix(c, specIndirect, factor);
-
-		// Do the same for diffuse
-		uv = computeCubemapVecAccurate(normal, R2, f);
-		Vec3 id = textureLod(u_irradianceTex, Vec4(uv, cubemapIndex), 0.0).rgb;
-		diffIndirect = mix(id, diffIndirect, factor);
-#endif
+		Vec3 aabbMin = probe.m_aabbMinPad1.xyz;
+		Vec3 aabbMax = probe.m_aabbMaxPad1.xyz;
+		Vec3 probeOrigin = probe.m_positionCubemapIndex.xyz;
+		F32 cubemapIndex = probe.m_positionCubemapIndex.w;
+
+		// Compute blend weight
+		F32 blendWeight = computeProbeBlendWeight(worldPos, aabbMin, aabbMax, 0.2);
+		totalBlendWeight += blendWeight;
+		
+		// Sample reflections
+		Vec3 cubeUv = intersectProbe(worldPos, reflDir, aabbMin, aabbMax, probeOrigin);
+		Vec3 c = textureLod(u_reflectionsTex, Vec4(cubeUv, cubemapIndex), reflLod).rgb;
+		specIndirect += c * blendWeight;
+		
+		// Sample irradiance
+		cubeUv = intersectProbe(worldPos, normal, aabbMin, aabbMax, probeOrigin);
+		c = textureLod(u_irradianceTex, Vec4(cubeUv, cubemapIndex), 0.0).rgb;
+		diffIndirect += c * blendWeight;
 	}
 	}
+
+	// Normalize the colors
+	specIndirect /= totalBlendWeight;
+	diffIndirect /= totalBlendWeight;
 }
 }
 
 
 void main()
 void main()

+ 1 - 1
tools/scene/Exporter.cpp

@@ -822,7 +822,7 @@ void Exporter::visitNode(const aiNode* ainode)
 				aiVector3D scale(trf.a1, trf.b2, trf.c3);
 				aiVector3D scale(trf.a1, trf.b2, trf.c3);
 				assert(scale.x > 0.0f && scale.y > 0.0f && scale.z > 0.0f);
 				assert(scale.x > 0.0f && scale.y > 0.0f && scale.z > 0.0f);
 
 
-				aiVector3D half = scale / aiVector3D(2.0f, 2.0f, 2.0f);
+				aiVector3D half = scale;
 				probe.m_aabbMin = probe.m_position - half - probe.m_position;
 				probe.m_aabbMin = probe.m_position - half - probe.m_position;
 				probe.m_aabbMax = probe.m_position + half - probe.m_position;
 				probe.m_aabbMax = probe.m_position + half - probe.m_position;