Skip to content

Commit c3e6749

Browse files
committed
added shadows
1 parent 545871d commit c3e6749

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use scene::light::Light;
77
use scene::scene::get_pixel;
88
use scene::sphere::Sphere;
99

10-
use crate::scene::triangle::{self, Triangle};
10+
use crate::scene::triangle::Triangle;
1111
use crate::util::color::Color;
1212
use crate::util::vector::Vec3;
1313

src/scene/scene.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::scene::light::Light;
22
use crate::scene::sphere::Sphere;
33
use crate::scene::triangle::Triangle;
44
use crate::util::hit::Hit;
5+
use crate::util::ray;
56
use crate::util::{color::Color, ray::Ray, vector::Vec3};
67

78
pub fn get_pixel(
@@ -37,7 +38,38 @@ pub fn get_pixel(
3738
if let Some(hit) = nearest_hit {
3839
color = hit.color * ambient_intensity;
3940
for light in lights {
40-
color += light.get_color(hit);
41+
let to_light = light.position - hit.point;
42+
let distance_to_light_2 = to_light.length2();
43+
let ray_to_light = ray::Ray::new(hit.point, to_light.normalized());
44+
let mut in_shadow = false;
45+
46+
for sphere in spheres {
47+
if let Some(intersection) = sphere.get_hit(&ray_to_light) {
48+
if intersection.lambda > 0.0
49+
&& intersection.lambda * intersection.lambda < distance_to_light_2
50+
{
51+
in_shadow = true;
52+
break;
53+
}
54+
}
55+
}
56+
57+
if !in_shadow {
58+
for triangle in triangles {
59+
if let Some(intersection) = triangle.get_hit(&ray_to_light) {
60+
if intersection.lambda > 0.0
61+
&& intersection.lambda * intersection.lambda < distance_to_light_2
62+
{
63+
in_shadow = true;
64+
break;
65+
}
66+
}
67+
}
68+
}
69+
70+
if !in_shadow {
71+
color += light.get_color(hit);
72+
}
4173
}
4274
}
4375

src/util/vector.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ impl Vec3 {
1212
Self { x, y, z }
1313
}
1414

15-
fn length(&self) -> f32 {
15+
pub fn length(&self) -> f32 {
1616
self.length2().sqrt()
1717
}
1818

19-
fn length2(&self) -> f32 {
19+
pub fn length2(&self) -> f32 {
2020
self.x * self.x + self.y * self.y + self.z * self.z
2121
}
2222

@@ -40,14 +40,6 @@ impl Vec3 {
4040
z: self.x * other.y - self.y * other.x,
4141
}
4242
}
43-
44-
pub fn distance(&self, other: &Self) -> f32 {
45-
(*self - *other).length()
46-
}
47-
48-
pub fn distance2(&self, other: &Self) -> f32 {
49-
(*self - *other).length2()
50-
}
5143
}
5244

5345
impl Add for Vec3 {

ui/src/app/services/scene.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ export interface SceneConfig {
4343
const DEFAULT_SCENE: SceneConfig = {
4444
diffuseIntensity: 0.1,
4545
spheres: [
46-
{ name: 'Lime Left', center: { x: 650, y: 250, z: 1050 }, radius: 50, color: { r: 0, g: 1, b: 0 } },
46+
{ name: 'Ball top', center: { x: 500, y: 500, z: 500 }, radius: 50, color: { r: 0, g: 1, b: 0 } },
47+
{ name: 'Ball bottom', center: { x: 570, y: 200, z: 500 }, radius: 50, color: { r: 0, g: 1, b: 0 } },
4748
],
4849
triangles: [
49-
{ name: 'TRI', pointA: { x: 500, y: 500, z: 500 }, pointB: { x: 600, y: 600, z: 500 }, pointC: { x: 500, y: 600, z: 500 }, color: { r: 1, g: 1, b: 1 } },
50-
{ name: 'TRI', pointA: { x: 500, y: 500, z: 500 }, pointB: { x: 600, y: 600, z: 500 }, pointC: { x: 600, y: 500, z: 500 }, color: { r: 1, g: 1, b: 1 } },
50+
{ name: 'Triangle', pointA: { x: 400, y: 400, z: 600 }, pointB: { x: 400, y: 300, z: 400 }, pointC: { x: 700, y: 300, z: 400 }, color: { r: 1, g: 1, b: 1 } },
5151
],
5252
lights: [
53-
{ name: 'Main Light', center: { x: 800, y: 300, z: -400 }, color: { r: 1, g: 1, b: 1 } },
53+
{ name: 'Main light', center: { x: 500, y: 900, z: 500 }, color: { r: 1, g: 1, b: 1 } },
5454
],
5555
};
5656

0 commit comments

Comments
 (0)