- Home /
Shader that changes color based on displacement from radius (height shader for deformed sphere).
I have create a GLSL shader that is supposed to find the vertex coordinates of a deformed sphere and compare that to the original radius to create a displacement value that can then be used to alter the color of a point based on its displacement from the normal. For some reason, in the following shader, displacement is always equal to zero and the object is white along all points of varying displacement. Is there anything that I am doing wrong and can fix to achieve the desired effect? Is it possible that I need to use Unity's coordinate system for the radius as the object is first created as a 0.5^3 cube and then normalized to make a sphere then scaled to the radius? (The Radius value is taken from the object by using renderer.material.SetFloat("_radius", radius) but can be changed manually.)
Shader "Custom/SphereShader" {
Properties
{
_radius("Radius", Float) = 50
}
SubShader {
Pass {
GLSLPROGRAM
#ifdef VERTEX
varying vec3 position;
varying float displacement;
uniform float _radius;
varying float radius;
varying float _length;
varying vec4 _color;
void main()
{
radius = _radius;
position = gl_Vertex;
_length = length(position);
displacement = ((_length - radius)/radius);
_color = ((1.0 - displacement), (displacement + 1.0), 1.0, 1.0);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#endif
#ifdef FRAGMENT
varying vec4 _color;
void main()
{
//RGBA
gl_FragColor = _color;
}
#endif
ENDGLSL
}
}
}
Hi, did you ever figure this out? @cclaypool1
I'm currently trying to do something really similar and I'm having trouble finding reliable methods.
Yeah I eventually figured it out. This was quite a while ago and I can't remember what I was struggling with in this shader but I basically made a bunch of if statements in the frag section that if the displacement value from the normalized point was within a certain range it would return a certain color.
Nice! I was having trouble getting to the normalized point in the shader and ended up writing vertex coloring into my program (using distance from the original vertex to the displaced vertex) working from here: http://catlikecoding.com/unity/tutorials/noise-derivatives/
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Cg Language incompatibility Desktop VS GLSL Android 0 Answers
vertex displacement shader and ssao 0 Answers
Changing Eye Colour (Colour only non-white parts of a texture?) 2 Answers
Color white does nothing consider mobile... really? 1 Answer