Created
April 4, 2019 00:14
-
-
Save eddietree/7595b51f24535d3e9dfcf899bb5b7837 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject' | |
Shader "Fuji/Moss" | |
{ | |
Properties | |
{ | |
[Header(Channels are affected by vertex colors (RGBA).)] | |
[Header(Please only use max 4 textures if possible.)] | |
// RED CHANNEL | |
[Header(Channel Red)] | |
[Toggle(EnableChannelR)] _EnableChannelR("Enable Red Channel", Float) = 1 | |
_GroundTex("Albedo Texture", 2D) = "white" {} | |
_ChannelTintR("Albedo Tint", Color) = (1,1,1,1) | |
[Toggle(EnableChannelRDetailMap)] _EnableChannelRDetailMap("Enable Detail Map (R-Channel)", Float) = 0 | |
_ChannelDetailR("Detail Map", 2D) = "gray" {} | |
// GREEN CHANNEL | |
[Header(Channel Green)] | |
[Toggle(EnableChannelG)] _EnableChannelG("Enable Green Channel", Float) = 1 | |
_MossTex0("Albedo Texture", 2D) = "white" {} | |
_ChannelTintG("Albedo Tint", Color) = (1,1,1,1) | |
[Toggle(EnableChannelGHeightMap)] _EnableChannelGHeightMap("Enable Height Map", Float) = 0 | |
_HeightMap0("Height Map", 2D) = "black" {} | |
_HeightScale0("Height Map Scale", Range(-0.5, 0.5)) = 0.02 | |
[Toggle(EnableChannelGDetailMap)] _EnableChannelGDetailMap("Enable Detail Map (G-Channel)", Float) = 0 | |
_ChannelDetailG("Detail Map", 2D) = "gray" {} | |
// BLUE CHANNEL | |
[Header(Channel Blue)] | |
[Toggle(EnableChannelB)] _EnableChannelB("Enable Blue Channel", Float) = 0 | |
_ChannelTintB("Albedo Tint", Color) = (1,1,1,1) | |
_MossTex1("Albedo Texture", 2D) = "white" {} | |
[Toggle(EnableChannelBHeightMap)] _EnableChannelBHeightMap("Enable Height Map", Float) = 0 | |
_HeightMap1("Height Map", 2D) = "black" {} | |
_HeightScale1("Height Map Scale", Range(0.0, 0.3)) = 0.02 | |
[Toggle(EnableChannelBDetailMap)] _EnableChannelBDetailMap("Enable Detail Map (B-Channel)", Float) = 0 | |
_ChannelDetailB("Detail Map", 2D) = "gray" {} | |
// ALPHA CHANNEL | |
[Header(Channel Alpha (Emission))] | |
[Toggle(EnableChannelA)] _EnableChannelA("Enable Alpha Channel", Float) = 0 | |
_ChannelAEmission("Emission Color (Alpha Channel)", Color) = (1,1,1,1) | |
[Header(Fresnel)] | |
[Toggle(EnableFresnel)] _EnableFresnel("Enable Fresnel", Float) = 0 | |
_FresnelColor("Fresnel Color", Color) = (0,0,0,0) | |
_FresnelSharpness("Fresnel Sharpness", Range(0.0, 5.0)) = 2.0 | |
[Header(Effects)] | |
[Toggle(EnableFog)] _EnableFog("Enable Fog", Float) = 1 | |
[Toggle(EnableEffectLightFallOffDirt)] _EnableEffectLightFallOffDirt("Enable Effect Falloff to Dirt (Red Channel)", Float) = 0 | |
_FogStrength("Fog Strength", Range(0.0, 1.0)) = 1 | |
_EffectFalloffDist("Effect Falloff Dist", Range(0.0, 1.0)) = 0.1 | |
[Header(Shimmer)] | |
[Toggle(EnableEffectShimmer)] _EnableEffectShimmer("Enable Shimmer", Float) = 0 | |
_ShimmerNoiseTex("Shimmer Texture", 2D) = "black" {} | |
_ShimmerNormalStrength("Shimmer Normal Strength", Range(0.0, 1.0)) = 1 | |
_ShimmerSpecularTightness("Shimmer Specular Tightness", Range(0.0, 100.0)) = 1 | |
_ShimmerTint("Shimmer Tint", Color) = (1,1,1,1) | |
[Header(Light Map)] | |
[Toggle(EnableLightMap)] _EnableLightMap("Enable Light Map", Float) = 0 | |
[Header(Debug)] | |
[Toggle(EnableDebugShowVertexColors)] _EnableDebugShowVertexColors("Show Vertex Colors (RGB)", Float) = 0 | |
[Toggle(EnableDebugShowVertexAlpha)] _EnableDebugShowVertexAlpha("Show Vertex Alpha (A)", Float) = 0 | |
[Toggle(EnableDebugShowUV)] _EnableDebugShowUV("Show Vertex UV (Green-Channel)", Float) = 0 | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" "Queue" = "Geometry" } | |
LOD 100 | |
Pass | |
{ | |
Tags{ "LightMode" = "ForwardBase" } | |
Lighting Off | |
CGPROGRAM | |
#pragma shader_feature EnableChannelR | |
#pragma shader_feature EnableChannelG | |
#pragma shader_feature EnableChannelB | |
#pragma shader_feature EnableChannelA | |
#pragma shader_feature EnableChannelRDetailMap | |
#pragma shader_feature EnableChannelGDetailMap | |
#pragma shader_feature EnableChannelBDetailMap | |
#pragma shader_feature EnableChannelGHeightMap | |
#pragma shader_feature EnableChannelBHeightMap | |
#pragma shader_feature EnableFresnel | |
#pragma shader_feature EnableFog | |
#pragma shader_feature EnableEffectShimmer | |
#pragma shader_feature EnableDebugShowVertexColors | |
#pragma shader_feature EnableDebugShowVertexAlpha | |
#pragma shader_feature EnableDebugShowUV | |
#pragma shader_feature EnableEffectLightFallOffDirt | |
#pragma shader_feature EnableLightMap | |
#ifdef EnableLightMap | |
#pragma multi_compile _ LIGHTMAP_ON | |
#endif | |
#pragma multi_compile_instancing | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight | |
//#pragma multi_compile _ SHADOWS_SCREEN | |
#include "UnityCG.cginc" | |
#include "Lighting.cginc" | |
//#include "UnityLightingCommon.cginc" | |
#include "AutoLight.cginc" | |
#include "Shared/FujiGlobalShared.cginc" | |
#include "Shared/FujiMathShared.cginc" | |
struct appdata | |
{ | |
float4 pos : POSITION; | |
float3 normal : NORMAL; | |
float3 tangent : TANGENT; | |
float2 uv : TEXCOORD0; | |
float2 uv1 : TEXCOORD1; | |
float4 color : COLOR; | |
UNITY_VERTEX_INPUT_INSTANCE_ID | |
}; | |
struct v2f | |
{ | |
float4 pos : SV_POSITION; | |
float4 uv0 : TEXCOORD0; | |
float3 diffuseColor : TEXCOORD1; | |
float3 camPosTexcoord : TEXCOORD2; | |
float4 vertexColor: TEXCOORD3; | |
float4 emissionData : TEXCOORD4; | |
float3 worldNormal : TEXCOORD5; | |
float3 worldPos : TEXCOORD6; | |
#ifdef EnableLightMap | |
float2 uvLightMap : TEXCOORD7; | |
#endif | |
#ifdef EnableFog | |
float4 fogData : TEXCOORD8; // [fogColor.xyz, fogAmount.w] | |
#endif | |
#ifdef EnableEffectShimmer | |
float3 viewDir : TEXCOORD9; | |
float3x3 tbn : TEXCOORD10; | |
#endif | |
#if defined(EnableChannelGDetailMap) || defined(EnableChannelBDetailMap) | |
float4 uvDetail12 : TEXCOORD11; // detail map uv for G+B channel | |
#endif | |
SHADOW_COORDS(13) | |
//UNITY_VERTEX_INPUT_INSTANCE_ID | |
UNITY_VERTEX_OUTPUT_STEREO | |
}; | |
// effect: falloff | |
#ifdef EnableEffectLightFallOffDirt | |
float _EffectFalloffDist; | |
#endif | |
// ground Rchannel | |
#ifdef EnableChannelR | |
sampler2D _GroundTex; | |
float4 _GroundTex_ST; | |
fixed4 _ChannelTintR; | |
#ifdef EnableChannelRDetailMap | |
sampler2D _ChannelDetailR; | |
float4 _ChannelDetailR_ST; | |
#endif | |
#endif | |
// moss G-channel | |
#ifdef EnableChannelG | |
sampler2D _MossTex0; | |
float4 _MossTex0_ST; | |
fixed4 _ChannelTintG; | |
#ifdef EnableChannelGHeightMap | |
sampler2D _HeightMap0; | |
float4 _HeightMap0_ST; | |
float _HeightScale0; | |
#endif | |
#ifdef EnableChannelGDetailMap | |
sampler2D _ChannelDetailG; | |
float4 _ChannelDetailG_ST; | |
#endif | |
#endif | |
// moss B-channel | |
#ifdef EnableChannelB | |
sampler2D _MossTex1; | |
float4 _MossTex1_ST; | |
fixed4 _ChannelTintB; | |
#ifdef EnableChannelBHeightMap | |
sampler2D _HeightMap1; | |
float4 _HeightMap1_ST; | |
float _HeightScale1; | |
#endif | |
#ifdef EnableChannelBDetailMap | |
sampler2D _ChannelDetailB; | |
float4 _ChannelDetailB_ST; | |
#endif | |
#endif | |
#ifdef EnableChannelA | |
fixed4 _ChannelAEmission; | |
#endif | |
// fresnel | |
#ifdef EnableFresnel | |
float4 _FresnelColor; | |
float _FresnelSharpness; | |
#endif | |
#ifdef EnableEffectShimmer | |
sampler2D _ShimmerNoiseTex; | |
float4 _ShimmerNoiseTex_ST; | |
float _ShimmerSpecularTightness; | |
float _ShimmerNormalStrength; | |
fixed4 _ShimmerTint; | |
#endif | |
#ifdef EnableFog | |
float _FogStrength; | |
#endif | |
v2f vert (appdata v) | |
{ | |
UNITY_SETUP_INSTANCE_ID(v); | |
float3 localPos = v.pos; | |
float3 worldPos = mul(unity_ObjectToWorld, v.pos).xyz; | |
float3 worldNormal = normalize(mul(unity_ObjectToWorld, float4(v.normal,0.0)).xyz); | |
// texture space (TBN) basis-vector | |
float3 binormal = cross(v.tangent, v.normal); | |
float3x3 tbn = float3x3(v.tangent, binormal, v.normal); | |
// get cam pos in texture (TBN) space | |
float3 camPosLocal = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1.0)).xyz; | |
float3 dirToCamLocal = camPosLocal - localPos; | |
float3 camPosTexcoord = mul(tbn, dirToCamLocal); | |
// cal light to pass to diffuseColor | |
float lightAtten = FujiCalcAttenuation(worldPos); | |
#ifdef EnableChannelA | |
{ | |
lightAtten = max(lightAtten, v.color.a); | |
} | |
#endif | |
// emission | |
float emissionStrength = FujiCalcEmissionStrength(worldPos); | |
// fresnel | |
float fresnel = 0.0;; | |
#ifdef EnableFresnel | |
fresnel = 1.0 - dot(worldNormal, normalize(_WorldSpaceCameraPos - worldPos)); | |
fresnel = pow(fresnel, _FresnelSharpness) * lightAtten; | |
#endif | |
v2f o; | |
UNITY_INITIALIZE_OUTPUT(v2f, o); | |
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); | |
o.pos = UnityObjectToClipPos(v.pos); | |
o.worldNormal = worldNormal; | |
o.worldPos = worldPos; | |
o.camPosTexcoord = normalize(camPosTexcoord); | |
o.vertexColor = v.color; | |
#ifdef EnableEffectShimmer | |
o.viewDir = normalize(_WorldSpaceCameraPos.xyz - worldPos); | |
o.tbn = transpose(tbn); | |
#endif | |
#ifdef EnableFog | |
o.fogData = FujiGetFogData(worldPos) * float4(lightAtten, lightAtten, lightAtten, 1.0); | |
o.fogData.w *= _FogStrength; | |
#endif | |
#ifdef EnableLightMap | |
o.uvLightMap = v.uv1 * unity_LightmapST.xy + unity_LightmapST.zw; | |
#endif | |
o.uv0 = float4(v.uv, 0, 0); | |
o.diffuseColor = FujiCalcDiffuseLightingBasicEdge(worldNormal, worldPos) * lightAtten; | |
o.emissionData = float4(emissionStrength, fresnel, lightAtten, 0.0); | |
//o.shadowCoordinates = ComputeScreenPos(i.position); | |
TRANSFER_SHADOW(o); | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i); | |
// vertex data | |
float4 vertexColor = i.vertexColor; | |
float emissionStrength = i.emissionData.x; | |
float fresnel = i.emissionData.y; | |
float lightAtten = i.emissionData.z; | |
float3 eyeVec = i.camPosTexcoord; // optimization: vertex-normalization instead of per-pixel | |
float2 uv0 = i.uv0.xy; | |
// output | |
fixed4 col = float4(0.0, 0.0, 0.0, 1.0); | |
// R-Channel | |
#ifdef EnableChannelR | |
fixed4 texGround = tex2D(_GroundTex, TRANSFORM_TEX(uv0, _GroundTex)); | |
float3 rChannelColor = texGround.rgb; | |
// detail map | |
#ifdef EnableChannelRDetailMap | |
rChannelColor *= tex2D(_ChannelDetailR, TRANSFORM_TEX(uv0, _ChannelDetailR)).rgb * 2; | |
#endif | |
col.xyz += rChannelColor * vertexColor.r * _ChannelTintR.xyz; | |
#endif | |
// G-channel | |
#ifdef EnableChannelG | |
{ | |
float2 texMoss0uv = TRANSFORM_TEX(uv0, _MossTex0); | |
#ifdef EnableChannelGHeightMap | |
{ | |
float2 height0uv = TRANSFORM_TEX(uv0, _HeightMap0); | |
float height0 = tex2D(_HeightMap0, height0uv).r; | |
float v0 = height0 * _HeightScale0 - _HeightScale0 * 0.5; | |
texMoss0uv += eyeVec.xy * v0; | |
} | |
#endif | |
fixed3 gChannelColor = tex2D(_MossTex0, texMoss0uv).xyz; | |
// detail map | |
#ifdef EnableChannelGDetailMap | |
gChannelColor *= tex2D(_ChannelDetailG, TRANSFORM_TEX(uv0, _ChannelDetailG)).rgb * 2; | |
#endif | |
col.xyz += gChannelColor * vertexColor.g * _ChannelTintG.xyz; | |
} | |
#endif | |
// B-channel | |
#ifdef EnableChannelB | |
{ | |
float2 texMoss1uv = TRANSFORM_TEX(uv0, _MossTex1); | |
#ifdef EnableChannelBHeightMap | |
{ | |
float2 height1uv = TRANSFORM_TEX(uv0, _HeightMap1); | |
float height1 = tex2D(_HeightMap1, height1uv).r; | |
float v1 = height1 * _HeightScale1 - _HeightScale1 * 0.5; | |
texMoss1uv += eyeVec.xy * v1; | |
} | |
#endif | |
fixed3 bChannelColor = tex2D(_MossTex1, texMoss1uv).xyz; | |
// detail map | |
#ifdef EnableChannelBDetailMap | |
bChannelColor *= tex2D(_ChannelDetailB, TRANSFORM_TEX(uv0, _ChannelDetailB)).rgb * 2; | |
#endif | |
col.xyz += bChannelColor * vertexColor.b * _ChannelTintB.xyz; | |
} | |
#endif | |
// apply effect light falloff | |
#ifdef EnableEffectLightFallOffDirt | |
{ | |
#ifdef EnableChannelR | |
float fallOff = saturate(lightAtten + _EffectFalloffDist); | |
col.xyz = lerp(rChannelColor.xyz, col.xyz, fallOff*fallOff*fallOff); | |
#endif | |
} | |
#endif | |
// phong shading | |
col.xyz *= i.diffuseColor.xyz; | |
// Alpha channel | |
#ifdef EnableChannelA | |
{ | |
col.xyz += vertexColor.a * emissionStrength * _ChannelAEmission.xyz; | |
} | |
#endif | |
// fresnel | |
#ifdef EnableFresnel | |
col.xyz += fresnel * _FresnelColor.xyz * lightAtten; | |
#endif | |
// shimmer specular | |
#ifdef EnableEffectShimmer | |
{ | |
float3 worldNormal = (i.worldNormal); | |
float3 viewDir = (i.viewDir); | |
float3 dirToLight = float3(0, 1, 0); | |
float3 noiseTex = tex2D(_ShimmerNoiseTex, uv0 * _ShimmerNoiseTex_ST.xy).rgb; | |
float3 noiseTexNormal = noiseTex.rgb * 2.0 - float3(1, 1, 1); | |
float3 noiseWorldDir = mul(i.tbn, noiseTexNormal).xyz; | |
worldNormal = normalize(lerp(worldNormal, noiseWorldDir, _ShimmerNormalStrength)); | |
// fresnel | |
float noiseFresnel = 1.0 - dot(worldNormal, viewDir); | |
col.xyz += _FresnelColor.xyz* noiseFresnel * noiseTex.x * lightAtten; | |
// specular | |
float3 reflection = reflect(-dirToLight, worldNormal); | |
float specular = pow(saturate(dot(reflection, viewDir)), _ShimmerSpecularTightness); | |
col.xyz += _ShimmerTint.xyz * specular * emissionStrength; | |
} | |
#endif | |
// lightmapping | |
#ifdef EnableLightMap | |
col.xyz *= DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uvLightMap)); | |
#endif | |
// fog out | |
#ifdef EnableFog | |
col.xyz = FujiApplyFog(col.xyz, i.fogData); | |
#endif | |
// Debug | |
{ | |
#ifdef EnableDebugShowVertexColors | |
col.xyz = vertexColor.rgb; | |
#endif | |
#ifdef EnableDebugShowVertexAlpha | |
col.xyz = vertexColor.a; | |
#endif | |
#ifdef EnableDebugShowUV | |
col.xyz = float3(uv0,0); | |
#endif | |
} | |
col = clampForBloomHDR( col ); | |
//float shadowAtten = 0.0; | |
//float shadowAtten = SHADOW_ATTENUATION(i); | |
//float temp = SHADOW_ATTENUATION(i); | |
//col.xyz = temp; | |
UNITY_LIGHT_ATTENUATION(shadowAtten, i, i.worldPos.xyz); | |
col.xyz = shadowAtten; | |
return col; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment