Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascading Shadows #1036

Merged
merged 18 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
4f182f0
Adding basic Cascading Shadows implementation
Dhruv-0-Arora Jul 20, 2024
323386e
Formatting
Dhruv-0-Arora Jul 20, 2024
6213b25
Merge remote-tracking branch 'origin/dev' into dhruv/1694/shadow-casc…
Dhruv-0-Arora Jul 22, 2024
e716d0b
Changing light color and intensity and removing old lighting implemen…
Dhruv-0-Arora Jul 22, 2024
4bf919b
Merge remote-tracking branch 'origin/dev' into dhruv/1694/shadow-casc…
Dhruv-0-Arora Jul 24, 2024
c85ad83
Adding changes to using the implementation built in
Dhruv-0-Arora Jul 24, 2024
3b304aa
Merge remote-tracking branch 'origin/dev' into dhruv/1694/shadow-casc…
Dhruv-0-Arora Jul 24, 2024
28dab58
Adding quality changing functionality
Dhruv-0-Arora Jul 25, 2024
8b10576
Merge remote-tracking branch 'origin/dev' into dhruv/1694/shadow-casc…
Dhruv-0-Arora Jul 26, 2024
ddc0a55
Created working light switching for cascading shadow maps
Dhruv-0-Arora Jul 26, 2024
f36bbc7
Formatted + removed testing cube + updated documentation
Dhruv-0-Arora Jul 26, 2024
015ca51
Fixing setupMaterials
Dhruv-0-Arora Jul 29, 2024
404f7be
Working shadows
Dhruv-0-Arora Jul 30, 2024
22b5e8c
Merge remote-tracking branch 'origin/dev' into dhruv/1694/shadow-casc…
Dhruv-0-Arora Jul 30, 2024
b692eb7
Merge remote-tracking branch 'origin/dev' into dhruv/1694/shadow-casc…
Dhruv-0-Arora Aug 6, 2024
dcca428
Formatting
Dhruv-0-Arora Aug 6, 2024
1fdb767
Increased number of cascades
Dhruv-0-Arora Aug 8, 2024
9de49d7
Merge remote-tracking branch 'origin/dev' into dhruv/1694/shadow-casc…
Dhruv-0-Arora Aug 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions fission/src/mirabuf/MirabufInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ class MirabufInstance {
opacity: opacity,
transparent: opacity < 1.0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the shadowSide parameters to render against both the front and back faces when casting the shadow. This will remove the gap from the object to the shadow caused by the normal bias.

})
World.SceneRenderer.SetupMaterial(material)
} else if (materialStyle == MaterialStyle.Normals) {
material = new THREE.MeshNormalMaterial()
World.SceneRenderer.SetupMaterial(material)
} else if (materialStyle == MaterialStyle.Toon) {
material = World.SceneRenderer.CreateToonMaterial(hex, 5)
console.debug("Toon Material")
Expand Down
71 changes: 52 additions & 19 deletions fission/src/systems/scene/SceneRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
import InputSystem from "../input/InputSystem"

import { PixelSpaceCoord, SceneOverlayEvent, SceneOverlayEventKey } from "@/ui/components/SceneOverlayEvents"
import {} from "@/ui/components/SceneOverlayEvents"
import PreferencesSystem from "../preferences/PreferencesSystem"
import { CSM } from "three/examples/jsm/csm/CSM.js"
import { CSMHelper } from "three/examples/jsm/csm/CSMHelper.js"

Check warning on line 17 in fission/src/systems/scene/SceneRenderer.ts

View workflow job for this annotation

GitHub Actions / ESLint Format Validation

'CSMHelper' is defined but never used. Allowed unused vars must match /^_/u

const CLEAR_COLOR = 0x121212
const GROUND_COLOR = 0x4066c7
Expand All @@ -34,6 +35,9 @@
private _orbitControls: OrbitControls
private _transformControls: Map<TransformControls, number> // maps all rendered transform controls to their size

private _csm: CSM
// private _csmHelper: CSMHelper

public get sceneObjects() {
return this._sceneObjects
}
Expand Down Expand Up @@ -74,31 +78,43 @@
this._renderer.shadowMap.type = THREE.PCFSoftShadowMap
this._renderer.setSize(window.innerWidth, window.innerHeight)

const directionalLight = new THREE.DirectionalLight(0xffffff, 3.0)
directionalLight.position.set(-1.0, 3.0, 2.0)
directionalLight.castShadow = true
this._scene.add(directionalLight)

// this._light = new CascadingShadows(this._mainCamera, this._scene, this._renderer)
const shadowMapSize = Math.min(4096, this._renderer.capabilities.maxTextureSize)
const shadowCamSize = 15
console.debug(`Shadow Map Size: ${shadowMapSize}`)

directionalLight.shadow.camera.top = shadowCamSize
directionalLight.shadow.camera.bottom = -shadowCamSize
directionalLight.shadow.camera.left = -shadowCamSize
directionalLight.shadow.camera.right = shadowCamSize
directionalLight.shadow.mapSize = new THREE.Vector2(shadowMapSize, shadowMapSize)
directionalLight.shadow.blurSamples = 16
directionalLight.shadow.normalBias = 0.01
directionalLight.shadow.bias = 0.0
this._csm = new CSM({
maxFar: 30,
cascades: 3,
mode: 'custom',
parent: this._scene,
shadowMapSize: shadowMapSize,
lightDirection: new THREE.Vector3( 0.5, -0.5, 0.5 ).normalize(),
lightIntensity: 3,
camera: this._mainCamera,
customSplitsCallback: (cascades: number, near: number, far: number, breaks: number[]) => {
const blend = 0.4;
for (let i = 1; i < cascades; i++) {
const uniformFactor = (near + (far - near) * i / cascades) / far;
const logarithmicFactor = (near * (far / near) ** (i / cascades)) / far;
const combinedFactor = uniformFactor * (1 - blend) + logarithmicFactor * blend;

breaks.push(combinedFactor);
}

breaks.push(1);
}
})
this._csm.fade = true

// this._csmHelper = new CSMHelper(this._csm)
// this._csmHelper.visible = true
// this._scene.add(this._csmHelper)

const ambientLight = new THREE.AmbientLight(0xffffff, 0.1)
this._scene.add(ambientLight)

const ground = new THREE.Mesh(new THREE.BoxGeometry(10, 1, 10), this.CreateToonMaterial(GROUND_COLOR))
ground.position.set(0.0, -2.0, 0.0)
ground.receiveShadow = true
ground.castShadow = true
ground.castShadow = false
this._scene.add(ground)

// Adding spherical skybox mesh
Expand Down Expand Up @@ -130,6 +146,11 @@
// Orbit controls
this._orbitControls = new OrbitControls(this._mainCamera, this._renderer.domElement)
this._orbitControls.update()

const cube = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), this.CreateToonMaterial())
cube.castShadow = true
cube.receiveShadow = true
this._scene.add(cube)
}

