- Home /
Camera spaced normals for a surface shader
Hi All,
I wanted to get an object fading away from the edges that are not facing the camera. I'm trying to do this by comparing the view direction with the normals.
I've written this:
Shader "gamevial/GasGiant" {
Properties {
_MainTex ("Base (RGB) Trans (A)", 2D) = "white"{}
_BumpTex ("Bump (RGB) Trans (A)", 2D) = "bump" {}
_RippleTex("Ripple (RGB) Trans (A)", 2D) = "black" {}
_OffsetX("UV Offset X",Float)=.5
_OffsetY("UV Offset Y",Float)=.5
_Push1("Direction",Vector)=(0,0,1,1)
_Fade("Fade Edges", Range (0,10)) = 5
}
SubShader {
Tags{"Queue"="Transparent-1"}
Blend SrcAlpha OneMinusSrcAlpha
//Blend One One
Cull Back
ZWrite Off
CGPROGRAM
#pragma surface surf Lambert addshadow halfasview
struct Input {
float2 uv_MainTex;
float2 uv_RippleTex;
float3 viewDir;
float3 worldNormal;
};
uniform sampler2D _MainTex;
uniform sampler2D _BumpTex;
uniform sampler2D _RippleTex;
float _OffsetX,_OffsetY;
float _Fade;
float4 _Push1;
void surf (Input IN, inout SurfaceOutput o) {
// float3 viewN = normalize( mul( UNITY_MATRIX_IT_MV, -o.Normal.xyzz).xyz );
float3 viewN = mul ((float3x3)UNITY_MATRIX_IT_MV,IN.worldNormal);
float2 ripuv1=IN.uv_RippleTex+_Push1.xy*_Time[0];
float2 ripuv2=IN.uv_RippleTex+_Push1.zw*_Time[0];
half4 vRip=tex2D(_RippleTex,ripuv1)+tex2D(_RippleTex,ripuv2)-half4(1,1,1,1);
float2 uv=IN.uv_MainTex+float2((vRip.r)*_OffsetX,(vRip.g)*_OffsetY);
float4 c=tex2D(_MainTex, uv);
o.Albedo = c.rgb;
o.Alpha = c.a*pow(max(0,dot(viewN,IN.viewDir)),_Fade);//;
//o.Normal=UnpackNormal(tex2D(_BumpTex, uv));
}
ENDCG
}
}
but for some reason it seems (SEEMS) to be "pointing" or fading with the light instead (?)
Any ideas of where this is flawed?
Answer by jamesflowerdew · Nov 04, 2014 at 10:52 AM
shader "gamevial/gasgiant2" {
properties{
_MainTex ("Diffuse", 2D) = "white" {}
_RippleTex("Ripple (RGB) Trans (A)", 2D) = "black" {}
_Shininess ("Shininess",Float)=10
_OffsetX("UV Offset X",Float)=.5
_OffsetY("UV Offset Y",Float)=.5
_Push1("Direction",Vector)=(0,0,1,1)
_Fade ("Fade Edges",Range(0,10))=.1
}
SubShader {
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
CGPROGRAM
#pragma target 3.0
#pragma surface surf Lambert alpha
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform sampler2D _BumpTex;
uniform sampler2D _RippleTex;
float _OffsetX,_OffsetY;
float _Fade;
float4 _Push1;
struct Input {
float2 uv_MainTex;
float2 uv_RippleTex;
float3 viewDir;
};
void surf (Input IN, inout SurfaceOutput o)
{
float2 ripuv1=IN.uv_RippleTex+_Push1.xy*_Time[0];
float2 ripuv2=IN.uv_RippleTex+_Push1.zw*_Time[0];
half4 vRip=tex2D(_RippleTex,ripuv1)+tex2D(_RippleTex,ripuv2)-half4(1,1,1,1);
float2 uv=IN.uv_MainTex+float2((vRip.r)*_OffsetX,(vRip.g)*_OffsetY);
float4 c=tex2D(_MainTex, uv);
o.Albedo = c.rgb;
o.Alpha = pow(dot (normalize(IN.viewDir),o.Normal),_Fade);
}
ENDCG
}
}
This works.
I'm afraid that I found the answer once on the generic unity forum but was unable to find it again (a humble nod in that lost stranger's direction). It was someone attempting to do a microscopic style shader and I inverted the logic.
the control leaves something to be desired, as it fades too fast or not at all, but it should be quite useful. Have already built a volume light version that gets rid of annoying stripes created when facing geometry used to fake rays of light at obtuse angles.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170335im_/https://answers.unity.com/themes/thub/images/avi.jpg)