Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / Project / shaders.js
blob: 9b9cf1e22c38b8ba0ff60bf1953138f8567cd57b (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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 highp float;

	uniform mat4 uProjectionMatrix;
	uniform mat4 uViewMatrix;
	uniform mat4 uModelMatrix;
	uniform mat4 uNormalMatrix;
	uniform bool uUseNormalMatrix;
	
	uniform vec3 uAmbientColor;
	uniform vec3 uDiffuseColor;
	
	uniform vec3 uLightDir;
	
	varying vec3 vColor;
	varying vec3 vXYZ;

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

	uniform vec3 uEye;

	// 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);
	}

	// Thanks https://en.wikipedia.org/wiki/Line%E2%80%93sphere_intersection#Calculation_using_vectors_in_3D
	vec3 spherePoint(vec3 lineOrigin, vec3 lineDir, vec4 sphere) {
		lineDir = normalize(lineDir);
		float b = dot(lineDir, lineOrigin - sphere.xyz);
		float Delta = b * b - (length(lineOrigin - sphere.xyz) * length(lineOrigin - sphere.xyz) - sphere.a * sphere.a);
		return lineOrigin + min(-b + sqrt(Delta), -b - sqrt(Delta)) * lineDir;
	}

	int rayMarchInBH(vec4 BH, inout vec3 ray, inout vec3 step) {
		ray += step * linePointDistance(ray, step, BH.xyz);
		for (int i = 0; i < 100; i++) {
			float dist = linePointDistance(ray, step, BH.xyz);
			if (dist <= BH.a) { // if inside event horizon
				return 1;
			}
			if (dist > BH.a * 3.0) { // if outside ISCO
				return 2;
			}
			step += normalize(BH.xyz - ray) * (3.5 - dist / BH.a) * 0.05;
			ray += step;
		}
		return 0; // inside photon sphere
	}

	vec4 calculateColor(bool notBent, vec4 star, vec3 ray, vec3 step) {
		if (notBent) {
			return vec4(vColor,linePointDistance(ray, step, star.xyz) / star.a + 0.1);
		}

		// vec3 anormal = spherePoint(ray, step, star) - star.xyz;
        //
		// mat4 mvMatrix = uViewMatrix * uModelMatrix;
		// mat4 nMatrix = uUseNormalMatrix?uNormalMatrix:mvMatrix;
		// vec3 color = uAmbientColor*vec3(0.95,0.5,0.215);
		// vec3 light = normalize(-uLightDir);
		// vec3 normal = vec3(normalize(nMatrix*vec4(anormal,0)));
		// color += vec3(0.95,0.5,0.215)*uDiffuseColor*max(dot(normal,light),0.0);

		return vec4(0.95,0.5,0.215,1);
	}

	vec4 rayMarch(vec3 ray, vec3 step) {
		int bhStatus = 3;
		for (int i = 0; i < 5; i++) {
			if (linePointDistance(ray, step, uBH[i].xyz) < uBH[i].a * 3.0) {
				bhStatus = rayMarchInBH(uBH[i], ray, step);
			}
		}

		if (bhStatus < 2) {
			return vec4(0,1,0,1);
		}

		for (int i = 0; i < 5; i++) {
			if (linePointDistance(ray, step, uStars[i].xyz) < uStars[i].a) {
				return calculateColor(bhStatus == 3, uStars[i], ray, step);
			}
		}
		return vec4(1,0,0, 1);
	}

	void main()
	{
		vec3 step = normalize(vXYZ - uEye);
		gl_FragColor = rayMarch(uEye, step);
	}
`;