RollerCoaster.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. ( function () {
  2. class RollerCoasterGeometry extends THREE.BufferGeometry {
  3. constructor( curve, divisions ) {
  4. super();
  5. const vertices = [];
  6. const normals = [];
  7. const colors = [];
  8. const color1 = [ 1, 1, 1 ];
  9. const color2 = [ 1, 1, 0 ];
  10. const up = new THREE.Vector3( 0, 1, 0 );
  11. const forward = new THREE.Vector3();
  12. const right = new THREE.Vector3();
  13. const quaternion = new THREE.Quaternion();
  14. const prevQuaternion = new THREE.Quaternion();
  15. prevQuaternion.setFromAxisAngle( up, Math.PI / 2 );
  16. const point = new THREE.Vector3();
  17. const prevPoint = new THREE.Vector3();
  18. prevPoint.copy( curve.getPointAt( 0 ) ); // shapes
  19. const step = [ new THREE.Vector3( - 0.225, 0, 0 ), new THREE.Vector3( 0, - 0.050, 0 ), new THREE.Vector3( 0, - 0.175, 0 ), new THREE.Vector3( 0, - 0.050, 0 ), new THREE.Vector3( 0.225, 0, 0 ), new THREE.Vector3( 0, - 0.175, 0 ) ];
  20. const PI2 = Math.PI * 2;
  21. let sides = 5;
  22. const tube1 = [];
  23. for ( let i = 0; i < sides; i ++ ) {
  24. const angle = i / sides * PI2;
  25. tube1.push( new THREE.Vector3( Math.sin( angle ) * 0.06, Math.cos( angle ) * 0.06, 0 ) );
  26. }
  27. sides = 6;
  28. const tube2 = [];
  29. for ( let i = 0; i < sides; i ++ ) {
  30. const angle = i / sides * PI2;
  31. tube2.push( new THREE.Vector3( Math.sin( angle ) * 0.025, Math.cos( angle ) * 0.025, 0 ) );
  32. }
  33. const vector = new THREE.Vector3();
  34. const normal = new THREE.Vector3();
  35. function drawShape( shape, color ) {
  36. normal.set( 0, 0, - 1 ).applyQuaternion( quaternion );
  37. for ( let j = 0; j < shape.length; j ++ ) {
  38. vector.copy( shape[ j ] );
  39. vector.applyQuaternion( quaternion );
  40. vector.add( point );
  41. vertices.push( vector.x, vector.y, vector.z );
  42. normals.push( normal.x, normal.y, normal.z );
  43. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  44. }
  45. normal.set( 0, 0, 1 ).applyQuaternion( quaternion );
  46. for ( let j = shape.length - 1; j >= 0; j -- ) {
  47. vector.copy( shape[ j ] );
  48. vector.applyQuaternion( quaternion );
  49. vector.add( point );
  50. vertices.push( vector.x, vector.y, vector.z );
  51. normals.push( normal.x, normal.y, normal.z );
  52. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  53. }
  54. }
  55. const vector1 = new THREE.Vector3();
  56. const vector2 = new THREE.Vector3();
  57. const vector3 = new THREE.Vector3();
  58. const vector4 = new THREE.Vector3();
  59. const normal1 = new THREE.Vector3();
  60. const normal2 = new THREE.Vector3();
  61. const normal3 = new THREE.Vector3();
  62. const normal4 = new THREE.Vector3();
  63. function extrudeShape( shape, offset, color ) {
  64. for ( let j = 0, jl = shape.length; j < jl; j ++ ) {
  65. const point1 = shape[ j ];
  66. const point2 = shape[ ( j + 1 ) % jl ];
  67. vector1.copy( point1 ).add( offset );
  68. vector1.applyQuaternion( quaternion );
  69. vector1.add( point );
  70. vector2.copy( point2 ).add( offset );
  71. vector2.applyQuaternion( quaternion );
  72. vector2.add( point );
  73. vector3.copy( point2 ).add( offset );
  74. vector3.applyQuaternion( prevQuaternion );
  75. vector3.add( prevPoint );
  76. vector4.copy( point1 ).add( offset );
  77. vector4.applyQuaternion( prevQuaternion );
  78. vector4.add( prevPoint );
  79. vertices.push( vector1.x, vector1.y, vector1.z );
  80. vertices.push( vector2.x, vector2.y, vector2.z );
  81. vertices.push( vector4.x, vector4.y, vector4.z );
  82. vertices.push( vector2.x, vector2.y, vector2.z );
  83. vertices.push( vector3.x, vector3.y, vector3.z );
  84. vertices.push( vector4.x, vector4.y, vector4.z ); //
  85. normal1.copy( point1 );
  86. normal1.applyQuaternion( quaternion );
  87. normal1.normalize();
  88. normal2.copy( point2 );
  89. normal2.applyQuaternion( quaternion );
  90. normal2.normalize();
  91. normal3.copy( point2 );
  92. normal3.applyQuaternion( prevQuaternion );
  93. normal3.normalize();
  94. normal4.copy( point1 );
  95. normal4.applyQuaternion( prevQuaternion );
  96. normal4.normalize();
  97. normals.push( normal1.x, normal1.y, normal1.z );
  98. normals.push( normal2.x, normal2.y, normal2.z );
  99. normals.push( normal4.x, normal4.y, normal4.z );
  100. normals.push( normal2.x, normal2.y, normal2.z );
  101. normals.push( normal3.x, normal3.y, normal3.z );
  102. normals.push( normal4.x, normal4.y, normal4.z );
  103. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  104. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  105. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  106. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  107. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  108. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  109. }
  110. }
  111. const offset = new THREE.Vector3();
  112. for ( let i = 1; i <= divisions; i ++ ) {
  113. point.copy( curve.getPointAt( i / divisions ) );
  114. up.set( 0, 1, 0 );
  115. forward.subVectors( point, prevPoint ).normalize();
  116. right.crossVectors( up, forward ).normalize();
  117. up.crossVectors( forward, right );
  118. const angle = Math.atan2( forward.x, forward.z );
  119. quaternion.setFromAxisAngle( up, angle );
  120. if ( i % 2 === 0 ) {
  121. drawShape( step, color2 );
  122. }
  123. extrudeShape( tube1, offset.set( 0, - 0.125, 0 ), color2 );
  124. extrudeShape( tube2, offset.set( 0.2, 0, 0 ), color1 );
  125. extrudeShape( tube2, offset.set( - 0.2, 0, 0 ), color1 );
  126. prevPoint.copy( point );
  127. prevQuaternion.copy( quaternion );
  128. } // console.log( vertices.length );
  129. this.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  130. this.setAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
  131. this.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 3 ) );
  132. }
  133. }
  134. class RollerCoasterLiftersGeometry extends THREE.BufferGeometry {
  135. constructor( curve, divisions ) {
  136. super();
  137. const vertices = [];
  138. const normals = [];
  139. const quaternion = new THREE.Quaternion();
  140. const up = new THREE.Vector3( 0, 1, 0 );
  141. const point = new THREE.Vector3();
  142. const tangent = new THREE.Vector3(); // shapes
  143. const tube1 = [ new THREE.Vector3( 0, 0.05, - 0.05 ), new THREE.Vector3( 0, 0.05, 0.05 ), new THREE.Vector3( 0, - 0.05, 0 ) ];
  144. const tube2 = [ new THREE.Vector3( - 0.05, 0, 0.05 ), new THREE.Vector3( - 0.05, 0, - 0.05 ), new THREE.Vector3( 0.05, 0, 0 ) ];
  145. const tube3 = [ new THREE.Vector3( 0.05, 0, - 0.05 ), new THREE.Vector3( 0.05, 0, 0.05 ), new THREE.Vector3( - 0.05, 0, 0 ) ];
  146. const vector1 = new THREE.Vector3();
  147. const vector2 = new THREE.Vector3();
  148. const vector3 = new THREE.Vector3();
  149. const vector4 = new THREE.Vector3();
  150. const normal1 = new THREE.Vector3();
  151. const normal2 = new THREE.Vector3();
  152. const normal3 = new THREE.Vector3();
  153. const normal4 = new THREE.Vector3();
  154. function extrudeShape( shape, fromPoint, toPoint ) {
  155. for ( let j = 0, jl = shape.length; j < jl; j ++ ) {
  156. const point1 = shape[ j ];
  157. const point2 = shape[ ( j + 1 ) % jl ];
  158. vector1.copy( point1 );
  159. vector1.applyQuaternion( quaternion );
  160. vector1.add( fromPoint );
  161. vector2.copy( point2 );
  162. vector2.applyQuaternion( quaternion );
  163. vector2.add( fromPoint );
  164. vector3.copy( point2 );
  165. vector3.applyQuaternion( quaternion );
  166. vector3.add( toPoint );
  167. vector4.copy( point1 );
  168. vector4.applyQuaternion( quaternion );
  169. vector4.add( toPoint );
  170. vertices.push( vector1.x, vector1.y, vector1.z );
  171. vertices.push( vector2.x, vector2.y, vector2.z );
  172. vertices.push( vector4.x, vector4.y, vector4.z );
  173. vertices.push( vector2.x, vector2.y, vector2.z );
  174. vertices.push( vector3.x, vector3.y, vector3.z );
  175. vertices.push( vector4.x, vector4.y, vector4.z ); //
  176. normal1.copy( point1 );
  177. normal1.applyQuaternion( quaternion );
  178. normal1.normalize();
  179. normal2.copy( point2 );
  180. normal2.applyQuaternion( quaternion );
  181. normal2.normalize();
  182. normal3.copy( point2 );
  183. normal3.applyQuaternion( quaternion );
  184. normal3.normalize();
  185. normal4.copy( point1 );
  186. normal4.applyQuaternion( quaternion );
  187. normal4.normalize();
  188. normals.push( normal1.x, normal1.y, normal1.z );
  189. normals.push( normal2.x, normal2.y, normal2.z );
  190. normals.push( normal4.x, normal4.y, normal4.z );
  191. normals.push( normal2.x, normal2.y, normal2.z );
  192. normals.push( normal3.x, normal3.y, normal3.z );
  193. normals.push( normal4.x, normal4.y, normal4.z );
  194. }
  195. }
  196. const fromPoint = new THREE.Vector3();
  197. const toPoint = new THREE.Vector3();
  198. for ( let i = 1; i <= divisions; i ++ ) {
  199. point.copy( curve.getPointAt( i / divisions ) );
  200. tangent.copy( curve.getTangentAt( i / divisions ) );
  201. const angle = Math.atan2( tangent.x, tangent.z );
  202. quaternion.setFromAxisAngle( up, angle ); //
  203. if ( point.y > 10 ) {
  204. fromPoint.set( - 0.75, - 0.35, 0 );
  205. fromPoint.applyQuaternion( quaternion );
  206. fromPoint.add( point );
  207. toPoint.set( 0.75, - 0.35, 0 );
  208. toPoint.applyQuaternion( quaternion );
  209. toPoint.add( point );
  210. extrudeShape( tube1, fromPoint, toPoint );
  211. fromPoint.set( - 0.7, - 0.3, 0 );
  212. fromPoint.applyQuaternion( quaternion );
  213. fromPoint.add( point );
  214. toPoint.set( - 0.7, - point.y, 0 );
  215. toPoint.applyQuaternion( quaternion );
  216. toPoint.add( point );
  217. extrudeShape( tube2, fromPoint, toPoint );
  218. fromPoint.set( 0.7, - 0.3, 0 );
  219. fromPoint.applyQuaternion( quaternion );
  220. fromPoint.add( point );
  221. toPoint.set( 0.7, - point.y, 0 );
  222. toPoint.applyQuaternion( quaternion );
  223. toPoint.add( point );
  224. extrudeShape( tube3, fromPoint, toPoint );
  225. } else {
  226. fromPoint.set( 0, - 0.2, 0 );
  227. fromPoint.applyQuaternion( quaternion );
  228. fromPoint.add( point );
  229. toPoint.set( 0, - point.y, 0 );
  230. toPoint.applyQuaternion( quaternion );
  231. toPoint.add( point );
  232. extrudeShape( tube3, fromPoint, toPoint );
  233. }
  234. }
  235. this.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  236. this.setAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
  237. }
  238. }
  239. class RollerCoasterShadowGeometry extends THREE.BufferGeometry {
  240. constructor( curve, divisions ) {
  241. super();
  242. const vertices = [];
  243. const up = new THREE.Vector3( 0, 1, 0 );
  244. const forward = new THREE.Vector3();
  245. const quaternion = new THREE.Quaternion();
  246. const prevQuaternion = new THREE.Quaternion();
  247. prevQuaternion.setFromAxisAngle( up, Math.PI / 2 );
  248. const point = new THREE.Vector3();
  249. const prevPoint = new THREE.Vector3();
  250. prevPoint.copy( curve.getPointAt( 0 ) );
  251. prevPoint.y = 0;
  252. const vector1 = new THREE.Vector3();
  253. const vector2 = new THREE.Vector3();
  254. const vector3 = new THREE.Vector3();
  255. const vector4 = new THREE.Vector3();
  256. for ( let i = 1; i <= divisions; i ++ ) {
  257. point.copy( curve.getPointAt( i / divisions ) );
  258. point.y = 0;
  259. forward.subVectors( point, prevPoint );
  260. const angle = Math.atan2( forward.x, forward.z );
  261. quaternion.setFromAxisAngle( up, angle );
  262. vector1.set( - 0.3, 0, 0 );
  263. vector1.applyQuaternion( quaternion );
  264. vector1.add( point );
  265. vector2.set( 0.3, 0, 0 );
  266. vector2.applyQuaternion( quaternion );
  267. vector2.add( point );
  268. vector3.set( 0.3, 0, 0 );
  269. vector3.applyQuaternion( prevQuaternion );
  270. vector3.add( prevPoint );
  271. vector4.set( - 0.3, 0, 0 );
  272. vector4.applyQuaternion( prevQuaternion );
  273. vector4.add( prevPoint );
  274. vertices.push( vector1.x, vector1.y, vector1.z );
  275. vertices.push( vector2.x, vector2.y, vector2.z );
  276. vertices.push( vector4.x, vector4.y, vector4.z );
  277. vertices.push( vector2.x, vector2.y, vector2.z );
  278. vertices.push( vector3.x, vector3.y, vector3.z );
  279. vertices.push( vector4.x, vector4.y, vector4.z );
  280. prevPoint.copy( point );
  281. prevQuaternion.copy( quaternion );
  282. }
  283. this.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  284. }
  285. }
  286. class SkyGeometry extends THREE.BufferGeometry {
  287. constructor() {
  288. super();
  289. const vertices = [];
  290. for ( let i = 0; i < 100; i ++ ) {
  291. const x = Math.random() * 800 - 400;
  292. const y = Math.random() * 50 + 50;
  293. const z = Math.random() * 800 - 400;
  294. const size = Math.random() * 40 + 20;
  295. vertices.push( x - size, y, z - size );
  296. vertices.push( x + size, y, z - size );
  297. vertices.push( x - size, y, z + size );
  298. vertices.push( x + size, y, z - size );
  299. vertices.push( x + size, y, z + size );
  300. vertices.push( x - size, y, z + size );
  301. }
  302. this.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  303. }
  304. }
  305. class TreesGeometry extends THREE.BufferGeometry {
  306. constructor( landscape ) {
  307. super();
  308. const vertices = [];
  309. const colors = [];
  310. const raycaster = new THREE.Raycaster();
  311. raycaster.ray.direction.set( 0, - 1, 0 );
  312. for ( let i = 0; i < 2000; i ++ ) {
  313. const x = Math.random() * 500 - 250;
  314. const z = Math.random() * 500 - 250;
  315. raycaster.ray.origin.set( x, 50, z );
  316. const intersections = raycaster.intersectObject( landscape );
  317. if ( intersections.length === 0 ) continue;
  318. const y = intersections[ 0 ].point.y;
  319. const height = Math.random() * 5 + 0.5;
  320. let angle = Math.random() * Math.PI * 2;
  321. vertices.push( x + Math.sin( angle ), y, z + Math.cos( angle ) );
  322. vertices.push( x, y + height, z );
  323. vertices.push( x + Math.sin( angle + Math.PI ), y, z + Math.cos( angle + Math.PI ) );
  324. angle += Math.PI / 2;
  325. vertices.push( x + Math.sin( angle ), y, z + Math.cos( angle ) );
  326. vertices.push( x, y + height, z );
  327. vertices.push( x + Math.sin( angle + Math.PI ), y, z + Math.cos( angle + Math.PI ) );
  328. const random = Math.random() * 0.1;
  329. for ( let j = 0; j < 6; j ++ ) {
  330. colors.push( 0.2 + random, 0.4 + random, 0 );
  331. }
  332. }
  333. this.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  334. this.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 3 ) );
  335. }
  336. }
  337. THREE.RollerCoasterGeometry = RollerCoasterGeometry;
  338. THREE.RollerCoasterLiftersGeometry = RollerCoasterLiftersGeometry;
  339. THREE.RollerCoasterShadowGeometry = RollerCoasterShadowGeometry;
  340. THREE.SkyGeometry = SkyGeometry;
  341. THREE.TreesGeometry = TreesGeometry;
  342. } )();