Almost there

This commit is contained in:
raccoon
2024-11-07 22:31:58 +05:00
parent c93b0a00ba
commit 0907ca5c54

View File

@@ -40,10 +40,30 @@ Shader "Custom/InteriorReflectionShader"
return o; return o;
} }
// ChatGPT snippet
float3 BoxProjectReflectionDir(float3 worldPos, float3 worldRefl)
{
// Calculate relative position within the box
float3 boxMin = unity_SpecCube0_BoxMin;
float3 boxMax = unity_SpecCube0_BoxMax;
float3 boxCenter = 0.5 * (boxMin + boxMax);
float3 boxSize = boxMax - boxMin;
float3 localPos = worldPos - boxCenter;
float3 absLocalRefl = abs(worldRefl);
// Scale the reflection vector based on which side of the box it hits
float3 scale = boxSize / absLocalRefl;
float scaleMin = min(min(scale.x, scale.y), scale.z);
return localPos + worldRefl * scaleMin;
}
fixed4 frag (v2f i) : SV_Target fixed4 frag (v2f i) : SV_Target
{ {
half3 worldViewDir = -normalize(UnityWorldSpaceViewDir(i.worldPos)); // Direction of ray from the camera towards the object surface half3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos)); // Direction of ray from the camera towards the object surface
half4 skyData = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, worldViewDir); half3 reflection = reflect(-worldViewDir, i.worldNormal); // Direction of ray after hitting the surface of object
reflection = BoxProjectReflectionDir(i.worldPos, reflection);
half4 skyData = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, reflection);
half3 skyColor = DecodeHDR(skyData, unity_SpecCube0_HDR); // This is done becasue the cubemap is stored HDR half3 skyColor = DecodeHDR(skyData, unity_SpecCube0_HDR); // This is done becasue the cubemap is stored HDR
return half4(skyColor, 1.0); return half4(skyColor, 1.0);
} }