Viewport.ViewHelper.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @author Mugen87 / https://github.com/Mugen87
  3. */
  4. import * as THREE from '../../build/three.module.js';
  5. function ViewHelper() {
  6. THREE.Object3D.call( this );
  7. var color1 = new THREE.Color( '#ff3653' );
  8. var color2 = new THREE.Color( '#8adb00' );
  9. var color3 = new THREE.Color( '#2c8fff' );
  10. var camera = new THREE.OrthographicCamera( - 2, 2, 2, - 2, 0, 4 );
  11. camera.position.set( 0, 0, 2 );
  12. var axesHelper = new THREE.AxesHelper();
  13. color1.toArray( axesHelper.geometry.attributes.color.array, 0 );
  14. color1.toArray( axesHelper.geometry.attributes.color.array, 3 );
  15. color2.toArray( axesHelper.geometry.attributes.color.array, 6 );
  16. color2.toArray( axesHelper.geometry.attributes.color.array, 9 );
  17. color3.toArray( axesHelper.geometry.attributes.color.array, 12 );
  18. color3.toArray( axesHelper.geometry.attributes.color.array, 15 );
  19. this.add( axesHelper );
  20. var posXAxisHelper = new THREE.Sprite( getAxisMaterial( color1, 'X' ) );
  21. var posYAxisHelper = new THREE.Sprite( getAxisMaterial( color2, 'Y' ) );
  22. var posZAxisHelper = new THREE.Sprite( getAxisMaterial( color3, 'Z' ) );
  23. var negXAxisHelper = new THREE.Sprite( getAxisMaterial( color1 ) );
  24. var negYAxisHelper = new THREE.Sprite( getAxisMaterial( color2 ) );
  25. var negZAxisHelper = new THREE.Sprite( getAxisMaterial( color3 ) );
  26. posXAxisHelper.position.x = 1;
  27. posYAxisHelper.position.y = 1;
  28. posZAxisHelper.position.z = 1;
  29. negXAxisHelper.position.x = - 1;
  30. negXAxisHelper.scale.setScalar( 0.8 );
  31. negYAxisHelper.position.y = - 1;
  32. negYAxisHelper.scale.setScalar( 0.8 );
  33. negZAxisHelper.position.z = - 1;
  34. negZAxisHelper.scale.setScalar( 0.8 );
  35. axesHelper.add( posXAxisHelper );
  36. axesHelper.add( posYAxisHelper );
  37. axesHelper.add( posZAxisHelper );
  38. axesHelper.add( negXAxisHelper );
  39. axesHelper.add( negYAxisHelper );
  40. axesHelper.add( negZAxisHelper );
  41. var point = new THREE.Vector3();
  42. var dim = 128;
  43. this.render = function ( renderer, editorCamera, container ) {
  44. this.quaternion.copy( editorCamera.quaternion ).inverse();
  45. this.updateMatrixWorld();
  46. point.set( 0, 0, 1 );
  47. point.applyQuaternion( editorCamera.quaternion );
  48. if ( point.x >= 0 ) {
  49. posXAxisHelper.material.opacity = 1;
  50. negXAxisHelper.material.opacity = 0.5;
  51. } else {
  52. posXAxisHelper.material.opacity = 0.5;
  53. negXAxisHelper.material.opacity = 1;
  54. }
  55. if ( point.y >= 0 ) {
  56. posYAxisHelper.material.opacity = 1;
  57. negYAxisHelper.material.opacity = 0.5;
  58. } else {
  59. posYAxisHelper.material.opacity = 0.5;
  60. negYAxisHelper.material.opacity = 1;
  61. }
  62. if ( point.z >= 0 ) {
  63. posZAxisHelper.material.opacity = 1;
  64. negZAxisHelper.material.opacity = 0.5;
  65. } else {
  66. posZAxisHelper.material.opacity = 0.5;
  67. negZAxisHelper.material.opacity = 1;
  68. }
  69. //
  70. var x = container.dom.offsetWidth - dim;
  71. renderer.clearDepth();
  72. renderer.setViewport( x, 0, dim, dim );
  73. renderer.render( this, camera );
  74. };
  75. function getAxisMaterial( color, text = null ) {
  76. var canvas = document.createElement( 'canvas' );
  77. canvas.width = 64;
  78. canvas.height = 64;
  79. var context = canvas.getContext( '2d' );
  80. context.beginPath();
  81. context.arc( 32, 32, 16, 0, 2 * Math.PI );
  82. context.closePath();
  83. context.fillStyle = color.getStyle();
  84. context.fill();
  85. if ( text !== null ) {
  86. context.font = '24px Arial';
  87. context.textAlign = 'center';
  88. context.fillStyle = '#000000';
  89. context.fillText( text, 32, 41 );
  90. }
  91. return new THREE.SpriteMaterial( {
  92. map: new THREE.CanvasTexture( canvas ),
  93. toneMapped: false
  94. } );
  95. }
  96. }
  97. ViewHelper.prototype = Object.assign( Object.create( THREE.Object3D.prototype ), {
  98. constructor: ViewHelper,
  99. isViewHelper: true
  100. } );
  101. export { ViewHelper };