配色がRedbull感
OpenGL Superbibleのサンプルコードです。レッドブルが飲みたくなる配色。
JXS / GLSLコード
[html]
<jittershader name="mrr-3colored-lights">
<description>
</description>
<language name="glsl" version="1.2">
<program name="vp" type="vertex">
<![CDATA[
#version 120
varying vec3 N,L,V;
void main(void)
{
vec3 lightPos = gl_LightSource[0].position.xyz;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
//map object-space position onto unit sphere
V =gl_Vertex.xyz;
//eye-space normal
N = gl_NormalMatrix * gl_Normal;
//eye-space light vector
vec4 Veye = gl_ModelViewMatrix * gl_Vertex;
L =lightPos – Veye.xyz;
}
]]>
</program>
<program name="fp" type="fragment">
<![CDATA[
#version 120
varying vec3 V;
varying vec3 N;
varying vec3 L;
const vec3 myRed = vec3(0.6,0,0);
const vec3 myYellow = vec3(0.6,0.5,0);
const vec3 myBlue = vec3(0,0.3,0.6);
//Star Vertex
const vec3 myHalfSpace0 = vec3(0.31,0.95,0);
const vec3 myHalfSpace1 = vec3(-0.81,0.59,0);
const vec3 myHalfSpace2 = vec3(-0.81,-0.59,0);
const vec3 myHalfSpace3 = vec3(0.31,-0.95,0);
const vec3 myHalfSpace4 = vec3(1.,0.,0.);
const float stripeThickness = 0.4;
const float starSize = 0.2;
const float smoothEdgeTol = 0.005;
const float ambientLighting = 0.5;
const float specularExp = 128.0;
const float specularIntensity = 1.;
void main()
{
vec4 distVector;
float distScalar;
vec3 NN = normalize(N);
vec3 NL = normalize(L); //for ambient;
vec3 NH = normalize(NL +vec3(0,0,1)); //for specular
vec3 NV = normalize(V);
float myInOut = -3;
distVector.x = dot(NV, myHalfSpace0);
distVector.y = dot(NV, myHalfSpace1);
distVector.z = dot(NV, myHalfSpace2);
distVector.w = dot(NV, myHalfSpace3);
distScalar = dot(NV,myHalfSpace4);
distVector += starSize;
distScalar += starSize;
//map 0 ~ 1
distVector = smoothstep(0.,smoothEdgeTol, distVector);
distScalar = smoothstep(0.,smoothEdgeTol, distScalar);
myInOut += dot(distVector, vec4(1.0));
myInOut += distScalar;
myInOut = clamp(myInOut, 0,1);
vec3 surfColor = mix(myYellow, myRed, myInOut);
myInOut = smoothstep(0,smoothEdgeTol,abs(NV.z) – stripeThickness);
surfColor = mix(myBlue,surfColor,myInOut);
surfColor *= (ambientLighting + vec3(max(0,dot(NN,NL))));
surfColor += (specularIntensity * vec3(pow(max(0,dot(NN,NH)),specularExp)));
gl_FragColor = vec4(surfColor,1);
}
]]>
</program>
</language>
</jittershader>
[/html]
星の各点との内積をとって、星の頂点方向の長さ(0以上1以下)を合計し星のサイズをオフセットとして加算。その値が閾値を超えたら赤色にして、超えなかったら黄色に塗って星を描画している。星の頂点位置とVertexの単位ベクトルの関係性を使って星の形を描画しているのだが、ちょっとまだ理解しきれていない・・・。