webgl_custom_attributes_lines.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - custom attributes [lines]</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</div>
  11. <div id="container"></div>
  12. <script type="x-shader/x-vertex" id="vertexshader">
  13. uniform float amplitude;
  14. attribute vec3 displacement;
  15. attribute vec3 customColor;
  16. varying vec3 vColor;
  17. void main() {
  18. vec3 newPosition = position + amplitude * displacement;
  19. vColor = customColor;
  20. gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );
  21. }
  22. </script>
  23. <script type="x-shader/x-fragment" id="fragmentshader">
  24. uniform vec3 color;
  25. uniform float opacity;
  26. varying vec3 vColor;
  27. void main() {
  28. gl_FragColor = vec4( vColor * color, opacity );
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from '../build/three.module.js';
  33. import Stats from './jsm/libs/stats.module.js';
  34. var renderer, scene, camera, stats;
  35. var line, uniforms;
  36. var loader = new THREE.FontLoader();
  37. loader.load( 'fonts/helvetiker_bold.typeface.json', function ( font ) {
  38. init( font );
  39. animate();
  40. } );
  41. function init( font ) {
  42. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  43. camera.position.z = 400;
  44. scene = new THREE.Scene();
  45. scene.background = new THREE.Color( 0x050505 );
  46. uniforms = {
  47. amplitude: { value: 5.0 },
  48. opacity: { value: 0.3 },
  49. color: { value: new THREE.Color( 0xffffff ) }
  50. };
  51. var shaderMaterial = new THREE.ShaderMaterial( {
  52. uniforms: uniforms,
  53. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  54. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  55. blending: THREE.AdditiveBlending,
  56. depthTest: false,
  57. transparent: true
  58. } );
  59. var geometry = new THREE.TextBufferGeometry( 'three.js', {
  60. font: font,
  61. size: 50,
  62. height: 15,
  63. curveSegments: 10,
  64. bevelThickness: 5,
  65. bevelSize: 1.5,
  66. bevelEnabled: true,
  67. bevelSegments: 10,
  68. } );
  69. geometry.center();
  70. var count = geometry.attributes.position.count;
  71. var displacement = new THREE.Float32BufferAttribute( count * 3, 3 );
  72. geometry.setAttribute( 'displacement', displacement );
  73. var customColor = new THREE.Float32BufferAttribute( count * 3, 3 );
  74. geometry.setAttribute( 'customColor', customColor );
  75. var color = new THREE.Color( 0xffffff );
  76. for ( var i = 0, l = customColor.count; i < l; i ++ ) {
  77. color.setHSL( i / l, 0.5, 0.5 );
  78. color.toArray( customColor.array, i * customColor.itemSize );
  79. }
  80. line = new THREE.Line( geometry, shaderMaterial );
  81. line.rotation.x = 0.2;
  82. scene.add( line );
  83. renderer = new THREE.WebGLRenderer( { antialias: true } );
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. var container = document.getElementById( 'container' );
  87. container.appendChild( renderer.domElement );
  88. stats = new Stats();
  89. container.appendChild( stats.dom );
  90. //
  91. window.addEventListener( 'resize', onWindowResize, false );
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. }
  98. function animate() {
  99. requestAnimationFrame( animate );
  100. render();
  101. stats.update();
  102. }
  103. function render() {
  104. var time = Date.now() * 0.001;
  105. line.rotation.y = 0.25 * time;
  106. uniforms.amplitude.value = Math.sin( 0.5 * time );
  107. uniforms.color.value.offsetHSL( 0.0005, 0, 0 );
  108. var attributes = line.geometry.attributes;
  109. var array = attributes.displacement.array;
  110. for ( var i = 0, l = array.length; i < l; i += 3 ) {
  111. array[ i ] += 0.3 * ( 0.5 - Math.random() );
  112. array[ i + 1 ] += 0.3 * ( 0.5 - Math.random() );
  113. array[ i + 2 ] += 0.3 * ( 0.5 - Math.random() );
  114. }
  115. attributes.displacement.needsUpdate = true;
  116. renderer.render( scene, camera );
  117. }
  118. </script>
  119. </body>
  120. </html>