webgl_custom_attributes_points3.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - custom attributes [particles][billboards][alphatest]</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. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - custom attributes example - billboards - alphatest</div>
  11. <div id="container"></div>
  12. <script type="x-shader/x-vertex" id="vertexshader">
  13. attribute float size;
  14. attribute vec4 ca;
  15. varying vec4 vColor;
  16. void main() {
  17. vColor = ca;
  18. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  19. gl_PointSize = size * ( 150.0 / -mvPosition.z );
  20. gl_Position = projectionMatrix * mvPosition;
  21. }
  22. </script>
  23. <script type="x-shader/x-fragment" id="fragmentshader">
  24. uniform vec3 color;
  25. uniform sampler2D pointTexture;
  26. varying vec4 vColor;
  27. void main() {
  28. vec4 outColor = texture2D( pointTexture, gl_PointCoord );
  29. if ( outColor.a < 0.5 ) discard;
  30. gl_FragColor = outColor * vec4( color * vColor.xyz, 1.0 );
  31. float depth = gl_FragCoord.z / gl_FragCoord.w;
  32. const vec3 fogColor = vec3( 0.0 );
  33. float fogFactor = smoothstep( 200.0, 600.0, depth );
  34. gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );
  35. }
  36. </script>
  37. <!-- Import maps polyfill -->
  38. <!-- Remove this when import maps will be widely supported -->
  39. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  40. <script type="importmap">
  41. {
  42. "imports": {
  43. "three": "../build/three.module.js"
  44. }
  45. }
  46. </script>
  47. <script type="module">
  48. import * as THREE from 'three';
  49. import Stats from './jsm/libs/stats.module.js';
  50. import * as BufferGeometryUtils from './jsm/utils/BufferGeometryUtils.js';
  51. let renderer, scene, camera, stats;
  52. let object;
  53. let vertices1;
  54. const WIDTH = window.innerWidth;
  55. const HEIGHT = window.innerHeight;
  56. init();
  57. animate();
  58. function init() {
  59. camera = new THREE.PerspectiveCamera( 40, WIDTH / HEIGHT, 1, 1000 );
  60. camera.position.z = 500;
  61. scene = new THREE.Scene();
  62. let radius = 100;
  63. const inner = 0.6 * radius;
  64. const vertex = new THREE.Vector3();
  65. const vertices = [];
  66. for ( let i = 0; i < 100000; i ++ ) {
  67. vertex.x = Math.random() * 2 - 1;
  68. vertex.y = Math.random() * 2 - 1;
  69. vertex.z = Math.random() * 2 - 1;
  70. vertex.multiplyScalar( radius );
  71. if ( ( vertex.x > inner || vertex.x < - inner ) ||
  72. ( vertex.y > inner || vertex.y < - inner ) ||
  73. ( vertex.z > inner || vertex.z < - inner ) )
  74. vertices.push( vertex.x, vertex.y, vertex.z );
  75. }
  76. vertices1 = vertices.length / 3;
  77. radius = 200;
  78. let boxGeometry1 = new THREE.BoxGeometry( radius, 0.1 * radius, 0.1 * radius, 50, 5, 5 );
  79. // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data
  80. boxGeometry1.deleteAttribute( 'normal' );
  81. boxGeometry1.deleteAttribute( 'uv' );
  82. boxGeometry1 = BufferGeometryUtils.mergeVertices( boxGeometry1 );
  83. const matrix = new THREE.Matrix4();
  84. const position = new THREE.Vector3();
  85. const rotation = new THREE.Euler();
  86. const quaternion = new THREE.Quaternion();
  87. const scale = new THREE.Vector3( 1, 1, 1 );
  88. function addGeo( geo, x, y, z, ry ) {
  89. position.set( x, y, z );
  90. rotation.set( 0, ry, 0 );
  91. matrix.compose( position, quaternion.setFromEuler( rotation ), scale );
  92. const positionAttribute = geo.getAttribute( 'position' );
  93. for ( var i = 0, l = positionAttribute.count; i < l; i ++ ) {
  94. vertex.fromBufferAttribute( positionAttribute, i );
  95. vertex.applyMatrix4( matrix );
  96. vertices.push( vertex.x, vertex.y, vertex.z );
  97. }
  98. }
  99. // side 1
  100. addGeo( boxGeometry1, 0, 110, 110, 0 );
  101. addGeo( boxGeometry1, 0, 110, - 110, 0 );
  102. addGeo( boxGeometry1, 0, - 110, 110, 0 );
  103. addGeo( boxGeometry1, 0, - 110, - 110, 0 );
  104. // side 2
  105. addGeo( boxGeometry1, 110, 110, 0, Math.PI / 2 );
  106. addGeo( boxGeometry1, 110, - 110, 0, Math.PI / 2 );
  107. addGeo( boxGeometry1, - 110, 110, 0, Math.PI / 2 );
  108. addGeo( boxGeometry1, - 110, - 110, 0, Math.PI / 2 );
  109. // corner edges
  110. let boxGeometry2 = new THREE.BoxGeometry( 0.1 * radius, radius * 1.2, 0.1 * radius, 5, 60, 5 );
  111. boxGeometry2.deleteAttribute( 'normal' );
  112. boxGeometry2.deleteAttribute( 'uv' );
  113. boxGeometry2 = BufferGeometryUtils.mergeVertices( boxGeometry2 );
  114. addGeo( boxGeometry2, 110, 0, 110, 0 );
  115. addGeo( boxGeometry2, 110, 0, - 110, 0 );
  116. addGeo( boxGeometry2, - 110, 0, 110, 0 );
  117. addGeo( boxGeometry2, - 110, 0, - 110, 0 );
  118. const positionAttribute = new THREE.Float32BufferAttribute( vertices, 3 );
  119. const colors = [];
  120. const sizes = [];
  121. const color = new THREE.Color();
  122. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  123. if ( i < vertices1 ) {
  124. color.setHSL( 0.5 + 0.2 * ( i / vertices1 ), 1, 0.5 );
  125. } else {
  126. color.setHSL( 0.1, 1, 0.5 );
  127. }
  128. color.toArray( colors, i * 3 );
  129. sizes[ i ] = i < vertices1 ? 10 : 40;
  130. }
  131. const geometry = new THREE.BufferGeometry();
  132. geometry.setAttribute( 'position', positionAttribute );
  133. geometry.setAttribute( 'ca', new THREE.Float32BufferAttribute( colors, 3 ) );
  134. geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ) );
  135. //
  136. const texture = new THREE.TextureLoader().load( 'textures/sprites/ball.png' );
  137. texture.wrapS = THREE.RepeatWrapping;
  138. texture.wrapT = THREE.RepeatWrapping;
  139. const material = new THREE.ShaderMaterial( {
  140. uniforms: {
  141. amplitude: { value: 1.0 },
  142. color: { value: new THREE.Color( 0xffffff ) },
  143. pointTexture: { value: texture }
  144. },
  145. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  146. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  147. } );
  148. //
  149. object = new THREE.Points( geometry, material );
  150. scene.add( object );
  151. //
  152. renderer = new THREE.WebGLRenderer();
  153. renderer.setPixelRatio( window.devicePixelRatio );
  154. renderer.setSize( WIDTH, HEIGHT );
  155. const container = document.getElementById( 'container' );
  156. container.appendChild( renderer.domElement );
  157. stats = new Stats();
  158. container.appendChild( stats.dom );
  159. //
  160. window.addEventListener( 'resize', onWindowResize );
  161. }
  162. function onWindowResize() {
  163. camera.aspect = window.innerWidth / window.innerHeight;
  164. camera.updateProjectionMatrix();
  165. renderer.setSize( window.innerWidth, window.innerHeight );
  166. }
  167. function animate() {
  168. requestAnimationFrame( animate );
  169. render();
  170. stats.update();
  171. }
  172. function render() {
  173. const time = Date.now() * 0.01;
  174. object.rotation.y = object.rotation.z = 0.02 * time;
  175. const geometry = object.geometry;
  176. const attributes = geometry.attributes;
  177. for ( let i = 0; i < attributes.size.array.length; i ++ ) {
  178. if ( i < vertices1 ) {
  179. attributes.size.array[ i ] = Math.max( 0, 26 + 32 * Math.sin( 0.1 * i + 0.6 * time ) );
  180. }
  181. }
  182. attributes.size.needsUpdate = true;
  183. renderer.render( scene, camera );
  184. }
  185. </script>
  186. </body>
  187. </html>