public UpdateCanvasSize() {
Expand All @@ -144,6 +165,11 @@
obj.Update()
})

this._mainCamera.updateMatrixWorld()

this._csm.update()
// this._csmHelper.update()

this._skybox.position.copy(this._mainCamera.position)

const mainCameraFovRadians = (Math.PI * (this._mainCamera.fov * 0.5)) / 180
Expand Down Expand Up @@ -189,6 +215,7 @@
public CreateSphere(radius: number, material?: THREE.Material | undefined): THREE.Mesh {
const geo = new THREE.SphereGeometry(radius)
if (material) {
this._csm.setupMaterial(material)
return new THREE.Mesh(geo, material)
} else {
return new THREE.Mesh(geo, this.CreateToonMaterial())
Expand All @@ -203,10 +230,12 @@
}
const gradientMap = new THREE.DataTexture(colors, colors.length, 1, format)
gradientMap.needsUpdate = true
return new THREE.MeshToonMaterial({
const material = new THREE.MeshToonMaterial({
color: color,
gradientMap: gradientMap,
})
this._csm.setupMaterial(material)
return material
}

/**
Expand Down Expand Up @@ -349,6 +378,10 @@
public RemoveObject(obj: THREE.Object3D) {
this._scene.remove(obj)
}

public SetupMaterial(material: THREE.Material) {
this._csm.setupMaterial(material)
}
}

export default SceneRenderer
Loading