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 all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules
package-lock.json
/test/*.png
/test/server_*
versions/
public/index.js*
public/worker.js*
Expand Down
4 changes: 3 additions & 1 deletion viewer/lib/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const { dispose3 } = require('./dispose')
function getEntityMesh (entity, scene) {
if (entity.name) {
try {
const e = new Entity('1.16.4', entity.name, scene)
// Old versions of minecraft use titlecase entity names (e.g. 'Zombie' instead of 'zombie')
// TODO old version name support. "entityhorse" instead of "horse" in some versions, causing a pink box to render instead
const e = new Entity('1.16.4', entity.name.toLowerCase(), scene)

if (entity.username !== undefined) {
const canvas = document.createElement('canvas')
Expand Down
52 changes: 52 additions & 0 deletions viewer/lib/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Viewer {
constructor (renderer) {
this.scene = new THREE.Scene()
this.scene.background = new THREE.Color('lightblue')
this.skyColour = this.scene.background.getHexString()

this.ambientLight = new THREE.AmbientLight(0xcccccc)
this.scene.add(this.ambientLight)
Expand Down Expand Up @@ -91,6 +92,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 @@ -102,6 +107,53 @@ class Viewer {
})
}

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

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

// Update colours
this.scene.background = new THREE.Color(newSkyColor)
const newAmbientIntensity = Math.min(0.43, lightIntensity * 0.75) + (0.04 - (moonPhase / 100))
const newDirectionalIntensity = Math.min(0.63, lightIntensity) + (0.06 - (moonPhase / 100))
this.ambientLight.itensity = newAmbientIntensity
Copy link
Contributor

@zardoy zardoy Nov 14, 2023

Choose a reason for hiding this comment

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

Hey! I was interested in wether it could improve the day cycle. When I was trying to port it to TS i noticed it gives a type error because it should be intensity instead of itensity (a typo). However I'm still not sure of using intensity like this w setting position like this, it still looks not accurate (on the left side is the original client with minimal brightness and this code on the right. time=18000):

At the very least you should not use intervals since time can be updated at any moment (night skip, time command), also isRaining can affect the sky color:
commit

Copy link
Author

Choose a reason for hiding this comment

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

Any improvements are very welcome - I will do a bit more testing but wont be able to do much about specific changes for weather lighting etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

As I remember weather don't affect lighting much - sky gets darker a bit and that's it (and imo it's enough). Anyway, it's too dark at almost any time. Also if you set evening you can see like native client doesn't really change directional light positioning. Really recommend running both clients connected to the same server so it's easy to compare

Copy link
Member

Choose a reason for hiding this comment

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

this should be fixed

itensity -> intensity at least

this.directionalLight.intensity = newDirectionalIntensity
this.directionalLight.position.set(
Math.cos(timeToRads(timeOfDay)),
Math.sin(timeToRads(timeOfDay)),
0.2
).normalize()
}

calculateIntensity (currentTicks) {
const transitionStart = 12000
const transitionEnd = 18000
const timeInDay = (currentTicks % 24000)
let lightIntensity

if (timeInDay < transitionStart) {
lightIntensity = 1.0
} else if (timeInDay < transitionEnd) {
lightIntensity = 1 - (timeInDay - transitionStart) / (transitionEnd - transitionStart)
} else {
lightIntensity = (timeInDay - transitionEnd) / (24000 - transitionEnd)
}

return lightIntensity
}

// Darken by factor (0 to black, 0.5 half as bright, 1 unchanged)
darkenSkyColour (factor) {
const skyColour = parseInt(this.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.emitter.emit('timecycleUpdate', { timeOfDay: bot.time.timeOfDay, moonPhase: bot.time.moonPhase })
}, 1000)
}

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