From 4018c07faf7d5d9174800f3dc1f8eb682914e896 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Sat, 30 Mar 2024 20:24:00 +0100 Subject: [PATCH] Updates shadows and conditional ground plane --- app2/src/lib/components/Visualization.svelte | 13 ++++++---- app2/src/lib/sceneBuilder.ts | 27 ++++++++++++-------- app2/src/routes/+page.svelte | 2 +- app2/src/routes/system/status/+page.svelte | 2 -- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/app2/src/lib/components/Visualization.svelte b/app2/src/lib/components/Visualization.svelte index c787277..4a2edf9 100644 --- a/app2/src/lib/components/Visualization.svelte +++ b/app2/src/lib/components/Visualization.svelte @@ -14,6 +14,7 @@ export let orbit = false export let panel = true export let debug = false + export let ground = true let sceneManager = new SceneBuilder(); let canvas: HTMLCanvasElement @@ -79,16 +80,18 @@ .addRenderer({ antialias: true, canvas, alpha: true }) .addPerspectiveCamera({ x: -0.5, y: 0.5, z: 1 }) .addOrbitControls(8, 30, orbit) - .addGroundPlane() - // .addGridHelper({ grid:{size: 250, divisions: 125 }}) - .addAmbientLight({ color: 0xffffff, intensity: 0.7 }) - .addDirectionalLight({ x: 10, y: 100, z: 10, color: 0xffffff, intensity: 1 }) - // .addArrowHelper({ origin: { x: 0, y: 0, z: 0 }, direction: { x: 0, y: -2, z: 0 } }) + .addDirectionalLight({ x: 10, y: 20, z: 10, color: 0xffffff, intensity: 0.9 }) + .addAmbientLight({ color: 0xffffff, intensity: 0.6 }) .addFogExp2(0xcccccc, 0.015) .addModel($model) .fillParent() .addRenderCb(render) .startRenderLoop(); + + if (ground) sceneManager + .addGroundPlane() + .addGridHelper({ size: 30, divisions: 25 }) + if (sky) sceneManager.addSky() if (debug) sceneManager.addDragControl(updateAngles) diff --git a/app2/src/lib/sceneBuilder.ts b/app2/src/lib/sceneBuilder.ts index 76abcae..7da392f 100644 --- a/app2/src/lib/sceneBuilder.ts +++ b/app2/src/lib/sceneBuilder.ts @@ -18,7 +18,8 @@ import { MeshPhongMaterial, EquirectangularReflectionMapping, ACESFilmicToneMapping, - MathUtils + MathUtils, + MeshStandardMaterial } from 'three'; import { Sky } from 'three/addons/objects/Sky.js'; import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'; @@ -75,6 +76,7 @@ export default class SceneBuilder { private isLoaded: boolean = false; public isDragging: boolean = false; highlightMaterial: any; + sky: Sky; constructor() { this.scene = new Scene(); @@ -91,14 +93,14 @@ export default class SceneBuilder { this.renderer.shadowMap.type = PCFSoftShadowMap; this.renderer.toneMapping = ACESFilmicToneMapping; this.renderer.toneMappingExposure = 0.85; - if (!parameters.canvas) document.body.appendChild(this.renderer.domElement); + if (!parameters?.canvas) document.body.appendChild(this.renderer.domElement); return this; }; public addSky = () => { - const sky = new Sky(); - sky.scale.setScalar(450000); - this.scene.add(sky); + this.sky = new Sky(); + this.sky.scale.setScalar(450000); + this.scene.add(this.sky); const effectController = { turbidity: 10, rayleigh: 3, @@ -108,7 +110,7 @@ export default class SceneBuilder { azimuth: 180, exposure: this.renderer.toneMappingExposure }; - const uniforms = sky.material.uniforms; + const uniforms = this.sky.material.uniforms; uniforms['turbidity'].value = effectController.turbidity; uniforms['rayleigh'].value = effectController.rayleigh; uniforms['mieCoefficient'].value = effectController.mieCoefficient; @@ -131,7 +133,8 @@ export default class SceneBuilder { }; public addGroundPlane = (options?: position) => { - this.ground = new Mesh(new PlaneGeometry(), new ShadowMaterial({ side: 2 })); + var planeMaterial = new MeshStandardMaterial({ color: 0x808080, side: 2, opacity: 0.5 }); + this.ground = new Mesh(new PlaneGeometry(), planeMaterial); this.ground.rotation.x = -Math.PI / 2; this.ground.scale.setScalar(30); this.ground.position.set(options?.x ?? 0, options?.y ?? 0, options?.z ?? 0); @@ -158,11 +161,13 @@ export default class SceneBuilder { public addDirectionalLight = (options: directionalLight) => { const directionalLight = new DirectionalLight(options.color, options.intensity); directionalLight.castShadow = true; - directionalLight.shadow.mapSize.setScalar(2048); - directionalLight.shadow.mapSize.width = 1024; - directionalLight.shadow.mapSize.height = 1024; + directionalLight.shadow.camera.top = 10; + directionalLight.shadow.camera.bottom = -10; + directionalLight.shadow.camera.right = 10; + directionalLight.shadow.camera.left = -10; + directionalLight.shadow.mapSize.set(4096, 4096); + directionalLight.position.set(options.x ?? 0, options.y ?? 0, options.z ?? 0); - directionalLight.shadow.radius = 5; this.scene.add(directionalLight); return this; }; diff --git a/app2/src/routes/+page.svelte b/app2/src/routes/+page.svelte index 038f9a7..1c036f5 100644 --- a/app2/src/routes/+page.svelte +++ b/app2/src/routes/+page.svelte @@ -9,7 +9,7 @@
- +

Welcome to {data.app_name}

diff --git a/app2/src/routes/system/status/+page.svelte b/app2/src/routes/system/status/+page.svelte index f63c574..09d1e83 100644 --- a/app2/src/routes/system/status/+page.svelte +++ b/app2/src/routes/system/status/+page.svelte @@ -1,8 +1,6 @@