Guest User

Untitled

a guest
Jan 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. // GLOBALS - ALLOCATE THESE OUTSIDE OF THE RENDER LOOP - CHANGED
  2. var cubes = [], marker, spline;
  3. var matrix = new THREE.Matrix4();
  4. var up = new THREE.Vector3( 0, 1, 0 );
  5. var axis = new THREE.Vector3( );
  6. var pt, radians, axis, tangent, path;
  7.  
  8. // the getPoint starting variable - !important - You get me ;)
  9. var t = 0;
  10.  
  11. //This function generates the cube and chooses a random color for it
  12. //on intial load.
  13.  
  14. function getCube(){
  15. // cube mats and cube
  16. var mats = [];
  17. for (var i = 0; i < 6; i ++) {
  18. mats.push(new THREE.MeshBasicMaterial({color:Math.random()*0xffffff}));
  19. }
  20.  
  21. var cube = new THREE.Mesh(
  22. new THREE.CubeGeometry(2, 2, 2),
  23. new THREE.MeshFaceMaterial( mats )
  24. );
  25.  
  26. return cube
  27. }
  28.  
  29. // Ellipse class, which extends the virtual base class Curve
  30. function Ellipse( xRadius, yRadius ) {
  31.  
  32. THREE.Curve.call( this );
  33.  
  34. // add radius as a property
  35. this.xRadius = xRadius;
  36. this.yRadius = yRadius;
  37.  
  38. }
  39.  
  40. Ellipse.prototype = Object.create( THREE.Curve.prototype );
  41. Ellipse.prototype.constructor = Ellipse;
  42.  
  43. // define the getPoint function for the subClass
  44. Ellipse.prototype.getPoint = function ( t ) {
  45.  
  46. var radians = 2 * Math.PI * t;
  47.  
  48. return new THREE.Vector3( this.xRadius * Math.cos( radians ),
  49. this.yRadius * Math.sin( radians ),
  50. 0 );
  51.  
  52. };
  53.  
  54. //
  55.  
  56. var mesh, renderer, scene, camera, controls;
  57.  
  58.  
  59. function init() {
  60.  
  61. // renderer
  62. renderer = new THREE.WebGLRenderer();
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. document.body.appendChild( renderer.domElement );
  65.  
  66. // scene
  67. scene = new THREE.Scene();
  68.  
  69. // camera
  70. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  71. camera.position.set( 20, 20, 20 );
  72.  
  73. // controls
  74. controls = new THREE.OrbitControls( camera, renderer.domElement );
  75. controls.addEventListener( 'change', render ); // use if there is no animation loop
  76. controls.minDistance = 10;
  77. controls.maxDistance = 50;
  78.  
  79. // light
  80. var light = new THREE.PointLight( 0xffffff, 0.7 );
  81. camera.add( light );
  82. scene.add( camera ); // add to scene only because the camera has a child
  83.  
  84. // axes
  85. scene.add( new THREE.AxisHelper( 20 ) );
  86.  
  87.  
  88. ////////////////////////////////////////
  89. // Create the cube //
  90. ////////////////////////////////////////
  91.  
  92. marker = getCube();
  93. marker.position.set(0,0,0);
  94. scene.add(marker);
  95.  
  96.  
  97. ////////////////////////////////////////
  98. // Create an Extruded shape //
  99. ////////////////////////////////////////
  100.  
  101. // path
  102. path = new Ellipse( 5, 10 );
  103.  
  104. // params
  105. var pathSegments = 64;
  106. var tubeRadius = 0.5;
  107. var radiusSegments = 16;
  108. var closed = true;
  109.  
  110. var geometry = new THREE.TubeBufferGeometry( path, pathSegments, tubeRadius, radiusSegments, closed );
  111.  
  112. // material
  113. var material = new THREE.MeshPhongMaterial( {
  114. color: 0x0080ff,
  115. } );
  116.  
  117. // mesh
  118. mesh = new THREE.Mesh( geometry, material );
  119. scene.add( mesh );
  120.  
  121. //////////////////////////////////////////////////////////////////////////
  122. // Create the path which is based on our shape above //
  123. //////////////////////////////////////////////////////////////////////////
  124.  
  125. //Please note that this red ellipse was only created has a guide so that I could be certain that the square is true to the tangent and positioning.
  126.  
  127. // Ellipse class, which extends the virtual base class Curve
  128. var curve = new THREE.EllipseCurve(
  129. 0, 0, // ax, aY
  130. 6, 11, // xRadius, yRadius
  131. 0, 2 * Math.PI, // aStartAngle, aEndAngle
  132. false, // aClockwise
  133. 0 // aRotation
  134. );
  135.  
  136. //defines the amount of points the path will have
  137. var path2 = new THREE.Path( curve.getPoints( 100 ) );
  138. geometrycirc = path2.createPointsGeometry( 100 );
  139. var materialcirc = new THREE.LineBasicMaterial( {
  140. color : 0xff0000
  141. } );
  142.  
  143. // Create the final object to add to the scene
  144. var ellipse = new THREE.Line( geometrycirc, materialcirc );
  145. ellipse.position.set(0,0,0);
  146. scene.add( ellipse );
  147.  
  148. }
  149.  
  150. function animate() {
  151. requestAnimationFrame(animate);
  152.  
  153. render();
  154. }
  155.  
  156.  
  157. function render() {
  158.  
  159. // set the marker position
  160. pt = path.getPoint( t );
  161.  
  162. // set the marker position
  163. marker.position.set( pt.x, pt.y, pt.z );
  164.  
  165. // get the tangent to the curve
  166. tangent = path.getTangent( t ).normalize();
  167.  
  168. // calculate the axis to rotate around
  169. axis.crossVectors( up, tangent ).normalize();
  170.  
  171. // calcluate the angle between the up vector and the tangent
  172. radians = Math.acos( up.dot( tangent ) );
  173.  
  174. // set the quaternion
  175. marker.quaternion.setFromAxisAngle( axis, radians );
  176.  
  177. t = (t >= 1) ? 0 : t += 0.002;
  178.  
  179. renderer.render( scene, camera );
  180.  
  181. }
  182.  
  183. init();
  184. animate();
Add Comment
Please, Sign In to add comment