This commit is contained in:
@@ -20,6 +20,7 @@ let renderer: THREE.WebGLRenderer
|
|||||||
let scene: THREE.Scene
|
let scene: THREE.Scene
|
||||||
let camera: THREE.PerspectiveCamera
|
let camera: THREE.PerspectiveCamera
|
||||||
let controls: OrbitControls
|
let controls: OrbitControls
|
||||||
|
let keyLight: THREE.DirectionalLight
|
||||||
|
|
||||||
// ---- UI helpers ----------------------------------------------------------
|
// ---- UI helpers ----------------------------------------------------------
|
||||||
|
|
||||||
@@ -80,13 +81,12 @@ function buildScene(canvas: HTMLCanvasElement) {
|
|||||||
const ambient = new THREE.AmbientLight(0xffffff, 0.5)
|
const ambient = new THREE.AmbientLight(0xffffff, 0.5)
|
||||||
scene.add(ambient)
|
scene.add(ambient)
|
||||||
|
|
||||||
const key = new THREE.DirectionalLight(0xffffff, 2.0)
|
keyLight = new THREE.DirectionalLight(0xffffff, 2.0)
|
||||||
key.position.set(10, 20, 10)
|
keyLight.position.set(10, 20, 10)
|
||||||
key.castShadow = true
|
keyLight.castShadow = true
|
||||||
key.shadow.mapSize.set(2048, 2048)
|
keyLight.shadow.mapSize.set(2048, 2048)
|
||||||
key.shadow.camera.near = 0.1
|
scene.add(keyLight)
|
||||||
key.shadow.camera.far = 500
|
scene.add(keyLight.target)
|
||||||
scene.add(key)
|
|
||||||
|
|
||||||
const fill = new THREE.DirectionalLight(0x8899cc, 0.4)
|
const fill = new THREE.DirectionalLight(0x8899cc, 0.4)
|
||||||
fill.position.set(-10, 5, -10)
|
fill.position.set(-10, 5, -10)
|
||||||
@@ -150,6 +150,31 @@ function fitCamera(object: THREE.Object3D) {
|
|||||||
camera.lookAt(center)
|
camera.lookAt(center)
|
||||||
controls.target.copy(center)
|
controls.target.copy(center)
|
||||||
controls.update()
|
controls.update()
|
||||||
|
|
||||||
|
// Scale fog so it stays subtle at any model size.
|
||||||
|
// density = 0.19/dist keeps fog-factor ≈ 0.95 at the viewing distance.
|
||||||
|
if (scene.fog instanceof THREE.FogExp2) {
|
||||||
|
scene.fog.density = 0.19 / dist
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aim key light at the model center and fit its shadow frustum around
|
||||||
|
// the model so shadows are correct regardless of scale.
|
||||||
|
const lightDist = maxDim * 2
|
||||||
|
keyLight.position.set(
|
||||||
|
center.x + lightDist * 0.5,
|
||||||
|
center.y + lightDist,
|
||||||
|
center.z + lightDist * 0.5,
|
||||||
|
)
|
||||||
|
keyLight.target.position.copy(center)
|
||||||
|
keyLight.target.updateMatrixWorld()
|
||||||
|
|
||||||
|
const sc = keyLight.shadow.camera
|
||||||
|
const ext = maxDim * 1.5
|
||||||
|
sc.left = -ext; sc.right = ext
|
||||||
|
sc.top = ext; sc.bottom = -ext
|
||||||
|
sc.near = lightDist * 0.1
|
||||||
|
sc.far = lightDist * 4
|
||||||
|
sc.updateProjectionMatrix()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Ground grid ---------------------------------------------------------
|
// ---- Ground grid ---------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user