When using the Nature/Windy Leaves shader, the Placement > Offset and Tiling parameters don’t work. Why?
this is just a stab in the dark but this may be the same issue i was looking at as well (read aras’s post at the bottom):
hmmm, yeah that does help. It appears that I would need to add the texture scale offset routine to the shader. thanks.
Sounded easier then it is. no luck
Which Nature/Windy Leaves shader are you talking about? (I don’t remember whether it’s from wiki, nature demo or somewhere else…)
In other words, if you’d post the shader here, maybe someone could help.
Thank you Aras. I believe this shader was taken from the nature demo.
Shader "Nature/Windy Leaves" {
Properties {
_Color ("Main Color", Color) = (.5, .5, .5, 0)
_Color2 ("Fade Color", Color) = (1, .9, .8, 0)
_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
_Cutoff ("Base Alpha cutoff", Range (0,1)) = 0.5
}
// ---- no vertex programs
SubShader {
Tags { "Queue" = "Transparent" }
BindChannels {
Bind "Color", color
Bind "Vertex", vertex
Bind "TexCoord", texcoord
}
Pass {
Colormask RGB
AlphaTest Greater [_Cutoff]
Cull Off
CGPROGRAM
// vertex Vertex
#include "UnityCG.cginc" // Standard Unity properties
#include "waves.cginc" // Helper wave functions
struct Appdata {
float4 vertex;
float3 normal;
float4 texcoord;
float4 color;
};
struct v2f {
float4 pos : POSITION;
float4 color : COLOR0;
float4 uv : TEXCOORD0;
float fog : FOGC;
};
uniform float4 _Color, _Color2;
v2f Vertex(Appdata v)
{
v2f o;
const float4 _waveXSize = float4(0.012, 0.02, -0.06, 0.048) * 2;
const float4 _waveZSize = float4 (0.006, .02, -0.02, 0.1) * 2;
const float4 waveSpeed = float4 (0.3, .3, .08, .07) * 4;
float4 _waveXmove = _waveXSize * waveSpeed * 25;
float4 _waveZmove = _waveZSize * waveSpeed * 25;
// We model the wind as basic waves...
// Calculate the wind input to leaves from their vertex positions...
// for now, we transform them into world-space x/z positions...
// Later on, we should actually be able to do the whole calc's in post-projective space
float3 worldPos = mul ((float3x4)_Object2World, v.vertex);
// This is the input to the sinusiodal warp
float4 waves;
waves = worldPos.x * _waveXSize;
waves += worldPos.z * _waveZSize;
// Add in time to model them over time
waves += _Time.x * waveSpeed;
float4 s, c;
waves = frac (waves);
FastSinCos (waves, s,c);
float waveAmount = v.texcoord.y;
s *= waveAmount;
// Faster winds move the grass more than slow winds
s *= normalize (waveSpeed);
s = s * s;
float fade = dot (s, 1.3);
s = s * s;
float3 waveMove = float3 (0,0,0);
waveMove.x = dot (s, _waveXmove);
waveMove.z = dot (s, _waveZmove);
v.vertex.xz -= mul ((float3x3)_World2Object, waveMove).xz;
o.pos = mul(glstate.matrix.mvp, v.vertex);
o.fog = o.pos.w;
o.uv = v.texcoord;
o.color = lerp (_Color, _Color2, fade.xxxx);
return o;
}
ENDCG
SetTexture [_MainTex] { combine texture * primary double, texture }
}
}
// ---- no vertex programs
SubShader {
Tags { "Queue" = "Transparent" }
Pass {
Colormask RGB
AlphaTest Greater [_Cutoff]
Cull Off
Color [_Color]
SetTexture [_MainTex] { combine texture * primary double, texture }
}
}
}
I think replacing this line near the end of vertex program:
o.uv = v.texcoord;
With this one
o.uv = TRANSFORM_UV(0);
should work. The first one just passes UVs from the mesh, while the second one actually transforms them with offset/tiling matrix.
Thanks Aras that worked! … Although I also had to change the v2f struct to define uv as a float2 instead of float4.
/*
Renders doubled sides objects without lighting. Useful for grass, trees or foliage.
This shader renders two passes for all geometry, one for opaque parts and one with semitransparent details.
This makes it possible to render transparent objects like grass without them being sorted by depth.
*/
Shader "Nature/V" {
Properties {
_Color ("Main Color", Color) = (.5, .5, .5, .5)
_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
_Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5
}
SubShader {
// Set up basic lighting
Material {
Diffuse [_Color]
Ambient [_Color]
}
Lighting On
// Render both front and back facing polygons.
Cull Off
CGPROGRAM
// vertex Vertex
#include "UnityCG.cginc" // Standard Unity properties
#include "waves.cginc" // Helper wave functions
struct Appdata {
float4 vertex;
float3 normal;
float4 texcoord;
float4 color;
};
struct v2f {
float4 pos : POSITION;
float4 color : COLOR0;
float2 uv : TEXCOORD0;
float fog : FOGC;
};
uniform float4 _Color, _Color2;
v2f Vertex(Appdata v)
{
v2f o;
const float4 _waveXSize = float4(0.012, 0.02, -0.06, 0.048) * 2;
const float4 _waveZSize = float4 (0.006, .02, -0.02, 0.1) * 2;
const float4 waveSpeed = float4 (0.3, .3, .08, .07) * 4;
float4 _waveXmove = _waveXSize * waveSpeed * 25;
float4 _waveZmove = _waveZSize * waveSpeed * 25;
// We model the wind as basic waves...
// Calculate the wind input to leaves from their vertex positions...
// for now, we transform them into world-space x/z positions...
// Later on, we should actually be able to do the whole calc's in post-projective space
float3 worldPos = mul ((float3x4)_Object2World, v.vertex);
// This is the input to the sinusiodal warp
float4 waves;
waves = worldPos.x * _waveXSize;
waves += worldPos.z * _waveZSize;
// Add in time to model them over time
waves += _Time.x * waveSpeed;
float4 s, c;
waves = frac (waves);
FastSinCos (waves, s,c);
float waveAmount = v.texcoord.y;
s *= waveAmount;
// Faster winds move the grass more than slow winds
s *= normalize (waveSpeed);
s = s * s;
float fade = dot (s, 1.3);
s = s * s;
float3 waveMove = float3 (0,0,0);
waveMove.x = dot (s, _waveXmove);
waveMove.z = dot (s, _waveZmove);
v.vertex.xz -= mul ((float3x3)_World2Object, waveMove).xz;
o.pos = mul(glstate.matrix.mvp, v.vertex);
o.fog = o.pos.w;
o.uv = TRANSFORM_UV(0);
o.color = lerp (_Color, _Color2, fade.xxxx);
return o;
}
ENDCG
// first pass:
// render any pixels that are more than [_Cutoff] opaque
Pass {
AlphaTest Greater [_Cutoff]
SetTexture [_MainTex] {
combine texture * primary, texture
}
}
}
}
Hm… that might not be a good idea. It will work almost everywhere, except Intel GMA950 (and possibly some other places). When using vertex programs and no fragment programs, the best is to always output float4 UV. Like this:
o.uv.xy = TRANSFORM_UV(0);
o.uv.zw = float2(0,1);
Ah, OK. thanks (again)! The shader was working in the Unity editor but not in built game (on my macbook).