Compute world-space normal in programmable portion of the shader

This commit is contained in:
Andrew Cassidy 2020-05-24 16:48:37 -07:00
parent 8fafe968d2
commit 27ac9691b7
No known key found for this signature in database
GPG Key ID: 963017B38FD477A1
2 changed files with 47 additions and 14 deletions

View File

@ -2,11 +2,18 @@ Shader "ConformalDecals/Feature/Bumped"
{ {
Properties Properties
{ {
[Header(Texture Maps)]
_Decal("Decal Texture", 2D) = "gray" {} _Decal("Decal Texture", 2D) = "gray" {}
_DecalBumpMap("Decal Normal Map", 2D) = "bump" {} _DecalBumpMap("Decal Bump Map", 2D) = "bump" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5 _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
_Opacity("_Opacity", Range(0,1) ) = 1 _Opacity("_Opacity", Range(0,1) ) = 1
[Header(Effects)]
[PerRendererData]_Opacity("_Opacity", Range(0,1) ) = 1
[PerRendererData]_RimFalloff("_RimFalloff", Range(0.01,5) ) = 0.1
[PerRendererData]_RimColor("_RimColor", Color) = (0,0,0,0)
[PerRendererData]_UnderwaterFogFactor ("Underwater Fog Factor", Range(0,1)) = 0
} }
SubShader SubShader
{ {
@ -35,9 +42,11 @@ Shader "ConformalDecals/Feature/Bumped"
sampler2D _Decal; sampler2D _Decal;
sampler2D _DecalBumpMap; sampler2D _DecalBumpMap;
float _Cutoff; float _Cutoff;
float _Opacity; float _Opacity;
float _RimFalloff;
float4 _RimColor;
void surf (DecalSurfaceInput IN, inout SurfaceOutput o) void surf (DecalSurfaceInput IN, inout SurfaceOutput o)
{ {
@ -52,11 +61,16 @@ Shader "ConformalDecals/Feature/Bumped"
float4 color = tex2D(_Decal, projUV); float4 color = tex2D(_Decal, projUV);
float3 normal = UnpackNormal(tex2D(_DecalBumpMap, projUV)); float3 normal = UnpackNormal(tex2D(_DecalBumpMap, projUV));
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal));
float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a;
// clip alpha
clip(color.a - _Cutoff); clip(color.a - _Cutoff);
o.Normal = normal; o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb;
o.Albedo = color.rgb;
o.Alpha = color.a * _Opacity; o.Alpha = color.a * _Opacity;
o.Emission = emission;
o.Normal = DECAL_ORIENT_NORMAL(normal, IN);
} }
ENDCG ENDCG
@ -85,6 +99,8 @@ Shader "ConformalDecals/Feature/Bumped"
float _Cutoff; float _Cutoff;
float _Opacity; float _Opacity;
float _RimFalloff;
float4 _RimColor;
void surf (DecalSurfaceInput IN, inout SurfaceOutput o) void surf (DecalSurfaceInput IN, inout SurfaceOutput o)
{ {
@ -99,11 +115,16 @@ Shader "ConformalDecals/Feature/Bumped"
float4 color = tex2D(_Decal, projUV); float4 color = tex2D(_Decal, projUV);
float3 normal = UnpackNormal(tex2D(_DecalBumpMap, projUV)); float3 normal = UnpackNormal(tex2D(_DecalBumpMap, projUV));
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal));
float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a;
// clip alpha
clip(color.a - _Cutoff); clip(color.a - _Cutoff);
o.Normal = normal; o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb;
o.Albedo = color.rgb;
o.Alpha = color.a * _Opacity; o.Alpha = color.a * _Opacity;
o.Emission = emission;
o.Normal = DECAL_ORIENT_NORMAL(normal, IN);
} }
ENDCG ENDCG

View File

@ -1,6 +1,17 @@
#ifndef DECALS_COMMON_INCLUDED #ifndef DECALS_COMMON_INCLUDED
#define DECALS_COMMON_INCLUDED #define DECALS_COMMON_INCLUDED
inline fixed3 OrientNormal(float3 normal, float4 tSpace0, float4 tSpace1, float4 tSpace2) {
float3 WorldNormal;
WorldNormal.x = dot(tSpace0.xyz, normal);
WorldNormal.y = dot(tSpace1.xyz, normal);
WorldNormal.z = dot(tSpace2.xyz, normal);
WorldNormal = normalize(WorldNormal);
return WorldNormal;
}
#define DECAL_ORIENT_NORMAL(normal, IN) OrientNormal(normal, IN.tSpace0, IN.tSpace1, IN.tSpace2)
struct DecalSurfaceInput struct DecalSurfaceInput
{ {
float4 uv_decal; float4 uv_decal;
@ -9,6 +20,11 @@ struct DecalSurfaceInput
#endif //DECAL_BASE_NORMAL #endif //DECAL_BASE_NORMAL
float3 normal; float3 normal;
float3 viewDir; float3 viewDir;
float3 worldPosition;
float4 tSpace0;
float4 tSpace1;
float4 tSpace2;
}; };
struct appdata_decal struct appdata_decal
@ -140,6 +156,10 @@ fixed4 frag_forward(v2f IN) : SV_Target
#endif #endif
i.normal = IN.normal; i.normal = IN.normal;
i.viewDir = viewDir; i.viewDir = viewDir;
i.worldPosition = worldPosition;
i.tSpace0 = IN.tSpace0;
i.tSpace1 = IN.tSpace1;
i.tSpace2 = IN.tSpace2;
// initialize surface output // initialize surface output
o.Albedo = 0.0; o.Albedo = 0.0;
@ -155,14 +175,6 @@ fixed4 frag_forward(v2f IN) : SV_Target
// compute lighting & shadowing factor // compute lighting & shadowing factor
UNITY_LIGHT_ATTENUATION(atten, IN, worldPosition) UNITY_LIGHT_ATTENUATION(atten, IN, worldPosition)
// compute world normal
float3 WorldNormal;
WorldNormal.x = dot(_unity_tbn_0, o.Normal);
WorldNormal.y = dot(_unity_tbn_1, o.Normal);
WorldNormal.z = dot(_unity_tbn_2, o.Normal);
WorldNormal = normalize(WorldNormal);
o.Normal = WorldNormal;
//KSP lighting function //KSP lighting function
c += LightingBlinnPhongSmooth(o, lightDir, viewDir, atten); c += LightingBlinnPhongSmooth(o, lightDir, viewDir, atten);