Skip to content

Commit 6e8e42b

Browse files
works well on ios
1 parent b4a4e51 commit 6e8e42b

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

ui.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class State{
6565
this.camera_position = camera_position
6666
this.interactive = interactive
6767
this.IS_IOS = /iP(hone|ad|od)/.test(navigator.platform) || /iPhone|iPad|iPod/.test(navigator.userAgent);
68+
this.lastCanvasWidth = 0
69+
this.lastCanvasHeight = 0
6870
}
6971
async initialize(){
7072
const width = this.canvas.width
@@ -80,6 +82,9 @@ class State{
8082
this.renderer.setClearColor(0xffffff, 0);
8183

8284
this.renderer.setSize(width/this.devicePixelRatio, height/this.devicePixelRatio);
85+
86+
this.lastCanvasWidth = this.canvas.width
87+
this.lastCanvasHeight = this.canvas.height
8388

8489

8590
// canvasContainer.appendChild(this.renderer.domElement );
@@ -402,20 +407,34 @@ export async function episode_init_multi(ui_state, parameters){
402407
}
403408

404409
function update_camera(ui_state){
405-
if(ui_state.IS_IOS && ui_state.render_tick % 10 == 0){
406-
const width = ui_state.canvas.width/ui_state.devicePixelRatio
407-
const height = ui_state.canvas.height/ui_state.devicePixelRatio
408-
ui_state.camera.aspect = width / height
409-
ui_state.camera.updateProjectionMatrix()
410-
ui_state.renderer.setPixelRatio(ui_state.devicePixelRatio)
411-
ui_state.renderer.setSize(width, height)
410+
const currentWidth = ui_state.canvas.width
411+
const currentHeight = ui_state.canvas.height
412+
const hasResized = currentWidth !== ui_state.lastCanvasWidth || currentHeight !== ui_state.lastCanvasHeight
413+
414+
if (hasResized) {
415+
const width = currentWidth / ui_state.devicePixelRatio;
416+
const height = currentHeight / ui_state.devicePixelRatio;
417+
418+
if (ui_state.camera.aspect !== width / height) {
419+
ui_state.camera.aspect = width / height;
420+
ui_state.camera.updateProjectionMatrix();
421+
}
422+
423+
if (ui_state.renderer) {
424+
ui_state.renderer.setPixelRatio(ui_state.devicePixelRatio);
425+
ui_state.renderer.setSize(width, height, false);
426+
}
427+
428+
ui_state.lastCanvasWidth = currentWidth
429+
ui_state.lastCanvasHeight = currentHeight
412430
}
413431

414-
if(ui_state.interactive){
432+
if(ui_state.interactive && ui_state.controls){
415433
ui_state.controls.update()
416434
}
417-
ui_state.renderer.render(ui_state.scene, ui_state.camera);
418-
435+
if(ui_state.renderer){
436+
ui_state.renderer.render(ui_state.scene, ui_state.camera);
437+
}
419438
ui_state.render_tick += 1
420439
}
421440

0 commit comments

Comments
 (0)