webgl_instancing_scatter.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 dummy = new THREE.Object3D();
  31. var _sPosition = new THREE.Vector3();
  32. var _sNormal = new THREE.Vector3();
  33. // var surfaceGeometry = new THREE.BoxBufferGeometry( 10, 10, 10 ).toNonIndexed();
  34. var surfaceGeometry = new THREE.TorusKnotBufferGeometry( 10, 3, 100, 16 ).toNonIndexed();
  35. var surfaceMaterial = new THREE.MeshLambertMaterial( { color: api.surfaceColor, wireframe: false } );
  36. var surface = new THREE.Mesh( surfaceGeometry, surfaceMaterial );
  37. // Source: https://gist.github.com/gre/1650294
  38. var easeOutCubic = function ( t ) { return ( -- t ) * t * t + 1; };
  39. var loader = new GLTFLoader();
  40. loader.load( './models/gltf/Flower/Flower.glb', function ( gltf ) {
  41. var _stemMesh = gltf.scene.getObjectByName('Stem');
  42. var _blossomMesh = gltf.scene.getObjectByName('Blossom');
  43. stemGeometry = _stemMesh.geometry;
  44. blossomGeometry = _blossomMesh.geometry;
  45. var defaultTransform = new THREE.Matrix4()
  46. .makeRotationX( Math.PI )
  47. .multiply( new THREE.Matrix4().makeScale( 7, 7, 7 ) )
  48. stemGeometry.applyMatrix( defaultTransform );
  49. blossomGeometry.applyMatrix( defaultTransform );
  50. stemMaterial = _stemMesh.material;
  51. blossomMaterial = _blossomMesh.material;
  52. // Assign random colors to the blossoms.
  53. var _color = new THREE.Color();
  54. var color = new Float32Array( count * 3 );
  55. var blossomPalette = [ 0xF20587, 0xF2D479, 0xF2C879, 0xF2B077, 0xF24405 ];
  56. for ( var i = 0; i < count; i ++ ) {
  57. _color.setHex( blossomPalette[ Math.floor( Math.random() * blossomPalette.length ) ] );
  58. _color.toArray( color, i * 3 );
  59. }
  60. blossomGeometry.setAttribute( 'color', new THREE.InstancedBufferAttribute( color, 3 ) );
  61. blossomMaterial.vertexColors = THREE.VertexColors;
  62. stemMesh = new THREE.InstancedMesh( stemGeometry, stemMaterial, count );
  63. blossomMesh = new THREE.InstancedMesh( blossomGeometry, blossomMaterial, count );
  64. resample();
  65. init();
  66. animate();
  67. } );
  68. function init() {
  69. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  70. camera.position.set( 25, 25, 25 );
  71. camera.lookAt( 0, 0, 0 );
  72. //
  73. scene = new THREE.Scene();
  74. scene.background = new THREE.Color( api.backgroundColor );
  75. var pointLight = new THREE.PointLight( 0xAA8899, 0.75 );
  76. pointLight.position.set( 50, -25, 75 );
  77. scene.add( pointLight );
  78. scene.add( new THREE.HemisphereLight() );
  79. //
  80. scene.add( stemMesh );
  81. scene.add( blossomMesh );
  82. scene.add( surface );
  83. //
  84. var gui = new GUI();
  85. gui.add( api, 'count', 0, count ).onChange( function () {
  86. stemMesh.count = api.count;
  87. blossomMesh.count = api.count;
  88. } );
  89. // gui.addColor( api, 'backgroundColor' ).onChange( function () {
  90. // scene.background.setHex( api.backgroundColor );
  91. // } );
  92. // gui.addColor( api, 'surfaceColor' ).onChange( function () {
  93. // surfaceMaterial.color.setHex( api.surfaceColor );
  94. // } );
  95. gui.add( api, 'distribution' ).options( [ 'random', 'weighted' ] ).onChange( resample );
  96. gui.add( api, 'resample' );
  97. //
  98. renderer = new THREE.WebGLRenderer( { antialias: true } );
  99. renderer.setPixelRatio( window.devicePixelRatio );
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. document.body.appendChild( renderer.domElement );
  102. //
  103. stats = new Stats();
  104. document.body.appendChild( stats.dom );
  105. //
  106. window.addEventListener( 'resize', onWindowResize, false );
  107. }
  108. function resample() {
  109. var vertexCount = surface.geometry.getAttribute( 'position' ).count;
  110. console.info( 'Sampling ' + count + ' points from a surface with ' + vertexCount + ' vertices...' );
  111. //
  112. console.time( '.build()' );
  113. sampler = new MeshSurfaceSampler( surface.geometry )
  114. .setWeightAttribute( api.distribution === 'weighted' ? 'uv' : null )
  115. .build();
  116. console.timeEnd( '.build()' );
  117. //
  118. console.time( '.sample()' );
  119. for ( var i = 0; i < count; i ++ ) {
  120. ages[ i ] = Math.random();
  121. resampleParticle( i );
  122. }
  123. console.timeEnd( '.sample()' );
  124. stemMesh.instanceMatrix.needsUpdate = true;
  125. blossomMesh.instanceMatrix.needsUpdate = true;
  126. }
  127. function resampleParticle ( i ) {
  128. sampler.sample( _sPosition, _sNormal );
  129. _sNormal.add( _sPosition );
  130. dummy.position.copy( _sPosition );
  131. dummy.scale.set( ages[ i ], ages[ i ], ages[ i ] );
  132. dummy.lookAt( _sNormal );
  133. dummy.updateMatrix();
  134. stemMesh.setMatrixAt( i, dummy.matrix );
  135. blossomMesh.setMatrixAt( i, dummy.matrix );
  136. }
  137. function updateParticle ( i, ) {
  138. ages[ i ] += 0.005;
  139. if ( ages[ i ] >= 1 ) {
  140. ages[ i ] = 0.001;
  141. resampleParticle( i );
  142. return;
  143. }
  144. var scale = Math.abs( easeOutCubic( ( ages[ i ] > 0.5 ? 1 - ages[ i ] : ages[ i ] ) * 2 ) );
  145. stemMesh.getMatrixAt( i, dummy.matrix );
  146. dummy.matrix.decompose( dummy.position, dummy.quaternion, dummy.scale );
  147. dummy.scale.set( scale, scale, scale );
  148. dummy.updateMatrix();
  149. stemMesh.setMatrixAt( i, dummy.matrix );
  150. blossomMesh.setMatrixAt( i, dummy.matrix );
  151. }
  152. function onWindowResize() {
  153. camera.aspect = window.innerWidth / window.innerHeight;
  154. camera.updateProjectionMatrix();
  155. renderer.setSize( window.innerWidth, window.innerHeight );
  156. }
  157. //
  158. function animate() {
  159. requestAnimationFrame( animate );
  160. render();
  161. stats.update();
  162. }
  163. function render() {
  164. if ( stemMesh && blossomMesh ) {
  165. var time = Date.now() * 0.001;
  166. scene.rotation.x = Math.sin( time / 4 );
  167. scene.rotation.y = Math.sin( time / 2 );
  168. for ( var i = 0; i < api.count; i ++ ) {
  169. updateParticle( i );
  170. }
  171. stemMesh.instanceMatrix.needsUpdate = true;
  172. blossomMesh.instanceMatrix.needsUpdate = true;
  173. }
  174. renderer.render( scene, camera );
  175. }
  176. </script>
  177. </body>
  178. </html>