Skip to content

Commit

Permalink
feat: add optional envmap intensity
Browse files Browse the repository at this point in the history
  • Loading branch information
Ledzz authored and saitonakamura committed Apr 26, 2023
1 parent d169cf7 commit b89a7bc
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/XRControllerModelFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import { fetchProfile, GLTFLoader, MotionController, MotionControllerConstants }
const DEFAULT_PROFILES_PATH = 'https://cdn.jsdelivr.net/npm/@webxr-input-profiles/[email protected]/dist/profiles'
const DEFAULT_PROFILE = 'generic-trigger'

const applyEnvironmentMap = (envMap: Texture, obj: Object3D): void => {
const applyEnvironmentMap = (envMap: Texture, envMapIntensity: number, obj: Object3D): void => {
obj.traverse((child) => {
if (child instanceof Mesh && 'envMap' in child.material) {
child.material.envMap = envMap
child.material.envMapIntensity = envMapIntensity
child.material.needsUpdate = true
}
})
}

export class XRControllerModel extends Object3D {
envMap: Texture | null
envMapIntensity: number
motionController: MotionController | null
scene: Object3D | null

Expand All @@ -24,16 +26,18 @@ export class XRControllerModel extends Object3D {

this.motionController = null
this.envMap = null
this.envMapIntensity = 1
this.scene = null
}

setEnvironmentMap(envMap: Texture): XRControllerModel {
if (this.envMap == envMap) {
setEnvironmentMap(envMap: Texture, envMapIntensity = 1): XRControllerModel {
if (this.envMap === envMap && this.envMapIntensity === envMapIntensity) {
return this
}

this.envMap = envMap
applyEnvironmentMap(this.envMap, this)
this.envMapIntensity = envMapIntensity
applyEnvironmentMap(this.envMap, envMapIntensity, this)

return this
}
Expand Down Expand Up @@ -179,7 +183,7 @@ function addAssetSceneToControllerModel(controllerModel: XRControllerModel, scen

// Apply any environment map that the mesh already has set.
if (controllerModel.envMap) {
applyEnvironmentMap(controllerModel.envMap, scene)
applyEnvironmentMap(controllerModel.envMap, controllerModel.envMapIntensity, scene)
}

// Add the glTF scene to the controllerModel.
Expand Down

0 comments on commit b89a7bc

Please sign in to comment.