diff --git a/examples/cornell-box-raytraced/index.ts b/examples/cornell-box-raytraced/index.ts index 8fd1131..a67f897 100644 --- a/examples/cornell-box-raytraced/index.ts +++ b/examples/cornell-box-raytraced/index.ts @@ -4,7 +4,6 @@ import { PerspectiveCamera, Scene, SolidColor, - BlinnPhong, Raytracer, AmbientLight, PointLight, @@ -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); @@ -106,8 +105,6 @@ renderer.init().then(() => { resizeObserver.observe(canvas); - // renderer.render(scene, camera, 20); - function frame() { renderer.render(scene, camera, i++); diff --git a/examples/cornell-box/index.ts b/examples/cornell-box/index.ts index f94f522..1875440 100644 --- a/examples/cornell-box/index.ts +++ b/examples/cornell-box/index.ts @@ -8,7 +8,7 @@ import { AmbientLight, PointLight, Plane, - Mirror, + Metal, Rasterizer, } from '../../src/index.js'; import {quat, vec3} from 'wgpu-matrix'; @@ -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); diff --git a/src/materials/lambert.wgsl b/src/materials/lambert.wgsl new file mode 100644 index 0000000..2357d51 --- /dev/null +++ b/src/materials/lambert.wgsl @@ -0,0 +1,10 @@ +fn lambert( + material: Material, + frag: VertexOutput, + light: ptr>, + cameraPosition: vec3f +) { + var color = vec3f(); + + return vec4f(1); +} diff --git a/src/renderers/Rasterizer.ts b/src/renderers/Rasterizer.ts index 0c7097f..73fb661 100644 --- a/src/renderers/Rasterizer.ts +++ b/src/renderers/Rasterizer.ts @@ -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; @@ -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; } diff --git a/src/renderers/raytracer.wgsl b/src/renderers/raytracer.wgsl index 010ead8..fb64fa8 100644 --- a/src/renderers/raytracer.wgsl +++ b/src/renderers/raytracer.wgsl @@ -53,6 +53,7 @@ struct HitRecord { @group(0) @binding(7) var materials: array; const WORKGROUP_SIZE = 8; +const NUM_PIXEL_SAMPLES = 1u; @compute @workgroup_size(WORKGROUP_SIZE, WORKGROUP_SIZE) fn computeMain(@builtin(global_invocation_id) pixel: vec3u) { @@ -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); @@ -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;