Trident.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @author sroucheray / http://sroucheray.org/
  3. */
  4. /**
  5. * @constructor
  6. * Three axis representing the cartesian coordinates
  7. * @param xAxisColor {number}
  8. * @param yAxisColor {number}
  9. * @param zAxisColor {number}
  10. * @param showArrows {Boolean}
  11. * @param length {number}
  12. * @param scale {number}
  13. *
  14. * @see THREE.Trident.defaultParams
  15. */
  16. THREE.Trident = function ( params /** Object */) {
  17. THREE.Object3D.call( this );
  18. var hPi = Math.PI / 2, cone;
  19. params = params || THREE.Trident.defaultParams;
  20. if(params !== THREE.Trident.defaultParams){
  21. for ( var key in THREE.Trident.defaultParams) {
  22. if(!params.hasOwnProperty(key)){
  23. params[key] = THREE.Trident.defaultParams[key];
  24. }
  25. }
  26. }
  27. this.scale = new THREE.Vector3( params.scale, params.scale, params.scale );
  28. this.addChild( getSegment( new THREE.Vector3(params.length,0,0), params.xAxisColor ) );
  29. this.addChild( getSegment( new THREE.Vector3(0,params.length,0), params.yAxisColor ) );
  30. this.addChild( getSegment( new THREE.Vector3(0,0,params.length), params.zAxisColor ) );
  31. if(params.showArrows){
  32. cone = getCone(params.xAxisColor);
  33. cone.rotation.y = - hPi;
  34. cone.position.x = params.length;
  35. this.addChild( cone );
  36. cone = getCone(params.yAxisColor);
  37. cone.rotation.x = hPi;
  38. cone.position.y = params.length;
  39. this.addChild( cone );
  40. cone = getCone(params.zAxisColor);
  41. cone.rotation.y = Math.PI;
  42. cone.position.z = params.length;
  43. this.addChild( cone );
  44. }
  45. function getCone ( color ) {
  46. //0.1 required to get a cone with a mapped bottom face
  47. return new THREE.Mesh( new THREE.Cylinder( 30, 0.1, params.length / 20, params.length / 5 ), new THREE.MeshBasicMaterial( { color : color } ) );
  48. }
  49. function getSegment ( point, color ){
  50. var geom = new THREE.Geometry();
  51. geom.vertices = [new THREE.Vertex(), new THREE.Vertex(point)];
  52. return new THREE.Line( geom, new THREE.MeshBasicMaterial( { color : color } ) );
  53. }
  54. };
  55. THREE.Trident.prototype = new THREE.Object3D();
  56. THREE.Trident.prototype.constructor = THREE.Trident;
  57. THREE.Trident.defaultParams = {
  58. xAxisColor : 0xFF0000,
  59. yAxisColor : 0x00FF00,
  60. zAxisColor : 0x0000FF,
  61. showArrows : true,
  62. length : 100,
  63. scale : 1
  64. };
粤ICP备19079148号