- Home /
Vertex displacement shader not working
I am trying to combine a vertex wiggle shader one our programmers converted from CGFx with the built-in Reflect BumpSpec shader.
Here is the original:
Shader "Bio/Wiggle" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_TimeScale ("Time Scaling", Range(0,10)) = 1.0
_XAmount ("X Wiggle", Range(0,5)) = 1.0
_YAmount ("Y Wiggle", Range(0,5)) = 1.0
}
SubShader {
Tags { "RenderType" = "Opaque" }
Cull Off
CGPROGRAM
#pragma surface surf Lambert vertex:vert
struct Input {
float2 uv_MainTex;
};
float _XAmount;
float _YAmount;
float _TimeScale;
void vert(inout appdata_full v)
{
float time = _TimeScale * _Time.y;
float iny = v.vertex.y * _YAmount + time;
float wiggleX = sin(iny) * _XAmount;
float wiggleY = cos(iny) * _XAmount;
v.normal.y = v.normal.y + wiggleY;
normalize(v.normal);
v.vertex.x = v.vertex.x + wiggleX;
}
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
Fallback "Diffuse"
}
And here is my attempt at combining with the reflective shader:
Shader "Bio/Reflective Bumped Specular Wiggle" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
_MainTex ("Base (RGB) RefStrGloss (A)", 2D) = "white" {}
_Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
_BumpMap ("Normalmap", 2D) = "bump" {}
_TimeScale ("Time Scaling", Range(0,10)) = 1.0
_XAmount ("X Wiggle", Range(0,5)) = 1.0
_YAmount ("Y Wiggle", Range(0,5)) = 1.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 400
CGPROGRAM
#pragma surface surf BlinnPhong
#pragma target 3.0
sampler2D _MainTex;
sampler2D _BumpMap;
samplerCUBE _Cube;
fixed4 _Color;
fixed4 _ReflectColor;
half _Shininess;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 worldRefl;
INTERNAL_DATA
};
float _XAmount;
float _YAmount;
float _TimeScale;
void vert(inout appdata_full v)
{
float time = _TimeScale * _Time.y;
float iny = v.vertex.y * _YAmount + time;
float wiggleX = sin(iny) * _XAmount;
float wiggleY = cos(iny) * _XAmount;
v.normal.y = v.normal.y + wiggleY;
normalize(v.normal);
v.vertex.x = v.vertex.x + wiggleX;
}
void surf (Input IN, inout SurfaceOutput o) {
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
fixed4 c = tex * _Color;
o.Albedo = c.rgb;
o.Gloss = tex.a;
o.Specular = _Shininess;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
float3 worldRefl = WorldReflectionVector (IN, o.Normal);
fixed4 reflcol = texCUBE (_Cube, worldRefl);
reflcol *= tex.a;
o.Emission = reflcol.rgb * _ReflectColor.rgb;
o.Alpha = reflcol.a * _ReflectColor.a;
}
ENDCG
}
FallBack "Reflective/Bumped Diffuse"
}
The original works fine for the vertex wiggle, but I can't figure out what I broke. I'm an artist with not much coding experience. Thanks!
Answer by Owen-Reynolds · Sep 10, 2011 at 03:14 PM
On the first, there's a line:
#pragma surface surf Lambert vertex:vert
...and below the replacement shader is named vert: void vert(inout appdata_full v)
. The part vertex:vert
(I assume) says to use the new vert
instead of the standard vertex shader. In your replacement you have just #pragma surface surf BlinnPhong
, which substitutes your BlinnPhong, but leaves out the replacement vert.
Answer by WilliamS · Sep 12, 2011 at 02:28 PM
I have one other question regarding this shader. I am trying to add in a random, smooth translation in the XYZ coordinates. I have tried the perlin noise that is part of the Unity Procedural package, but it wouldn't work for me. I kept getting warnings about behaviours missing, etc.
We are making a game that takes place inside of a cell, so I need some cheap, ambient movement for organelles. Any suggestions on where to look or how to get started would be appreciated.
I'd write this as a separate Q. You're wanting to warp the background "cell wall" texture like a lava-lamp? (it looks like you are already wiggling the verts)
I'd guess someone who knows the Perlin package can give you an easier Unity way than my hammer and tongs approach. If not, I can dig it up, but you won't like it.