Move projection code into shared library

This commit is contained in:
Andrew Cassidy 2020-05-24 18:16:23 -07:00
parent f630fb2215
commit 8ca3925e04
3 changed files with 35 additions and 65 deletions

View File

@ -34,12 +34,6 @@ Shader "ConformalDecals/Feature/Bumped"
#pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
#include "LightingKSP.cginc"
#include "DecalsCommon.cginc"
sampler2D _Decal; sampler2D _Decal;
sampler2D _DecalBumpMap; sampler2D _DecalBumpMap;
@ -51,20 +45,16 @@ Shader "ConformalDecals/Feature/Bumped"
float _RimFalloff; float _RimFalloff;
float4 _RimColor; float4 _RimColor;
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
#include "LightingKSP.cginc"
#include "DecalsCommon.cginc"
void surf (DecalSurfaceInput IN, inout SurfaceOutput o) void surf (DecalSurfaceInput IN, inout SurfaceOutput o)
{ {
fixed4 uv_projected = UNITY_PROJ_COORD(IN.uv_decal); float4 color = tex2D(_Decal, IN.uv_decal);
float3 normal = UnpackNormal(tex2D(_DecalBumpMap, IN.uv_decal));
// since I cant easily affect the clamping mode in KSP, do it here
clip(uv_projected.xyz);
clip(1-uv_projected.xyz);
// clip backsides
clip(dot(_DecalNormal, IN.normal));
float2 uv_decal = TRANSFORM_TEX(uv_projected, _Decal);
float4 color = tex2D(_Decal, uv_decal);
float3 normal = UnpackNormal(tex2D(_DecalBumpMap, uv_decal));
// clip alpha // clip alpha
clip(color.a - _Cutoff); clip(color.a - _Cutoff);
@ -93,12 +83,6 @@ Shader "ConformalDecals/Feature/Bumped"
#pragma multi_compile_fwdadd nolightmap nodirlightmap nodynlightmap #pragma multi_compile_fwdadd nolightmap nodirlightmap nodynlightmap
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
#include "LightingKSP.cginc"
#include "DecalsCommon.cginc"
sampler2D _Decal; sampler2D _Decal;
sampler2D _DecalBumpMap; sampler2D _DecalBumpMap;
@ -110,20 +94,16 @@ Shader "ConformalDecals/Feature/Bumped"
float _RimFalloff; float _RimFalloff;
float4 _RimColor; float4 _RimColor;
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
#include "LightingKSP.cginc"
#include "DecalsCommon.cginc"
void surf (DecalSurfaceInput IN, inout SurfaceOutput o) void surf (DecalSurfaceInput IN, inout SurfaceOutput o)
{ {
fixed4 uv_projected = UNITY_PROJ_COORD(IN.uv_decal); float4 color = tex2D(_Decal, IN.uv_decal);
float3 normal = UnpackNormal(tex2D(_DecalBumpMap, IN.uv_decal));
// since I cant easily affect the clamping mode in KSP, do it here
clip(uv_projected.xyz);
clip(1-uv_projected.xyz);
// clip backsides
clip(dot(_DecalNormal, IN.normal));
float2 uv_decal = TRANSFORM_TEX(uv_projected, _Decal);
float4 color = tex2D(_Decal, uv_decal);
float3 normal = UnpackNormal(tex2D(_DecalBumpMap, uv_decal));
// clip alpha // clip alpha
clip(color.a - _Cutoff); clip(color.a - _Cutoff);

View File

@ -61,22 +61,12 @@ Shader "ConformalDecals/Paint/Diffuse"
void surf (DecalSurfaceInput IN, inout SurfaceOutput o) void surf (DecalSurfaceInput IN, inout SurfaceOutput o)
{ {
fixed4 uv_projected = UNITY_PROJ_COORD(IN.uv_decal); float4 color = tex2D(_Decal, IN.uv_decal);
float3 normal = UnpackNormal(tex2D(_BumpMap, IN.uv_base));
// since I cant easily affect the clamping mode in KSP, do it here
clip(uv_projected.xyz);
clip(1-uv_projected.xyz);
// clip backsides
clip(dot(_DecalNormal, IN.normal));
float2 uv_decal = TRANSFORM_TEX(uv_projected, _Decal);
float4 color = tex2D(_Decal, uv_decal);
// clip alpha // clip alpha
clip(color.a - _Cutoff); clip(color.a - _Cutoff);
float3 normal = UnpackNormal(tex2D(_BumpMap, IN.uv_base));
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal)); half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal));
float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a; float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a;
@ -130,22 +120,12 @@ Shader "ConformalDecals/Paint/Diffuse"
void surf (DecalSurfaceInput IN, inout SurfaceOutput o) void surf (DecalSurfaceInput IN, inout SurfaceOutput o)
{ {
fixed4 uv_projected = UNITY_PROJ_COORD(IN.uv_decal); float4 color = tex2D(_Decal, IN.uv_decal);
float3 normal = UnpackNormal(tex2D(_BumpMap, IN.uv_base));
// since I cant easily affect the clamping mode in KSP, do it here
clip(uv_projected.xyz);
clip(1-uv_projected.xyz);
// clip backsides
clip(dot(_DecalNormal, IN.normal));
float2 uv_decal = TRANSFORM_TEX(uv_projected, _Decal);
float4 color = tex2D(_Decal, uv_decal);
// clip alpha // clip alpha
clip(color.a - _Cutoff); clip(color.a - _Cutoff);
float3 normal = UnpackNormal(tex2D(_BumpMap, IN.uv_base));
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal)); half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal));
float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a; float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a;

View File

@ -3,7 +3,7 @@
struct DecalSurfaceInput struct DecalSurfaceInput
{ {
float4 uv_decal; float2 uv_decal;
#ifdef DECAL_BASE_NORMAL #ifdef DECAL_BASE_NORMAL
float2 uv_base; float2 uv_base;
#endif //DECAL_BASE_NORMAL #endif //DECAL_BASE_NORMAL
@ -134,9 +134,19 @@ fixed4 frag_forward(v2f IN) : SV_Target
float3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPosition)); float3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPosition));
float3 viewDir = _unity_tbn_0 * worldViewDir.x + _unity_tbn_1 * worldViewDir.y + _unity_tbn_2 * worldViewDir.z; float3 viewDir = _unity_tbn_0 * worldViewDir.x + _unity_tbn_1 * worldViewDir.y + _unity_tbn_2 * worldViewDir.z;
// perform decal projection
fixed4 uv_projected = UNITY_PROJ_COORD(IN.uv_decal);
// clip texture outside of xyz bounds
clip(uv_projected.xyz);
clip(1-uv_projected.xyz);
// clip backsides
clip(dot(_DecalNormal, IN.normal));
// initialize surface input // initialize surface input
UNITY_INITIALIZE_OUTPUT(DecalSurfaceInput, i) UNITY_INITIALIZE_OUTPUT(DecalSurfaceInput, i)
i.uv_decal = IN.uv_decal; i.uv_decal = TRANSFORM_TEX(uv_projected, _Decal);;
#ifdef DECAL_BASE_NORMAL #ifdef DECAL_BASE_NORMAL
i.uv_base = IN.uv_base; i.uv_base = IN.uv_base;
#endif #endif