-
Notifications
You must be signed in to change notification settings - Fork 6
/
planet.js
112 lines (96 loc) · 3.21 KB
/
planet.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
"use strict"
let gl, canvas, viewProjectionMatrix, sphereBufferInfo, sphereTranslation, sphereUniforms, programInfo;
let shapes;
function main(){
// Get A WebGL context
/** @type {HTMLCanvasElement} */
canvas = document.querySelector("#myCanvas");
gl = canvas.getContext("webgl");
if (!gl) {
return;
}
// setup GLSL program
programInfo = webglUtils.createProgramInfo(gl, ["vertex-shader-3d", "fragment-shader-3d"]);
setupShapes();
requestAnimationFrame(mainLoop);
}
function mainLoop(time) {
time *= 0.001;
clearCanvas();
updateCamera();
updateShapes(time);
drawShapes();
requestAnimationFrame(mainLoop);
}
function clearCanvas() {
webglUtils.resizeCanvasToDisplaySize(gl.canvas);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.enable(gl.CULL_FACE);
gl.enable(gl.DEPTH_TEST);
// Clear the canvas AND the depth buffer.
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
}
function updateCamera() {
// Compute the projection matrix
var aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
function degToRad(d) {
return d * Math.PI / 180;
}
var fieldOfViewRadians = degToRad(60);
var projectionMatrix =
m4.perspective(fieldOfViewRadians, aspect, 1, 2000);
// Compute the camera's matrix using look at.
var cameraPosition = [0, 0, 100];
var target = [0, 0, 0];
var up = [0, 1, 0];
var cameraMatrix = m4.lookAt(cameraPosition, target, up);
// Make a view matrix from the camera matrix.
var viewMatrix = m4.inverse(cameraMatrix);
viewProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
}
function setupShapes() {
// creates buffers with position, normal, texcoord, and vertex color
// data for primitives by calling gl.createBuffer, gl.bindBuffer,
// and gl.bufferData
let radius = 30;
let subdivisionsAxis = 24;
let subdivisionsHeight = 24;
sphereBufferInfo = primitives.createSphereWithVertexColorsBufferInfo(gl, radius, subdivisionsAxis, subdivisionsHeight);
sphereUniforms = {
u_colorMult: [1, 0.5, 1, 1],
u_matrix: m4.identity(),
};
shapes = [
{
programInfo: programInfo,
bufferInfo: sphereBufferInfo,
uniforms: sphereUniforms
}
];
}
function updateShapes(time) {
time *= 8;
// Setup all the needed attributes.
sphereTranslation = [0, 0, 0];
let matrix = m4.translate(viewProjectionMatrix,
sphereTranslation[0],
sphereTranslation[1],
sphereTranslation[2]);
matrix = m4.xRotate(matrix, Math.PI * 0.1);
matrix = m4.zRotate(matrix, Math.PI * 0.15 + time * 0.1);
matrix = m4.yRotate(matrix, -time);
sphereUniforms.u_matrix = matrix;
}
function drawShapes() {
shapes.forEach(function(object) {
let bufferInfo = object.bufferInfo;
gl.useProgram(object.programInfo.program);
// Setup all the needed attributes.
webglUtils.setBuffersAndAttributes(gl, object.programInfo, bufferInfo);
// Set the uniforms.
webglUtils.setUniforms(object.programInfo, object.uniforms);
// Draw
gl.drawArrays(gl.TRIANGLES, 0, bufferInfo.numElements);
});
}
main();