Spaces:
Running
Running
Update test.js
Browse files
test.js
CHANGED
|
@@ -1,222 +1,205 @@
|
|
| 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 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
gl.uniform2f(mouseLocation, mouseX, mouseY);
|
| 207 |
-
gl.uniform1f(timeLocation, time);
|
| 208 |
-
|
| 209 |
-
gl.drawArrays(
|
| 210 |
-
gl.TRIANGLES,
|
| 211 |
-
0, // offset
|
| 212 |
-
6 // num vertices to process
|
| 213 |
-
);
|
| 214 |
-
|
| 215 |
-
requestFrame();
|
| 216 |
-
}
|
| 217 |
-
|
| 218 |
-
requestFrame();
|
| 219 |
-
requestAnimationFrame(cancelFrame);
|
| 220 |
-
}
|
| 221 |
-
|
| 222 |
-
main();
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<title>3D Cymatic Display in WebGL2</title>
|
| 6 |
+
<style>
|
| 7 |
+
body, html { margin: 0; height: 100%; overflow: hidden; background: #000; }
|
| 8 |
+
canvas { width: 100%; height: 100%; display: block; }
|
| 9 |
+
</style>
|
| 10 |
+
</head>
|
| 11 |
+
<body>
|
| 12 |
+
<canvas id="canvas"></canvas>
|
| 13 |
+
<!-- Include glMatrix and webgl-utils scripts -->
|
| 14 |
+
<script src="https://cdn.jsdelivr.net/npm/gl-matrix@3.4.3/gl-matrix-min.js"></script>
|
| 15 |
+
<script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script>
|
| 16 |
+
<script type="text/javascript">
|
| 17 |
+
"use strict";
|
| 18 |
+
function main() {
|
| 19 |
+
const canvas = document.querySelector("#canvas");
|
| 20 |
+
const gl = canvas.getContext("webgl2");
|
| 21 |
+
if (!gl) {
|
| 22 |
+
console.error("WebGL 2 not supported");
|
| 23 |
+
return;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// Vertex shader: displaces a grid using two vibrational modes and computes normals
|
| 27 |
+
const vsSource = `#version 300 es
|
| 28 |
+
precision highp float;
|
| 29 |
+
in vec3 a_position;
|
| 30 |
+
uniform float u_time;
|
| 31 |
+
uniform mat4 u_MVP;
|
| 32 |
+
out vec3 v_normal;
|
| 33 |
+
out vec3 v_position;
|
| 34 |
+
const float PI = 3.14159;
|
| 35 |
+
void main() {
|
| 36 |
+
// Amplitudes for two modes
|
| 37 |
+
float A1 = 0.1;
|
| 38 |
+
float A2 = 0.05;
|
| 39 |
+
// Mode 1: Fundamental vibration (nodal lines at the boundaries)
|
| 40 |
+
float mode1 = A1 * sin(PI * (a_position.x + 1.0) / 2.0)
|
| 41 |
+
* sin(PI * (a_position.y + 1.0) / 2.0)
|
| 42 |
+
* cos(3.0 * u_time);
|
| 43 |
+
// Mode 2: A higher-order vibration for additional detail
|
| 44 |
+
float mode2 = A2 * sin(2.0 * PI * (a_position.x + 1.0) / 2.0)
|
| 45 |
+
* sin(PI * (a_position.y + 1.0) / 2.0)
|
| 46 |
+
* cos(5.0 * u_time);
|
| 47 |
+
float z = mode1 + mode2;
|
| 48 |
+
|
| 49 |
+
// Compute partial derivatives for normal calculation
|
| 50 |
+
float dx1 = A1 * (PI/2.0) * cos(PI*(a_position.x+1.0)/2.0)
|
| 51 |
+
* sin(PI*(a_position.y+1.0)/2.0) * cos(3.0 * u_time);
|
| 52 |
+
float dx2 = A2 * (2.0*PI/2.0) * cos(2.0*PI*(a_position.x+1.0)/2.0)
|
| 53 |
+
* sin(PI*(a_position.y+1.0)/2.0) * cos(5.0 * u_time);
|
| 54 |
+
float dfdx = dx1 + dx2;
|
| 55 |
+
|
| 56 |
+
float dy1 = A1 * (PI/2.0) * sin(PI*(a_position.x+1.0)/2.0)
|
| 57 |
+
* cos(PI*(a_position.y+1.0)/2.0) * cos(3.0 * u_time);
|
| 58 |
+
float dy2 = A2 * (PI/2.0) * sin(2.0*PI*(a_position.x+1.0)/2.0)
|
| 59 |
+
* cos(PI*(a_position.y+1.0)/2.0) * cos(5.0 * u_time);
|
| 60 |
+
float dfdy = dy1 + dy2;
|
| 61 |
+
|
| 62 |
+
vec3 displacedPos = vec3(a_position.x, a_position.y, z);
|
| 63 |
+
// The normal is computed as the normalized cross of the tangent derivatives.
|
| 64 |
+
// For a height field, an approximate normal is: (-dfdx, -dfdy, 1)
|
| 65 |
+
vec3 normal = normalize(vec3(-dfdx, -dfdy, 1.0));
|
| 66 |
+
|
| 67 |
+
v_normal = normal;
|
| 68 |
+
v_position = displacedPos;
|
| 69 |
+
gl_Position = u_MVP * vec4(displacedPos, 1.0);
|
| 70 |
+
}`;
|
| 71 |
+
|
| 72 |
+
// Fragment shader: uses Phong shading for realistic lighting
|
| 73 |
+
const fsSource = `#version 300 es
|
| 74 |
+
precision highp float;
|
| 75 |
+
in vec3 v_normal;
|
| 76 |
+
in vec3 v_position;
|
| 77 |
+
uniform vec3 u_lightPos;
|
| 78 |
+
uniform vec3 u_viewPos;
|
| 79 |
+
out vec4 outColor;
|
| 80 |
+
void main() {
|
| 81 |
+
vec3 normal = normalize(v_normal);
|
| 82 |
+
vec3 lightDir = normalize(u_lightPos - v_position);
|
| 83 |
+
float diff = max(dot(normal, lightDir), 0.0);
|
| 84 |
+
vec3 ambient = vec3(0.2);
|
| 85 |
+
vec3 diffuse = diff * vec3(0.7, 0.7, 0.8);
|
| 86 |
+
vec3 viewDir = normalize(u_viewPos - v_position);
|
| 87 |
+
vec3 reflectDir = reflect(-lightDir, normal);
|
| 88 |
+
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
|
| 89 |
+
vec3 specular = vec3(0.3) * spec;
|
| 90 |
+
vec3 color = ambient + diffuse + specular;
|
| 91 |
+
outColor = vec4(color, 1.0);
|
| 92 |
+
}`;
|
| 93 |
+
|
| 94 |
+
// Create the shader program using webgl-utils
|
| 95 |
+
const program = webglUtils.createProgramFromSources(gl, [vsSource, fsSource]);
|
| 96 |
+
|
| 97 |
+
// Look up attribute and uniform locations
|
| 98 |
+
const positionAttribLocation = gl.getAttribLocation(program, "a_position");
|
| 99 |
+
const timeUniformLocation = gl.getUniformLocation(program, "u_time");
|
| 100 |
+
const mvpUniformLocation = gl.getUniformLocation(program, "u_MVP");
|
| 101 |
+
const lightPosUniformLocation = gl.getUniformLocation(program, "u_lightPos");
|
| 102 |
+
const viewPosUniformLocation = gl.getUniformLocation(program, "u_viewPos");
|
| 103 |
+
|
| 104 |
+
// Create a grid covering [-1,1]x[-1,1]
|
| 105 |
+
const gridSize = 150;
|
| 106 |
+
const positions = [];
|
| 107 |
+
for (let j = 0; j <= gridSize; j++) {
|
| 108 |
+
for (let i = 0; i <= gridSize; i++) {
|
| 109 |
+
// Map grid coordinates to [-1,1]
|
| 110 |
+
let x = (i / gridSize) * 2 - 1;
|
| 111 |
+
let y = (j / gridSize) * 2 - 1;
|
| 112 |
+
positions.push(x, y, 0);
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
// Generate indices for triangles
|
| 117 |
+
const indices = [];
|
| 118 |
+
for (let j = 0; j < gridSize; j++) {
|
| 119 |
+
for (let i = 0; i < gridSize; i++) {
|
| 120 |
+
let a = j * (gridSize + 1) + i;
|
| 121 |
+
let b = a + 1;
|
| 122 |
+
let c = a + (gridSize + 1);
|
| 123 |
+
let d = c + 1;
|
| 124 |
+
indices.push(a, b, c);
|
| 125 |
+
indices.push(b, d, c);
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
// Create and bind a Vertex Array Object
|
| 130 |
+
const vao = gl.createVertexArray();
|
| 131 |
+
gl.bindVertexArray(vao);
|
| 132 |
+
|
| 133 |
+
// Create and fill the position buffer
|
| 134 |
+
const positionBuffer = gl.createBuffer();
|
| 135 |
+
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
|
| 136 |
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
|
| 137 |
+
gl.enableVertexAttribArray(positionAttribLocation);
|
| 138 |
+
gl.vertexAttribPointer(positionAttribLocation, 3, gl.FLOAT, false, 0, 0);
|
| 139 |
+
|
| 140 |
+
// Create and fill the index buffer
|
| 141 |
+
const indexBuffer = gl.createBuffer();
|
| 142 |
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
|
| 143 |
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint32Array(indices), gl.STATIC_DRAW);
|
| 144 |
+
|
| 145 |
+
// Set up camera matrices using glMatrix
|
| 146 |
+
const fieldOfView = 45 * Math.PI / 180;
|
| 147 |
+
const near = 0.1;
|
| 148 |
+
const far = 100;
|
| 149 |
+
let projectionMatrix = glMatrix.mat4.create();
|
| 150 |
+
let viewMatrix = glMatrix.mat4.create();
|
| 151 |
+
let modelMatrix = glMatrix.mat4.create();
|
| 152 |
+
const eye = [0, -2.5, 2.5]; // Camera position
|
| 153 |
+
const center = [0, 0, 0]; // Look at center of plate
|
| 154 |
+
const up = [0, 0, 1];
|
| 155 |
+
glMatrix.mat4.perspective(projectionMatrix, fieldOfView, canvas.clientWidth / canvas.clientHeight, near, far);
|
| 156 |
+
glMatrix.mat4.lookAt(viewMatrix, eye, center, up);
|
| 157 |
+
// Optionally, rotate the model slightly for a dramatic view
|
| 158 |
+
glMatrix.mat4.rotateX(modelMatrix, modelMatrix, -0.5);
|
| 159 |
+
let mvpMatrix = glMatrix.mat4.create();
|
| 160 |
+
glMatrix.mat4.multiply(mvpMatrix, viewMatrix, modelMatrix);
|
| 161 |
+
glMatrix.mat4.multiply(mvpMatrix, projectionMatrix, mvpMatrix);
|
| 162 |
+
|
| 163 |
+
// Set light and view positions (for the shader)
|
| 164 |
+
const lightPos = [2.0, -2.0, 3.0];
|
| 165 |
+
const viewPos = eye;
|
| 166 |
+
|
| 167 |
+
// Animation loop
|
| 168 |
+
let startTime = null;
|
| 169 |
+
function render(now) {
|
| 170 |
+
if (!startTime) startTime = now;
|
| 171 |
+
const timeInSeconds = (now - startTime) * 0.001;
|
| 172 |
+
|
| 173 |
+
// Resize canvas if needed
|
| 174 |
+
webglUtils.resizeCanvasToDisplaySize(gl.canvas);
|
| 175 |
+
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
|
| 176 |
+
gl.enable(gl.DEPTH_TEST);
|
| 177 |
+
gl.clearColor(0.0, 0.0, 0.0, 1.0);
|
| 178 |
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
| 179 |
+
|
| 180 |
+
// Update the projection in case the canvas size changed
|
| 181 |
+
glMatrix.mat4.perspective(projectionMatrix, fieldOfView, gl.canvas.clientWidth / gl.canvas.clientHeight, near, far);
|
| 182 |
+
glMatrix.mat4.lookAt(viewMatrix, eye, center, up);
|
| 183 |
+
glMatrix.mat4.multiply(mvpMatrix, viewMatrix, modelMatrix);
|
| 184 |
+
glMatrix.mat4.multiply(mvpMatrix, projectionMatrix, mvpMatrix);
|
| 185 |
+
|
| 186 |
+
// Use our program and bind the VAO
|
| 187 |
+
gl.useProgram(program);
|
| 188 |
+
gl.bindVertexArray(vao);
|
| 189 |
+
|
| 190 |
+
// Set shader uniforms
|
| 191 |
+
gl.uniform1f(timeUniformLocation, timeInSeconds);
|
| 192 |
+
gl.uniformMatrix4fv(mvpUniformLocation, false, mvpMatrix);
|
| 193 |
+
gl.uniform3fv(lightPosUniformLocation, lightPos);
|
| 194 |
+
gl.uniform3fv(viewPosUniformLocation, viewPos);
|
| 195 |
+
|
| 196 |
+
// Draw the grid
|
| 197 |
+
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_INT, 0);
|
| 198 |
+
requestAnimationFrame(render);
|
| 199 |
+
}
|
| 200 |
+
requestAnimationFrame(render);
|
| 201 |
+
}
|
| 202 |
+
main();
|
| 203 |
+
</script>
|
| 204 |
+
</body>
|
| 205 |
+
</html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|