webgl_custom_attributes_points3.html 6.4 KB

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