webgl_custom_attributes_points3.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. <script type="importmap">
  38. {
  39. "imports": {
  40. "three": "../build/three.module.js",
  41. "three/addons/": "./jsm/"
  42. }
  43. }
  44. </script>
  45. <script type="module">
  46. import * as THREE from 'three';
  47. import Stats from 'three/addons/libs/stats.module.js';
  48. import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
  49. let renderer, scene, camera, stats;
  50. let object;
  51. let vertices1;
  52. const WIDTH = window.innerWidth;
  53. const HEIGHT = window.innerHeight;
  54. init();
  55. function init() {
  56. camera = new THREE.PerspectiveCamera( 40, WIDTH / HEIGHT, 1, 1000 );
  57. camera.position.z = 500;
  58. scene = new THREE.Scene();
  59. let radius = 100;
  60. const inner = 0.6 * radius;
  61. const vertex = new THREE.Vector3();
  62. const vertices = [];
  63. for ( let i = 0; i < 100000; i ++ ) {
  64. vertex.x = Math.random() * 2 - 1;
  65. vertex.y = Math.random() * 2 - 1;
  66. vertex.z = Math.random() * 2 - 1;
  67. vertex.multiplyScalar( radius );
  68. if ( ( vertex.x > inner || vertex.x < - inner ) ||
  69. ( vertex.y > inner || vertex.y < - inner ) ||
  70. ( vertex.z > inner || vertex.z < - inner ) )
  71. vertices.push( vertex.x, vertex.y, vertex.z );
  72. }
  73. vertices1 = vertices.length / 3;
  74. radius = 200;
  75. let boxGeometry1 = new THREE.BoxGeometry( radius, 0.1 * radius, 0.1 * radius, 50, 5, 5 );
  76. // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data
  77. boxGeometry1.deleteAttribute( 'normal' );
  78. boxGeometry1.deleteAttribute( 'uv' );
  79. boxGeometry1 = BufferGeometryUtils.mergeVertices( boxGeometry1 );
  80. const matrix = new THREE.Matrix4();
  81. const position = new THREE.Vector3();
  82. const rotation = new THREE.Euler();
  83. const quaternion = new THREE.Quaternion();
  84. const scale = new THREE.Vector3( 1, 1, 1 );
  85. function addGeo( geo, x, y, z, ry ) {
  86. position.set( x, y, z );
  87. rotation.set( 0, ry, 0 );
  88. matrix.compose( position, quaternion.setFromEuler( rotation ), scale );
  89. const positionAttribute = geo.getAttribute( 'position' );
  90. for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
  91. vertex.fromBufferAttribute( positionAttribute, i );
  92. vertex.applyMatrix4( matrix );
  93. vertices.push( vertex.x, vertex.y, vertex.z );
  94. }
  95. }
  96. // side 1
  97. addGeo( boxGeometry1, 0, 110, 110, 0 );
  98. addGeo( boxGeometry1, 0, 110, - 110, 0 );
  99. addGeo( boxGeometry1, 0, - 110, 110, 0 );
  100. addGeo( boxGeometry1, 0, - 110, - 110, 0 );
  101. // side 2
  102. addGeo( boxGeometry1, 110, 110, 0, Math.PI / 2 );
  103. addGeo( boxGeometry1, 110, - 110, 0, Math.PI / 2 );
  104. addGeo( boxGeometry1, - 110, 110, 0, Math.PI / 2 );
  105. addGeo( boxGeometry1, - 110, - 110, 0, Math.PI / 2 );
  106. // corner edges
  107. let boxGeometry2 = new THREE.BoxGeometry( 0.1 * radius, radius * 1.2, 0.1 * radius, 5, 60, 5 );
  108. boxGeometry2.deleteAttribute( 'normal' );
  109. boxGeometry2.deleteAttribute( 'uv' );
  110. boxGeometry2 = BufferGeometryUtils.mergeVertices( boxGeometry2 );
  111. addGeo( boxGeometry2, 110, 0, 110, 0 );
  112. addGeo( boxGeometry2, 110, 0, - 110, 0 );
  113. addGeo( boxGeometry2, - 110, 0, 110, 0 );
  114. addGeo( boxGeometry2, - 110, 0, - 110, 0 );
  115. const positionAttribute = new THREE.Float32BufferAttribute( vertices, 3 );
  116. const colors = [];
  117. const sizes = [];
  118. const color = new THREE.Color();
  119. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  120. if ( i < vertices1 ) {
  121. color.setHSL( 0.5 + 0.2 * ( i / vertices1 ), 1, 0.5 );
  122. } else {
  123. color.setHSL( 0.1, 1, 0.5 );
  124. }
  125. color.toArray( colors, i * 3 );
  126. sizes[ i ] = i < vertices1 ? 10 : 40;
  127. }
  128. const geometry = new THREE.BufferGeometry();
  129. geometry.setAttribute( 'position', positionAttribute );
  130. geometry.setAttribute( 'ca', new THREE.Float32BufferAttribute( colors, 3 ) );
  131. geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ) );
  132. //
  133. const texture = new THREE.TextureLoader().load( 'textures/sprites/ball.png' );
  134. texture.wrapS = THREE.RepeatWrapping;
  135. texture.wrapT = THREE.RepeatWrapping;
  136. const material = new THREE.ShaderMaterial( {
  137. uniforms: {
  138. amplitude: { value: 1.0 },
  139. color: { value: new THREE.Color( 0xffffff ) },
  140. pointTexture: { value: texture }
  141. },
  142. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  143. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  144. } );
  145. //
  146. object = new THREE.Points( geometry, material );
  147. scene.add( object );
  148. //
  149. renderer = new THREE.WebGLRenderer();
  150. renderer.setPixelRatio( window.devicePixelRatio );
  151. renderer.setSize( WIDTH, HEIGHT );
  152. renderer.setAnimationLoop( animate );
  153. const container = document.getElementById( 'container' );
  154. container.appendChild( renderer.domElement );
  155. stats = new Stats();
  156. container.appendChild( stats.dom );
  157. //
  158. window.addEventListener( 'resize', onWindowResize );
  159. }
  160. function onWindowResize() {
  161. camera.aspect = window.innerWidth / window.innerHeight;
  162. camera.updateProjectionMatrix();
  163. renderer.setSize( window.innerWidth, window.innerHeight );
  164. }
  165. function animate() {
  166. render();
  167. stats.update();
  168. }
  169. function render() {
  170. const time = Date.now() * 0.01;
  171. object.rotation.y = object.rotation.z = 0.02 * time;
  172. const geometry = object.geometry;
  173. const attributes = geometry.attributes;
  174. for ( let i = 0; i < attributes.size.array.length; i ++ ) {
  175. if ( i < vertices1 ) {
  176. attributes.size.array[ i ] = Math.max( 0, 26 + 32 * Math.sin( 0.1 * i + 0.6 * time ) );
  177. }
  178. }
  179. attributes.size.needsUpdate = true;
  180. renderer.render( scene, camera );
  181. }
  182. </script>
  183. </body>
  184. </html>