Skip to content

Commit

Permalink
fixup! fix: better scene reset (eg lighting) after world unload
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Jan 20, 2024
1 parent 82781d1 commit b24f791
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions prismarine-viewer/viewer/lib/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Viewer {
renderPass: RenderPass

constructor(public renderer: THREE.WebGLRenderer, numWorkers?: number, public enableFXAA = false) {
this.scene = new THREE.Scene()
this.resetScene()
if (this.enableFXAA) {
this.enableFxaaScene()
Expand All @@ -40,43 +41,14 @@ export class Viewer {
this.domElement = renderer.domElement
}

enableFxaaScene () {
let renderTarget
if (this.renderer.capabilities.isWebGL2) {
// Use float precision depth if possible
// see https://github.com/bs-community/skinview3d/issues/111
renderTarget = new THREE.WebGLRenderTarget(0, 0, {
depthTexture: new THREE.DepthTexture(0, 0, THREE.FloatType),
})
}
this.composer = new EffectComposer(this.renderer, renderTarget)
this.renderPass = new RenderPass(this.scene, this.camera)
this.composer.addPass(this.renderPass)
this.fxaaPass = new ShaderPass(FXAAShader)
this.composer.addPass(this.fxaaPass)
this.updateComposerSize()
this.enableFXAA = true
}

// todo
updateComposerSize (): void {
if (!this.composer) return
const { width, height } = this.renderer.getSize(new THREE.Vector2())
this.composer.setSize(width, height)
// todo auto-update
const pixelRatio = this.renderer.getPixelRatio()
this.composer.setPixelRatio(pixelRatio)
this.fxaaPass.material.uniforms["resolution"].value.x = 1 / (width * pixelRatio)
this.fxaaPass.material.uniforms["resolution"].value.y = 1 / (height * pixelRatio)
}

resetScene () {
this.scene = new THREE.Scene()
this.scene.background = new THREE.Color('lightblue')

if (this.ambientLight) this.scene.remove(this.ambientLight)
this.ambientLight = new THREE.AmbientLight(0xcc_cc_cc)
this.scene.add(this.ambientLight)

if (this.directionalLight) this.scene.remove(this.directionalLight)
this.directionalLight = new THREE.DirectionalLight(0xff_ff_ff, 0.5)
this.directionalLight.position.set(1, 1, 0.5).normalize()
this.directionalLight.castShadow = true
Expand Down Expand Up @@ -228,4 +200,34 @@ export class Viewer {
async waitForChunksToRender () {
await this.world.waitForChunksToRender()
}

enableFxaaScene () {
let renderTarget
if (this.renderer.capabilities.isWebGL2) {
// Use float precision depth if possible
// see https://github.com/bs-community/skinview3d/issues/111
renderTarget = new THREE.WebGLRenderTarget(0, 0, {
depthTexture: new THREE.DepthTexture(0, 0, THREE.FloatType),
})
}
this.composer = new EffectComposer(this.renderer, renderTarget)
this.renderPass = new RenderPass(this.scene, this.camera)
this.composer.addPass(this.renderPass)
this.fxaaPass = new ShaderPass(FXAAShader)
this.composer.addPass(this.fxaaPass)
this.updateComposerSize()
this.enableFXAA = true
}

// todo
updateComposerSize (): void {
if (!this.composer) return
const { width, height } = this.renderer.getSize(new THREE.Vector2())
this.composer.setSize(width, height)
// todo auto-update
const pixelRatio = this.renderer.getPixelRatio()
this.composer.setPixelRatio(pixelRatio)
this.fxaaPass.material.uniforms["resolution"].value.x = 1 / (width * pixelRatio)
this.fxaaPass.material.uniforms["resolution"].value.y = 1 / (height * pixelRatio)
}
}

0 comments on commit b24f791

Please sign in to comment.