Notebooks 2.0 is here.

Published
Edited
Oct 25, 2021
1 star
Insert cell
Insert cell
Insert cell
Insert cell
textureImage = FileAttachment("Rayman.png").image()
Insert cell
modelURL = FileAttachment("raymanModel.obj").url()
Insert cell
Insert cell
Insert cell
Insert cell
bgUniforms = ({
tex: texture,
aspectRatio
})
Insert cell
bgAttributes = ({
position: {
numComponents: 2,
data: quadData.positions.flat()
},
uv: { numComponents: 2, data: quadData.uvs.flat() },
indices: quadData.cells.flat()
})
Insert cell
bgBufferInfo = twgl.createBufferInfoFromArrays(gl, bgAttributes)
Insert cell
bgShaders = ({
vert: `#version 300 es
precision mediump float;
in vec2 position;
in vec2 uv;
out vec2 fragUV;
uniform vec2 aspectRatio;
void main () {
fragUV = uv;
gl_Position = vec4(position/aspectRatio, 1, 1);
}`,
frag: `#version 300 es
precision mediump float;
uniform sampler2D tex;
in vec2 fragUV;
out vec4 outColor;
void main () {
outColor = texture(tex, fragUV);
}`,
})
Insert cell
Insert cell
Insert cell
function bgRender() {
gl.useProgram(bgProgramInfo.program);
twgl.setUniforms(bgProgramInfo, bgUniforms);
twgl.setBuffersAndAttributes(gl, bgProgramInfo, bgBufferInfo);
twgl.drawBufferInfo(gl, bgBufferInfo);
}
Insert cell
Insert cell
modelUniforms = ({
aspectRatio,
lineColor: [0.0, 1.0, 0.0]
})
Insert cell
Insert cell
Insert cell
modelShaders = ({
vert: `#version 300 es
in vec2 uv;

uniform vec2 aspectRatio;

out vec3 fragNormal;
void main () {
vec2 normalizedUV = (uv * 2. - 1.) / aspectRatio;
gl_Position = vec4(normalizedUV, 0, 1);
}`,

frag: `#version 300 es
precision mediump float;
out vec4 outColor;
uniform vec3 lineColor;
void main () {
outColor = vec4(lineColor, 1);
}`
})
Insert cell
Insert cell
modelProgramInfo = {
errorBlock2.style.height = "20px";
errorBlock2.innerHTML = "Program Shader compilation successful";

return twgl.createProgramInfo(gl, [modelShaders.vert, modelShaders.frag], (message) => {
errorBlock2.style.height = "400px";
errorBlock2.innerHTML = "Program Shader compilation error\n" + message;
});
}
Insert cell
function modelRender() {
gl.useProgram(modelProgramInfo.program);
twgl.setUniforms(modelProgramInfo, modelUniforms);
gl.lineWidth(5.0);
modelBufferInfoArrays.forEach(bufferInfo => {
twgl.setBuffersAndAttributes(gl, modelProgramInfo, bufferInfo);
// Because WebGL2 doesn't support glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ),
// We have to render the triangles 3 points at a time
for(let i = 0; i < bufferInfo.numElements; i += 3) {
twgl.drawBufferInfo(gl, bufferInfo, gl.LINE_LOOP, 3, i);
}
});
}
Insert cell
function render() {
gl.clear(gl.COLOR_BUFFER_BIT);
bgRender();
modelRender();
}
Insert cell
Insert cell
quadData = {
return {
positions: [
[-1, -1],
[-1, 1],
[1, 1],
[1, -1]
],
uvs: [
[0, 0],
[0, 1],
[1, 1],
[1, 0]
],
cells: [
[0, 1, 2],
[2, 3, 0]
]
};
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
twgl = require("twgl.js")
Insert cell
import {
loadModelFromURL,
computeModelExtent
} from "@spattana/model-loaders-using-threejs"
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