TransformControlsPlane.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @author arodic / https://github.com/arodic
  3. */
  4. THREE.TransformControlsPlane = function () {
  5. 'use strict';
  6. THREE.Mesh.call( this,
  7. new THREE.PlaneBufferGeometry( 1000, 1000, 2, 2 ),
  8. new THREE.MeshBasicMaterial( { visible: false, wireframe: true, side: THREE.DoubleSide, transparent: true, opacity: 0.1 } )
  9. );
  10. this.type = 'TransformControlsPlane';
  11. var unitX = new THREE.Vector3( 1, 0, 0 );
  12. var unitY = new THREE.Vector3( 0, 1, 0 );
  13. var unitZ = new THREE.Vector3( 0, 0, 1 );
  14. var dirVector = new THREE.Vector3();
  15. var alignVector = new THREE.Vector3();
  16. var tempMatrix = new THREE.Matrix4();
  17. var camRotation = new THREE.Euler();
  18. this.update = function( rotation, eye ) {
  19. var axis = this.parent.axis;
  20. var mode = this.parent.mode;
  21. unitX.set( 1, 0, 0 ).applyEuler( rotation );
  22. unitY.set( 0, 1, 0 ).applyEuler( rotation );
  23. unitZ.set( 0, 0, 1 ).applyEuler( rotation );
  24. alignVector.copy( unitY );
  25. switch ( mode ) {
  26. case 'translate':
  27. case 'scale':
  28. switch ( axis ) {
  29. case 'X':
  30. alignVector.copy( eye ).cross( unitX );
  31. dirVector.copy( unitX ).cross( alignVector );
  32. break;
  33. case 'Y':
  34. alignVector.copy( eye ).cross( unitY );
  35. dirVector.copy( unitY ).cross( alignVector );
  36. break;
  37. case 'Z':
  38. alignVector.copy( eye ).cross( unitZ );
  39. dirVector.copy( unitZ ).cross( alignVector );
  40. break;
  41. case 'XY':
  42. dirVector.copy( unitZ );
  43. break;
  44. case 'YZ':
  45. dirVector.copy( unitX );
  46. break;
  47. case 'XZ':
  48. dirVector.copy( unitY );
  49. break;
  50. case 'XYZ':
  51. case 'E':
  52. dirVector.set( 0,0,0 );
  53. break;
  54. }
  55. break;
  56. case 'rotate':
  57. default:
  58. switch ( axis ) {
  59. // case 'X':
  60. // dirVector.copy( unitX );
  61. // break;
  62. // case 'Y':
  63. // dirVector.copy( unitY );
  64. // break;
  65. // case 'Z':
  66. // dirVector.copy( unitZ );
  67. // break;
  68. default:
  69. dirVector.set( 0,0,0 );
  70. break;
  71. }
  72. break;
  73. }
  74. if ( dirVector.length() === 0 ) {
  75. var camRotation = new THREE.Euler();
  76. // camRotation.setFromRotationMatrix( _tempMatrix.extractRotation( camera.matrixWorld ) );
  77. camRotation.setFromRotationMatrix( this.parent.camera.matrixWorld );
  78. this.quaternion.setFromEuler( camRotation );
  79. } else {
  80. tempMatrix.lookAt( this.position, dirVector, alignVector );
  81. this.quaternion.setFromRotationMatrix( tempMatrix );
  82. }
  83. this.updateMatrixWorld();
  84. };
  85. };
  86. THREE.TransformControlsPlane.prototype = Object.assign( Object.create( THREE.Mesh.prototype ), {
  87. constructor: THREE.TransformControlsPlane,
  88. isTransformControlsPlane: true
  89. } );