webgl_custom_attributes.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - custom attributes</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 float displacement;
  15. varying vec3 vNormal;
  16. varying vec2 vUv;
  17. void main() {
  18. vNormal = normal;
  19. vUv = ( 0.5 + amplitude ) * uv + vec2( amplitude );
  20. vec3 newPosition = position + amplitude * normal * vec3( displacement );
  21. gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );
  22. }
  23. </script>
  24. <script type="x-shader/x-fragment" id="fragmentshader">
  25. varying vec3 vNormal;
  26. varying vec2 vUv;
  27. uniform vec3 color;
  28. uniform sampler2D colorTexture;
  29. void main() {
  30. vec3 light = vec3( 0.5, 0.2, 1.0 );
  31. light = normalize( light );
  32. float dProd = dot( vNormal, light ) * 0.5 + 0.5;
  33. vec4 tcolor = texture2D( colorTexture, vUv );
  34. vec4 gray = vec4( vec3( tcolor.r * 0.3 + tcolor.g * 0.59 + tcolor.b * 0.11 ), 1.0 );
  35. gl_FragColor = gray * vec4( vec3( dProd ) * vec3( color ), 1.0 );
  36. }
  37. </script>
  38. <script type="module">
  39. import * as THREE from '../build/three.module.js';
  40. import Stats from './jsm/libs/stats.module.js';
  41. var renderer, scene, camera, stats;
  42. var sphere, uniforms;
  43. var displacement, noise;
  44. init();
  45. animate();
  46. function init() {
  47. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  48. camera.position.z = 300;
  49. scene = new THREE.Scene();
  50. scene.background = new THREE.Color( 0x050505 );
  51. uniforms = {
  52. "amplitude": { value: 1.0 },
  53. "color": { value: new THREE.Color( 0xff2200 ) },
  54. "colorTexture": { value: new THREE.TextureLoader().load( "textures/water.jpg" ) }
  55. };
  56. uniforms[ "colorTexture" ].value.wrapS = uniforms[ "colorTexture" ].value.wrapT = THREE.RepeatWrapping;
  57. var shaderMaterial = new THREE.ShaderMaterial( {
  58. uniforms: uniforms,
  59. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  60. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  61. } );
  62. var radius = 50, segments = 128, rings = 64;
  63. var geometry = new THREE.SphereBufferGeometry( radius, segments, rings );
  64. displacement = new Float32Array( geometry.attributes.position.count );
  65. noise = new Float32Array( geometry.attributes.position.count );
  66. for ( var i = 0; i < displacement.length; i ++ ) {
  67. noise[ i ] = Math.random() * 5;
  68. }
  69. geometry.setAttribute( 'displacement', new THREE.BufferAttribute( displacement, 1 ) );
  70. sphere = new THREE.Mesh( geometry, shaderMaterial );
  71. scene.add( sphere );
  72. renderer = new THREE.WebGLRenderer();
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. var container = document.getElementById( 'container' );
  76. container.appendChild( renderer.domElement );
  77. stats = new Stats();
  78. container.appendChild( stats.dom );
  79. //
  80. window.addEventListener( 'resize', onWindowResize, false );
  81. }
  82. function onWindowResize() {
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. camera.updateProjectionMatrix();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. }
  87. function animate() {
  88. requestAnimationFrame( animate );
  89. render();
  90. stats.update();
  91. }
  92. function render() {
  93. var time = Date.now() * 0.01;
  94. sphere.rotation.y = sphere.rotation.z = 0.01 * time;
  95. uniforms[ "amplitude" ].value = 2.5 * Math.sin( sphere.rotation.y * 0.125 );
  96. uniforms[ "color" ].value.offsetHSL( 0.0005, 0, 0 );
  97. for ( var i = 0; i < displacement.length; i ++ ) {
  98. displacement[ i ] = Math.sin( 0.1 * i + time );
  99. noise[ i ] += 0.5 * ( 0.5 - Math.random() );
  100. noise[ i ] = THREE.MathUtils.clamp( noise[ i ], - 5, 5 );
  101. displacement[ i ] += noise[ i ];
  102. }
  103. sphere.geometry.attributes.displacement.needsUpdate = true;
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>