Better solution

This commit is contained in:
raccoon
2024-11-07 21:25:07 +05:00
parent a5772b2c5d
commit c93b0a00ba

View File

@@ -1,15 +1,14 @@
// Original code from https://gist.github.com/josephbk117/a43f5335cea9b3e12a44f196dc81f30f
Shader "Custom/InteriorReflectionShader"
{
Properties
{
_ReflectionIntensity ("Reflection Intensity", Range(0, 1)) = 1.0
// TODO: Implement me!
_Opacity("Opacity", Range(0.0, 1.0)) = 1.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 300
Pass
{
CGPROGRAM
@@ -17,10 +16,7 @@ Shader "Custom/InteriorReflectionShader"
#pragma fragment frag
#include "UnityCG.cginc"
float _ReflectionIntensity;
// Use built-in cubemap sampler (unity_SpecCube0) for reflection probe
// UNITY_DECLARE_TEXCUBE(unity_SpecCube0);
float _Opacity; // TODO: Implement me!
struct appdata
{
@@ -30,9 +26,9 @@ Shader "Custom/InteriorReflectionShader"
struct v2f
{
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
float4 pos : SV_POSITION;
};
v2f vert (appdata v)
@@ -40,38 +36,18 @@ Shader "Custom/InteriorReflectionShader"
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldNormal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));
o.worldNormal = UnityObjectToWorldNormal(v.normal);
return o;
}
half3 ComputeBoxProjectedReflection(float3 worldPos, float3 worldNormal)
fixed4 frag (v2f i) : SV_Target
{
// Calculate direction from the object to the reflection probe position
float3 probeToPixel = worldPos - unity_SpecCube0_ProbePosition;
// Calculate inverse box scale manually
float3 boxSize = unity_SpecCube0_BoxMax - unity_SpecCube0_BoxMin;
float3 invBoxSize = 1.0 / boxSize;
// Transform the position into the box projection space
float3 localPos = probeToPixel * invBoxSize + 0.5;
// Calculate reflection direction
float3 reflectionDir = normalize(worldPos - _WorldSpaceCameraPos);
// Sample the cubemap using the adjusted reflection direction
return UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, reflectionDir).rgb;
}
half4 frag (v2f i) : SV_Target
{
// Calculate reflection using box projection
half3 reflectionColor = ComputeBoxProjectedReflection(i.worldPos, i.worldNormal);
return half4(reflectionColor, 1.0);
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 skyColor = DecodeHDR(skyData, unity_SpecCube0_HDR); // This is done becasue the cubemap is stored HDR
return half4(skyColor, 1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}