Skip to content

Commit

Permalink
feat: support for envmap intensity and update gainmap
Browse files Browse the repository at this point in the history
  • Loading branch information
drcmda committed Apr 3, 2024
1 parent a3db5c8 commit 94c0752
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4723,6 +4723,7 @@ Sets up a global cubemap, which affects the default `scene.environment`, and opt
<Environment
background={false} // can be true, false or "only" (which only sets the background) (default: false)
blur={0} // optional blur factor between 0 and 1 (default: 0, only works with three 0.146 and up)
intensity={1} // optional intensity factor (default: 1, only works with three 0.163 and up)
rotation={[0, Math.PI / 2, 0]} // optional rotation (default: 0, only works with three 0.163 and up)
files={['px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png']}
path="/"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"dependencies": {
"@babel/runtime": "^7.11.2",
"@mediapipe/tasks-vision": "0.10.8",
"@monogrid/gainmap-js": "^3.0.4",
"@monogrid/gainmap-js": "^3.0.5",
"@react-spring/three": "~9.6.1",
"@use-gesture/react": "^10.2.24",
"camera-controls": "^2.4.2",
Expand Down
19 changes: 14 additions & 5 deletions src/core/Environment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type EnvironmentProps = {
resolution?: number
background?: boolean | 'only'
blur?: number
intensity?: number
rotation?: Vector3
map?: Texture
preset?: PresetsType
Expand All @@ -35,6 +36,7 @@ function setEnvProps(
defaultScene: Scene,
texture: Texture,
blur: number = 0,
intensity: number = 1,
rotation: Vector3 = [0, 0, 0]
) {
const target = resolveScene(scene || defaultScene)
Expand All @@ -43,13 +45,17 @@ function setEnvProps(
// @ts-ignore
const oldBlur = target.backgroundBlurriness || 0
// @ts-ignore
const oldIntensity = target.environmentIntensity || 1
// @ts-ignore
const oldRotation = target.backgroundBlurriness !== undefined ? target.backgroundRotation.copy() : 0
if (background !== 'only') target.environment = texture
if (background) {
target.background = texture
// @ts-ignore
if (target.backgroundBlurriness !== undefined) target.backgroundBlurriness = blur
// @ts-ignore
if (target.environmentIntensity !== undefined) target.environmentIntensity = intensity
// @ts-ignore
if (target.backgroundRotation !== undefined) applyProps(target, { backgroundRotation: rotation })
}
return () => {
Expand All @@ -59,24 +65,26 @@ function setEnvProps(
// @ts-ignore
if (target.backgroundBlurriness !== undefined) target.backgroundBlurriness = oldBlur
// @ts-ignore
if (target.environmentIntensity !== undefined) target.environmentIntensity = oldIntensity
// @ts-ignore
if (target.backgroundRotation !== undefined) applyProps(target, { backgroundRotation: oldRotation })
}
}
}

export function EnvironmentMap({ scene, background = false, blur, rotation, map }: EnvironmentProps) {
export function EnvironmentMap({ scene, background = false, blur, rotation, intensity, map }: EnvironmentProps) {
const defaultScene = useThree((state) => state.scene)
React.useLayoutEffect(() => {
if (map) return setEnvProps(background, scene, defaultScene, map, blur, rotation)
if (map) return setEnvProps(background, scene, defaultScene, map, blur, intensity, rotation)
}, [defaultScene, scene, map, background, blur, rotation])
return null
}

export function EnvironmentCube({ background = false, scene, blur, rotation, ...rest }: EnvironmentProps) {
export function EnvironmentCube({ background = false, scene, blur, rotation, intensity, ...rest }: EnvironmentProps) {
const texture = useEnvironment(rest)
const defaultScene = useThree((state) => state.scene)
React.useLayoutEffect(() => {
return setEnvProps(background, scene, defaultScene, texture, blur, rotation)
return setEnvProps(background, scene, defaultScene, texture, blur, intensity, rotation)
}, [texture, background, scene, defaultScene, blur, rotation])
return null
}
Expand All @@ -90,6 +98,7 @@ export function EnvironmentPortal({
map,
background = false,
blur,
intensity,
rotation,
scene,
files,
Expand All @@ -109,7 +118,7 @@ export function EnvironmentPortal({

React.useLayoutEffect(() => {
if (frames === 1) camera.current.update(gl, virtualScene)
return setEnvProps(background, scene, defaultScene, fbo.texture, blur, rotation)
return setEnvProps(background, scene, defaultScene, fbo.texture, blur, intensity, rotation)
}, [children, virtualScene, fbo.texture, scene, defaultScene, background, frames, gl])

let count = 1
Expand Down
13 changes: 9 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1880,10 +1880,10 @@
resolved "https://registry.yarnpkg.com/@mediapipe/tasks-vision/-/tasks-vision-0.10.8.tgz#a78e137018a19933b7a1d0e887d553d4ab833d10"
integrity sha512-Rp7ll8BHrKB3wXaRFKhrltwZl1CiXGdibPxuWXvqGnKTnv8fqa/nvftYNuSbf+pbJWKYCXdBtYTITdAUTGGh0Q==

"@monogrid/gainmap-js@^3.0.4":
version "3.0.4"
resolved "https://registry.yarnpkg.com/@monogrid/gainmap-js/-/gainmap-js-3.0.4.tgz#f822dcf208cadf7f97c1246c4e60cf0c68f304e6"
integrity sha512-6EDkW+z4wpiMfISRcWkqGiEOD73RG3rPvlIMAwrQ0xbnRTcyjNVsoKcOhm2OpXTD83fYWJO2bXMVYVdBx5m2hg==
"@monogrid/gainmap-js@^3.0.5":
version "3.0.5"
resolved "https://registry.yarnpkg.com/@monogrid/gainmap-js/-/gainmap-js-3.0.5.tgz#8c736088012ae8cd1f7b3d5adbd221c3905ab4ed"
integrity sha512-53sCTG4FaJBaAq/tcufARtVYDMDGqyBT9i7F453pWGhZ5LqubDHDWtYoHo9VhQqMcHTEexdJqSsR58y+9HVmQA==
dependencies:
promise-worker-transferable "^1.0.4"

Expand Down Expand Up @@ -7823,6 +7823,11 @@ he@^1.2.0:
resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f"
integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==

hls.js@1.3.5:
version "1.3.5"
resolved "https://registry.yarnpkg.com/hls.js/-/hls.js-1.3.5.tgz#0e8b0799ecf2feb7ba199f5e95f35ba9552e04f4"
integrity sha512-uybAvKS6uDe0MnWNEPnO0krWVr+8m2R0hJ/viql8H3MVK+itq8gGQuIYoFHL3rECkIpNH98Lw8YuuWMKZxp3Ew==

hoist-non-react-statics@^3.3.1:
version "3.3.2"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
Expand Down

0 comments on commit 94c0752

Please sign in to comment.