90 lines
3.5 KiB
Plaintext
90 lines
3.5 KiB
Plaintext
// Original code from https://gist.github.com/josephbk117/a43f5335cea9b3e12a44f196dc81f30f
|
|
Shader "Custom/InteriorReflectionShader"
|
|
{
|
|
Properties
|
|
{
|
|
// TODO: Implement me!
|
|
_Opacity("Opacity", Range(0.0, 1.0)) = 1.0
|
|
}
|
|
|
|
SubShader
|
|
{
|
|
Pass
|
|
{
|
|
CGPROGRAM
|
|
#pragma vertex vert
|
|
#pragma fragment frag
|
|
#include "UnityCG.cginc"
|
|
|
|
float _Opacity; // TODO: Implement me!
|
|
|
|
struct appdata
|
|
{
|
|
float4 vertex : POSITION;
|
|
float3 normal : NORMAL;
|
|
};
|
|
|
|
struct v2f
|
|
{
|
|
float3 worldPos : TEXCOORD0;
|
|
float3 worldNormal : TEXCOORD1;
|
|
float4 pos : SV_POSITION;
|
|
};
|
|
|
|
v2f vert (appdata v)
|
|
{
|
|
v2f o;
|
|
o.pos = UnityObjectToClipPos(v.vertex);
|
|
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
|
|
o.worldNormal = UnityObjectToWorldNormal(v.normal);
|
|
return o;
|
|
}
|
|
|
|
// ChatGPT snippet
|
|
float3 BoxProjectReflectionDir(float3 worldPos, float3 worldRefl)
|
|
{
|
|
// Calculate relative position within the box
|
|
float3 boxMin = unity_SpecCube0_BoxMin.xyz;
|
|
float3 boxMax = unity_SpecCube0_BoxMax.xyz;
|
|
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
|
|
{
|
|
float3 worldViewDir = normalize(i.worldPos);
|
|
float3 worldNormal = normalize(i.worldNormal);
|
|
float3 worldRefl = normalize(reflect(-worldViewDir, worldNormal));
|
|
|
|
float3 scale = ((worldRefl > 0 ? unity_SpecCube0_BoxMax.xyz : unity_SpecCube0_BoxMin.xyz) - i.worldPos) / worldRefl;
|
|
float scaleMin = min(min(scale.x, scale.y), scale.z);
|
|
float3 uvw = worldRefl * scaleMin + (i.worldPos - unity_SpecCube0_ProbePosition.xyz);
|
|
|
|
// float4 sampleRefl = SAMPLE_TEXTURECUBE_LOD(unity_SpecCube0, samplerunity_SpecCube0, uvw, LOD);
|
|
// float3 specCol = DecodeHDREnvironment(sampleRefl, unity_SpecCube0_HDR);
|
|
half4 sampleRefl = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, uvw);
|
|
half3 specCol = DecodeHDR(sampleRefl, unity_SpecCube0_HDR); // This is done becasue the cubemap is stored HDR
|
|
return half4(specCol, 1.0);
|
|
|
|
/*
|
|
half3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos)); // Direction of ray from the camera towards the object surface
|
|
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
|
|
return half4(skyColor, 1.0);
|
|
*/
|
|
}
|
|
ENDCG
|
|
}
|
|
}
|
|
}
|