Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ViewHelper: Simplified and prettified #28312

Merged
merged 1 commit into from
May 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 32 additions & 67 deletions examples/jsm/helpers/ViewHelper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
BoxGeometry,
CylinderGeometry,
CanvasTexture,
Color,
Euler,
Expand All @@ -11,6 +11,7 @@ import {
Raycaster,
Sprite,
SpriteMaterial,
SRGBColorSpace,
Vector2,
Vector3,
Vector4
Expand All @@ -27,9 +28,10 @@ class ViewHelper extends Object3D {
this.animating = false;
this.center = new Vector3();

const color1 = new Color( '#ff3653' );
const color2 = new Color( '#8adb00' );
const color3 = new Color( '#2c8fff' );
const color1 = new Color( '#ff4466' );
const color2 = new Color( '#88ff44' );
const color3 = new Color( '#4488ff' );
const color4 = new Color( '#000000' );

const interactiveObjects = [];
const raycaster = new Raycaster();
Expand All @@ -39,7 +41,7 @@ class ViewHelper extends Object3D {
const orthoCamera = new OrthographicCamera( - 2, 2, 2, - 2, 0, 4 );
orthoCamera.position.set( 0, 0, 2 );

const geometry = new BoxGeometry( 0.8, 0.05, 0.05 ).translate( 0.4, 0, 0 );
const geometry = new CylinderGeometry( 0.04, 0.04, 0.8, 5 ).rotateZ( - Math.PI / 2 ).translate( 0.4, 0, 0 );

const xAxis = new Mesh( geometry, getAxisMaterial( color1 ) );
const yAxis = new Mesh( geometry, getAxisMaterial( color2 ) );
Expand All @@ -52,28 +54,35 @@ class ViewHelper extends Object3D {
this.add( zAxis );
this.add( yAxis );

const posXAxisHelper = new Sprite( getSpriteMaterial( color1, 'X' ) );
posXAxisHelper.userData.type = 'posX';
const posYAxisHelper = new Sprite( getSpriteMaterial( color2, 'Y' ) );
posYAxisHelper.userData.type = 'posY';
const posZAxisHelper = new Sprite( getSpriteMaterial( color3, 'Z' ) );
posZAxisHelper.userData.type = 'posZ';
const negXAxisHelper = new Sprite( getSpriteMaterial( color1 ) );
negXAxisHelper.userData.type = 'negX';
const negYAxisHelper = new Sprite( getSpriteMaterial( color2 ) );
negYAxisHelper.userData.type = 'negY';
const negZAxisHelper = new Sprite( getSpriteMaterial( color3 ) );
negZAxisHelper.userData.type = 'negZ';
const spriteMaterial1 = getSpriteMaterial( color1 );
const spriteMaterial2 = getSpriteMaterial( color2 );
const spriteMaterial3 = getSpriteMaterial( color3 );
const spriteMaterial4 = getSpriteMaterial( color4 );

const posXAxisHelper = new Sprite( spriteMaterial1 );
const posYAxisHelper = new Sprite( spriteMaterial2 );
const posZAxisHelper = new Sprite( spriteMaterial3 );
const negXAxisHelper = new Sprite( spriteMaterial4 );
const negYAxisHelper = new Sprite( spriteMaterial4 );
const negZAxisHelper = new Sprite( spriteMaterial4 );

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 );

negXAxisHelper.material.opacity = 0.2;
negYAxisHelper.material.opacity = 0.2;
negZAxisHelper.material.opacity = 0.2;

posXAxisHelper.userData.type = 'posX';
posYAxisHelper.userData.type = 'posY';
posZAxisHelper.userData.type = 'posZ';
negXAxisHelper.userData.type = 'negX';
negYAxisHelper.userData.type = 'negY';
negZAxisHelper.userData.type = 'negZ';

this.add( posXAxisHelper );
this.add( posYAxisHelper );
Expand Down Expand Up @@ -101,42 +110,6 @@ class ViewHelper extends Object3D {
point.set( 0, 0, 1 );
point.applyQuaternion( camera.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 = domElement.offsetWidth - dim;
Expand Down Expand Up @@ -298,29 +271,21 @@ class ViewHelper extends Object3D {

}

function getSpriteMaterial( color, text = null ) {
function getSpriteMaterial( color ) {

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.arc( 32, 32, 14, 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 CanvasTexture( canvas );
texture.colorSpace = SRGBColorSpace;

return new SpriteMaterial( { map: texture, toneMapped: false } );

Expand Down
Loading