webgl_custom_attributes_points3.html 7.1 KB

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