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

Implement Timecycle lighting for viewer #410

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@
"assert": "^2.0.0",
"buffer": "^6.0.3",
"canvas": "^2.6.1",
"process": "^0.11.10",
"node-canvas-webgl": "PrismarineJS/node-canvas-webgl",
"minecraft-assets": "^1.9.0",
"fs-extra": "^11.0.0",
"jest": "^27.0.4",
"jest-puppeteer": "^6.0.0",
"minecraft-assets": "^1.9.0",
"minecraft-wrap": "^1.3.0",
"minecrafthawkeye": "^1.2.5",
"mineflayer": "^4.0.0",
"mineflayer-pathfinder": "^2.0.0",
"node-canvas-webgl": "PrismarineJS/node-canvas-webgl",
"prismarine-schematic": "^1.2.0",
"minecrafthawkeye": "^1.2.5",
"prismarine-viewer": "file:./",
"process": "^0.11.10",
"puppeteer": "^16.0.0",
"standard": "^17.0.0",
"webpack": "^5.10.2",
"webpack-cli": "^5.1.1",
"fs-extra": "^11.0.0"
"webpack-cli": "^5.1.1"
Copy link
Author

@test137E29B test137E29B Nov 4, 2023

Choose a reason for hiding this comment

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

No version changes here, just alphabetical re-order for readability

}
}
47 changes: 47 additions & 0 deletions viewer/lib/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class Viewer {
this.setBlockStateId(new Vec3(pos.x, pos.y, pos.z), stateId)
})

emitter.on('timecycleUpdate', ({ timeOfDay, moonPhase }) => {
this.updateTimecycleLighting(timeOfDay, moonPhase)
})

this.domElement.addEventListener('pointerdown', (evt) => {
const raycaster = new THREE.Raycaster()
const mouse = new THREE.Vector2()
Expand All @@ -96,6 +100,49 @@ class Viewer {
})
}

updateTimecycleLighting(timeOfDay, moonPhase) {
if (timeOfDay === undefined) { return }
const lightIntensity = this.calculateIntensity(timeOfDay)
const skyColor = scene.background.getHexString()
const newSkyColor = `#${this.darkenSkyColour(skyColor, lightIntensity).padStart(6, 0)}`

function timeToRads(time) {
return time * (Math.PI / 12000)
}

// Update colours
this.scene.background = new THREE.Color(newSkyColor)
this.ambientLight.itensity = ((lightIntensity < 0.25) ? 0.25 : lightIntensity) + (0.07 - (moonPhase / 100))
this.directionalLight.intensity = lightIntensity + (0.07 - (moonPhase / 100))
this.directionalLight.position.set(
Math.cos(timeToRads(timeOfDay)),
Math.sin(timeToRads(timeOfDay)),
0.2
).normalize()
}

calculateIntensity(timeOfDay) {
if ((timeOfDay >= 13000) && (timeOfDay <= 23000)) {
return 0
} else if ((timeOfDay <= 12000) && (timeOfDay >= 0)) {
return 0.75
} else if ((timeOfDay < 13000) && (timeOfDay > 12000)) {
const transition = timeOfDay - 12000
return (0.75 - (0.75 * transition / 1000))
} else {
const transition = timeOfDay - 23000
return (0.75 * transition / 1000)
}
}

// Darken by factor (0 to black, 0.5 half as bright, 1 unchanged)
darkenSkyColour(skyColour, factor) {
skyColour = parseInt(skyColour, 16)
return (Math.round((skyColour & 0x0000FF) * factor) |
(Math.round(((skyColour >> 8) & 0x00FF) * factor) << 8) |
(Math.round((skyColour >> 16) * factor) << 16)).toString(16)
}

update () {
TWEEN.update()
}
Expand Down
8 changes: 8 additions & 0 deletions viewer/lib/worldView.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class WorldView extends EventEmitter {
this.loadedChunks = {}
this.lastPos = new Vec3(0, 0, 0).update(position)
this.emitter = emitter || this
this._timecycleUpdateInterval = null

this.listeners = {}
this.emitter.on('mouseClick', async (click) => {
Expand Down Expand Up @@ -54,13 +55,20 @@ class WorldView extends EventEmitter {
this.emitter.emit('entity', { id: e.id, name: e.name, pos: e.position, width: e.width, height: e.height, username: e.username })
}
}

this._timecycleUpdateInterval = setInterval(() => {
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure about this. Shouldn't it be controlled by the client ticks ? eg mineflayer

this.emit('timecycleUpdate', { timeOfDay: bot.time.timeOfDay, moonPhase: bot.time.moonPhase })
}, 750)
}

removeListenersFromBot (bot) {
for (const [evt, listener] of Object.entries(this.listeners[bot.username])) {
bot.removeListener(evt, listener)
}
delete this.listeners[bot.username]

clearInterval(this._timecycleUpdateInterval)
this._timecycleUpdateInterval = null
}

async init (pos) {
Expand Down
Loading