Skip to content

Instantly share code, notes, and snippets.

@micahscopes
Created June 23, 2012 20:20
Show Gist options
  • Save micahscopes/2979815 to your computer and use it in GitHub Desktop.
Save micahscopes/2979815 to your computer and use it in GitHub Desktop.
import bge
cont = bge.logic.getCurrentController()
VertexShader = """
uniform float leftIsoclinic[ 4 ];
uniform float rightIsoclinic[ 4 ];
uniform float radius;
uniform float offset[ 3 ];
varying vec3 vNormal;
void main() {
vec3 pos = vec3(gl_Vertex);
vec3 off = vec3(offset[0],offset[1],offset[2]);
vec3 transformedNormal = gl_NormalMatrix * gl_Normal;
float a = leftIsoclinic[0];
float b = leftIsoclinic[1];
float c = leftIsoclinic[2];
float d = leftIsoclinic[3];
float aa = rightIsoclinic[0];
float bb = rightIsoclinic[1];
float cc = rightIsoclinic[2];
float dd = rightIsoclinic[3];
mat4 hRot = mat4(a,-b,-c,-d,b,a,-d,c,c,d,a,-b,d,-c,b,a)*mat4(aa,-bb,-cc,-dd,bb,aa,dd,-cc,cc,-dd,aa,bb,dd,cc,-bb,aa);
vec3 p = (pos+off)/radius;
vec3 pn = p+(transformedNormal)/radius;
float dist = p[0]*p[0]+p[1]*p[1]+p[2]*p[2];
float distn = pn[0]*pn[0]+pn[1]*pn[1]+pn[2]*pn[2];
vec4 h = vec4(2.0*p,(dist-1.0));
vec4 hn = vec4(2.0*pn,(distn-1.0));
h = h/(1.0+dist);
hn = hn/(1.0+distn);
h = hRot*h;
hn = hRot*hn;
vec3 newPosition = radius*(vec3(h[0],h[1],h[2])/(1.0-h[3]));
vNormal = radius*(vec3(hn[0],hn[1],hn[2])/(1.0-hn[3]))-newPosition;
gl_Position = gl_ProjectionMatrix *
gl_ModelViewMatrix * vec4(newPosition-off,1.0);
}
"""
VertexShaderB = """
varying vec3 vNormal;
// normalized surface normal vector
varying vec3 varyingViewDirection;
// normalized view direction
void main()
{
vNormal =
normalize(gl_NormalMatrix * gl_Normal);
varyingViewDirection =
-normalize(vec3(gl_ModelViewMatrix * gl_Vertex));
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
"""
FragmentShader = """
varying vec3 vNormal;
// normalized surface normal vector
void main() {
// calc the dot product and clamp
// 0 -> 1 rather than -1 -> 1
vec3 light = vec3(0.5,0.9,1.0);
// ensure it's normalized
light = normalize(light);
float r = max(0.05, dot(normalize(vNormal), vec3(1,0,0)));
float g = max(0.05, dot(normalize(vNormal), vec3(0,1,0)));
float b = max(0.05, dot(normalize(vNormal), vec3(0,0,1)));
// feed into our frag colour
gl_FragColor = vec4(r,g,b,1.0);
}
"""
mesh = cont.owner.meshes[0]
for mat in mesh.materials:
shader = mat.getShader()
if shader != None:
if not shader.isValid():
shader.setSource(VertexShader, FragmentShader, 1)
shader.setUniform4f("leftIsoclinic", 1.0, 0.0, 0.0, 0.0)
shader.setUniform4f("rightIsoclinic", 1.0, 0.0, 0.0, 0.0)
shader.setUniform1f("radius",1.0)
shader.setUniform3f("offset",0.0, 0.0, 0.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment