Published
Edited
Jun 27, 2020
2 forks
1 star
Insert cell
Insert cell
canvas = html`<canvas width=640 height=480>`;
Insert cell
Insert cell
picogl = require('picogl@0.17.3/build/picogl.min.js')
Insert cell
Insert cell
app = picogl.createApp(canvas)
.clearColor(0.0, 0.0, 0.0, 1.0)
.blend()
.blendFunc(picogl.ONE, picogl.ONE_MINUS_SRC_ALPHA)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
program = app.createProgram(vertexShader, fragShader, {transformFeedbackVaryings: ["tfPosition","tfVelocity"]})
Insert cell
Insert cell
NUM_PARTICLES = 100
Insert cell
VB = {
var positionData = new Float32Array(NUM_PARTICLES * 3);
var colorData = new Float32Array(NUM_PARTICLES * 3);

for (var i = 0; i < NUM_PARTICLES; ++i) {
var vec3i = i * 3;

positionData[vec3i] = Math.random() * 2 - 1;
positionData[vec3i + 1] = Math.random() * 2 - 1;
positionData[vec3i + 2] = Math.random() * 2 - 1;

colorData[vec3i] = Math.random();
colorData[vec3i + 1] = Math.random();
colorData[vec3i + 2] = Math.random();
}

var positionsA = app.createVertexBuffer(picogl.FLOAT, 3, positionData);
var velocitiesA = app.createVertexBuffer(picogl.FLOAT, 3, positionData.length);
var positionsB = app.createVertexBuffer(picogl.FLOAT, 3, positionData.length);
var velocitiesB = app.createVertexBuffer(picogl.FLOAT, 3, positionData.length);

var colors = app.createVertexBuffer(picogl.FLOAT, 3, colorData);
return {positionsA, velocitiesA, positionsB, velocitiesB, colors};
}
Insert cell
Insert cell
vertexArrayA = app.createVertexArray()
.vertexAttributeBuffer(0, VB.positionsA)
.vertexAttributeBuffer(1, VB.velocitiesA)
.vertexAttributeBuffer(2, VB.colors)
Insert cell
vertexArrayB = app.createVertexArray()
.vertexAttributeBuffer(0, VB.positionsB)
.vertexAttributeBuffer(1, VB.velocitiesB)
.vertexAttributeBuffer(2, VB.colors)
Insert cell
Insert cell
transformFeedbackA = app.createTransformFeedback()
.feedbackBuffer(0, VB.positionsA)
.feedbackBuffer(1, VB.velocitiesA)
Insert cell
transformFeedbackB = app.createTransformFeedback()
.feedbackBuffer(0, VB.positionsB)
.feedbackBuffer(1, VB.velocitiesB)

Insert cell
Insert cell
massUniforms = app.createUniformBuffer([
picogl.FLOAT_VEC4,
picogl.FLOAT_VEC4,
picogl.FLOAT_VEC4,
picogl.FLOAT,
picogl.FLOAT,
picogl.FLOAT
]).set(0, new Float32Array([
Math.random() * 2.0 - 1.0,
Math.random() * 2.0 - 1.0,
Math.random() * 2.0 - 1.0,
1.0
])).set(1, new Float32Array([
Math.random() * 2.0 - 1.0,
Math.random() * 2.0 - 1.0,
Math.random() * 2.0 - 1.0,
1.0
])).set(2, new Float32Array([
Math.random() * 2.0 - 1.0,
Math.random() * 2.0 - 1.0,
Math.random() * 2.0 - 1.0,
1.0
]))
.set(3, Math.random() / 30000)
.set(4, Math.random() / 30000)
.set(5, Math.random() / 30000)
.update()
Insert cell
Insert cell
drawCallA = app.createDrawCall(program, vertexArrayA, picogl.POINTS)
.transformFeedback(transformFeedbackB)
.uniformBlock("Mass", massUniforms)
Insert cell
drawCallB = app.createDrawCall(program, vertexArrayB, picogl.POINTS)
.transformFeedback(transformFeedbackA)
.uniformBlock("Mass", massUniforms)
Insert cell
Insert cell
draw = {
let frame = 0;
while (true) {
app.clear();
drawCallA.draw();
yield frame++;
app.clear();
drawCallB.draw();
yield frame++;
}
}
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