tanghaolin
2022-07-28 62aa8e0f0b366bc131309b72e1350d800d30be72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { UIPanel } from './libs/ui.js';
 
import * as THREE from 'three/build/three.module.js';
 
class ViewHelper extends THREE.Object3D {
 
    constructor( editorCamera, container ) {
 
        super();
 
        this.animating = false;
        this.controls = null;
 
        const panel = new UIPanel();
        panel.setId( 'viewHelper' );
        panel.setPosition( 'absolute' );
        panel.setRight( '0px' );
        panel.setBottom( '0px' );
        panel.setHeight( '128px' );
        panel.setWidth( '128px' );
 
        const scope = this;
 
        panel.dom.addEventListener( 'mouseup', function ( event ) {
 
            event.stopPropagation();
 
            scope.handleClick( event );
 
        } );
 
        panel.dom.addEventListener( 'mousedown', function ( event ) {
 
            event.stopPropagation();
 
        } );
 
        container.appendChild( panel.dom );
 
        const color1 = new THREE.Color( '#ff3653' );
        const color2 = new THREE.Color( '#8adb00' );
        const color3 = new THREE.Color( '#2c8fff' );
 
        const interactiveObjects = [];
        const raycaster = new THREE.Raycaster();
        const mouse = new THREE.Vector2();
        const dummy = new THREE.Object3D();
 
        const camera = new THREE.OrthographicCamera( - 2, 2, 2, - 2, 0, 4 );
        camera.position.set( 0, 0, 2 );
 
        const geometry = new THREE.BoxGeometry( 0.8, 0.05, 0.05 ).translate( 0.4, 0, 0 );
 
        const xAxis = new THREE.Mesh( geometry, getAxisMaterial( color1 ) );
        const yAxis = new THREE.Mesh( geometry, getAxisMaterial( color2 ) );
        const zAxis = new THREE.Mesh( geometry, getAxisMaterial( color3 ) );
 
        yAxis.rotation.z = Math.PI / 2;
        zAxis.rotation.y = - Math.PI / 2;
 
        this.add( xAxis );
        this.add( zAxis );
        this.add( yAxis );
 
        const posXAxisHelper = new THREE.Sprite( getSpriteMaterial( color1, 'X' ) );
        posXAxisHelper.userData.type = 'posX';
        const posYAxisHelper = new THREE.Sprite( getSpriteMaterial( color2, 'Y' ) );
        posYAxisHelper.userData.type = 'posY';
        const posZAxisHelper = new THREE.Sprite( getSpriteMaterial( color3, 'Z' ) );
        posZAxisHelper.userData.type = 'posZ';
        const negXAxisHelper = new THREE.Sprite( getSpriteMaterial( color1 ) );
        negXAxisHelper.userData.type = 'negX';
        const negYAxisHelper = new THREE.Sprite( getSpriteMaterial( color2 ) );
        negYAxisHelper.userData.type = 'negY';
        const negZAxisHelper = new THREE.Sprite( getSpriteMaterial( color3 ) );
        negZAxisHelper.userData.type = 'negZ';
 
        posXAxisHelper.position.x = 1;
        posYAxisHelper.position.y = 1;
        posZAxisHelper.position.z = 1;
        negXAxisHelper.position.x = - 1;
        negXAxisHelper.scale.setScalar( 0.8 );
        negYAxisHelper.position.y = - 1;
        negYAxisHelper.scale.setScalar( 0.8 );
        negZAxisHelper.position.z = - 1;
        negZAxisHelper.scale.setScalar( 0.8 );
 
        this.add( posXAxisHelper );
        this.add( posYAxisHelper );
        this.add( posZAxisHelper );
        this.add( negXAxisHelper );
        this.add( negYAxisHelper );
        this.add( negZAxisHelper );
 
        interactiveObjects.push( posXAxisHelper );
        interactiveObjects.push( posYAxisHelper );
        interactiveObjects.push( posZAxisHelper );
        interactiveObjects.push( negXAxisHelper );
        interactiveObjects.push( negYAxisHelper );
        interactiveObjects.push( negZAxisHelper );
 
        const point = new THREE.Vector3();
        const dim = 128;
        const turnRate = 2 * Math.PI; // turn rate in angles per second
 
        this.render = function ( renderer ) {
 
            this.quaternion.copy( editorCamera.quaternion ).invert();
            this.updateMatrixWorld();
 
            point.set( 0, 0, 1 );
            point.applyQuaternion( editorCamera.quaternion );
 
            if ( point.x >= 0 ) {
 
                posXAxisHelper.material.opacity = 1;
                negXAxisHelper.material.opacity = 0.5;
 
            } else {
 
                posXAxisHelper.material.opacity = 0.5;
                negXAxisHelper.material.opacity = 1;
 
            }
 
            if ( point.y >= 0 ) {
 
                posYAxisHelper.material.opacity = 1;
                negYAxisHelper.material.opacity = 0.5;
 
            } else {
 
                posYAxisHelper.material.opacity = 0.5;
                negYAxisHelper.material.opacity = 1;
 
            }
 
            if ( point.z >= 0 ) {
 
                posZAxisHelper.material.opacity = 1;
                negZAxisHelper.material.opacity = 0.5;
 
            } else {
 
                posZAxisHelper.material.opacity = 0.5;
                negZAxisHelper.material.opacity = 1;
 
            }
 
            //
 
            const x = container.offsetWidth - dim;
 
            renderer.clearDepth();
            renderer.setViewport( x, 0, dim, dim );
            renderer.render( this, camera );
 
        };
 
        const targetPosition = new THREE.Vector3();
        const targetQuaternion = new THREE.Quaternion();
 
        const q1 = new THREE.Quaternion();
        const q2 = new THREE.Quaternion();
        let radius = 0;
 
        this.handleClick = function ( event ) {
 
            if ( this.animating === true ) return false;
 
            const rect = container.getBoundingClientRect();
            const offsetX = rect.left + ( container.offsetWidth - dim );
            const offsetY = rect.top + ( container.offsetHeight - dim );
            mouse.x = ( ( event.clientX - offsetX ) / ( rect.width - offsetX ) ) * 2 - 1;
            mouse.y = - ( ( event.clientY - offsetY ) / ( rect.bottom - offsetY ) ) * 2 + 1;
 
            raycaster.setFromCamera( mouse, camera );
 
            const intersects = raycaster.intersectObjects( interactiveObjects );
 
            if ( intersects.length > 0 ) {
 
                const intersection = intersects[ 0 ];
                const object = intersection.object;
 
                this.prepareAnimationData( object, this.controls.center );
 
                this.animating = true;
 
                return true;
 
            } else {
 
                return false;
 
            }
 
        };
 
        this.update = function ( delta ) {
 
            const step = delta * turnRate;
            const focusPoint = this.controls.center;
 
            // animate position by doing a slerp and then scaling the position on the unit sphere
 
            q1.rotateTowards( q2, step );
            editorCamera.position.set( 0, 0, 1 ).applyQuaternion( q1 ).multiplyScalar( radius ).add( focusPoint );
 
            // animate orientation
 
            editorCamera.quaternion.rotateTowards( targetQuaternion, step );
 
            if ( q1.angleTo( q2 ) === 0 ) {
 
                this.animating = false;
 
            }
 
        };
 
        this.prepareAnimationData=function( object, focusPoint ) {
 
            switch ( object.userData.type ) {
 
                case 'posX':
                    targetPosition.set( 1, 0, 0 );
                    targetQuaternion.setFromEuler( new THREE.Euler( 0, Math.PI * 0.5, 0 ) );
                    break;
 
                case 'posY':
                    targetPosition.set( 0, 1, 0 );
                    targetQuaternion.setFromEuler( new THREE.Euler( - Math.PI * 0.5, 0, 0 ) );
                    break;
 
                case 'posZ':
                    targetPosition.set( 0, 0, 1 );
                    targetQuaternion.setFromEuler( new THREE.Euler() );
                    break;
 
                case 'negX':
                    targetPosition.set( - 1, 0, 0 );
                    targetQuaternion.setFromEuler( new THREE.Euler( 0, - Math.PI * 0.5, 0 ) );
                    break;
 
                case 'negY':
                    targetPosition.set( 0, - 1, 0 );
                    targetQuaternion.setFromEuler( new THREE.Euler( Math.PI * 0.5, 0, 0 ) );
                    break;
 
                case 'negZ':
                    targetPosition.set( 0, 0, - 1 );
                    targetQuaternion.setFromEuler( new THREE.Euler( 0, Math.PI, 0 ) );
                    break;
 
                default:
                    console.error( 'ViewHelper: Invalid axis.' );
 
            }
 
            //
 
            radius = editorCamera.position.distanceTo( focusPoint );
            targetPosition.multiplyScalar( radius ).add( focusPoint );
 
            dummy.position.copy( focusPoint );
 
            dummy.lookAt( editorCamera.position );
            q1.copy( dummy.quaternion );
 
            dummy.lookAt( targetPosition );
            q2.copy( dummy.quaternion );
 
        }
 
        function getAxisMaterial( color ) {
 
            return new THREE.MeshBasicMaterial( { color: color, toneMapped: false } );
 
        }
 
        function getSpriteMaterial( color, text = null ) {
 
            const canvas = document.createElement( 'canvas' );
            canvas.width = 64;
            canvas.height = 64;
 
            const context = canvas.getContext( '2d' );
            context.beginPath();
            context.arc( 32, 32, 16, 0, 2 * Math.PI );
            context.closePath();
            context.fillStyle = color.getStyle();
            context.fill();
 
            if ( text !== null ) {
 
                context.font = '24px Arial';
                context.textAlign = 'center';
                context.fillStyle = '#000000';
                context.fillText( text, 32, 41 );
 
            }
 
            const texture = new THREE.CanvasTexture( canvas );
 
            return new THREE.SpriteMaterial( { map: texture, toneMapped: false } );
 
        }
 
    }
 
}
 
ViewHelper.prototype.isViewHelper = true;
 
export { ViewHelper };