@woosh/meep-engine 2.109.4 → 2.109.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/build/meep.cjs +109 -14
  2. package/build/meep.min.js +1 -1
  3. package/build/meep.module.js +109 -14
  4. package/package.json +1 -1
  5. package/src/core/collection/queue/Deque.d.ts.map +1 -1
  6. package/src/core/collection/queue/Deque.js +52 -1
  7. package/src/core/collection/queue/Deque.spec.js +29 -0
  8. package/src/core/geom/3d/Ray3.d.ts +23 -10
  9. package/src/core/geom/3d/Ray3.d.ts.map +1 -1
  10. package/src/core/geom/3d/Ray3.js +59 -20
  11. package/src/core/geom/3d/ray/ray3_interval_array_apply_matrix4.d.ts +2 -2
  12. package/src/core/geom/3d/ray/ray3_interval_array_apply_matrix4.d.ts.map +1 -1
  13. package/src/core/geom/3d/ray/ray3_interval_array_apply_matrix4.js +42 -27
  14. package/src/core/geom/3d/tetrahedra/triangle/prototype.js +0 -2
  15. package/src/core/geom/3d/tetrahedra/triangle/trace_triangular_depth_map.d.ts.map +1 -1
  16. package/src/core/geom/3d/tetrahedra/triangle/trace_triangular_depth_map.js +5 -2
  17. package/src/core/geom/vec3/v3_matrix4_rotate.d.ts +10 -0
  18. package/src/core/geom/vec3/v3_matrix4_rotate.d.ts.map +1 -0
  19. package/src/core/geom/vec3/v3_matrix4_rotate.js +27 -0
  20. package/src/engine/graphics/sh3/lpv/PathTracerProbeRenderer.d.ts +1 -0
  21. package/src/engine/graphics/sh3/lpv/PathTracerProbeRenderer.d.ts.map +1 -1
  22. package/src/engine/graphics/sh3/lpv/PathTracerProbeRenderer.js +5 -3
  23. package/src/engine/graphics/sh3/lpv/sh3_bake_depth.d.ts.map +1 -1
  24. package/src/engine/graphics/sh3/lpv/sh3_bake_depth.js +2 -1
  25. package/src/engine/graphics/sh3/path_tracer/BufferedGeometryBVH.d.ts +9 -4
  26. package/src/engine/graphics/sh3/path_tracer/BufferedGeometryBVH.d.ts.map +1 -1
  27. package/src/engine/graphics/sh3/path_tracer/BufferedGeometryBVH.js +61 -6
  28. package/src/engine/graphics/sh3/path_tracer/PathTracedMesh.d.ts +11 -4
  29. package/src/engine/graphics/sh3/path_tracer/PathTracedMesh.d.ts.map +1 -1
  30. package/src/engine/graphics/sh3/path_tracer/PathTracedMesh.js +28 -36
  31. package/src/engine/graphics/sh3/path_tracer/PathTracedScene.d.ts +17 -8
  32. package/src/engine/graphics/sh3/path_tracer/PathTracedScene.d.ts.map +1 -1
  33. package/src/engine/graphics/sh3/path_tracer/PathTracedScene.js +83 -44
  34. package/src/engine/graphics/sh3/path_tracer/PathTracer.d.ts +3 -3
  35. package/src/engine/graphics/sh3/path_tracer/PathTracer.d.ts.map +1 -1
  36. package/src/engine/graphics/sh3/path_tracer/PathTracer.js +40 -33
  37. package/src/engine/graphics/sh3/path_tracer/material/MaterialConverter.d.ts.map +1 -1
  38. package/src/engine/graphics/sh3/path_tracer/material/MaterialConverter.js +43 -40
  39. package/src/engine/graphics/sh3/path_tracer/material/StandardMaterial.d.ts +14 -0
  40. package/src/engine/graphics/sh3/path_tracer/material/StandardMaterial.d.ts.map +1 -1
  41. package/src/engine/graphics/sh3/path_tracer/material/StandardMaterial.js +34 -1
  42. package/src/engine/graphics/sh3/path_tracer/prepare_scene_gltf.d.ts +21 -0
  43. package/src/engine/graphics/sh3/path_tracer/prepare_scene_gltf.d.ts.map +1 -0
  44. package/src/engine/graphics/sh3/path_tracer/prepare_scene_gltf.js +107 -0
  45. package/src/engine/graphics/sh3/path_tracer/prototypePathTracer.js +22 -122
  46. package/src/engine/graphics/sh3/path_tracer/ray_hit_apply_transform.d.ts +0 -9
  47. package/src/engine/graphics/sh3/path_tracer/ray_hit_apply_transform.d.ts.map +1 -1
  48. package/src/engine/graphics/sh3/path_tracer/ray_hit_apply_transform.js +2 -28
  49. package/src/engine/graphics/sh3/path_tracer/sampling/getBiasedNormalSample.d.ts +0 -9
  50. package/src/engine/graphics/sh3/path_tracer/sampling/getBiasedNormalSample.d.ts.map +1 -1
  51. package/src/engine/graphics/sh3/path_tracer/sampling/getBiasedNormalSample.js +0 -39
  52. package/src/engine/graphics/sh3/path_tracer/sampling/getCosineDirection.d.ts +10 -0
  53. package/src/engine/graphics/sh3/path_tracer/sampling/getCosineDirection.d.ts.map +1 -0
  54. package/src/engine/graphics/sh3/path_tracer/sampling/getCosineDirection.js +41 -0
  55. package/src/engine/graphics/sh3/path_tracer/sampling/v3_orthonormal_matrix_from_normal.d.ts +10 -0
  56. package/src/engine/graphics/sh3/path_tracer/sampling/v3_orthonormal_matrix_from_normal.d.ts.map +1 -0
  57. package/src/engine/graphics/sh3/path_tracer/sampling/v3_orthonormal_matrix_from_normal.js +45 -0
  58. package/src/engine/graphics/sh3/path_tracer/texture/sample_material.d.ts.map +1 -1
  59. package/src/engine/graphics/sh3/path_tracer/texture/sample_material.js +28 -8
  60. package/src/engine/graphics/sh3/prototypeSH3Probe.js +7 -5
  61. package/src/engine/graphics/texture/sampler/convertTexture2Sampler2D.d.ts.map +1 -1
  62. package/src/engine/graphics/texture/sampler/convertTexture2Sampler2D.js +62 -13
  63. package/src/engine/graphics/three/material/iterate_three_material_textures.d.ts +10 -0
  64. package/src/engine/graphics/three/material/iterate_three_material_textures.d.ts.map +1 -0
  65. package/src/engine/graphics/three/material/iterate_three_material_textures.js +33 -0
  66. package/src/core/math/max4.d.ts +0 -10
  67. package/src/core/math/max4.d.ts.map +0 -1
  68. package/src/core/math/max4.js +0 -24
  69. package/src/core/math/min4.d.ts +0 -10
  70. package/src/core/math/min4.d.ts.map +0 -1
  71. package/src/core/math/min4.js +0 -23
