-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathplanetStart.js
175 lines (125 loc) · 7.61 KB
/
planetStart.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var planetStart = {};
planetStart.create = function() {
var obj = new Object();
obj.collision = collisionMesh.create();
obj.model = modelCreate();
obj.gravity = gravityMesh.create();
var subdiv = 10;
for(var j = 0; j < subdiv; j++) {
for(var i = 0; i < subdiv; i++) {
var x = [((i - subdiv / 2) / (subdiv / 2)) * 100, (((i + 1) - subdiv / 2) / (subdiv / 2)) * 100];
var y = [((j - subdiv / 2) / (subdiv / 2)) * 100, (((j + 1) - subdiv / 2) / (subdiv / 2)) * 100];
var z = [ Math.cos(((i) - subdiv / 2) / (subdiv / 2)) * 30 + Math.cos(((j) - subdiv / 2) / (subdiv / 2)) * 30,
Math.cos(((i + 1) - subdiv / 2) / (subdiv / 2)) * 30 + Math.cos(((j) - subdiv / 2) / (subdiv / 2)) * 30,
Math.cos(((i + 1) - subdiv / 2) / (subdiv / 2)) * 30 + Math.cos(((j + 1) - subdiv / 2) / (subdiv / 2)) * 30,
Math.cos(((i) - subdiv / 2) / (subdiv / 2)) * 30 + Math.cos(((j + 1) - subdiv / 2) / (subdiv / 2)) * 30];
var vecs = [[x[0],y[0],z[0]], [x[1],y[0],z[1]], [x[1],y[1],z[2]], [x[0],y[1],z[3]]];
obj.collision.addTriangle(vecs[0],vecs[1],vecs[2]);
obj.collision.addTriangle(vecs[0],vecs[2],vecs[3]);
modelAddVertices(obj.model, [vecs[0][0],vecs[0][1],vecs[0][2], vecs[0][0],vecs[0][1],vecs[0][2], 0,0,1], 9);
modelAddVertices(obj.model, [vecs[1][0],vecs[1][1],vecs[1][2], vecs[1][0],vecs[1][1],vecs[1][2], 0,0,1], 9);
modelAddVertices(obj.model, [vecs[2][0],vecs[2][1],vecs[2][2], vecs[2][0],vecs[2][1],vecs[2][2], 0,0,1], 9);
modelAddVertices(obj.model, [vecs[0][0],vecs[0][1],vecs[0][2], vecs[0][0],vecs[0][1],vecs[0][2], 0,0,1], 9);
modelAddVertices(obj.model, [vecs[2][0],vecs[2][1],vecs[2][2], vecs[2][0],vecs[2][1],vecs[2][2], 0,0,1], 9);
modelAddVertices(obj.model, [vecs[3][0],vecs[3][1],vecs[3][2], vecs[3][0],vecs[3][1],vecs[3][2], 0,0,1], 9);
}
}
modelFinish(obj.model);
obj.modelSky = modelCreate();
modelAddSphere(obj.modelSky, 0,0,0, 400, 10);
modelFinish(obj.modelSky);
obj.shader = hglCreateProgram(shaderPlanetStartVertex, shaderPlanetStartFragment);
obj.shaderSky = hglCreateProgram(shaderSkyStartVertex, shaderSkyStartFragment);
obj.tempVec = vec3.create();
obj.tempVec2 = vec3.create();
obj.tempVec3 = vec3.create();
obj.getGravity = function(ret, pos, lastGrav) {
vec3.set([0,0,-1], ret);//obj.gravity.getGravity(pos), ret);
return 0;
}
obj.getMovementStickness = function(ind) {
return 1;
}
obj.getCollision = function(orig, dir) {
return obj.collision.checkRay(orig, dir);
}
obj.getCollisionNormal = function(i) {
return obj.collision.mesh[i].normal;
}
obj.draw = function() {
gl.disable(gl.CULL_FACE);
gl.bindBuffer(gl.ARRAY_BUFFER, obj.model.buffer);
gl.vertexAttribPointer(obj.shader.vertexPositionAttribute, 3, gl.FLOAT, false, 9 * 4, 0);
gl.vertexAttribPointer(obj.shader.vertexTextureAttribute, 3, gl.FLOAT, false, 9 * 4, 3 * 4);
gl.vertexAttribPointer(obj.shader.vertexNormalAttribute, 3, gl.FLOAT, false, 9 * 4, 6 * 4);
obj.shader.use();
obj.shader.setMatrices();
gl.drawArrays(gl.TRIANGLES, 0, obj.model.vertices);
gl.bindBuffer(gl.ARRAY_BUFFER, obj.modelSky.buffer);
gl.vertexAttribPointer(obj.shaderSky.vertexPositionAttribute, 3, gl.FLOAT, false, 9 * 4, 0);
gl.vertexAttribPointer(obj.shaderSky.vertexTextureAttribute, 3, gl.FLOAT, false, 9 * 4, 3 * 4);
gl.vertexAttribPointer(obj.shaderSky.vertexNormalAttribute, 3, gl.FLOAT, false, 9 * 4, 6 * 4);
obj.shaderSky.use();
obj.shaderSky.setMatrices();
gl.drawArrays(gl.TRIANGLES, 0, obj.modelSky.vertices);
gl.enable(gl.CULL_FACE);
}
return obj;
}
var shaderSkyStartFragment =
"precision mediump float;" +
"uniform float uTime;" +
"varying vec3 vFragColor;" +
"varying vec3 vFragNormal;" +
"varying vec3 vFragTexture;" +
"float hash3D(vec3 p) { return fract(sin(dot(p,vec3(12.193,89.123,-54.433))) * 43758.5453); } float noise(vec3 p) { vec3 pint = floor(p); vec3 pfrc = fract(p); const vec3 padd = vec3(1,0,0); float v000 = hash3D(pint + padd.yyy); float v001 = hash3D(pint + padd.yyx); float v010 = hash3D(pint + padd.yxy); float v011 = hash3D(pint + padd.yxx); float v100 = hash3D(pint + padd.xyy); float v101 = hash3D(pint + padd.xyx); float v110 = hash3D(pint + padd.xxy); float v111 = hash3D(pint + padd.xxx); return mix( mix( mix(v000,v100,pfrc.x), mix(v010,v110,pfrc.x), pfrc.y), mix( mix(v001,v101,pfrc.x), mix(v011,v111,pfrc.x), pfrc.y), pfrc.z); } float fbm(vec3 p) { float r = 0.0; float div = 0.75; for(int i = 0; i < 4; i++) { r += noise(p) * div; p *= 2.03; div *= 0.5; } return r; }" +
"void main(void) {" +
" gl_FragColor = mix(vec4(1,1,0,1), mix(vec4(1,1,1,1), mix(vec4(0.3,0.8,1,1), vec4(0.2,0.5,0.8,1), vFragTexture.z), clamp(fbm(vFragTexture * 16.0) + 0.25, 0.0, 1.0)), 1.0);/*clamp(1.0 - (1.0 - length(vFragTexture - vec3(0,0,0))) * 2.0, 0.0, 1.0));*/" +
"}";
var shaderSkyStartVertex =
"attribute vec3 aVertexPosition;" +
"attribute vec3 aVertexColor;" +
"attribute vec3 aVertexTexture;" +
"attribute vec3 aVertexNormal;" +
"uniform mat4 uMVMatrix;" +
"uniform mat4 uPMatrix;" +
"varying vec3 vFragColor;" +
"varying vec3 vFragNormal;" +
"varying vec3 vFragTexture;" +
"void main(void) {" +
" gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);" +
" vFragNormal = vec3(uMVMatrix * vec4(aVertexNormal, 0.0));" +
" vFragColor = aVertexTexture;" +
" vFragTexture = aVertexTexture;" +
"}";
var shaderPlanetStartFragment =
"precision mediump float;" +
"uniform float uTime;" +
"varying vec3 vFragColor;" +
"varying vec3 vFragNormal;" +
"varying vec3 vFragTexture;" +
"float hash3D(vec3 p) { return fract(sin(dot(p,vec3(12.193,89.123,-54.433))) * 43758.5453); } float noise(vec3 p) { vec3 pint = floor(p); vec3 pfrc = fract(p); const vec3 padd = vec3(1,0,0); float v000 = hash3D(pint + padd.yyy); float v001 = hash3D(pint + padd.yyx); float v010 = hash3D(pint + padd.yxy); float v011 = hash3D(pint + padd.yxx); float v100 = hash3D(pint + padd.xyy); float v101 = hash3D(pint + padd.xyx); float v110 = hash3D(pint + padd.xxy); float v111 = hash3D(pint + padd.xxx); return mix( mix( mix(v000,v100,pfrc.x), mix(v010,v110,pfrc.x), pfrc.y), mix( mix(v001,v101,pfrc.x), mix(v011,v111,pfrc.x), pfrc.y), pfrc.z); } float fbm(vec3 p) { float r = 0.0; float div = 0.75; for(int i = 0; i < 4; i++) { r += noise(p) * div; p *= 2.03; div *= 0.5; } return r; }" +
"void main(void) {" +
" vec3 normal = normalize(vFragNormal);" +
" /*float eyeZ = -dot(vec3(0,0,-1), normal);" +
" float eyeXY = (vec3(0,0,-1) + (eyeZ * normal));*/" +
" vec3 col = mix(vec3(0.1,0.9,0.2), vec3(0.1,0.7,0.0), clamp(fbm(vFragTexture) + 0.25, 0.0, 1.0));" +
" float diffuse = clamp(dot(normalize(normal), normalize(vec3(0,0,1))), 0., 1.);" +
" gl_FragColor = mix(vec4(col * (diffuse * 0.5 + 0.5), 1), vec4(1,1,1,1), clamp(1. - (diffuse * 2.), 0., 1.) * 0.5);" +
"}";
var shaderPlanetStartVertex =
"attribute vec3 aVertexPosition;" +
"attribute vec3 aVertexColor;" +
"attribute vec3 aVertexTexture;" +
"attribute vec3 aVertexNormal;" +
"uniform mat4 uMVMatrix;" +
"uniform mat4 uPMatrix;" +
"varying vec3 vFragColor;" +
"varying vec3 vFragNormal;" +
"varying vec3 vFragTexture;" +
"void main(void) {" +
" gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);" +
" vFragNormal = vec3(uMVMatrix * vec4(aVertexNormal, 0.0));" +
" vFragColor = aVertexTexture;" +
" vFragTexture = aVertexTexture;" +
"}";