- Home /
Question by
WintersBane · Apr 23 at 08:29 PM ·
shadershader writing
Create a moss/snow shader
i have two different shaders with some isolated results, i would like to combine them.
Shader 1 Code:
Shader "Custom/MossySnowShadderCombined" {
Properties{
_MainTex("Base (RGB)", 2D) = "white" {}
_MainBump("MainBump", 2D) = "bump" {}
_LayerTex("Layer (RGB)", 2D) = "white" {}
_LayerBump("LayerBump", 2D) = "bump" {}
_LayerStrength("Layer Strength", Range(0, 1)) = 0
_LayerDirection("Layer Direction", Vector) = (0, 1, 0)
_LayerDepth("Layer Depth", Range(0, 0.005)) = 0.0005
}
SubShader{
Tags{ "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
#pragma target 3.0
#pragma surface surf Lambert vertex:vert
sampler2D _MainTex;
sampler2D _MainBump;
sampler2D _LayerTex;
sampler2D _LayerBump;
float _LayerStrength;
float3 _LayerDirection;
float _LayerDepth;
struct Input {
float2 uv_MainTex;
float2 uv_MainBump;
float2 uv_LayerTex;
float2 uv_LayerBump;
float3 worldNormal;
INTERNAL_DATA
};
void vert(inout appdata_full v) {
// Convert the normal to world coordinates/world space
float3 sn = mul((float3x3)unity_WorldToObject, _LayerDirection);
if (dot(v.normal, sn.xyz) >= lerp(1, -1, (_LayerStrength * 2) / 3))
{
v.vertex.xyz += (sn.xyz + v.normal) * _LayerDepth * _LayerStrength;
}
}
void surf(Input IN, inout SurfaceOutput o) {
// Diffuse color of pixel
half4 mainDiffuse = tex2D(_MainTex, IN.uv_MainTex);
half4 layerDiffuse = tex2D(_LayerTex, IN.uv_LayerTex);
// Normal vector of pixel
o.Normal = UnpackNormal(tex2D(_MainBump, IN.uv_MainBump));
half3 layerNormal = half3(0, 0, 0);
// Snow mask
half sm = dot(WorldNormalVector(IN, o.Normal), _LayerDirection);
sm = pow(0.5 * sm + 0.5, 2.0);
//sm = cos(sm);
half difference = 1 - pow((clamp(lerp(1, 0, _LayerStrength),0,1) / sm),3);
o.Albedo = (layerDiffuse.rgb *clamp(layerDiffuse.a * difference,0,1)) + (mainDiffuse.rgb * (clamp(1 - layerDiffuse.a * difference,0,1)));
layerNormal = UnpackNormal(tex2D(_LayerBump, IN.uv_LayerBump));
//o.Normal = normalize(o.Normal + layerNormal);
o.Normal = mul(unity_ObjectToWorld, normalize(o.Normal + layerNormal));
o.Alpha = mainDiffuse.a;
}
ENDCG
}
FallBack "Diffuse"
}
Shader 2 Code:
Shader "Custom/Moss" {
Properties {
_MainTex ("Main texture", 2D) = "white" {}
_MossTex ("Moss texture", 2D) = "gray" {}
_Direction ("Direction", Vector) = (0, 1, 0)
_Amount ("Amount", Range(0, 1)) = 1
_LayerStrength("Layer Strength", Range(0, 1)) = 0
_LayerDirection("Layer Direction", Vector) = (0, 1, 0)
_LayerDepth("Layer Depth", Range(0, 0.005)) = 0.0005
}
SubShader {
Pass {
Tags { "RenderType"="Opaque" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float3 normal : NORMAL;
float2 uv_Main : TEXCOORD0;
float2 uv_Moss : TEXCOORD1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _MossTex;
float4 _MossTex_ST;
float _LayerStrength;
float3 _LayerDirection;
float _LayerDepth;
v2f vert(appdata_full v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv_Main = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv_Moss = TRANSFORM_TEX(v.texcoord, _MossTex);
o.normal = mul(unity_ObjectToWorld, v.normal);
return o;
}
float3 _Direction;
fixed _Amount;
fixed4 frag(v2f i) : COLOR {
fixed val = dot(normalize(i.normal), _Direction);
if(val < 1 - _Amount)
val = 0;
fixed4 tex1 = tex2D(_MainTex, i.uv_Main);
fixed4 tex2 = tex2D(_MossTex, i.uv_Moss);
return lerp(tex1, tex2, val);
}
ENDCG
}
}
FallBack "Diffuse"
}
Sources
Shader 1: Ramaraunt
Shader 2: Shaders Laboratory
shadercompare.png
(139.9 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613055733im_/https://answers.unity.com/themes/thub/images/avi.jpg)