Changed around line 1
-
-
-
-
GLSL Shader-
- body {
- margin: 0;
- overflow: hidden;
- background: #000;
- }
- canvas {
- width: 100vw;
- height: 100vh;
- display: block;
- }
-
-
-
-
-
- const vertexShaderSource = `
+
+ body {
+ margin: 0;
+ overflow: hidden;
+ background: #000;
+ }
+ canvas {
+ width: 100vw;
+ height: 100vh;
+ display: block;
+ }
+
+
+
+ const vertexShaderSource = `
- const fragmentShaderSource = `
+ const fragmentShaderSource = `
Changed around line 78
- function createShader(gl, type, source) {
- const shader = gl.createShader(type);
- gl.shaderSource(shader, source);
- gl.compileShader(shader);
-
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
- console.error("Shader compile error:", gl.getShaderInfoLog(shader));
- gl.deleteShader(shader);
- throw new Error(
- "Shader compilation failed: " + gl.getShaderInfoLog(shader),
- );
- }
- return shader;
- }
-
- function initShaderProgram(gl, vsSource, fsSource) {
- const vertexShader = createShader(gl, gl.VERTEX_SHADER, vsSource);
- const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fsSource);
-
- const shaderProgram = gl.createProgram();
- gl.attachShader(shaderProgram, vertexShader);
- gl.attachShader(shaderProgram, fragmentShader);
- gl.linkProgram(shaderProgram);
-
- if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
- console.error(
- "Program link error:",
- gl.getProgramInfoLog(shaderProgram),
- );
- return null;
- }
- return shaderProgram;
- }
-
- function main() {
- const canvas = document.querySelector("#glCanvas");
- const gl = canvas.getContext("webgl");
-
- if (!gl) {
- console.error("WebGL not available");
- return;
- }
-
- const shaderProgram = initShaderProgram(
- gl,
- vertexShaderSource,
- fragmentShaderSource,
- );
- if (!shaderProgram) return;
-
- const programInfo = {
- program: shaderProgram,
- attribLocations: {
- vertexPosition: gl.getAttribLocation(
- shaderProgram,
- "aVertexPosition",
- ),
- },
- uniformLocations: {
- resolution: gl.getUniformLocation(shaderProgram, "r"),
- time: gl.getUniformLocation(shaderProgram, "t"),
- },
- };
-
- const positions = new Float32Array([
- -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0,
- ]);
-
- const positionBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
- gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
-
- let then = 0;
- function render(now) {
- now *= 0.001; // Convert to seconds
- const deltaTime = now - then;
- then = now;
-
- canvas.width = window.innerWidth;
- canvas.height = window.innerHeight;
- gl.viewport(0, 0, canvas.width, canvas.height);
-
- gl.clearColor(0.0, 0.0, 0.0, 1.0);
- gl.clear(gl.COLOR_BUFFER_BIT);
-
- gl.useProgram(programInfo.program);
-
- gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
- gl.vertexAttribPointer(
- programInfo.attribLocations.vertexPosition,
- 2, // 2 components per vertex
- gl.FLOAT, // the data is 32bit floats
- false, // don't normalize
- 0, // stride
- 0, // offset
- );
- gl.enableVertexAttribArray(
- programInfo.attribLocations.vertexPosition,
- );
-
- gl.uniform2f(
- programInfo.uniformLocations.resolution,
- canvas.width,
- canvas.height,
- );
- gl.uniform1f(programInfo.uniformLocations.time, now);
-
- gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
-
- requestAnimationFrame(render);
- }
- requestAnimationFrame(render);
- }
-
- window.onload = main;
- window.addEventListener("resize", () => {
- const canvas = document.querySelector("#glCanvas");
- canvas.width = window.innerWidth;
- canvas.height = window.innerHeight;
- });
-
-
-
+ function createShader(gl, type, source) {
+ const shader = gl.createShader(type);
+ gl.shaderSource(shader, source);
+ gl.compileShader(shader);
+
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
+ console.error("Shader compile error:", gl.getShaderInfoLog(shader));
+ gl.deleteShader(shader);
+ throw new Error(
+ "Shader compilation failed: " + gl.getShaderInfoLog(shader),
+ );
+ }
+ return shader;
+ }
+
+ function initShaderProgram(gl, vsSource, fsSource) {
+ const vertexShader = createShader(gl, gl.VERTEX_SHADER, vsSource);
+ const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fsSource);
+
+ const shaderProgram = gl.createProgram();
+ gl.attachShader(shaderProgram, vertexShader);
+ gl.attachShader(shaderProgram, fragmentShader);
+ gl.linkProgram(shaderProgram);
+
+ if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
+ console.error("Program link error:", gl.getProgramInfoLog(shaderProgram));
+ return null;
+ }
+ return shaderProgram;
+ }
+
+ function main() {
+ const canvas = document.querySelector("#glCanvas");
+ const gl = canvas.getContext("webgl");
+
+ if (!gl) {
+ console.error("WebGL not available");
+ return;
+ }
+
+ const shaderProgram = initShaderProgram(
+ gl,
+ vertexShaderSource,
+ fragmentShaderSource,
+ );
+ if (!shaderProgram) return;
+
+ const programInfo = {
+ program: shaderProgram,
+ attribLocations: {
+ vertexPosition: gl.getAttribLocation(shaderProgram, "aVertexPosition"),
+ },
+ uniformLocations: {
+ resolution: gl.getUniformLocation(shaderProgram, "r"),
+ time: gl.getUniformLocation(shaderProgram, "t"),
+ },
+ };
+
+ const positions = new Float32Array([
+ -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0,
+ ]);
+
+ const positionBuffer = gl.createBuffer();
+ gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
+ gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
+
+ let then = 0;
+ function render(now) {
+ now *= 0.001; // Convert to seconds
+ const deltaTime = now - then;
+ then = now;
+
+ canvas.width = window.innerWidth;
+ canvas.height = window.innerHeight;
+ gl.viewport(0, 0, canvas.width, canvas.height);
+
+ gl.clearColor(0.0, 0.0, 0.0, 1.0);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+
+ gl.useProgram(programInfo.program);
+
+ gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
+ gl.vertexAttribPointer(
+ programInfo.attribLocations.vertexPosition,
+ 2, // 2 components per vertex
+ gl.FLOAT, // the data is 32bit floats
+ false, // don't normalize
+ 0, // stride
+ 0, // offset
+ );
+ gl.enableVertexAttribArray(programInfo.attribLocations.vertexPosition);
+
+ gl.uniform2f(
+ programInfo.uniformLocations.resolution,
+ canvas.width,
+ canvas.height,
+ );
+ gl.uniform1f(programInfo.uniformLocations.time, now);
+
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
+
+ requestAnimationFrame(render);
+ }
+ requestAnimationFrame(render);
+ }
+
+ window.onload = main;
+ window.addEventListener("resize", () => {
+ const canvas = document.querySelector("#glCanvas");
+ canvas.width = window.innerWidth;
+ canvas.height = window.innerHeight;
+ });
+