Compute world-space normal in programmable portion of the shader

feature-multiSDF
Andrew Cassidy 4 years ago
parent 8382a97b72
commit 289988cf11

@ -2,11 +2,18 @@ Shader "ConformalDecals/Feature/Bumped"
{
Properties
{
[Header(Texture Maps)]
_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
_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
{
@ -35,9 +42,11 @@ Shader "ConformalDecals/Feature/Bumped"
sampler2D _Decal;
sampler2D _DecalBumpMap;
float _Cutoff;
float _Opacity;
float _RimFalloff;
float4 _RimColor;
void surf (DecalSurfaceInput IN, inout SurfaceOutput o)
{
@ -52,11 +61,16 @@ Shader "ConformalDecals/Feature/Bumped"
float4 color = tex2D(_Decal, 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);
o.Normal = normal;
o.Albedo = color.rgb;
o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb;
o.Alpha = color.a * _Opacity;
o.Emission = emission;
o.Normal = DECAL_ORIENT_NORMAL(normal, IN);
}
ENDCG
@ -85,6 +99,8 @@ Shader "ConformalDecals/Feature/Bumped"
float _Cutoff;
float _Opacity;
float _RimFalloff;
float4 _RimColor;
void surf (DecalSurfaceInput IN, inout SurfaceOutput o)
{
@ -99,11 +115,16 @@ Shader "ConformalDecals/Feature/Bumped"
float4 color = tex2D(_Decal, 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);
o.Normal = normal;
o.Albedo = color.rgb;
o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb;
o.Alpha = color.a * _Opacity;
o.Emission = emission;
o.Normal = DECAL_ORIENT_NORMAL(normal, IN);
}
ENDCG

@ -1,6 +1,17 @@
#ifndef 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
{
float4 uv_decal;
@ -9,6 +20,11 @@ struct DecalSurfaceInput
#endif //DECAL_BASE_NORMAL
float3 normal;
float3 viewDir;
float3 worldPosition;
float4 tSpace0;
float4 tSpace1;
float4 tSpace2;
};
struct appdata_decal
@ -140,6 +156,10 @@ fixed4 frag_forward(v2f IN) : SV_Target
#endif
i.normal = IN.normal;
i.viewDir = viewDir;
i.worldPosition = worldPosition;
i.tSpace0 = IN.tSpace0;
i.tSpace1 = IN.tSpace1;
i.tSpace2 = IN.tSpace2;
// initialize surface output
o.Albedo = 0.0;
@ -155,14 +175,6 @@ fixed4 frag_forward(v2f IN) : SV_Target
// compute lighting & shadowing factor
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
c += LightingBlinnPhongSmooth(o, lightDir, viewDir, atten);

Loading…
Cancel
Save