Skip to content

Commit

Permalink
feat: short cut for shadow type
Browse files Browse the repository at this point in the history
  • Loading branch information
drcmda committed Jan 3, 2023
1 parent dd6975a commit 2cd6e53
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/API/canvas.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const App = () => (
| children | three.js JSX elements or regular components | |
| gl | Props that go into the default renderer, or your own renderer. Also accepts a synchronous callback like `gl={canvas => new Renderer({ canvas })}` | `{}` |
| camera | Props that go into the default camera, or your own `THREE.Camera` | `{ fov: 75, near: 0.1, far: 1000, position: [0, 0, 5] }` |
| shadows | Props that go into `gl.shadowMap`, can also be set true for `PCFsoft` | `false` |
| shadows | Props that go into `gl.shadowMap`, can be set true for `PCFsoft` or one of the following: 'basic', 'percentage', 'soft', 'variance' | `false` |
| raycaster | Props that go into the default raycaster | `{}` |
| frameloop | Render mode: always, demand, never | `always` |
| resize | Resize config, see react-use-measure's options | `{ scroll: true, debounce: { scroll: 50, resize: 0 } }` |
Expand Down
30 changes: 21 additions & 9 deletions packages/fiber/src/core/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ export type RenderProps<TCanvas extends Element> = {
/** Dimensions to fit the renderer to. Will measure canvas dimensions if omitted */
size?: Size
/**
* Enables PCFsoft shadows. Can accept `gl.shadowMap` options for fine-tuning.
* Enables shadows (by default PCFsoft). Can accept `gl.shadowMap` options for fine-tuning,
* but also strings: 'basic' | 'percentage' | 'soft' | 'variance'.
* @see https://threejs.org/docs/#api/en/renderers/WebGLRenderer.shadowMap
*/
shadows?: boolean | Partial<THREE.WebGLShadowMap>
shadows?: boolean | 'basic' | 'percentage' | 'soft' | 'variance' | Partial<THREE.WebGLShadowMap>
/**
* Disables three r139 color management.
* @see https://threejs.org/docs/#manual/en/introduction/Color-management
Expand Down Expand Up @@ -261,14 +262,25 @@ function createRoot<TCanvas extends Element>(canvas: TCanvas): ReconcilerRoot<TC

// Set shadowmap
if (gl.shadowMap) {
const isBoolean = is.boo(shadows)
if ((isBoolean && gl.shadowMap.enabled !== shadows) || !is.equ(shadows, gl.shadowMap, shallowLoose)) {
const old = gl.shadowMap.enabled
gl.shadowMap.enabled = !!shadows
if (!isBoolean) Object.assign(gl.shadowMap, shadows)
else gl.shadowMap.type = THREE.PCFSoftShadowMap
if (old !== gl.shadowMap.enabled) gl.shadowMap.needsUpdate = true
const oldEnabled = gl.shadowMap.enabled
const oldType = gl.shadowMap.type
gl.shadowMap.enabled = !!shadows

if (is.boo(shadows)) {
gl.shadowMap.type = THREE.PCFSoftShadowMap
} else if (is.str(shadows)) {
const types = {
basic: THREE.BasicShadowMap,
percentage: THREE.PCFShadowMap,
soft: THREE.PCFSoftShadowMap,
variance: THREE.VSMShadowMap,
}
gl.shadowMap.type = types[shadows] ?? THREE.PCFSoftShadowMap
} else if (is.obj(shadows)) {
Object.assign(gl.shadowMap, shadows)
}

if (oldEnabled !== gl.shadowMap.enabled || oldType !== gl.shadowMap.type) gl.shadowMap.needsUpdate = true
}

// Safely set color management if available.
Expand Down

0 comments on commit 2cd6e53

Please sign in to comment.