Skip to content

Commit 9a6f817

Browse files
committed
add ViewHelper to examples
1 parent c7d2bb2 commit 9a6f817

File tree

2 files changed

+300
-282
lines changed

2 files changed

+300
-282
lines changed

editor/js/Viewport.ViewHelper.js

Lines changed: 5 additions & 282 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { UIPanel } from './libs/ui.js';
22

3-
import * as THREE from 'three';
3+
import { ViewHelper as ViewHelperBase } from '../../examples/jsm/helpers/ViewHelper.js';
44

5-
class ViewHelper extends THREE.Object3D {
5+
class ViewHelper extends ViewHelperBase {
66

77
constructor( editorCamera, container ) {
88

9-
super();
10-
11-
this.animating = false;
12-
this.controls = null;
9+
super( editorCamera, container.dom );
1310

1411
const panel = new UIPanel();
1512
panel.setId( 'viewHelper' );
@@ -19,13 +16,11 @@ class ViewHelper extends THREE.Object3D {
1916
panel.setHeight( '128px' );
2017
panel.setWidth( '128px' );
2118

22-
const scope = this;
23-
24-
panel.dom.addEventListener( 'pointerup', function ( event ) {
19+
panel.dom.addEventListener( 'pointerup', ( event ) => {
2520

2621
event.stopPropagation();
2722

28-
scope.handleClick( event );
23+
this.handleClick( event );
2924

3025
} );
3126

@@ -37,280 +32,8 @@ class ViewHelper extends THREE.Object3D {
3732

3833
container.add( panel );
3934

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

31237
}
31338

314-
ViewHelper.prototype.isViewHelper = true;
315-
31639
export { ViewHelper };

0 commit comments

Comments
 (0)