Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / Project / shaders.js
blob: c3a2ab40287c5e77b4ce05bfe7ed7f52c0055588 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var vShader =`
	uniform mat4 uProjectionMatrix;
	uniform mat4 uViewMatrix;
	uniform mat4 uModelMatrix;
	uniform mat4 uNormalMatrix;
	uniform bool uUseNormalMatrix;
	
	uniform vec3 uAmbientColor;
	uniform vec3 uDiffuseColor;
	
	uniform vec3 uLightDir;
	
	attribute vec3 aXYZ;
	attribute vec3 aColor;
	attribute vec3 aNormal;

	varying vec3 vColor;
	varying vec3 vXYZ;
	
	void main ()
	{
		mat4 mvMatrix = uViewMatrix * uModelMatrix;
		gl_Position = uProjectionMatrix * mvMatrix * vec4(aXYZ,1);
		mat4 nMatrix = uUseNormalMatrix?uNormalMatrix:mvMatrix;
	
		vColor = uAmbientColor*aColor;
	
		vec3 light = normalize(-uLightDir);
		vec3 normal = vec3(normalize(nMatrix*vec4(aNormal,0)));
		vColor += aColor*uDiffuseColor*max(dot(normal,light),0.0);
		vXYZ = (uModelMatrix * vec4(aXYZ,1)).xyz;
	}
`;
	
var fShader = `
	precision mediump float;
	
	varying vec3 vColor;
	varying vec3 vXYZ;

	uniform vec4 uStars[5];
	uniform vec4 uBH[5];

	uniform vec3 uEye;
	uniform vec3 uEyeForward;
	uniform vec3 uEyeUp;
	uniform vec3 uEyeLeft;

	uniform vec2 uViewPort;
	uniform vec2 uPerspSize;
	uniform float uPerspDist;

	// Thanks https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Vector_formulation
	float linePointDistance(vec3 linePoint, vec3 lineDir, vec3 point) {
		return length((point - linePoint) - dot(point - linePoint, lineDir) * lineDir);
	}

	// vec4 rayMarchInBH(vec4 BH, vec3 ray, vec3 step) {
	// 	for (int i = 0; i < 100; i++) {
	// 		float dist = linePointDistance(ray, ray+step, BH.xyz);
	// 		if (dist <= BH.a) { // if past event horizon
	// 			return vec4(0,0,0,1);
	// 		}
	// 		if (dist > BH.a * 1.5) { // if outside photon sphere
	// 			return rayMarch(ray, step);
	// 		}
	// 		step += bend; // ROADBLOCK
	// 		ray += step;
	// 	}
	// 	return vec4(0,0,0,1);
	// }

	vec4 rayMarch(vec3 ray, vec3 step) {
		// for (int i = 0; i < 5; i++) {
		// 	if (linePointDistance(ray, ray+step, uBH[i].xyz) <= uBH[i].a * 1.5) {
		// 		return rayMarchInBh(uBH, ray, step);
		// 	}
		// }

		for (int i = 0; i < 1; i++) {
			if (linePointDistance(uEye, ray, uStars[i].xyz) < uStars[i].a) {
				return vec4(vColor, 1);
			}
		}
		return vec4(1,0,0, 1);
	}

	void main()
	{
		// Refer to Project.html for more info, but in brief, we're transforming
		// the screen pixel coordinates to the proper 3D coordinates
		vec2 offset = gl_FragCoord.xy / uViewPort; // from pixel coordinates to [0,1]
		offset = 2.0 * offset - 1.0; // to [-1,1]
		offset.x *= uPerspSize.x;
		offset.y *= uPerspSize.y;

		vec3 ray = normalize(uEyeForward * uPerspDist + offset.y * uEyeUp - offset.x * uEyeLeft);

		gl_FragColor = rayMarch(ray, ray);
	}
`;