我必须在这两个 vertexshader/fragmentshader 中进行哪些更改才能使其在three.js 中工作?或者是否可以更改“MeshDepthMaterial”以显示色深配色方案?
**The vertex shader looks like this:**
varying vec3 MCposition;
varying float LightIntensity;
varying float Z;
void
main( void )
{
gl_TexCoord[0] = gl_MultiTexCoord0;
vec3 tnorm = normalize( vec3( gl_NormalMatrix * gl_Normal ) );
vec3 LightPos = vec3( -2., 0., 10. );
vec3 ECposition = vec3( gl_ModelViewMatrix * gl_Vertex );
Z = ECposition.z;
LightIntensity = dot( normalize(LightPos - ECposition), tnorm );
LightIntensity = abs( LightIntensity );
MCposition = gl_Vertex.xyz;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
**The fragment shader looks like this:**
varying vec3 MCposition;
varying float LightIntensity;
varying float Z;
uniform float Ad;
uniform float Bd;
uniform float NoiseAmp;
uniform float NoiseFreq;
uniform float Alpha;
uniform float Tol;
uniform sampler3D Noise3;
uniform float ChromaBlue;
uniform float ChromaRed;
uniform float Blend;
const vec3 BEAVER = vec3( 1., .5, 0. );
const vec3 WHITE = vec3( 1., 1., 1.);
// return 0. if < left-tol or > right+tol
// return 1. if >= left+tol and <= right-tol
// else blend
float
Pulse( float value, float left, float right, float tol )
{
float t = ( smoothstep( left-tol, left+tol, value ) - smoothstep( right-tol, right+tol, value ) );
return t;
}
vec3
ChromaDepth( float t )
{
t = clamp( t, 0., 1. );
float r = 1.;
float g = 0.0;
float b = 1. - 6. * ( t - (5./6.) );
if( t <= (5./6.) )
{
r = 6. * ( t - (4./6.) );
g = 0.;
b = 1.;
}
if( t <= (4./6.) )
{
r = 0.;
g = 1. - 6. * ( t - (3./6.) );
b = 1.;
}
if( t <= (3./6.) )
{
r = 0.;
g = 1.;
b = 6. * ( t - (2./6.) );
}
if( t <= (2./6.) )
{
r = 1. - 6. * ( t - (1./6.) );
g = 1.;
b = 0.;
}
if( t <= (1./6.) )
{
r = 1.;
g = 6. * t;
}
return vec3( r, g, b );
}
void
main( void )
{
vec4 noisevec = texture3D( Noise3, NoiseFreq*MCposition );
float n = noisevec[0] + noisevec[1] + noisevec[2] + noisevec[3]; // 1. -> 3.
// n = ( n - 1. ) / 2.; // 0. -> 1.
n = ( n - 2. ); // -1. -> 1.
vec2 st = gl_TexCoord[0].st;
st.s *= 2.;
float Ar = Ad/2.;
float Br = Bd/2.;
int numinu = int( st.s / Ad );
int numinv = int( st.t / Bd );
vec3 TheColor = WHITE;
float alfa = 1.;
st.s -= float(numinu) * Ad;
st.t -= float(numinv) * Bd;
vec3 upvp = vec3( st, 0. );
vec3 cntr = vec3( Ar, Br, 0. );
vec3 delta = upvp - cntr;
float oldrad = length( delta );
float newrad = oldrad + NoiseAmp*n;
delta = delta * newrad / oldrad;
float du = delta.x/Ar;
float dv = delta.y/Br;
float d = du*du + dv*dv;
if( abs( d - 1. ) <= Tol )
{
float t = smoothstep( 1.-Tol, 1.+Tol, d );
TheColor = mix( BEAVER, WHITE, t );
//alfa = mix( 1., 0., t );
}
if( d <= 1.-Tol )
{
TheColor = BEAVER;
}
if( d >= 1.+Tol )
{
alfa = Alpha;
if( alfa == 0. )
discard;
}
float t = (2./3.) * ( Z - ChromaRed ) / ( ChromaBlue - ChromaRed );
t = clamp( t, 0., 2./3. );
vec3 rgb = ChromaDepth( t );
TheColor = mix( TheColor, rgb, Blend );
gl_FragColor = vec4( LightIntensity*TheColor, alfa );
}
...或者这个版本更容易实现:
** vertex shader **
varying float depth;
varying float lighting;
varying float specular;
#define NEAR 10.0
#define FAR 20.0
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
float ambient = length(gl_FrontMaterial.ambient.rgb * gl_LightModel.ambient.rgb);
vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
vec3 light = normalize(vec3(gl_LightSource[0].position));
float diffuse = length(max(dot(normal,light),0.0) * gl_FrontMaterial.diffuse
* gl_LightSource[0].diffuse);
lighting = diffuse + ambient;
specular = length(pow(max(dot(normal, gl_LightSource[0].halfVector.xyz),0.0)
,gl_FrontMaterial.shininess) * gl_LightSource[0].specular.rgb * gl_FrontMaterial.specular.rgb);
depth = clamp((gl_Position.z-NEAR)/FAR,0.0,1.0);
}
**fragment shader**
uniform sampler2D diffusemap;
varying float depth;
varying float lighting;
varying float specular;
void main (void)
{
vec4 rgb;
float depth2 = depth*depth;
if (depth < 0.5) {
rgb.g = 1.6*depth2+1.2*depth;
} else {
rgb.g = 3.2*depth2-6.8*depth+3.6;
rgb.b = depth2*-4.8+9.2*depth-3.4;
}
depth = depth/0.9;
depth2 = depth2/0.81;
rgb.r = -2.14*depth2*depth2 -1.07*depth2*depth + 0.133*depth2 +0.0667*depth +1.0;
float texval = length(texture2D(diffusemap,gl_TexCoord[0].st).rgb)/2.0 + 0.5;
gl_FragColor = rgb*(texval*lighting + specular);
}---