2
0

webgl_custom_attributes_points3.html 7.3 KB

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