webgl_instancing_scatter.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instancing - scatter</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <script type="module">
  11. import * as THREE from '../build/three.module.js';
  12. import { MeshSurfaceSampler } from './jsm/math/MeshSurfaceSampler.js';
  13. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  14. import Stats from './jsm/libs/stats.module.js';
  15. import { GUI } from './jsm/libs/dat.gui.module.js';
  16. var camera, scene, renderer, stats;
  17. var api = {
  18. count: 2000,
  19. distribution: 'random',
  20. resample: resample,
  21. surfaceColor: 0xFFF784,
  22. backgroundColor: 0xE39469,
  23. };
  24. var stemMesh, blossomMesh;
  25. var stemGeometry, blossomGeometry;
  26. var stemMaterial, blossomMaterial;
  27. var sampler;
  28. var count = api.count;
  29. var ages = new Float32Array( count );
  30. var scales = new Float32Array( count );
  31. var dummy = new THREE.Object3D();
  32. var _position = new THREE.Vector3();
  33. var _normal = new THREE.Vector3();
  34. var _scale = new THREE.Vector3();
  35. // var surfaceGeometry = new THREE.BoxBufferGeometry( 10, 10, 10 ).toNonIndexed();
  36. var surfaceGeometry = new THREE.TorusKnotBufferGeometry( 10, 3, 100, 16 ).toNonIndexed();
  37. var surfaceMaterial = new THREE.MeshLambertMaterial( { color: api.surfaceColor, wireframe: false } );
  38. var surface = new THREE.Mesh( surfaceGeometry, surfaceMaterial );
  39. // Source: https://gist.github.com/gre/1650294
  40. var easeOutCubic = function ( t ) { return ( -- t ) * t * t + 1; };
  41. // Scaling curve causes particles to grow quickly, ease gradually into full scale, then
  42. // disappear quickly. More of the particle's lifetime is spent around full scale.
  43. var scaleCurve = function ( t ) {
  44. return Math.abs( easeOutCubic( ( t > 0.5 ? 1 - t : t ) * 2 ) );
  45. };
  46. var loader = new GLTFLoader();
  47. loader.load( './models/gltf/Flower/Flower.glb', function ( gltf ) {
  48. var _stemMesh = gltf.scene.getObjectByName('Stem');
  49. var _blossomMesh = gltf.scene.getObjectByName('Blossom');
  50. stemGeometry = _stemMesh.geometry;
  51. blossomGeometry = _blossomMesh.geometry;
  52. var defaultTransform = new THREE.Matrix4()
  53. .makeRotationX( Math.PI )
  54. .multiply( new THREE.Matrix4().makeScale( 7, 7, 7 ) )
  55. stemGeometry.applyMatrix( defaultTransform );
  56. blossomGeometry.applyMatrix( defaultTransform );
  57. stemMaterial = _stemMesh.material;
  58. blossomMaterial = _blossomMesh.material;
  59. // Assign random colors to the blossoms.
  60. var _color = new THREE.Color();
  61. var color = new Float32Array( count * 3 );
  62. var blossomPalette = [ 0xF20587, 0xF2D479, 0xF2C879, 0xF2B077, 0xF24405 ];
  63. for ( var i = 0; i < count; i ++ ) {
  64. _color.setHex( blossomPalette[ Math.floor( Math.random() * blossomPalette.length ) ] );
  65. _color.toArray( color, i * 3 );
  66. }
  67. blossomGeometry.setAttribute( 'color', new THREE.InstancedBufferAttribute( color, 3 ) );
  68. blossomMaterial.vertexColors = THREE.VertexColors;
  69. stemMesh = new THREE.InstancedMesh( stemGeometry, stemMaterial, count );
  70. blossomMesh = new THREE.InstancedMesh( blossomGeometry, blossomMaterial, count );
  71. resample();
  72. init();
  73. animate();
  74. } );
  75. function init() {
  76. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  77. camera.position.set( 25, 25, 25 );
  78. camera.lookAt( 0, 0, 0 );
  79. //
  80. scene = new THREE.Scene();
  81. scene.background = new THREE.Color( api.backgroundColor );
  82. var pointLight = new THREE.PointLight( 0xAA8899, 0.75 );
  83. pointLight.position.set( 50, -25, 75 );
  84. scene.add( pointLight );
  85. scene.add( new THREE.HemisphereLight() );
  86. //
  87. scene.add( stemMesh );
  88. scene.add( blossomMesh );
  89. scene.add( surface );
  90. //
  91. var gui = new GUI();
  92. gui.add( api, 'count', 0, count ).onChange( function () {
  93. stemMesh.count = api.count;
  94. blossomMesh.count = api.count;
  95. } );
  96. // gui.addColor( api, 'backgroundColor' ).onChange( function () {
  97. // scene.background.setHex( api.backgroundColor );
  98. // } );
  99. // gui.addColor( api, 'surfaceColor' ).onChange( function () {
  100. // surfaceMaterial.color.setHex( api.surfaceColor );
  101. // } );
  102. gui.add( api, 'distribution' ).options( [ 'random', 'weighted' ] ).onChange( resample );
  103. gui.add( api, 'resample' );
  104. //
  105. renderer = new THREE.WebGLRenderer( { antialias: true } );
  106. renderer.setPixelRatio( window.devicePixelRatio );
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. document.body.appendChild( renderer.domElement );
  109. //
  110. stats = new Stats();
  111. document.body.appendChild( stats.dom );
  112. //
  113. window.addEventListener( 'resize', onWindowResize, false );
  114. }
  115. function resample() {
  116. var vertexCount = surface.geometry.getAttribute( 'position' ).count;
  117. console.info( 'Sampling ' + count + ' points from a surface with ' + vertexCount + ' vertices...' );
  118. //
  119. console.time( '.build()' );
  120. sampler = new MeshSurfaceSampler( surface )
  121. .setWeightAttribute( api.distribution === 'weighted' ? 'uv' : null )
  122. .build();
  123. console.timeEnd( '.build()' );
  124. //
  125. console.time( '.sample()' );
  126. for ( var i = 0; i < count; i ++ ) {
  127. ages[ i ] = Math.random();
  128. scales[ i ] = scaleCurve( ages[ i ] );
  129. resampleParticle( i );
  130. }
  131. console.timeEnd( '.sample()' );
  132. stemMesh.instanceMatrix.needsUpdate = true;
  133. blossomMesh.instanceMatrix.needsUpdate = true;
  134. }
  135. function resampleParticle ( i ) {
  136. sampler.sample( _position, _normal );
  137. _normal.add( _position );
  138. dummy.position.copy( _position );
  139. dummy.scale.set( scales[ i ], scales[ i ], scales[ i ] );
  140. dummy.lookAt( _normal );
  141. dummy.updateMatrix();
  142. stemMesh.setMatrixAt( i, dummy.matrix );
  143. blossomMesh.setMatrixAt( i, dummy.matrix );
  144. }
  145. function updateParticle ( i ) {
  146. // Update lifecycle.
  147. ages[ i ] += 0.005;
  148. if ( ages[ i ] >= 1 ) {
  149. ages[ i ] = 0.001;
  150. scales[ i ] = scaleCurve( ages[ i ] );
  151. resampleParticle( i );
  152. return;
  153. }
  154. // Update scale.
  155. var prevScale = scales[ i ];
  156. scales[ i ] = scaleCurve( ages[ i ] );
  157. _scale.set( scales[ i ] / prevScale, scales[ i ] / prevScale, scales[ i ] / prevScale );
  158. // Update transform.
  159. stemMesh.getMatrixAt( i, dummy.matrix );
  160. dummy.matrix.scale( _scale );
  161. stemMesh.setMatrixAt( i, dummy.matrix );
  162. blossomMesh.setMatrixAt( i, dummy.matrix );
  163. }
  164. function onWindowResize() {
  165. camera.aspect = window.innerWidth / window.innerHeight;
  166. camera.updateProjectionMatrix();
  167. renderer.setSize( window.innerWidth, window.innerHeight );
  168. }
  169. //
  170. function animate() {
  171. requestAnimationFrame( animate );
  172. render();
  173. stats.update();
  174. }
  175. function render() {
  176. if ( stemMesh && blossomMesh ) {
  177. var time = Date.now() * 0.001;
  178. scene.rotation.x = Math.sin( time / 4 );
  179. scene.rotation.y = Math.sin( time / 2 );
  180. for ( var i = 0; i < api.count; i ++ ) {
  181. updateParticle( i );
  182. }
  183. stemMesh.instanceMatrix.needsUpdate = true;
  184. blossomMesh.instanceMatrix.needsUpdate = true;
  185. }
  186. renderer.render( scene, camera );
  187. }
  188. </script>
  189. </body>
  190. </html>