cartesian.fs 3.58 KB
Newer Older
1 2
#version 120

3 4
#define SHADOW_STRENGTH (0.7)

5
uniform vec3 vPosition, vScale;
6
uniform float fTime;
7 8
varying vec4 vDiffuse, vGlobal, vAmbient;
varying vec3 vDir, vHalfVector;
9
varying float fDist;
10
varying vec3 vFuncParams;
11 12 13 14
varying mat3 mNormalMatrix;
uniform sampler2D tTexture;
uniform sampler2D tBumpMap;
uniform sampler2DShadow tShadow;
15
uniform sampler2D tAudio;
16 17 18 19 20 21 22
uniform float fBumpScale;
uniform float fTextureStrength;
varying vec4 vShadowPos;
uniform mat4 mLightTransform;
uniform float fShadowBias;
uniform float fShadowBlurX;
uniform float fShadowBlurY;
23

24 25
/*REPLACE:controlvars*//*END*/

26 27
float DifferentialX (float x, float y, float t);
float DifferentialY (float x, float y, float t);
28
vec4 Colour (float x, float y, float z, float t);
29
float audio (float x);
30 31 32 33 34

void main () {
	vec3 vHalf;
	float fNDotL;
	float fNDotHV;
35 36
	vec4 vColour;
	vec4 vColourRaw;
37
	float fAttenuation;
38
	vec3 vNormalBidir;
39
	vec3 vNormal;
40 41
	vec3 vTangentX;
	vec3 vTangentY;
42 43 44 45 46 47
	vec3 vBump;
	vec3 vNormInt;
	vec4 vShadowTexPos;
	float fShadowStrength;
	int nShadowX;
	int nShadowY;
48 49 50 51 52 53 54 55 56 57 58 59

	// Calculate two tangents to the curve at the point where the fragment touches
	vTangentX.x = 1.0f;
	vTangentX.y = 0.0f;
	vTangentX.z = (DifferentialX (vFuncParams.x, vFuncParams.y, fTime) - vPosition.z) / vScale.z;

	vTangentY.x = 0.0f;
	vTangentY.y = 1.0f;
	vTangentY.z = (DifferentialY (vFuncParams.x, vFuncParams.y, fTime) - vPosition.z) / vScale.z;

	// Calculate the normal from the tangents
	vNormal = cross (vTangentX, vTangentY);
60
	vNormal = normalize (mNormalMatrix * vNormal);
61 62 63 64 65 66 67

	if (gl_FrontFacing) {
		vNormalBidir = vNormal;
	}
	else {
		vNormalBidir = -vNormal;
	}
68

69 70 71 72
	// Calculate the material colour
	vColourRaw = Colour (vFuncParams.x, vFuncParams.y, vFuncParams.z, fTime);
	vColour = vGlobal * vColourRaw;

73
	// Calculate dot product between normal and light direction
74
	fNDotL = max (dot (vNormalBidir, normalize (vDir)), 0.0);
75 76 77

	if (fNDotL > 0.0) {
		fAttenuation = 1.0 / (gl_LightSource[0].constantAttenuation + gl_LightSource[0].linearAttenuation * fDist + gl_LightSource[0].quadraticAttenuation * fDist * fDist);
78
		vColour += fAttenuation * (vDiffuse * fNDotL + vAmbient) * vColourRaw;
79 80

		vHalf = normalize (vHalfVector);
81
		fNDotHV = max (dot (vNormalBidir, vHalf), 0.0);
82 83 84 85
		vColour += fAttenuation * gl_FrontMaterial.specular * gl_LightSource[0].specular * pow (fNDotHV, gl_FrontMaterial.shininess);
	}

	gl_FragColor = vColour;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	
	// Convert the fragment coordinate into a shadow texture coordinate
	vShadowTexPos = ((vShadowPos / vShadowPos.w) + 1.0f) / 2.0f;
	// Add slight bias to avoid shadow acne
	vShadowTexPos.z -= fShadowBias;

	// Blur the shadow
	fShadowStrength = 0.0;
	for (nShadowX = -1; nShadowX < 2; nShadowX++) {
		for (nShadowY = -1; nShadowY < 2; nShadowY++) {
			fShadowStrength += shadow2D (tShadow, vShadowTexPos.xyz + vec3(nShadowX * fShadowBlurX, nShadowY * fShadowBlurY, 0.0f)).x;
		}
	}

	// Darken the colour in proportion to the shadow level	
	gl_FragColor *= vec4 ((1.0 - SHADOW_STRENGTH), (1.0 - SHADOW_STRENGTH), (1.0 - SHADOW_STRENGTH), (1.0 - SHADOW_STRENGTH)) + (SHADOW_STRENGTH * fShadowStrength / 9.0f);
	
103 104 105
	gl_FragColor.a = vColourRaw.a;
}

106 107 108 109
float audio (float x) {
	return texture2D (tAudio, vec2(x, 0)).r;
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123
float DifferentialX (float x, float y, float t) {
	float z;
	
	z = /*REPLACE:diffX*/0.0f/*END*/;
	
	return z;
}

float DifferentialY (float x, float y, float t) {
	float z;

	z = /*REPLACE:diffY*/0.0f/*END*/;
	
	return z;
124 125 126 127 128 129 130 131 132 133 134
}

vec4 Colour (float x, float y, float z, float t) {
	vec4 c;

	c.r = /*REPLACE:red*/1.0f/*END*/;
	c.g = /*REPLACE:green*/1.0f/*END*/;
	c.b = /*REPLACE:blue*/1.0f/*END*/;
	c.a = /*REPLACE:alpha*/1.0f/*END*/;
	
	return c;
135 136
}