Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

Scene: FogExp2.clone, as three.js has it, keeping the height-fog settings (#135)

Kevin Rajan42e50dcee592
diff --git a/packages/scene/src/core.ts b/packages/scene/src/core.ts
index c940b15..6b7242d 100644
--- a/packages/scene/src/core.ts
+++ b/packages/scene/src/core.ts
@@ -2384,6 +2384,10 @@ export class FogExp2 extends ExponentialHeightFog {
constructor(color: ColorLike = 0xffffff, density = 0.00025) {
super(density, 0, color);
}
+ clone(): FogExp2 {
+ const { heightFalloff, intensity, height, startDistance, maxOpacity } = this;
+ return Object.assign(new FogExp2(this.color, this.density), { heightFalloff, intensity, height, startDistance, maxOpacity, volumetric: structuredClone(this.volumetric) });
+ }
}
export function vec3(v: Vector3 | Color): Vec3 {
diff --git a/packages/scene/test/three-compat-fog-scene.test.ts b/packages/scene/test/three-compat-fog-scene.test.ts
new file mode 100644
index 0000000..4dc1ebd
--- /dev/null
+++ b/packages/scene/test/three-compat-fog-scene.test.ts
@@ -0,0 +1,34 @@
+// FogExp2 clone preserves density and color values without aliasing its color.
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import * as THREE from "../src/index.ts";
+
+test("FogExp2.clone returns an independent fog and color, as three.js r187 does", () => {
+ const fog = new THREE.FogExp2(0x336699, 0.125);
+ const cloned = fog.clone();
+
+ assert.notEqual(cloned, fog);
+ assert.equal(cloned.isFogExp2, true);
+ assert.equal(cloned.density, 0.125);
+ assert.notEqual(cloned.color, fog.color);
+ assert.equal(cloned.color.getHex(), 0x336699);
+
+ cloned.color.setHex(0xff0000);
+ assert.equal(cloned.color.getHex(), 0xff0000);
+ assert.equal(fog.color.getHex(), 0x336699);
+});
+
+test("FogExp2.clone keeps the height-fog and volumetric settings it inherits", () => {
+ const fog = new THREE.FogExp2(0x336699, 0.125);
+ Object.assign(fog, { heightFalloff: 0.3, intensity: 2, height: 4, startDistance: 5, maxOpacity: 0.75 });
+ fog.volumetric = { scattering: 0.4, anisotropy: 0.2, albedo: [0.1, 0.2, 0.3] };
+ const cloned = fog.clone();
+
+ assert.deepEqual(
+ [cloned.heightFalloff, cloned.intensity, cloned.height, cloned.startDistance, cloned.maxOpacity],
+ [0.3, 2, 4, 5, 0.75],
+ );
+ assert.deepEqual(cloned.volumetric, fog.volumetric);
+ assert.notEqual(cloned.volumetric, fog.volumetric);
+ assert.notEqual(cloned.volumetric!.albedo, fog.volumetric.albedo);
+});