Skip to content

Instantly share code, notes, and snippets.

@terminalia
Last active February 2, 2021 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terminalia/cfc40e51c30ffc3ed6bc735d5f79b29c to your computer and use it in GitHub Desktop.
Save terminalia/cfc40e51c30ffc3ed6bc735d5f79b29c to your computer and use it in GitHub Desktop.
Shader "Mistwork/Tree Wind Tutorial" {
Properties {
[HDR]
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Leaves texture", 2D) = "white" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.3
_TurbulenceTex("Turbulence texture", 2D) = "black" {}
_TurbulenceAmount("Turbulence amount", Float) = 0
_TurbulenceSpeed("Turbulence speed", Float) = 0
[Header(Swaying)]
_WindSpeed("Wind speed", Range(0,50)) = 25
_SwayingRigidness("Swaying rigidness", Range(1,50)) = 25
_SwayMax("Sway Max", Range(0, 5)) = .05
_YOffset("Y offset", float) = 0.0
}
SubShader {
Tags { "IgnoreProjector"="True" "RenderType"="TreeLeaf" }
LOD 200
CGPROGRAM
#pragma surface surf TreeLeaf alphatest:_Cutoff vertex:vert addshadow nolightmap noforwardadd
#include "UnityBuiltin3xTreeLibrary.cginc"
sampler2D _MainTex;
float _WindSpeed;
float _SwayingRigidness;
float _SwayMax;
float _YOffset;
sampler2D _TurbulenceTex;
float _TurbulenceAmount;
float _TurbulenceSpeed;
struct Input {
float2 uv_MainTex;
float2 uv_TurbulenceTex;
};
void wind_force(inout appdata_full v, float3 v_world)
{
float x = sin(v_world.x / _SwayingRigidness + (_Time.x * _WindSpeed));
float z = sin(v_world.z / _SwayingRigidness + (_Time.x * _WindSpeed));
v.vertex.x += (step(0,v.vertex.y - _YOffset) * x * _SwayMax);
v.vertex.z += (step(0,v.vertex.y - _YOffset) * z * _SwayMax);
}
void vert(inout appdata_full v)
{
//Convert grass vertex position from object to world space
float3 vertex_world_pos = mul(unity_ObjectToWorld, v.vertex).xyz;
wind_force(v, v.vertex.xyz);
}
void surf (Input IN, inout LeafSurfaceOutput o) {
float2 turbulence = (tex2D(_TurbulenceTex, IN.uv_TurbulenceTex + _Time.y * _TurbulenceSpeed).xy * 2.0 - 1.0) * _TurbulenceAmount;
float4 leavesTex = tex2D(_MainTex, IN.uv_MainTex + turbulence);
o.Albedo = leavesTex * _Color;
o.Alpha = leavesTex.a;
}
ENDCG
}
Dependency "OptimizedShader" = "Hidden/Nature/Tree Creator Leaves Optimized"
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment