RollerCoaster.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. import {
  2. BufferAttribute,
  3. BufferGeometry,
  4. Color,
  5. Quaternion,
  6. Raycaster,
  7. SRGBColorSpace,
  8. Vector3
  9. } from 'three';
  10. /**
  11. * A procedural roller coaster geometry.
  12. *
  13. * @augments BufferGeometry
  14. * @three_import import { RollerCoasterGeometry } from 'three/addons/misc/RollerCoaster.js';
  15. */
  16. class RollerCoasterGeometry extends BufferGeometry {
  17. /**
  18. * Constructs a new geometry.
  19. *
  20. * @param {Curve} curve - The curve to generate the geometry along.
  21. * @param {number} divisions - The number of divisions which defines the detail of the geometry.
  22. */
  23. constructor( curve, divisions ) {
  24. super();
  25. const vertices = [];
  26. const normals = [];
  27. const colors = [];
  28. const color1 = [ 1, 1, 1 ];
  29. const color2 = [ 1, 1, 0 ];
  30. const up = new Vector3( 0, 1, 0 );
  31. const forward = new Vector3();
  32. const right = new Vector3();
  33. const quaternion = new Quaternion();
  34. const prevQuaternion = new Quaternion();
  35. prevQuaternion.setFromAxisAngle( up, Math.PI / 2 );
  36. const point = new Vector3();
  37. const prevPoint = new Vector3();
  38. prevPoint.copy( curve.getPointAt( 0 ) );
  39. // shapes
  40. const step = [
  41. new Vector3( - 0.225, 0, 0 ),
  42. new Vector3( 0, - 0.050, 0 ),
  43. new Vector3( 0, - 0.175, 0 ),
  44. new Vector3( 0, - 0.050, 0 ),
  45. new Vector3( 0.225, 0, 0 ),
  46. new Vector3( 0, - 0.175, 0 )
  47. ];
  48. const PI2 = Math.PI * 2;
  49. let sides = 5;
  50. const tube1 = [];
  51. for ( let i = 0; i < sides; i ++ ) {
  52. const angle = ( i / sides ) * PI2;
  53. tube1.push( new Vector3( Math.sin( angle ) * 0.06, Math.cos( angle ) * 0.06, 0 ) );
  54. }
  55. sides = 6;
  56. const tube2 = [];
  57. for ( let i = 0; i < sides; i ++ ) {
  58. const angle = ( i / sides ) * PI2;
  59. tube2.push( new Vector3( Math.sin( angle ) * 0.025, Math.cos( angle ) * 0.025, 0 ) );
  60. }
  61. const vector = new Vector3();
  62. const normal = new Vector3();
  63. function drawShape( shape, color ) {
  64. normal.set( 0, 0, - 1 ).applyQuaternion( quaternion );
  65. for ( let j = 0; j < shape.length; j ++ ) {
  66. vector.copy( shape[ j ] );
  67. vector.applyQuaternion( quaternion );
  68. vector.add( point );
  69. vertices.push( vector.x, vector.y, vector.z );
  70. normals.push( normal.x, normal.y, normal.z );
  71. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  72. }
  73. normal.set( 0, 0, 1 ).applyQuaternion( quaternion );
  74. for ( let j = shape.length - 1; j >= 0; j -- ) {
  75. vector.copy( shape[ j ] );
  76. vector.applyQuaternion( quaternion );
  77. vector.add( point );
  78. vertices.push( vector.x, vector.y, vector.z );
  79. normals.push( normal.x, normal.y, normal.z );
  80. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  81. }
  82. }
  83. const vector1 = new Vector3();
  84. const vector2 = new Vector3();
  85. const vector3 = new Vector3();
  86. const vector4 = new Vector3();
  87. const normal1 = new Vector3();
  88. const normal2 = new Vector3();
  89. const normal3 = new Vector3();
  90. const normal4 = new Vector3();
  91. function extrudeShape( shape, offset, color ) {
  92. for ( let j = 0, jl = shape.length; j < jl; j ++ ) {
  93. const point1 = shape[ j ];
  94. const point2 = shape[ ( j + 1 ) % jl ];
  95. vector1.copy( point1 ).add( offset );
  96. vector1.applyQuaternion( quaternion );
  97. vector1.add( point );
  98. vector2.copy( point2 ).add( offset );
  99. vector2.applyQuaternion( quaternion );
  100. vector2.add( point );
  101. vector3.copy( point2 ).add( offset );
  102. vector3.applyQuaternion( prevQuaternion );
  103. vector3.add( prevPoint );
  104. vector4.copy( point1 ).add( offset );
  105. vector4.applyQuaternion( prevQuaternion );
  106. vector4.add( prevPoint );
  107. vertices.push( vector1.x, vector1.y, vector1.z );
  108. vertices.push( vector2.x, vector2.y, vector2.z );
  109. vertices.push( vector4.x, vector4.y, vector4.z );
  110. vertices.push( vector2.x, vector2.y, vector2.z );
  111. vertices.push( vector3.x, vector3.y, vector3.z );
  112. vertices.push( vector4.x, vector4.y, vector4.z );
  113. //
  114. normal1.copy( point1 );
  115. normal1.applyQuaternion( quaternion );
  116. normal1.normalize();
  117. normal2.copy( point2 );
  118. normal2.applyQuaternion( quaternion );
  119. normal2.normalize();
  120. normal3.copy( point2 );
  121. normal3.applyQuaternion( prevQuaternion );
  122. normal3.normalize();
  123. normal4.copy( point1 );
  124. normal4.applyQuaternion( prevQuaternion );
  125. normal4.normalize();
  126. normals.push( normal1.x, normal1.y, normal1.z );
  127. normals.push( normal2.x, normal2.y, normal2.z );
  128. normals.push( normal4.x, normal4.y, normal4.z );
  129. normals.push( normal2.x, normal2.y, normal2.z );
  130. normals.push( normal3.x, normal3.y, normal3.z );
  131. normals.push( normal4.x, normal4.y, normal4.z );
  132. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  133. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  134. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  135. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  136. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  137. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  138. }
  139. }
  140. const offset = new Vector3();
  141. const sample1 = new Vector3();
  142. const sample2 = new Vector3();
  143. const rollQuaternion = new Quaternion();
  144. for ( let i = 1; i <= divisions; i ++ ) {
  145. point.copy( curve.getPointAt( i / divisions ) );
  146. up.set( 0, 1, 0 );
  147. forward.subVectors( point, prevPoint ).normalize();
  148. right.crossVectors( up, forward ).normalize();
  149. up.crossVectors( forward, right );
  150. const angle = Math.atan2( forward.x, forward.z );
  151. quaternion.setFromAxisAngle( up, angle );
  152. // banking
  153. const bankDelta = 0.01;
  154. const t = i / divisions;
  155. sample1.copy( curve.getTangentAt( ( ( t - bankDelta ) % 1 + 1 ) % 1 ) );
  156. sample2.copy( curve.getTangentAt( ( t + bankDelta ) % 1 ) );
  157. let headingChange = Math.atan2( sample2.x, sample2.z ) - Math.atan2( sample1.x, sample1.z );
  158. if ( headingChange > Math.PI ) headingChange -= Math.PI * 2;
  159. if ( headingChange < -Math.PI ) headingChange += Math.PI * 2;
  160. quaternion.premultiply( rollQuaternion.setFromAxisAngle( forward, - Math.atan( headingChange * 8 ) * 0.5 ) );
  161. if ( i % 2 === 0 ) {
  162. drawShape( step, color2 );
  163. }
  164. extrudeShape( tube1, offset.set( 0, - 0.125, 0 ), color2 );
  165. extrudeShape( tube2, offset.set( 0.2, 0, 0 ), color1 );
  166. extrudeShape( tube2, offset.set( - 0.2, 0, 0 ), color1 );
  167. prevPoint.copy( point );
  168. prevQuaternion.copy( quaternion );
  169. }
  170. // console.log( vertices.length );
  171. this.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  172. this.setAttribute( 'normal', new BufferAttribute( new Float32Array( normals ), 3 ) );
  173. this.setAttribute( 'color', new BufferAttribute( new Float32Array( colors ), 3 ) );
  174. }
  175. }
  176. /**
  177. * A procedural roller coaster lifters geometry.
  178. *
  179. * @augments BufferGeometry
  180. * @three_import import { RollerCoasterLiftersGeometry } from 'three/addons/misc/RollerCoaster.js';
  181. */
  182. class RollerCoasterLiftersGeometry extends BufferGeometry {
  183. /**
  184. * Constructs a new geometry.
  185. *
  186. * @param {Curve} curve - The curve to generate the geometry along.
  187. * @param {number} divisions - The number of divisions which defines the detail of the geometry.
  188. */
  189. constructor( curve, divisions ) {
  190. super();
  191. const vertices = [];
  192. const normals = [];
  193. const quaternion = new Quaternion();
  194. const up = new Vector3( 0, 1, 0 );
  195. const point = new Vector3();
  196. const tangent = new Vector3();
  197. // shapes
  198. const tube1 = [
  199. new Vector3( 0, 0.05, - 0.05 ),
  200. new Vector3( 0, 0.05, 0.05 ),
  201. new Vector3( 0, - 0.05, 0 )
  202. ];
  203. const tube2 = [
  204. new Vector3( - 0.05, 0, 0.05 ),
  205. new Vector3( - 0.05, 0, - 0.05 ),
  206. new Vector3( 0.05, 0, 0 )
  207. ];
  208. const tube3 = [
  209. new Vector3( 0.05, 0, - 0.05 ),
  210. new Vector3( 0.05, 0, 0.05 ),
  211. new Vector3( - 0.05, 0, 0 )
  212. ];
  213. const vector1 = new Vector3();
  214. const vector2 = new Vector3();
  215. const vector3 = new Vector3();
  216. const vector4 = new Vector3();
  217. const normal1 = new Vector3();
  218. const normal2 = new Vector3();
  219. const normal3 = new Vector3();
  220. const normal4 = new Vector3();
  221. function extrudeShape( shape, fromPoint, toPoint ) {
  222. for ( let j = 0, jl = shape.length; j < jl; j ++ ) {
  223. const point1 = shape[ j ];
  224. const point2 = shape[ ( j + 1 ) % jl ];
  225. vector1.copy( point1 );
  226. vector1.applyQuaternion( quaternion );
  227. vector1.add( fromPoint );
  228. vector2.copy( point2 );
  229. vector2.applyQuaternion( quaternion );
  230. vector2.add( fromPoint );
  231. vector3.copy( point2 );
  232. vector3.applyQuaternion( quaternion );
  233. vector3.add( toPoint );
  234. vector4.copy( point1 );
  235. vector4.applyQuaternion( quaternion );
  236. vector4.add( toPoint );
  237. vertices.push( vector1.x, vector1.y, vector1.z );
  238. vertices.push( vector2.x, vector2.y, vector2.z );
  239. vertices.push( vector4.x, vector4.y, vector4.z );
  240. vertices.push( vector2.x, vector2.y, vector2.z );
  241. vertices.push( vector3.x, vector3.y, vector3.z );
  242. vertices.push( vector4.x, vector4.y, vector4.z );
  243. //
  244. normal1.copy( point1 );
  245. normal1.applyQuaternion( quaternion );
  246. normal1.normalize();
  247. normal2.copy( point2 );
  248. normal2.applyQuaternion( quaternion );
  249. normal2.normalize();
  250. normal3.copy( point2 );
  251. normal3.applyQuaternion( quaternion );
  252. normal3.normalize();
  253. normal4.copy( point1 );
  254. normal4.applyQuaternion( quaternion );
  255. normal4.normalize();
  256. normals.push( normal1.x, normal1.y, normal1.z );
  257. normals.push( normal2.x, normal2.y, normal2.z );
  258. normals.push( normal4.x, normal4.y, normal4.z );
  259. normals.push( normal2.x, normal2.y, normal2.z );
  260. normals.push( normal3.x, normal3.y, normal3.z );
  261. normals.push( normal4.x, normal4.y, normal4.z );
  262. }
  263. }
  264. const fromPoint = new Vector3();
  265. const toPoint = new Vector3();
  266. const sample1 = new Vector3();
  267. const sample2 = new Vector3();
  268. const bankedQuaternion = new Quaternion();
  269. const rollQuaternion = new Quaternion();
  270. for ( let i = 1; i <= divisions; i ++ ) {
  271. point.copy( curve.getPointAt( i / divisions ) );
  272. tangent.copy( curve.getTangentAt( i / divisions ) );
  273. const angle = Math.atan2( tangent.x, tangent.z );
  274. quaternion.setFromAxisAngle( up, angle );
  275. // banking
  276. const bankDelta = 0.01;
  277. const t = i / divisions;
  278. sample1.copy( curve.getTangentAt( ( ( t - bankDelta ) % 1 + 1 ) % 1 ) );
  279. sample2.copy( curve.getTangentAt( ( t + bankDelta ) % 1 ) );
  280. let headingChange = Math.atan2( sample2.x, sample2.z ) - Math.atan2( sample1.x, sample1.z );
  281. if ( headingChange > Math.PI ) headingChange -= Math.PI * 2;
  282. if ( headingChange < -Math.PI ) headingChange += Math.PI * 2;
  283. bankedQuaternion.copy( quaternion );
  284. rollQuaternion.setFromAxisAngle( tangent, - Math.atan( headingChange * 8 ) * 0.5 );
  285. bankedQuaternion.premultiply( rollQuaternion );
  286. //
  287. if ( point.y > 10 ) {
  288. fromPoint.set( - 0.75, - 0.35, 0 );
  289. fromPoint.applyQuaternion( quaternion );
  290. fromPoint.add( point );
  291. toPoint.set( 0.75, - 0.35, 0 );
  292. toPoint.applyQuaternion( quaternion );
  293. toPoint.add( point );
  294. extrudeShape( tube1, fromPoint, toPoint );
  295. fromPoint.set( - 0.7, - 0.3, 0 );
  296. fromPoint.applyQuaternion( quaternion );
  297. fromPoint.add( point );
  298. toPoint.set( - 0.7, - point.y, 0 );
  299. toPoint.applyQuaternion( quaternion );
  300. toPoint.add( point );
  301. extrudeShape( tube2, fromPoint, toPoint );
  302. fromPoint.set( 0.7, - 0.3, 0 );
  303. fromPoint.applyQuaternion( quaternion );
  304. fromPoint.add( point );
  305. toPoint.set( 0.7, - point.y, 0 );
  306. toPoint.applyQuaternion( quaternion );
  307. toPoint.add( point );
  308. extrudeShape( tube3, fromPoint, toPoint );
  309. } else {
  310. fromPoint.set( 0, - 0.2, 0 );
  311. fromPoint.applyQuaternion( bankedQuaternion );
  312. fromPoint.add( point );
  313. toPoint.copy( fromPoint );
  314. toPoint.y = 0;
  315. extrudeShape( tube3, fromPoint, toPoint );
  316. }
  317. }
  318. this.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  319. this.setAttribute( 'normal', new BufferAttribute( new Float32Array( normals ), 3 ) );
  320. }
  321. }
  322. /**
  323. * A procedural roller coaster shadow geometry.
  324. *
  325. * @augments BufferGeometry
  326. * @three_import import { RollerCoasterShadowGeometry } from 'three/addons/misc/RollerCoaster.js';
  327. */
  328. class RollerCoasterShadowGeometry extends BufferGeometry {
  329. /**
  330. * Constructs a new geometry.
  331. *
  332. * @param {Curve} curve - The curve to generate the geometry along.
  333. * @param {number} divisions - The number of divisions which defines the detail of the geometry.
  334. */
  335. constructor( curve, divisions ) {
  336. super();
  337. const vertices = [];
  338. const up = new Vector3( 0, 1, 0 );
  339. const forward = new Vector3();
  340. const quaternion = new Quaternion();
  341. const prevQuaternion = new Quaternion();
  342. prevQuaternion.setFromAxisAngle( up, Math.PI / 2 );
  343. const point = new Vector3();
  344. const prevPoint = new Vector3();
  345. prevPoint.copy( curve.getPointAt( 0 ) );
  346. prevPoint.y = 0;
  347. const vector1 = new Vector3();
  348. const vector2 = new Vector3();
  349. const vector3 = new Vector3();
  350. const vector4 = new Vector3();
  351. for ( let i = 1; i <= divisions; i ++ ) {
  352. point.copy( curve.getPointAt( i / divisions ) );
  353. point.y = 0;
  354. forward.subVectors( point, prevPoint );
  355. const angle = Math.atan2( forward.x, forward.z );
  356. quaternion.setFromAxisAngle( up, angle );
  357. vector1.set( - 0.3, 0, 0 );
  358. vector1.applyQuaternion( quaternion );
  359. vector1.add( point );
  360. vector2.set( 0.3, 0, 0 );
  361. vector2.applyQuaternion( quaternion );
  362. vector2.add( point );
  363. vector3.set( 0.3, 0, 0 );
  364. vector3.applyQuaternion( prevQuaternion );
  365. vector3.add( prevPoint );
  366. vector4.set( - 0.3, 0, 0 );
  367. vector4.applyQuaternion( prevQuaternion );
  368. vector4.add( prevPoint );
  369. vertices.push( vector1.x, vector1.y, vector1.z );
  370. vertices.push( vector2.x, vector2.y, vector2.z );
  371. vertices.push( vector4.x, vector4.y, vector4.z );
  372. vertices.push( vector2.x, vector2.y, vector2.z );
  373. vertices.push( vector3.x, vector3.y, vector3.z );
  374. vertices.push( vector4.x, vector4.y, vector4.z );
  375. prevPoint.copy( point );
  376. prevQuaternion.copy( quaternion );
  377. }
  378. this.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  379. }
  380. }
  381. /**
  382. * A procedural sky geometry.
  383. *
  384. * @augments BufferGeometry
  385. * @three_import import { SkyGeometry } from 'three/addons/misc/RollerCoaster.js';
  386. */
  387. class SkyGeometry extends BufferGeometry {
  388. /**
  389. * Constructs a new geometry.
  390. */
  391. constructor() {
  392. super();
  393. const vertices = [];
  394. for ( let i = 0; i < 100; i ++ ) {
  395. const x = Math.random() * 800 - 400;
  396. const y = Math.random() * 50 + 50;
  397. const z = Math.random() * 800 - 400;
  398. const size = Math.random() * 40 + 20;
  399. vertices.push( x - size, y, z - size );
  400. vertices.push( x + size, y, z - size );
  401. vertices.push( x - size, y, z + size );
  402. vertices.push( x + size, y, z - size );
  403. vertices.push( x + size, y, z + size );
  404. vertices.push( x - size, y, z + size );
  405. }
  406. this.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  407. }
  408. }
  409. /**
  410. * A procedural trees geometry.
  411. *
  412. * @augments BufferGeometry
  413. * @three_import import { TreesGeometry } from 'three/addons/misc/RollerCoaster.js';
  414. */
  415. class TreesGeometry extends BufferGeometry {
  416. /**
  417. * Constructs a new geometry.
  418. *
  419. * @param {Mesh} landscape - A mesh representing the landscape. Trees will be positioned
  420. * randomly on the landscape's surface.
  421. */
  422. constructor( landscape ) {
  423. super();
  424. const vertices = [];
  425. const colors = [];
  426. const raycaster = new Raycaster();
  427. raycaster.ray.direction.set( 0, - 1, 0 );
  428. const _color = new Color();
  429. for ( let i = 0; i < 2000; i ++ ) {
  430. const x = Math.random() * 500 - 250;
  431. const z = Math.random() * 500 - 250;
  432. raycaster.ray.origin.set( x, 50, z );
  433. const intersections = raycaster.intersectObject( landscape );
  434. if ( intersections.length === 0 ) continue;
  435. const y = intersections[ 0 ].point.y;
  436. const height = Math.random() * 5 + 0.5;
  437. let angle = Math.random() * Math.PI * 2;
  438. vertices.push( x + Math.sin( angle ), y, z + Math.cos( angle ) );
  439. vertices.push( x, y + height, z );
  440. vertices.push( x + Math.sin( angle + Math.PI ), y, z + Math.cos( angle + Math.PI ) );
  441. angle += Math.PI / 2;
  442. vertices.push( x + Math.sin( angle ), y, z + Math.cos( angle ) );
  443. vertices.push( x, y + height, z );
  444. vertices.push( x + Math.sin( angle + Math.PI ), y, z + Math.cos( angle + Math.PI ) );
  445. const random = Math.random() * 0.1;
  446. for ( let j = 0; j < 6; j ++ ) {
  447. _color.setRGB( 0.2 + random, 0.4 + random, 0, SRGBColorSpace );
  448. colors.push( _color.r, _color.g, _color.b );
  449. }
  450. }
  451. this.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  452. this.setAttribute( 'color', new BufferAttribute( new Float32Array( colors ), 3 ) );
  453. }
  454. }
  455. export { RollerCoasterGeometry, RollerCoasterLiftersGeometry, RollerCoasterShadowGeometry, SkyGeometry, TreesGeometry };
粤ICP备19079148号