Published
Edited
Feb 9, 2022
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
let count = 0;
let cameraAt = [0, 0, 0];
let cameraUp = [0, 1, 0];
let M = twgl.m4.axisRotation(cameraUp, deg2rad(1));
let cameraU = [1, 0, 0],
cameraV = [0, 1, 0],
cameraW = [0, 0, 1];

const computeUVW = () => {
cameraW = twgl.v3.normalize([
cameraEye[0] - cameraAt[0],
cameraEye[1] - cameraAt[1],
cameraEye[2] - cameraAt[2]
]);
cameraU = twgl.v3.normalize(twgl.v3.cross(cameraUp, cameraW));
cameraV = twgl.v3.normalize(twgl.v3.cross(cameraW, cameraU));
};
const rotateCamera = () => {
mutable cameraEye = twgl.m4.transformDirection(M, cameraEye);
computeUVW();
return count++;
};

computeUVW();
render();
function render() {
gl.clear(gl.COLOR_BUFFER_BIT);
const uniforms = {
Background: hex2rgb(backgroundColor),
resolution: [gl.canvas.width, gl.canvas.height],
cameraU: cameraU,
cameraV: cameraV,
cameraW: cameraW,
cameraEye: cameraEye,
fov: deg2rad(30),
minBound: [-0.5, -0.5, -0.5],
maxBound: [0.5, 0.5, 0.5]
};
gl.useProgram(programInfo.program);
twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
twgl.setUniforms(programInfo, uniforms);
twgl.drawBufferInfo(gl, bufferInfo);
return count++;
}
while (rotate) {
render();
yield Promises.delay(1000 / 30, rotateCamera());
}
return md`### WebGL Render code`;
}
Insert cell
deg2rad = deg => (deg * Math.PI) / 180
Insert cell
md`### Leave the following code unchanged.`
Insert cell
bufferInfo = twgl.createBufferInfoFromArrays(gl, {
position: [-1, -1, 0, 1, -1, 0, -1, 1, 0, -1, 1, 0, 1, -1, 0, 1, 1, 0]
})
Insert cell
programInfo = {
errorBlock.style.display = "none";
return twgl.createProgramInfo(gl, [shaders.vs, shaders.fs], message => {
errorBlock.style.display = "block";
errorBlock.innerHTML = message;
});
}
Insert cell
shaders = ({
fs: `#version 300 es
precision highp float;
out vec4 fragColor;
//-------You Code appears below this line--------------------------
${pixelColorCode.join("\n//----------------")}
//-------You Code appears above this line--------------------------
void main() {
fragColor = vec4(pixelColor(gl_FragCoord.xy),1.0);
}`,
vs: `#version 300 es
precision highp float;
in vec4 position;

void main() {
gl_Position = position;
}`
})
Insert cell
gl = {
const gl = canvas.getContext("webgl2", {
antialias: true,
preserveDrawingBuffer: true
});
gl.clearColor(0, 0, 0, 1); // Choose a clear color
gl.clear(gl.COLOR_BUFFER_BIT);
return gl;
}
Insert cell
height = 256
Insert cell
width = 512
Insert cell
md`### Local Helper Functions`
Insert cell
hex2rgb = hex =>
(hex = hex.replace('#', ''))
.match(new RegExp('(.{' + hex.length / 3 + '})', 'g'))
.map(l => parseInt(hex.length % 2 ? l + l : l, 16) / 255)
Insert cell
md`### External Libraries and Functions (imports)`
Insert cell
import { color, checkbox } from "@jashkenas/inputs"
Insert cell
twgl = require("twgl.js")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more