Skip to content

Commit

Permalink
[Rasterizer]: Make rasterizer works with new materials (somewhat)
Browse files Browse the repository at this point in the history
  • Loading branch information
cszach committed Apr 29, 2024
1 parent 7d16727 commit 63bba25
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
5 changes: 1 addition & 4 deletions examples/cornell-box-raytraced/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
PerspectiveCamera,
Scene,
SolidColor,
BlinnPhong,
Raytracer,
AmbientLight,
PointLight,
Expand Down Expand Up @@ -50,7 +49,7 @@ renderer.init().then(() => {

const box = new Box(0.3, 0.3, 0.3);

const tallBox = new Mesh(box, new Metal([1, 1, 1], 0));
const tallBox = new Mesh(box, new Metal(white, 0));
vec3.set(-0.25, -0.2, -0.25, tallBox.localPosition);
vec3.set(1, 2, 1, tallBox.localScale);
quat.fromEuler(0, Math.PI / 10, 0, 'xyz', tallBox.localQuaternion);
Expand Down Expand Up @@ -106,8 +105,6 @@ renderer.init().then(() => {

resizeObserver.observe(canvas);

// renderer.render(scene, camera, 20);

function frame() {
renderer.render(scene, camera, i++);

Expand Down
4 changes: 2 additions & 2 deletions examples/cornell-box/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
AmbientLight,
PointLight,
Plane,
Mirror,
Metal,
Rasterizer,
} from '../../src/index.js';
import {quat, vec3} from 'wgpu-matrix';
Expand Down Expand Up @@ -55,7 +55,7 @@ renderer.init().then(() => {

const box = new Box(0.3, 0.3, 0.3);

const tallBox = new Mesh(box, new Mirror([1, 1, 1]));
const tallBox = new Mesh(box, new Metal([1, 1, 1], 0));
vec3.set(-0.25, -0.2, -0.25, tallBox.localPosition);
vec3.set(1, 2, 1, tallBox.localScale);
quat.fromEuler(0, Math.PI / 10, 0, 'xyz', tallBox.localQuaternion);
Expand Down
10 changes: 10 additions & 0 deletions src/materials/lambert.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn lambert(
material: Material,
frag: VertexOutput,
light: ptr<storage, array<Light>>,
cameraPosition: vec3f
) {
var color = vec3f();

return vec4f(1);
}
11 changes: 6 additions & 5 deletions src/renderers/Rasterizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {vec3, mat4, quat} from 'wgpu-matrix';
import {BLINN_PHONG, SOLID_COLOR} from '../materials/constants.js';
import {BlinnPhong} from '../materials/BlinnPhong.js';
import {SolidColor} from '../materials/SolidColor.js';
import {Metal} from '../materials/Metal.js';

class Rasterizer implements Renderer {
readonly canvas: HTMLCanvasElement;
Expand Down Expand Up @@ -247,11 +248,11 @@ class Rasterizer implements Renderer {
let specular = [0, 0, 0];
let shininess = 0;

if (
mesh.material.type === SOLID_COLOR ||
mesh.material.type === BLINN_PHONG
) {
const coloredMaterial = mesh.material as SolidColor | BlinnPhong;
if (mesh.material.type < 5) {
const coloredMaterial = mesh.material as
| SolidColor
| BlinnPhong
| Metal;

color = coloredMaterial.color;
}
Expand Down
7 changes: 4 additions & 3 deletions src/renderers/raytracer.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct HitRecord {
@group(0) @binding(7) var<storage, read> materials: array<Material>;

const WORKGROUP_SIZE = 8;
const NUM_PIXEL_SAMPLES = 1u;

@compute @workgroup_size(WORKGROUP_SIZE, WORKGROUP_SIZE)
fn computeMain(@builtin(global_invocation_id) pixel: vec3u) {
Expand All @@ -63,11 +64,11 @@ fn computeMain(@builtin(global_invocation_id) pixel: vec3u) {
return;
}

const NUM_SAMPLES = 12u;
var seed = init_rng(pixel.xy, frame_dimensions, frame);
var color = vec3f(0);
var sample = 0u;

for (var sample = 0u; sample < NUM_SAMPLES; sample++) {
for (sample = 0u; sample < NUM_PIXEL_SAMPLES; sample++) {
var ray = ray(pixel.xy, &seed);
var attenuation = vec3f(1);

Expand Down Expand Up @@ -110,7 +111,7 @@ fn computeMain(@builtin(global_invocation_id) pixel: vec3u) {
color += attenuation;
}

color /= f32(NUM_SAMPLES);
color /= f32(sample);

let frame_buffer_index = pixel.x + pixel.y * frame_dimensions.x;

Expand Down

0 comments on commit 63bba25

Please sign in to comment.