From d027f10a4556d3a8571cb52474d3d983b3f2a399 Mon Sep 17 00:00:00 2001 From: Conrad Chan Date: Thu, 27 May 2021 16:16:23 -0700 Subject: [PATCH] fix(model3d): Save defaults returned from metadata (#1392) --- .../viewers/box3d/model3d/Model3DViewer.js | 28 +++++++++--- .../model3d/__tests__/Model3DViewer-test.js | 43 ++++++++++++++----- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/lib/viewers/box3d/model3d/Model3DViewer.js b/src/lib/viewers/box3d/model3d/Model3DViewer.js index 770f298a7..aea715af9 100644 --- a/src/lib/viewers/box3d/model3d/Model3DViewer.js +++ b/src/lib/viewers/box3d/model3d/Model3DViewer.js @@ -40,6 +40,13 @@ class Model3DViewer extends Box3DViewer { forward: null, }; + /** @property {Object} - Stores the defaults for the model settings */ + defaults = { + projection: CAMERA_PROJECTION_PERSPECTIVE, + renderMode: RENDER_MODE_LIT, + showGrid: true, + }; + /** @property {Object[]} - List of Box3D instances added to the scene */ instances = []; @@ -226,6 +233,13 @@ class Model3DViewer extends Box3DViewer { this.showGrid = DEFAULT_RENDER_GRID; } + // Save the defaults so handleReset will change the values appropriately + this.defaults = { + projection: this.projection, + renderMode: this.renderMode, + showGrid: this.showGrid, + }; + if (this.axes.up !== DEFAULT_AXIS_UP || this.axes.forward !== DEFAULT_AXIS_FORWARD) { this.handleRotationAxisSet(this.axes.up, this.axes.forward, false); } @@ -347,12 +361,14 @@ class Model3DViewer extends Box3DViewer { * @inheritdoc */ handleReset() { + const { projection, renderMode, showGrid } = this.defaults; + super.handleReset(); this.setAnimationState(false); - this.handleSetCameraProjection(CAMERA_PROJECTION_PERSPECTIVE); - this.handleSetRenderMode(RENDER_MODE_LIT); - this.handleShowGrid(true); + this.handleSetCameraProjection(projection); + this.handleSetRenderMode(renderMode); + this.handleShowGrid(showGrid); this.handleShowSkeletons(false); this.handleShowWireframes(false); @@ -360,11 +376,11 @@ class Model3DViewer extends Box3DViewer { if (this.getViewerOption('useReactControls')) { this.renderUI(); } else { - this.controls.handleSetRenderMode(RENDER_MODE_LIT); - this.controls.setCurrentProjectionMode(CAMERA_PROJECTION_PERSPECTIVE); + this.controls.handleSetRenderMode(renderMode); + this.controls.setCurrentProjectionMode(projection); this.controls.handleSetSkeletonsVisible(false); this.controls.handleSetWireframesVisible(false); - this.controls.handleSetGridVisible(this.showGrid); + this.controls.handleSetGridVisible(showGrid); } } diff --git a/src/lib/viewers/box3d/model3d/__tests__/Model3DViewer-test.js b/src/lib/viewers/box3d/model3d/__tests__/Model3DViewer-test.js index b90157581..856b18d68 100644 --- a/src/lib/viewers/box3d/model3d/__tests__/Model3DViewer-test.js +++ b/src/lib/viewers/box3d/model3d/__tests__/Model3DViewer-test.js @@ -6,16 +6,20 @@ import Model3DControls from '../Model3DControls'; import Model3DRenderer from '../Model3DRenderer'; import Model3DViewer from '../Model3DViewer'; import { + CAMERA_PROJECTION_ORTHOGRAPHIC, + CAMERA_PROJECTION_PERSPECTIVE, EVENT_CANVAS_CLICK, EVENT_ROTATE_ON_AXIS, EVENT_SELECT_ANIMATION_CLIP, EVENT_SET_CAMERA_PROJECTION, + EVENT_SET_GRID_VISIBLE, EVENT_SET_RENDER_MODE, EVENT_SET_SKELETONS_VISIBLE, EVENT_SET_WIREFRAMES_VISIBLE, - EVENT_SET_GRID_VISIBLE, EVENT_TOGGLE_ANIMATION, EVENT_TOGGLE_HELPERS, + RENDER_MODE_NORMALS, + RENDER_MODE_UNLIT, } from '../model3DConstants'; const sandbox = sinon.createSandbox(); @@ -798,12 +802,29 @@ describe('lib/viewers/box3d/model3d/Model3DViewer', () => { }); describe('handleReset()', () => { + beforeEach(() => { + model3d.defaults = { + projection: CAMERA_PROJECTION_ORTHOGRAPHIC, + renderMode: RENDER_MODE_NORMALS, + showGrid: false, + }; + }); + test('should reset control settings', () => { - sandbox.mock(model3d.controls).expects('handleSetRenderMode'); - sandbox.mock(model3d.controls).expects('setCurrentProjectionMode'); + sandbox + .mock(model3d.controls) + .expects('handleSetRenderMode') + .withArgs(RENDER_MODE_NORMALS); + sandbox + .mock(model3d.controls) + .expects('setCurrentProjectionMode') + .withArgs(CAMERA_PROJECTION_ORTHOGRAPHIC); sandbox.mock(model3d.controls).expects('handleSetSkeletonsVisible'); sandbox.mock(model3d.controls).expects('handleSetWireframesVisible'); - sandbox.mock(model3d.controls).expects('handleSetGridVisible'); + sandbox + .mock(model3d.controls) + .expects('handleSetGridVisible') + .withArgs(false); const renderMock = sandbox.mock(model3d.renderer); renderMock.expects('stopAnimation').once(); model3d.handleReset(); @@ -833,18 +854,18 @@ describe('lib/viewers/box3d/model3d/Model3DViewer', () => { test('should reset controls state and call renderUI', () => { model3d.isAnimationPlaying = true; - model3d.projection = 'Orthographic'; - model3d.renderMode = 'Normals'; - model3d.showGrid = false; + model3d.projection = CAMERA_PROJECTION_PERSPECTIVE; + model3d.renderMode = RENDER_MODE_UNLIT; + model3d.showGrid = true; model3d.showSkeletons = true; model3d.showWireframes = true; model3d.handleReset(); expect(model3d.isAnimationPlaying).toBe(false); - expect(model3d.projection).toBe('Perspective'); - expect(model3d.renderMode).toBe('Lit'); - expect(model3d.showGrid).toBe(true); + expect(model3d.projection).toBe(CAMERA_PROJECTION_ORTHOGRAPHIC); + expect(model3d.renderMode).toBe(RENDER_MODE_NORMALS); + expect(model3d.showGrid).toBe(false); expect(model3d.showSkeletons).toBe(false); expect(model3d.showWireframes).toBe(false); expect(model3d.renderUI).toBeCalled(); @@ -986,7 +1007,7 @@ describe('lib/viewers/box3d/model3d/Model3DViewer', () => { expect(getProps(model3d)).toMatchObject({ animationClips: [], - cameraProjection: 'Perspective', + cameraProjection: CAMERA_PROJECTION_PERSPECTIVE, currentAnimationClipId: '123', isPlaying: false, isVrShown: false,