Last active
November 15, 2021 19:39
-
-
Save stonstad/e89582fef0bdc85b04c6df520db0f48d to your computer and use it in GitHub Desktop.
ReflectionProbeMaterial.Shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Example/ReflectionProbeMaterial" | |
{ | |
Properties | |
{ | |
_Blend0("CubeMap 1 Blend", Range(0.0, 1.0)) = 0.5 | |
_Blend1("CubeMap 2 Blend", Range(0.0, 1.0)) = 0.5 | |
_Scalar0("CubeMap 1 Scalar", Range(0.0, 20.0)) = 1.0 | |
_Scalar1("CubeMap 2 Scalar", Range(0.0, 20.0)) = 1.0 | |
_Alpha("Alpha", Range(0.0, 1.0)) = 1.0 | |
[NoScaleOffset] _AlbedoMap("Albedo Map", 2D) = "black" {} | |
_AlbedoStrength("Albedo Strength", Range(0, 10)) = 0 | |
} | |
SubShader | |
{ | |
Tags | |
{ | |
"Queue" = "Transparent" | |
"IgnoreProjector" = "True" | |
"RenderType" = "Transparent" | |
} | |
Pass | |
{ | |
Cull Off | |
Lighting Off | |
ZWrite Off | |
Offset -1, 1 | |
Fog { Mode Off } | |
ColorMask RGB | |
Blend SrcAlpha OneMinusSrcAlpha | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
float _Blend0; | |
float _Blend1; | |
float _Scalar0; | |
float _Scalar1; | |
float _Alpha; | |
sampler2D _AlbedoMap; | |
float4 _AlbedoMap_ST; | |
float _AlbedoStrength; | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float4 pos : SV_POSITION; | |
float3 worldPos : TEXCOORD0; | |
float3 worldNormal : TEXCOORD1; | |
half3 reflection : ROTATION; | |
float2 uv : TEXCOORD2; | |
}; | |
v2f vert(appdata v) | |
{ | |
v2f o; | |
o.pos = UnityObjectToClipPos(v.vertex); | |
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; | |
o.worldNormal = UnityObjectToWorldNormal(v.normal); | |
half3 worldViewDir = normalize(UnityWorldSpaceViewDir(o.worldPos)); | |
o.reflection = reflect(-worldViewDir, o.worldNormal); | |
o.uv = TRANSFORM_TEX(v.uv, _AlbedoMap); | |
return o; | |
} | |
float4 SampleCubeMap(TextureCube tex, SamplerState samplertex, float3 uvw) | |
{ | |
return tex.Sample(samplertex, uvw); | |
} | |
half4 frag(v2f i) : SV_Target | |
{ | |
#if UNITY_SPECCUBE_BLENDING | |
// reflection | |
half4 skyData0 = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, i.reflection); | |
half4 skyData1 = UNITY_SAMPLE_TEXCUBE_SAMPLER(unity_SpecCube1, unity_SpecCube0, i.reflection); | |
half3 skyColor0 = DecodeHDR(skyData0, unity_SpecCube0_HDR) * _Scalar0; | |
half3 skyColor1 = DecodeHDR(skyData1, unity_SpecCube1_HDR) * _Scalar1; | |
half4 reflectionColor; | |
reflectionColor = half4(1, 1, 1, 1); | |
if (_Blend0 > 0 && _Blend1 > 0) | |
reflectionColor.rgb = (skyColor0 * _Blend0 + skyColor1 * _Blend1) / 2.0; | |
else if (_Blend0 > 0) | |
reflectionColor.rgb = (skyColor0 * _Blend0); | |
else if (_Blend1 > 0) | |
reflectionColor.rgb = (skyColor1 * _Blend1); | |
reflectionColor.a = _Alpha; | |
// albedo | |
half4 albedo = tex2D(_AlbedoMap, i.uv) * _AlbedoStrength; | |
if (_AlbedoStrength == 0) | |
albedo = half4(1, 1, 1, 1); | |
// result | |
float4 result = (reflectionColor * albedo) / 2.0; | |
result.rgb *= unity_ColorSpaceDouble.rgb; | |
return result; | |
// test skyColor1 | |
//float4 result2 = float4(skyColor1, 1); | |
//return result2; | |
#else | |
half4 skyData0 = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, i.reflection); | |
half3 skyColor0 = DecodeHDR(skyData0, unity_SpecCube0_HDR); | |
half4 result; | |
result.xyz = (skyColor0 * _Blend0); | |
result.a = _Alpha; | |
return result; | |
#endif | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment