Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. function rotateAroundWorldAxis( object, axis, radians ) {
  2.  
  3. var rotationMatrix = new THREE.Matrix4();
  4. rotationMatrix.makeRotationAxis( axis.normalize(), radians );
  5. rotationMatrix.multiply( object.matrix ); // pre-multiply
  6. object.matrix = rotationMatrix;
  7. object.rotation.setFromRotationMatrix(object.matrix);
  8. }
  9.  
  10. function render() {
  11.  
  12. var yAxis = new THREE.Vector3(0,1,0);
  13. rotateAroundWorldAxis(albero,yAxis,Math.PI / 180);
  14. requestAnimationFrame( render );
  15. renderer.render( scene, camera );
  16. }
  17.  
  18. var rotObjectMatrix;
  19. function rotateAroundObjectAxis(object, axis, radians) {
  20. rotObjectMatrix = new THREE.Matrix4();
  21. rotObjectMatrix.makeRotationAxis(axis.normalize(), radians);
  22. object.matrix.multiply(rotObjectMatrix);
  23. object.rotation.setFromRotationMatrix(object.matrix);
  24. }
  25.  
  26. function drawStuff() {
  27.  
  28.  
  29. var albero = new THREE.Object3D();
  30. var scene = new THREE.Scene();
  31. var camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 0.1, 1000 );
  32.  
  33. var renderer = new THREE.WebGLRenderer();
  34. renderer.setSize( window.innerWidth, window.innerHeight );
  35. document.body.appendChild( renderer.domElement );
  36.  
  37.  
  38. var cone = createCone(0, 0, 0); //create cone of the tree
  39. var cylinder = createCylinder(0, -1.1, 0); //create cylinder of the tree
  40. albero = createAlbero(-4, 2, 3);
  41.  
  42. scene.add(albero);
  43.  
  44. var axisHelper = new THREE.AxisHelper( 5 );
  45. scene.add( axisHelper );
  46.  
  47. camera.position.z = 20;
  48.  
  49.  
  50. function createCone(x, y, z){
  51. var coneGeometry = new THREE.CylinderGeometry(0.0, 0.7, 2, 32, 32);
  52. var coneMaterial = new THREE.MeshBasicMaterial( {color: 0xFF0000} );
  53. var cone = new THREE.Mesh( coneGeometry, coneMaterial );
  54. cone.position.set(x, y, z);
  55. return cone;
  56. }
  57.  
  58. function createCylinder(x, y, z){
  59.  
  60. var cylGeometry = new THREE.CylinderGeometry(0.1, 0.1, 0.4, 32, 32);
  61. var cylinderMaterial = new THREE.MeshBasicMaterial( {color: 0xffff00} );
  62. var cylinder = new THREE.Mesh( cylGeometry, cylinderMaterial );
  63. cylinder.position.set(x, y, z);
  64. return cylinder;
  65.  
  66. }
  67.  
  68. function createAlbero(x ,y, z){
  69.  
  70. albero.add(cone);
  71. albero.add(cylinder);
  72. albero.position.set(x ,y, z);
  73. return albero;
  74. }
  75.  
  76. var rotObjectMatrix;
  77. function rotateAroundObjectAxis(object, axis, radians) {
  78. rotObjectMatrix = new THREE.Matrix4();
  79. rotObjectMatrix.makeRotationAxis(axis.normalize(), radians);
  80.  
  81. //moltiplico la matrice dell'oggetto per la matrice di rotazione
  82. object.matrix.multiply(rotObjectMatrix);
  83.  
  84. object.rotation.setFromRotationMatrix(object.matrix);
  85. }
  86.  
  87.  
  88. function rotateAroundWorldAxis( object, axis, radians ) {
  89.  
  90. var rotationMatrix = new THREE.Matrix4();
  91.  
  92. rotationMatrix.makeRotationAxis( axis.normalize(), radians );
  93. rotationMatrix.multiply( object.matrix ); // pre-multiply
  94. object.matrix = rotationMatrix;
  95. object.rotation.setFromRotationMatrix(object.matrix);
  96. }
  97.  
  98.  
  99.  
  100. function render() {
  101.  
  102. var yAxis = new THREE.Vector3(30,50,1);
  103. rotateAroundWorldAxis(cone2,yAxis,Math.PI / 180);
  104. requestAnimationFrame( render );
  105. renderer.render( scene, camera );
  106. }
  107. render();
  108.  
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement