webgl_custom_attributes_points3.html 7.0 KB

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