Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. //FUNCTION TO CREATE A CYLINDER
  2. function create_cylinder(radiusTop,radiusBottom, height, segmentsRadius, segmentsHeight, openEnded, color)
  3. {
  4. var material = new THREE.MeshLambertMaterial({
  5. color: color, //0x0000ff
  6. opacity: 0.2
  7. });
  8. var cylinder = new THREE.Mesh(new THREE.CylinderGeometry(radiusTop,radiusBottom, height, segmentsRadius, segmentsHeight, openEnded), material);
  9.  
  10. cylinder.overdraw = true;
  11.  
  12. return cylinder;
  13. }
  14.  
  15. //ALIGN THE CYLINDER TO A GIVEN VECTOR
  16. var alignVector=new THREE.Vector3(-50,50,50); //the vector to be aligned with
  17.  
  18. var newcylinder = create_cylinder(0.1, 10, 40, 50, 50, false, "0x0ff0f0"); // the object to be aligned with the vector above
  19.  
  20. var cylinderQuaternion = new THREE.Quaternion();
  21. cylinderQuaternion.setFromEuler(alignVector);
  22. newcylinder.useQuaternion = true;
  23. newcylinder.quaternion=cylinderQuaternion;
  24.  
  25. scatterPlot.add(newcylinder);
  26.  
  27. var vector = THREE.Vector(100, 60, 20);
  28.  
  29. var geometry = new THREE.CylinderGeometry(2, 2, vector.length(), 4, 4);
  30. var mesh = new THREE.Mesh(geometry, someMaterial);
  31. var axis = new THREE.Vector3(0, 1, 0);
  32. mesh.quaternion.setFromUnitVectors(axis, vector.clone().normalize());
  33.  
  34. mesh.position.copy(vector.clone().multiplyScalar(0.5));
  35.  
  36. //Mesh to align
  37. var material = new THREE.MeshLambertMaterial({color: 0x0000ff});
  38. var cylinder = new THREE.Mesh(new THREE.CylinderGeometry(10, 10, 15), material);
  39.  
  40. //vector to align to
  41. var vector = new THREE.Vector3(
  42. 5,//x
  43. 10,//y
  44. 15 //z
  45. );
  46.  
  47. //create a point to lookAt
  48. var focalPoint = new THREE.Vector3(
  49. cylinder.position.x + vector.x,
  50. cylinder.position.y + vector.y,
  51. cylinder.position.z + vector.z
  52. );
  53.  
  54. //all that remains is setting the up vector (if needed) and use lookAt
  55. cylinder.up = new THREE.Vector3(0,0,1);//Z axis up
  56. cylinder.lookAt(focalPoint);
  57.  
  58. var HALF_PI = -Math.PI * .5;
  59. var p1 = new THREE.Vector3(Math.random()-.5,Math.random()-.5,Math.random()-.5).multiplyScalar(30);
  60. var p2 = new THREE.Vector3(Math.random(),Math.random(),Math.random()).multiplyScalar(300);
  61. var halfLength = diff.length() * .5;
  62.  
  63. var c = new THREE.CylinderGeometry(10, 10, halfLength * 2, 12, 1, false );
  64. var orientation = new THREE.Matrix4();
  65. orientation.setRotationFromEuler(new THREE.Vector3(HALF_PI,0,0));//rotate on X 90 degrees
  66. orientation.setPosition(new THREE.Vector3(0,0,halfLength));//move half way on Z, since default pivot is at centre
  67. c.applyMatrix(orientation);//apply transformation for geometry
  68.  
  69. var m = new THREE.Mesh( c, new THREE.MeshLambertMaterial( { color: 0x009900, wireframe: true, shading: THREE.FlatShading } ) );
  70. scene.add(m);
  71. m.lookAt(p2);//tell mesh to orient itself towards p2
  72. //just for debugging - to illustrate orientation
  73. m.add(new THREE.Axes());
  74.  
  75. //visualize p1,p2 vectors
  76. var PI2 = Math.PI * 2;
  77. var program = function ( context ) {
  78.  
  79. context.beginPath();
  80. context.arc( 0, 0, 1, 0, PI2, true );
  81. context.closePath();
  82. context.fill();
  83.  
  84. }
  85.  
  86. particleMaterial = new THREE.ParticleCanvasMaterial( { color: 0x990000, program: program } );
  87. var pp1 = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: 0x990000, program: program } ) );
  88. pp1.scale.multiplyScalar(10);
  89. pp1.position.copy(p1);
  90. scene.add( pp1 );
  91. var pp2 = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: 0x009900, program: program } ) );
  92. pp2.scale.multiplyScalar(10);
  93. pp2.position.copy(p2);
  94. scene.add( pp2 );
  95.  
  96. plane.add(getCylinderBetweenPoints(p1,p2,new THREE.MeshLambertMaterial( { color: 0x009900, wireframe: true, shading: THREE.FlatShading } )));
  97.  
  98. function getCylinderBetweenPoints(point1,point2,material){
  99. var HALF_PI = -Math.PI * .5;
  100. var diff = new THREE.Vector3().sub(point1,point2);//delta vector
  101. var halfLength = diff.length() * .5;
  102. var c = new THREE.CylinderGeometry(10, 10, halfLength * 2, 12, 1, false );
  103. var orientation = new THREE.Matrix4();//a new orientation matrix to offset pivot
  104. var offsetRotation = new THREE.Matrix4();//a matrix to fix pivot rotation
  105. var offsetPosition = new THREE.Matrix4();//a matrix to fix pivot position
  106. orientation.lookAt(point1,point2,new THREE.Vector3(0,1,0));//look at destination
  107. offsetRotation.setRotationX(HALF_PI);//rotate 90 degs on X
  108. offsetPosition.setPosition(new THREE.Vector3(-point1.x,diff.length()*.5+point1.z,point1.y*.5));//move by pivot offset on Y
  109. orientation.multiplySelf(offsetRotation);//combine orientation with rotation transformations
  110. orientation.multiplySelf(offsetPosition);//combine orientation with position transformations
  111. c.applyMatrix(orientation);//apply the final matrix
  112. var m = new THREE.Mesh( c, material );
  113. m.add(new THREE.Axes());
  114. return m;
  115. }
  116.  
  117. var PI2 = Math.PI * 2;
  118. var program = function ( context ) {
  119.  
  120. context.beginPath();
  121. context.arc( 0, 0, 1, 0, PI2, true );
  122. context.closePath();
  123. context.fill();
  124.  
  125. }
  126.  
  127. //visualize p1,p2 vectors
  128. particleMaterial = new THREE.ParticleCanvasMaterial( { color: 0x990000, program: program } );
  129. var pp1 = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: 0x990000, program: program } ) );
  130. pp1.scale.multiplyScalar(10);
  131. pp1.position.copy(p1);
  132. plane.add( pp1 );
  133. var pp2 = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: 0x009900, program: program } ) );
  134. pp2.scale.multiplyScalar(10);
  135. pp2.position.copy(p2);
  136. plane.add( pp2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement