Skip to content

Commit

Permalink
minified spotlight turn on lag
Browse files Browse the repository at this point in the history
  • Loading branch information
Romsha committed May 13, 2022
1 parent 7bd004e commit ef667fa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Project Tasks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Basic primitives:
- description on picture interaction - V

More:
- code organization - V
- code organization - X
- cieling?
- consider lights?
- consider lights - V
- intro + loading - V
- "lock screen" - V
58 changes: 38 additions & 20 deletions src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ camera.add(listener)
const ambiantLight = new THREE.AmbientLight(0xffffff, .85)
scene.add(ambiantLight)



/**
* Loaders
*/
Expand Down Expand Up @@ -131,15 +129,18 @@ const fontLoader = new FontLoader()
// Mouse Controls
var pauseAllMusic = () => {}
var resumeAllMusic = () => {}
var controlsLocked = false
const controls = new PointerLockControls(camera, document.body);
controls.addEventListener( 'lock', () => {
overlay.classList.remove('visible')
resumeAllMusic()
controlsLocked = true
} );
if (!DEBUG) {
controls.addEventListener( 'unlock', () => {
overlay.classList.add('visible')
pauseAllMusic()
controlsLocked = false
} );
}
startButton.addEventListener('click', () => {
Expand Down Expand Up @@ -170,7 +171,6 @@ document.addEventListener('keydown', (event) => {
case 'KeyD':
moveRight = true;
break;

}
})
document.addEventListener('keyup', (event) => {
Expand Down Expand Up @@ -248,18 +248,22 @@ const pictureMeshes = {}
const audioObjects = {}
const pictureLights = {}
let shouldPauseMusic = false

pauseAllMusic = () => {
shouldPauseMusic = true
for (const audioObject of Object.values(audioObjects)) {
if (audioObject.isPlaying) { audioObject.pause() }
}

}

resumeAllMusic = () => {
shouldPauseMusic = false
}

const setPictureLights = (pictureID, lightState) => {
for (const light of pictureLights[pictureID]) { light.visible = lightState }
}

const createPictureLights = (pictureConfig) => {
let light1X = pictureConfig.x + pictureConfig.offsetX
let light1Z = pictureConfig.z + pictureConfig.offsetZ
Expand Down Expand Up @@ -308,7 +312,7 @@ const createPictureLights = (pictureConfig) => {
spotLight2.castShadow = true
spotLight.visible = false
spotLight2.visible = false

pictureLights[pictureConfig.id] = [spotLight, spotLight2]
}

Expand Down Expand Up @@ -404,13 +408,20 @@ let prevTime = clock.getElapsedTime()
// Music Playing
const totalMusicSteps = (config.pictures.pictureMusicVolumeMax - config.pictures.pictureMusicVolumeMin) / config.pictures.pictureMusicVolumeStep
const distanceVolumeStepSize = config.pictures.pictureMusicDistance / totalMusicSteps
let currentDescPictureID = null
let prevDescPictureID = null

const render = () => {
renderer.render(scene, camera)
window.requestAnimationFrame(tick)
}

const tick = () => {
const elapsedTime = clock.getElapsedTime()
const timeDelta = elapsedTime - prevTime
prevTime = elapsedTime


const tick = () => {
if (!controlsLocked) {
render()
return
}
/**
* Handle pictures
*/
Expand Down Expand Up @@ -456,22 +467,29 @@ const tick = () => {
}
if (closestPictureID) {
pictureDescContainer.classList.add('visible')
if (!currentDescPictureID || currentDescPictureID !== closestPictureID) {
if (currentDescPictureID) {
for (const light of pictureLights[currentDescPictureID]) { light.visible = false }
if (!prevDescPictureID || prevDescPictureID !== closestPictureID) {
if (prevDescPictureID) {
setPictureLights(prevDescPictureID, false)
}
currentDescPictureID = closestPictureID
// This line causes leg in animation
console.log('will turn on lights')
setPictureLights(closestPictureID, true)
console.log('turned on lights')
pictureDescContainer.src = getPictureConfig(closestPictureID).desc
for (const light of pictureLights[currentDescPictureID]) { light.visible = true }
prevDescPictureID = closestPictureID
}
} else {
if (currentDescPictureID) {
for (const light of pictureLights[currentDescPictureID]) { light.visible = false }
if (prevDescPictureID) {
setPictureLights(prevDescPictureID, false)
}
pictureDescContainer.classList.remove('visible')
currentDescPictureID = null
prevDescPictureID = null
}

const elapsedTime = clock.getElapsedTime()
const timeDelta = Math.min(1/25, elapsedTime - prevTime)
prevTime = elapsedTime

/**
* Moving animation
*/
Expand Down Expand Up @@ -518,11 +536,11 @@ const tick = () => {
}

// Do move
if (velocity.x !== 0 || velocity.z !== 0) {console.log('time delta: ', timeDelta, elapsedTime)}
controls.moveRight(velocity.x * timeDelta)
controls.moveForward(velocity.z * timeDelta)

renderer.render(scene, camera)
window.requestAnimationFrame(tick)
render()
}

tick()

0 comments on commit ef667fa

Please sign in to comment.