PathControls.js 7.9 KB

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