@@ -1,5 +1,6 @@
1
1
  import { vec3 } from "gl-matrix";
2
2
  import { array_copy } from "../../../../core/collection/array/array_copy.js";
3
+ import { Ray3 } from "../../../../core/geom/3d/Ray3.js";
3
4
  import { getBiasedNormalSample } from "./sampling/getBiasedNormalSample.js";
4
5
 
5
6
  /*
@@ -18,21 +19,25 @@ const irradiance = [0, 0, 0];
18
19
 
19
20
  const trace_result = [];
20
21
 
21
- const _ray_0 = [];
22
+ const _ray_0 = new Ray3();
22
23
 
23
- const tmp_0 = [];
24
- const tmp_1 = [];
24
+ const tmp_0 = new Float32Array(6);
25
25
 
26
+ /**
27
+ * Normalized probability of terminating the ray
28
+ * @type {number}
29
+ */
26
30
  const RUSSIAN_ROULETTE_PROBABILITY = 0.5;
27
31
 
32
+ const color_mask = new Float32Array(3);
33
+
28
34
  export class PathTracer {
29
35
 
30
36
 
31
37
  /**
32
38
  *
33
39
  * @param {number[]|Float32Array} out
34
- * @param {number[]|Float32Array} ray
35
- * @param {number} max_distance
40
+ * @param {number[]|Float32Array|Ray3} ray
36
41
  * @param {number} min_bounce
37
42
  * @param {number} max_bounce
38
43
  * @param {function} random
@@ -41,19 +46,21 @@ export class PathTracer {
41
46
  path_trace(
42
47
  out,
43
48
  ray,
44
- max_distance,
45
49
  min_bounce,
46
50
  max_bounce,
47
51
  random = Math.random,
48
52
  scene
49
53
  ) {
50
- //TODO add importance sampling, see https://raytracing.github.io/books/RayTracingTheRestOfYourLife.html#lightscattering/thescatteringpdf
51
54
 
52
- array_copy(ray, 0, _ray_0, 0, 6);
55
+ _ray_0.copy(ray);
56
+
57
+ irradiance[0] = 0;
58
+ irradiance[1] = 0;
59
+ irradiance[2] = 0;
53
60
 
54
- irradiance[0] = 1;
55
- irradiance[1] = 1;
56
- irradiance[2] = 1;
61
+ color_mask[0] = 1;
62
+ color_mask[1] = 1;
63
+ color_mask[2] = 1;
57
64
 
58
65
  let i;
59
66
  for (i = 0; i < max_bounce; ++i) {
@@ -61,15 +68,13 @@ export class PathTracer {
61
68
  if (i >= min_bounce) {
62
69
  const russian_roulette_roll = random();
63
70
 
64
- if (russian_roulette_roll > RUSSIAN_ROULETTE_PROBABILITY) {
71
+ if (russian_roulette_roll < RUSSIAN_ROULETTE_PROBABILITY) {
65
72
  // terminate ray
66
73
  break;
67
74
  }
68
75
  }
69
76
 
70
-
71
- const hit_distance = scene.trace(trace_result, _ray_0, 0.0001, max_distance);
72
-
77
+ const hit_distance = scene.trace(trace_result, _ray_0);
73
78
 
74
79
  if (hit_distance < 0) {
75
80
  // ray didn't hit anything
@@ -77,30 +82,33 @@ export class PathTracer {
77
82
  break;
78
83
  }
79
84
 
80
- scene.sample_material(tmp_0, trace_result);
81
-
82
- // adjust normal on the hit
83
- array_copy(tmp_0, 3, trace_result, 3, 3);
84
- array_copy(trace_result, 0, _ray_0, 0, 3);
85
+ scene.sample_material(tmp_0, trace_result, _ray_0);
85
86
 
86
- // accumulate irradiance
87
- vec3.multiply(irradiance, irradiance, tmp_0);
88
87
 
89
- // reflect ray
90
- getBiasedNormalSample(_ray_0, 3, tmp_0, 3, 1, random);
88
+ vec3.multiply(color_mask, color_mask, tmp_0);
91
89
 
92
- }
90
+ // construct new ray, aligned on the normal of the contact
91
+ array_copy(tmp_0, 3, _ray_0, 3, 3); // copy new direction
92
+ array_copy(trace_result, 0, _ray_0, 0, 3); // copy new origin
93
93
 
94
+ // move ray forward by a small amount to avoid self-occlusion
95
+ _ray_0.shiftForward(0.00001);
94
96
 
95
- if (i > 0) {
96
97
  // light sampling
97
- scene.sample_lights(tmp_1, 0, trace_result, 0);
98
+ scene.sample_lights(tmp_0, 0, _ray_0);
99
+
100
+ // sample on cosine lobe ray
101
+ getBiasedNormalSample(_ray_0, 3, _ray_0, 3, 1, random);
102
+
103
+ // // accumulate irradiance
104
+ irradiance[0] += color_mask[0] * tmp_0[0];
105
+ irradiance[1] += color_mask[1] * tmp_0[1];
106
+ irradiance[2] += color_mask[2] * tmp_0[2];
107
+
108
+ }
98
109
 
99
- irradiance[0] *= tmp_1[0];
100
- irradiance[1] *= tmp_1[1];
101
- irradiance[2] *= tmp_1[2];
102
- } else {
103
110
 
111
+ if (i === 0) {
104
112
  // ray didn't hit anything on the very first bounce
105
113
 
106
114
  // sample "environment" and terminate path as there is nothing to reflect off of
@@ -113,6 +121,5 @@ export class PathTracer {
113
121
  out[1] = irradiance[1]
114
122
  out[2] = irradiance[2]
115
123
 
116
- // array_copy(irradiance, 0, out, 0, 3);
117
124
  }
118
- }
125
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"MaterialConverter.d.ts","sourceRoot":"","sources":["../../../../../../../src/engine/graphics/sh3/path_tracer/material/MaterialConverter.js"],"names":[],"mappings":"AASA;IA4BI;;;;OAIG;IACH,kBAHW,MAAM,QAAQ,GACZ,gBAAgB,CA2D5B;;CAEJ;iCA/FgC,uBAAuB"}
1
+ {"version":3,"file":"MaterialConverter.d.ts","sourceRoot":"","sources":["../../../../../../../src/engine/graphics/sh3/path_tracer/material/MaterialConverter.js"],"names":[],"mappings":"AA8BA;IA4BI;;;;OAIG;IACH,kBAHW,MAAM,QAAQ,GACZ,gBAAgB,CAyC5B;;CAEJ;iCApGgC,uBAAuB"}
@@ -1,12 +1,33 @@
1
1
  import { Color } from "three";
2
- import {
3
- TextureAttachmentsByMaterialType
4
- } from "../../../../asset/loaders/material/TextureAttachmensByMaterialType.js";
5
2
  import { convertTexture2Sampler2D } from "../../../texture/sampler/convertTexture2Sampler2D.js";
6
3
  import { sampler_multiply_vector } from "../../../texture/sampler/sampler_multiply_vector.js";
4
+ import { iterate_three_material_textures } from "../../../three/material/iterate_three_material_textures.js";
7
5
  import { StandardMaterial } from "./StandardMaterial.js";
8
6
  import { StandardTexture } from "./StandardTexture.js";
9
7
 
8
+ /**
9
+ *
10
+ * @param {Map<number,StandardTexture>} cache
11
+ * @param {AssetManager} asset_manager
12
+ * @param {THREE.Material} material
13
+ * @returns {Promise<void>}
14
+ */
15
+ async function precache_textures(cache, asset_manager, material) {
16
+
17
+ const textures = iterate_three_material_textures(material);
18
+
19
+ for (const { name, texture } of textures) {
20
+ if (cache.has(texture.id)) {
21
+ // already processed
22
+ continue;
23
+ }
24
+
25
+
26
+ }
27
+
28
+
29
+ }
30
+
10
31
  export class MaterialConverter {
11
32
  /**
12
33
  * Maps {@link Texture.id} to relevant sampler
@@ -44,46 +65,28 @@ export class MaterialConverter {
44
65
 
45
66
  const result = new StandardMaterial();
46
67
 
47
- //patch textures
48
- const materialType = material.type;
49
-
50
- /**
51
- *
52
- * @type {TextureAttachment[]}
53
- */
54
- const attachments = TextureAttachmentsByMaterialType[materialType];
55
-
56
- if (attachments !== undefined) {
57
-
58
- for (let i = 0; i < attachments.length; i++) {
59
- const attachment = attachments[i];
60
-
61
- const texture = attachment.read(material);
62
-
63
- if (texture === undefined || texture === null) {
64
- continue;
65
- }
66
-
67
- const sampler = this.#ensure_texture(texture);
68
-
69
- const name = attachment.name;
70
-
71
- if (name === "diffuse") {
72
- result.diffuse = sampler;
73
- } else if (name === "normal") {
74
- result.normal = sampler;
75
- } else if (name === "metalness") {
76
- result.metalness = sampler;
77
- } else if (name === "roughness") {
78
- result.roughness = sampler;
79
- } else if (name === "emissive") {
80
- result.emissive = sampler;
81
- } else {
82
- // unused
83
- }
68
+ const textures = iterate_three_material_textures(material);
84
69
 
70
+ //patch textures
85
71
 
72
+ for (const { name, texture } of textures) {
73
+
74
+ const sampler = this.#ensure_texture(texture);
75
+
76
+ if (name === "diffuse") {
77
+ result.diffuse = sampler;
78
+ } else if (name === "normal") {
79
+ result.normal = sampler;
80
+ } else if (name === "metalness") {
81
+ result.metalness = sampler;
82
+ } else if (name === "roughness") {
83
+ result.roughness = sampler;
84
+ } else if (name === "emissive") {
85
+ result.emissive = sampler;
86
+ } else {
87
+ // unused
86
88
  }
89
+
87
90
  }
88
91
 
89
92
  if (material.color !== undefined && !material.color.equals(new Color(1, 1, 1))) {
@@ -5,6 +5,20 @@ export class StandardMaterial {
5
5
  emissive: StandardTexture;
6
6
  roughness: StandardTexture;
7
7
  metalness: StandardTexture;
8
+ /**
9
+ *
10
+ * @param {number} incoming_direction_x
11
+ * @param {number} incoming_direction_y
12
+ * @param {number} incoming_direction_z
13
+ * @param {number} normal_x
14
+ * @param {number} normal_y
15
+ * @param {number} normal_z
16
+ * @param {number} outgoing_x
17
+ * @param {number} outgoing_y
18
+ * @param {number} outgoing_z
19
+ * @returns {number}
20
+ */
21
+ scattering_pdf(incoming_direction_x: number, incoming_direction_y: number, incoming_direction_z: number, normal_x: number, normal_y: number, normal_z: number, outgoing_x: number, outgoing_y: number, outgoing_z: number): number;
8
22
  }
9
23
  import { StandardTexture } from "./StandardTexture.js";
10
24
  //# sourceMappingURL=StandardMaterial.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"StandardMaterial.d.ts","sourceRoot":"","sources":["../../../../../../../src/engine/graphics/sh3/path_tracer/material/StandardMaterial.js"],"names":[],"mappings":"AAWA;IACI,WAAkB;IAElB,yBAA+C;IAC/C,wBAA6C;IAC7C,0BAAiD;IACjD,2BAAmD;IACnD,2BAAmD;CACtD;gCAlB+B,sBAAsB"}
1
+ {"version":3,"file":"StandardMaterial.d.ts","sourceRoot":"","sources":["../../../../../../../src/engine/graphics/sh3/path_tracer/material/StandardMaterial.js"],"names":[],"mappings":"AAaA;IACI,WAAkB;IAElB,yBAA+C;IAC/C,wBAA6C;IAC7C,0BAAiD;IACjD,2BAAmD;IACnD,2BAAmD;IAEnD;;;;;;;;;;;;OAYG;IACH,qCAXW,MAAM,wBACN,MAAM,wBACN,MAAM,YACN,MAAM,YACN,MAAM,YACN,MAAM,cACN,MAAM,cACN,MAAM,cACN,MAAM,GACJ,MAAM,CAkBlB;CACJ;gCAjD+B,sBAAsB"}
@@ -1,8 +1,10 @@
1
+ import { v3_dot } from "../../../../../core/geom/vec3/v3_dot.js";
2
+ import { PI_RECIPROCAL } from "../../../../../core/math/PI_RECIPROCAL.js";
1
3
  import { one_pixel_sampler_uint8 } from "../../../texture/sampler/one_pixel_sampler_uint8.js";
2
4
  import { StandardTexture } from "./StandardTexture.js";
3
5
 
4
6
  const DEFAULT_DIFFUSE = one_pixel_sampler_uint8([255, 255, 255, 255]);
5
- const DEFAULT_NORMAL = one_pixel_sampler_uint8([0, 0, 255]);
7
+ const DEFAULT_NORMAL = one_pixel_sampler_uint8([127, 127, 255]);
6
8
  const DEFAULT_EMISSIVE = one_pixel_sampler_uint8([0, 0, 0]);
7
9
  const DEFAULT_ROUGHNESS = one_pixel_sampler_uint8([255]);
8
10
  const DEFAULT_METALNESS = one_pixel_sampler_uint8([0]);
@@ -17,5 +19,36 @@ export class StandardMaterial {
17
19
  emissive = StandardTexture.from(DEFAULT_EMISSIVE)
18
20
  roughness = StandardTexture.from(DEFAULT_ROUGHNESS)
19
21
  metalness = StandardTexture.from(DEFAULT_METALNESS)
22
+
23
+ /**
24
+ *
25
+ * @param {number} incoming_direction_x
26
+ * @param {number} incoming_direction_y
27
+ * @param {number} incoming_direction_z
28
+ * @param {number} normal_x
29
+ * @param {number} normal_y
30
+ * @param {number} normal_z
31
+ * @param {number} outgoing_x
32
+ * @param {number} outgoing_y
33
+ * @param {number} outgoing_z
34
+ * @returns {number}
35
+ */
36
+ scattering_pdf(
37
+ incoming_direction_x, incoming_direction_y, incoming_direction_z,
38
+ normal_x, normal_y, normal_z,
39
+ outgoing_x, outgoing_y, outgoing_z,
40
+ ) {
41
+ const cos_theta = v3_dot(
42
+ normal_x, normal_y, normal_z,
43
+ outgoing_x, outgoing_y, outgoing_z
44
+ );
45
+
46
+ if (cos_theta < 0) {
47
+ // clamp rays below horizon
48
+ return 0;
49
+ }
50
+
51
+ return cos_theta * PI_RECIPROCAL;
52
+ }
20
53
  }
21
54
 
@@ -0,0 +1,21 @@
1
+ export function make_sun({ temperature, color, intensity, direction }?: {
2
+ temperature?: number;
3
+ color?: string;
4
+ intensity?: number;
5
+ direction?: Vector3;
6
+ }): DirectionalLight;
7
+ /**
8
+ *
9
+ * @param {PathTracedScene} scene
10
+ * @param {THREE.Camera} camera
11
+ * @param {string} url
12
+ * @param {number} [zoom]
13
+ * @param {number} [floor_level]
14
+ * @param {Object} [sun]
15
+ * @param {boolean} [no_materials]
16
+ * @return {Promise<void>}
17
+ */
18
+ export function prepare_scene_gltf({ scene, camera, url, zoom, floor_level, sun, no_materials }: PathTracedScene): Promise<void>;
19
+ import Vector3 from "../../../../core/geom/Vector3.js";
20
+ import { DirectionalLight } from "../../render/forward_plus/model/DirectionalLight.js";
21
+ //# sourceMappingURL=prepare_scene_gltf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prepare_scene_gltf.d.ts","sourceRoot":"","sources":["../../../../../../src/engine/graphics/sh3/path_tracer/prepare_scene_gltf.js"],"names":[],"mappings":"AAaA;;;;;qBAyBC;AAaD;;;;;;;;;;GAUG;AACH,mHAFY,QAAQ,IAAI,CAAC,CA8CxB;oBAnGmB,kCAAkC;iCAErB,qDAAqD"}
@@ -0,0 +1,107 @@
1
+ import { vec3 } from "gl-matrix";
2
+ import { Box3, MeshStandardMaterial, PlaneBufferGeometry, Sphere } from "three";
3
+ import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
4
+ import { Color } from "../../../../core/color/Color.js";
5
+ import { kelvin_to_rgb } from "../../../../core/color/kelvin/kelvin_to_rgb.js";
6
+ import { noop } from "../../../../core/function/noop.js";
7
+ import Quaternion from "../../../../core/geom/Quaternion.js";
8
+ import Vector3 from "../../../../core/geom/Vector3.js";
9
+ import { Transform } from "../../../ecs/transform/Transform.js";
10
+ import { DirectionalLight } from "../../render/forward_plus/model/DirectionalLight.js";
11
+ import { three_object_replace_materials } from "../../three/three_object_replace_materials.js";
12
+ import { three_object_to_path_traced_scene } from "./three_object_to_path_traced_scene.js";
13
+
14
+ export function make_sun({
15
+ temperature = 5000, // around clear-sky day
16
+ color = '#FFFFFF',
17
+ intensity = 1,
18
+ direction = new Vector3(-0.5, -1, -0.05)
19
+ } = {}) {
20
+
21
+ const temp_c = new Color();
22
+
23
+ kelvin_to_rgb(temp_c, 0, temperature);
24
+
25
+ const light = new DirectionalLight();
26
+
27
+ light.color.parse(color);
28
+ vec3.multiply(light.color, light.color, temp_c);
29
+
30
+ const n = vec3.length(light.color);
31
+ vec3.scale(light.color, light.color, 1 / n); // normalize color vector
32
+
33
+ light.intensity.set(intensity * n);
34
+ light.direction.copy(direction);
35
+ light.direction.normalize();
36
+
37
+
38
+ return light;
39
+ }
40
+
41
+ /**
42
+ *
43
+ * @param {string} url
44
+ * @return {Promise<GLTF>}
45
+ */
46
+ function promise_gltf(url) {
47
+ return new Promise((resolve, reject) => {
48
+ new GLTFLoader().load(url, resolve, noop, reject);
49
+ });
50
+ }
51
+
52
+ /**
53
+ *
54
+ * @param {PathTracedScene} scene
55
+ * @param {THREE.Camera} camera
56
+ * @param {string} url
57
+ * @param {number} [zoom]
58
+ * @param {number} [floor_level]
59
+ * @param {Object} [sun]
60
+ * @param {boolean} [no_materials]
61
+ * @return {Promise<void>}
62
+ */
63
+ export async function prepare_scene_gltf({
64
+ scene,
65
+ camera,
66
+ url,
67
+ zoom = 1,
68
+ floor_level = 0,
69
+ sun,
70
+ no_materials = false
71
+ }) {
72
+ const gltf = await promise_gltf(url);
73
+
74
+
75
+ const ground_material = new MeshStandardMaterial({
76
+ color: '#ffceae'
77
+ });
78
+ scene.createMesh(new PlaneBufferGeometry(), ground_material, Transform.fromJSON({
79
+ position: new Vector3(0, floor_level, 0),
80
+ scale: 5000,
81
+ rotation: Quaternion.fromEulerAngles(-Math.PI / 2, 0, 0)
82
+ }).matrix);
83
+
84
+
85
+ const gltf_scene = gltf.scene;
86
+
87
+ if (no_materials) {
88
+ three_object_replace_materials(gltf_scene, new MeshStandardMaterial({ color: 'white' }));
89
+ }
90
+
91
+ three_object_to_path_traced_scene(gltf_scene, scene);
92
+
93
+ const box3 = new Box3();
94
+ box3.setFromObject(gltf_scene);
95
+
96
+ const sphere = new Sphere();
97
+
98
+ box3.getBoundingSphere(sphere);
99
+
100
+ scene.addLight(make_sun(sun));
101
+
102
+ camera.position.set(1, 1.3, 1)
103
+ .normalize()
104
+ .multiplyScalar(sphere.radius * 2.6 * (1 / zoom));
105
+
106
+ camera.lookAt(sphere.center);
107
+ }