Skip to content

Commit

Permalink
Animating lights
Browse files Browse the repository at this point in the history
  • Loading branch information
tsherif committed Feb 8, 2024
1 parent 5c8fb32 commit f739478
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions deferred.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
import { checkSupport, addDescription, loadImageBitmaps, createCube, createSphere, createQuad, xformMatrix, randomRange } from "./utils/utils.js";
const { mat4, vec3 } = glMatrix;

const NUM_BOXES = 256;
const NUM_LIGHTS = 512;

checkSupport();
addDescription(
"Deferred Rendering",
"Interact with rendered objects using color picking.",
"pick.html"
`Rendering ${NUM_BOXES} boxes to a gBuffer and animationg ${NUM_LIGHTS} lights.`,
"deferred.html"
);

(async () => {
Expand Down Expand Up @@ -169,9 +172,6 @@
const viewProjectionMatrix = mat4.create();
mat4.multiply(viewProjectionMatrix, projMatrix, viewMatrix);

const NUM_BOXES = 256;
const NUM_LIGHTS = 256;

/////////////////////
// Geo pipeline
/////////////////////
Expand Down Expand Up @@ -493,7 +493,13 @@
randomRange(0, 0.1),
randomRange(0, 0.1),
randomRange(0, 0.1)
]
],
offset: [
randomRange(-3, 3),
randomRange(-3, 3),
randomRange(-3, 3)
],
t: randomRange(0, 2 * Math.PI),
}));
const lightPositionsBufferData = new Float32Array(lights.length * 4);
const lightPositionsBuffer = device.createBuffer({
Expand Down Expand Up @@ -614,8 +620,8 @@

function draw() {
for (const box of boxes) {
box.rotate[0] += 0.005;
box.rotate[1] += 0.01;
box.rotate[0] += 0.0005;
box.rotate[1] += 0.001;

xformMatrix(box.modelMatrix, box.translate, box.rotate, box.scale);

Expand Down Expand Up @@ -646,6 +652,15 @@
// Draw
///////////

lights.forEach((light, i) => {
light.t = (light.t + 0.002) % (2 * Math.PI);
const position = lightPositionsBufferData.subarray(i * 4, i * 4 + 3);
vec3.scale(position, light.offset, Math.sin(light.t));
vec3.add(position, light.position, position);
});

device.queue.writeBuffer(lightPositionsBuffer, 0, lightPositionsBufferData);

drawRenderPassDescription.colorAttachments[0].view = context.getCurrentTexture().createView();

const drawRenderPass = commandEncoder.beginRenderPass(drawRenderPassDescription);
Expand Down

0 comments on commit f739478

Please sign in to comment.