- Home /
Question by
3dpowermax · Oct 01, 2019 at 08:56 AM ·
shaderprojectorcubemap
Add reflection (cubemap) to projector shader
Hey All,
I'm trying to figure out how to add cubemap reflections to the below projector shader ?
Shader "Projector/Projector MultiplyCG"
{
Properties
{
_Color ("Main Colour", Color) = (1,1,1,0)
_ShadowTex ("Cookie", 2D) = "gray" { TexGen ObjectLinear }
}
Subshader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent+100"}
Pass
{
ZWrite Off
Offset -1, -1
Fog { Mode Off }
AlphaTest Less 1
ColorMask RGB
Blend OneMinusSrcAlpha SrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f
{
float4 pos : SV_POSITION;
float4 uv_Main : TEXCOORD0;
};
sampler2D _ShadowTex;
float4 _Color;
float4x4 unity_Projector;
v2f vert(appdata_tan v)
{
v2f o;
o.pos = UnityObjectToClipPos (v.vertex);
o.uv_Main = mul (unity_Projector, v.vertex);
return o;
}
half4 frag (v2f i) : COLOR
{
half4 tex =tex2Dproj(_ShadowTex, UNITY_PROJ_COORD(i.uv_Main)) * _Color;
tex.a = 1-tex.a;
return tex;
}
ENDCG
}
}
}
Comment
Best Answer
Answer by Namey5 · Oct 07, 2019 at 09:36 AM
Shader "Projector/Projector Reflection"
{
Properties
{
_Color ("Main Colour", Color) = (1,1,1,0)
_ShadowTex ("Cookie", 2D) = "gray" { TexGen ObjectLinear }
_Cube ("Reflection Cubemap", CUBE) = "" {}
}
Subshader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent+100"}
Pass
{
ZWrite Off
Offset -1, -1
Fog { Mode Off }
AlphaTest Less 1
ColorMask RGB
Blend OneMinusSrcAlpha SrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f
{
float4 pos : SV_POSITION;
float4 uv_Main : TEXCOORD0;
float3 worldRefl : TEXCOORD1;
};
sampler2D _ShadowTex;
samplerCUBE _Cube;
float4 _Color;
float4x4 unity_Projector;
v2f vert(appdata_tan v)
{
v2f o;
o.pos = UnityObjectToClipPos (v.vertex);
o.uv_Main = mul (unity_Projector, v.vertex);
float3 wn = mul ((float3x3)UNITY_MATRIX_M, v.normal);
float3 wp = mul (UNITY_MATRIX_M, float4 (v.vertex.xyz, 1)).xyz;
float3 vd = (wp - _WorldSpaceCameraPos.xyz);
o.worldRefl = reflect (vd, wn);
return o;
}
half4 frag (v2f i) : COLOR
{
half4 tex = tex2Dproj(_ShadowTex, UNITY_PROJ_COORD(i.uv_Main)) * _Color;
tex.a = 1-tex.a;
tex.rgb *= texCUBE (_Cube, normalize (i.worldRefl)).rgb;
return tex;
}
ENDCG
}
}
}
Works the same way as any other shader - find the normal and view direction vectors, then use the reflection of that to sample a cubemap.
Your answer
Follow this Question
Related Questions
Custom lightmaps? 1 Answer
Cubemap on alpha texture? 0 Answers
Hard Surface Shaders Pro, Unity 4.3 support? 1 Answer
Different reflection cubemaps for different materials? 0 Answers
blob shadow z-fighting on iphone 3 Answers