PathControls.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.PathControls = function ( object, domElement ) {
  5. this.object = object;
  6. this.domElement = ( domElement !== undefined ) ? domElement : document;
  7. this.id = "PathControls" + THREE.PathControlsIdCounter ++;
  8. // API
  9. this.duration = 10 * 1000; // milliseconds
  10. this.waypoints = [];
  11. this.useConstantSpeed = true;
  12. this.resamplingCoef = 50;
  13. this.debugPath = new THREE.Object3D();
  14. this.debugDummy = new THREE.Object3D();
  15. this.animationParent = new THREE.Object3D();
  16. this.lookSpeed = 0.005;
  17. this.lookVertical = true;
  18. this.lookHorizontal = true;
  19. this.verticalAngleMap = { srcRange: [ 0, 2 * Math.PI ], dstRange: [ 0, 2 * Math.PI ] };
  20. this.horizontalAngleMap = { srcRange: [ 0, 2 * Math.PI ], dstRange: [ 0, 2 * Math.PI ] };
  21. // internals
  22. this.target = new THREE.Object3D();
  23. this.mouseX = 0;
  24. this.mouseY = 0;
  25. this.lat = 0;
  26. this.lon = 0;
  27. this.phi = 0;
  28. this.theta = 0;
  29. var PI2 = Math.PI * 2;
  30. this.viewHalfX = 0;
  31. this.viewHalfY = 0;
  32. if ( this.domElement !== document ) {
  33. this.domElement.setAttribute( 'tabindex', -1 );
  34. }
  35. // methods
  36. this.handleResize = function () {
  37. if ( this.domElement === document ) {
  38. this.viewHalfX = window.innerWidth / 2;
  39. this.viewHalfY = window.innerHeight / 2;
  40. } else {
  41. this.viewHalfX = this.domElement.offsetWidth / 2;
  42. this.viewHalfY = this.domElement.offsetHeight / 2;
  43. }
  44. };
  45. this.update = function ( delta ) {
  46. var srcRange, dstRange;
  47. if( this.lookHorizontal ) this.lon += this.mouseX * this.lookSpeed * delta;
  48. if( this.lookVertical ) this.lat -= this.mouseY * this.lookSpeed * delta;
  49. this.lon = Math.max( 0, Math.min( 360, this.lon ) );
  50. this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
  51. this.phi = THREE.Math.degToRad( 90 - this.lat );
  52. this.theta = THREE.Math.degToRad( this.lon );
  53. this.phi = normalize_angle_rad( this.phi );
  54. // constrain vertical look angle
  55. srcRange = this.verticalAngleMap.srcRange;
  56. dstRange = this.verticalAngleMap.dstRange;
  57. var tmpPhi = THREE.Math.mapLinear( this.phi, srcRange[ 0 ], srcRange[ 1 ], dstRange[ 0 ], dstRange[ 1 ] );
  58. var tmpPhiFullRange = dstRange[ 1 ] - dstRange[ 0 ];
  59. var tmpPhiNormalized = ( tmpPhi - dstRange[ 0 ] ) / tmpPhiFullRange;
  60. this.phi = QuadraticEaseInOut( tmpPhiNormalized ) * tmpPhiFullRange + dstRange[ 0 ];
  61. // constrain horizontal look angle
  62. srcRange = this.horizontalAngleMap.srcRange;
  63. dstRange = this.horizontalAngleMap.dstRange;
  64. var tmpTheta = THREE.Math.mapLinear( this.theta, srcRange[ 0 ], srcRange[ 1 ], dstRange[ 0 ], dstRange[ 1 ] );
  65. var tmpThetaFullRange = dstRange[ 1 ] - dstRange[ 0 ];
  66. var tmpThetaNormalized = ( tmpTheta - dstRange[ 0 ] ) / tmpThetaFullRange;
  67. this.theta = QuadraticEaseInOut( tmpThetaNormalized ) * tmpThetaFullRange + dstRange[ 0 ];
  68. var targetPosition = this.target.position,
  69. position = this.object.position;
  70. targetPosition.x = 100 * Math.sin( this.phi ) * Math.cos( this.theta );
  71. targetPosition.y = 100 * Math.cos( this.phi );
  72. targetPosition.z = 100 * Math.sin( this.phi ) * Math.sin( this.theta );
  73. this.object.lookAt( this.target.position );
  74. };
  75. this.onMouseMove = function ( event ) {
  76. if ( this.domElement === document ) {
  77. this.mouseX = event.pageX - this.viewHalfX;
  78. this.mouseY = event.pageY - this.viewHalfY;
  79. } else {
  80. this.mouseX = event.pageX - this.domElement.offsetLeft - this.viewHalfX;
  81. this.mouseY = event.pageY - this.domElement.offsetTop - this.viewHalfY;
  82. }
  83. };
  84. // utils
  85. function normalize_angle_rad( a ) {
  86. var b = a % PI2;
  87. return b >= 0 ? b : b + PI2;
  88. };
  89. function distance( a, b ) {
  90. var dx = a[ 0 ] - b[ 0 ],
  91. dy = a[ 1 ] - b[ 1 ],
  92. dz = a[ 2 ] - b[ 2 ];
  93. return Math.sqrt( dx * dx + dy * dy + dz * dz );
  94. };
  95. function QuadraticEaseInOut ( k ) {
  96. if ( ( k *= 2 ) < 1 ) return 0.5 * k * k;
  97. return - 0.5 * ( --k * ( k - 2 ) - 1 );
  98. };
  99. function bind( scope, fn ) {
  100. return function () {
  101. fn.apply( scope, arguments );
  102. };
  103. };
  104. function initAnimationPath( parent, spline, name, duration ) {
  105. var animationData = {
  106. name: name,
  107. fps: 0.6,
  108. length: duration,
  109. hierarchy: []
  110. };
  111. var i,
  112. parentAnimation, childAnimation,
  113. path = spline.getControlPointsArray(),
  114. sl = spline.getLength(),
  115. pl = path.length,
  116. t = 0,
  117. first = 0,
  118. last = pl - 1;
  119. parentAnimation = { parent: -1, keys: [] };
  120. parentAnimation.keys[ first ] = { time: 0, pos: path[ first ], rot: [ 0, 0, 0, 1 ], scl: [ 1, 1, 1 ] };
  121. parentAnimation.keys[ last ] = { time: duration, pos: path[ last ], rot: [ 0, 0, 0, 1 ], scl: [ 1, 1, 1 ] };
  122. for ( i = 1; i < pl - 1; i++ ) {
  123. // real distance (approximation via linear segments)
  124. t = duration * sl.chunks[ i ] / sl.total;
  125. // equal distance
  126. //t = duration * ( i / pl );
  127. // linear distance
  128. //t += duration * distance( path[ i ], path[ i - 1 ] ) / sl.total;
  129. parentAnimation.keys[ i ] = { time: t, pos: path[ i ] };
  130. }
  131. animationData.hierarchy[ 0 ] = parentAnimation;
  132. THREE.AnimationHandler.add( animationData );
  133. var animation = new THREE.Animation( parent, name );
  134. animation.interpolationType = THREE.AnimationHandler.CATMULLROM_FORWARD;
  135. return animation;
  136. };
  137. function createSplineGeometry( spline, n_sub ) {
  138. var i, index, position,
  139. geometry = new THREE.Geometry();
  140. for ( i = 0; i < spline.points.length * n_sub; i ++ ) {
  141. index = i / ( spline.points.length * n_sub );
  142. position = spline.getPoint( index );
  143. geometry.vertices[ i ] = new THREE.Vector3( position.x, position.y, position.z );
  144. }
  145. return geometry;
  146. };
  147. function createPath( parent, spline ) {
  148. var lineGeo = createSplineGeometry( spline, 10 ),
  149. particleGeo = createSplineGeometry( spline, 10 ),
  150. lineMat = new THREE.LineBasicMaterial( { color: 0xff0000, linewidth: 3 } ),
  151. lineObj = new THREE.Line( lineGeo, lineMat ),
  152. particleObj = new THREE.PointCloud( particleGeo, new THREE.PointCloudMaterial( { color: 0xffaa00, size: 3 } ) );
  153. lineObj.scale.set( 1, 1, 1 );
  154. parent.add( lineObj );
  155. particleObj.scale.set( 1, 1, 1 );
  156. parent.add( particleObj );
  157. var waypoint,
  158. geo = new THREE.SphereGeometry( 1, 16, 8 ),
  159. mat = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
  160. for ( var i = 0; i < spline.points.length; i ++ ) {
  161. waypoint = new THREE.Mesh( geo, mat );
  162. waypoint.position.copy( spline.points[ i ] );
  163. parent.add( waypoint );
  164. }
  165. };
  166. this.init = function ( ) {
  167. // constructor
  168. this.spline = new THREE.Spline();
  169. this.spline.initFromArray( this.waypoints );
  170. if ( this.useConstantSpeed ) {
  171. this.spline.reparametrizeByArcLength( this.resamplingCoef );
  172. }
  173. if ( this.createDebugDummy ) {
  174. var dummyParentMaterial = new THREE.MeshLambertMaterial( { color: 0x0077ff } ),
  175. dummyChildMaterial = new THREE.MeshLambertMaterial( { color: 0x00ff00 } ),
  176. dummyParentGeo = new THREE.BoxGeometry( 10, 10, 20 ),
  177. dummyChildGeo = new THREE.BoxGeometry( 2, 2, 10 );
  178. this.animationParent = new THREE.Mesh( dummyParentGeo, dummyParentMaterial );
  179. var dummyChild = new THREE.Mesh( dummyChildGeo, dummyChildMaterial );
  180. dummyChild.position.set( 0, 10, 0 );
  181. this.animation = initAnimationPath( this.animationParent, this.spline, this.id, this.duration );
  182. this.animationParent.add( this.object );
  183. this.animationParent.add( this.target );
  184. this.animationParent.add( dummyChild );
  185. } else {
  186. this.animation = initAnimationPath( this.animationParent, this.spline, this.id, this.duration );
  187. this.animationParent.add( this.target );
  188. this.animationParent.add( this.object );
  189. }
  190. if ( this.createDebugPath ) {
  191. createPath( this.debugPath, this.spline );
  192. }
  193. this.domElement.addEventListener( 'mousemove', bind( this, this.onMouseMove ), false );
  194. };
  195. this.handleResize();
  196. };
  197. THREE.PathControlsIdCounter = 0;