Skip to content

Commit 899e292

Browse files
committed
Materials: Transmissive material, first part
1 parent 7fc769e commit 899e292

File tree

8 files changed

+422
-36
lines changed

8 files changed

+422
-36
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,14 @@ _**please review the [CONTRIBUTING][] document for the most effective way to pro
108108

109109
This work is licensed under a
110110
[Creative Commons Attribution 4.0 International License][cc-by]. You can find a copy of the license
111-
text at [LICENSE][]
111+
text in the [LICENSE][] file. All images and figures are original works with the exception of a few borrowings,
112+
which have been credited in [images/README.md][]
112113

113114
[![CC BY 4.0][cc-by-image]][cc-by]
114115

115116
[CONTRIBUTING]: CONTRIBUTING.md
116117
[LICENSE]: LICENSE
118+
[images/README.md]: images/README.md
117119
[cc-by]: http://creativecommons.org/licenses/by/4.0/
118120
[cc-by-image]: https://i.creativecommons.org/l/by/4.0/88x31.png
119121
[cc-by-shield]: https://img.shields.io/badge/License-CC%20BY%204.0-lightgrey.svg

book/RayTracingGPUEdition.html

Lines changed: 189 additions & 19 deletions
Large diffs are not rendered by default.

code/complete/src/shaders.wgsl

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,21 @@ struct Scatter {
145145
ray: Ray,
146146
}
147147

148-
fn sample_perfectly_specular(input_dir: vec3f, normal: vec3f) -> vec3f {
149-
return reflect(input_dir, normal);
150-
}
151-
152148
fn sample_lambertian(input_dir: vec3f, normal: vec3f) -> vec3f {
153149
return normal + sample_sphere() * (1. - EPSILON);
154150
}
155151

156152
fn scatter(input_ray: Ray, hit: Intersection, material: Material) -> Scatter {
157-
var reflected: vec3f;
158-
if material.specular == 1 {
159-
reflected = sample_perfectly_specular(input_ray.direction, hit.normal);
153+
var scattered: vec3f;
154+
if material.specular_or_ior > 0. {
155+
scattered = reflect(input_ray.direction, hit.normal);
156+
} else if material.specular_or_ior < 0. {
157+
let ior = abs(material.specular_or_ior);
158+
scattered = refract(input_ray.direction, hit.normal, ior);
160159
} else {
161-
reflected = sample_lambertian(input_ray.direction, hit.normal);
160+
scattered = sample_lambertian(input_ray.direction, hit.normal);
162161
}
163-
let output_ray = Ray(point_on_ray(input_ray, hit.t), reflected);
162+
let output_ray = Ray(point_on_ray(input_ray, hit.t), scattered);
164163
let attenuation = material.color;
165164
return Scatter(attenuation, output_ray);
166165
}
@@ -176,27 +175,31 @@ fn point_on_ray(ray: Ray, t: f32) -> vec3<f32> {
176175

177176
struct Material {
178177
color: vec3f,
179-
specular: u32,
178+
specular_or_ior: f32,
180179
}
181180

182181
fn sky_color(ray: Ray) -> vec3f {
183182
let t = 0.5 * (normalize(ray.direction).y + 1.);
184183
return (1. - t) * vec3(1.) + t * vec3(0.3, 0.5, 1.);
185184
}
186185

187-
const OBJECT_COUNT: u32 = 3;
186+
const OBJECT_COUNT: u32 = 4;
188187
alias Scene = array<Sphere, OBJECT_COUNT>;
189188
alias Materials = array<Material, OBJECT_COUNT>;
190189

191190
var<private> materials: Materials = Materials(
192-
Material(/*color*/ vec3(0.7, 0.5, 0.5), /*specular*/1),
193-
Material(/*color*/ vec3(0.5, 0.5, 0.9), /*specular*/0),
194-
Material(/*color*/ vec3(0.7, 0.9, 0.2), /*specular*/0),
191+
Material(/*color*/ vec3(0.7, 0.5, 0.5), /*specular_or_ior*/1.),
192+
Material(/*color*/ vec3(0.5, 0.5, 0.9), /*specular_or_ior*/0.),
193+
Material(/*color*/ vec3(0.7, 0.9, 0.2), /*specular_or_ior*/0.),
194+
Material(/*color*/ vec3(1.), /*specular_or_ior*/-1.5),
195195
);
196196

197197
var<private> scene: Scene = Scene(
198-
Sphere(/*center*/ vec3(-0.6, 0.5, 0.), /*radius*/ 0.5, /*material_index*/ 0),
199-
Sphere(/*center*/ vec3(0.6, 0.5, 0.), /*radius*/ 0.5, /*material_index*/ 1),
198+
Sphere(/*center*/ vec3(-1.1, 0.5, 0.), /*radius*/ 0.5, /*material_index*/ 0),
199+
Sphere(/*center*/ vec3(0., 0.5, 0.), /*radius*/ 0.5, /*material_index*/ 1),
200+
Sphere(/*center*/ vec3(1.1, 0.5, 0.), /*radius*/ 0.5, /*material_index*/ 3),
201+
202+
// Ground
200203
Sphere(/*center*/ vec3(0., -2e2 - EPSILON, 0.), /*radius*/ 2e2, /*material_index*/ 2),
201204
);
202205

images/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
All images and diagrams in this directory are original works and gifted to the public domain unless listed below.
2+
3+
Image Credits:
4+
--------------
5+
- Snells_law_wavefronts_by_Oleg_Alexandrov.gif by Oleg Alexandrov (public domain)
Loading

images/fig-20-snell.svg

Lines changed: 200 additions & 0 deletions
Loading

images/vid-09-nan-propagation.mp4

12.9 MB
Binary file not shown.

style/book.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ div.indented {
177177
image-rendering: crisp-edges;
178178
}
179179

180+
.md img.small-gif {
181+
margin-top: 1em;
182+
width: 48ex;
183+
image-rendering: crisp-edges;
184+
}
185+
180186
.md video {
181187
margin-top: 1em;
182188
width: 72ex;

0 commit comments

Comments
 (0)