webgl_custom_attributes.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. <!-- Import maps polyfill -->
  39. <!-- Remove this when import maps will be widely supported -->
  40. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  41. <script type="importmap">
  42. {
  43. "imports": {
  44. "three": "../build/three.module.js"
  45. }
  46. }
  47. </script>
  48. <script type="module">
  49. import * as THREE from 'three';
  50. import Stats from './jsm/libs/stats.module.js';
  51. let renderer, scene, camera, stats;
  52. let sphere, uniforms;
  53. let displacement, noise;
  54. init();
  55. animate();
  56. function init() {
  57. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  58. camera.position.z = 300;
  59. scene = new THREE.Scene();
  60. scene.background = new THREE.Color( 0x050505 );
  61. uniforms = {
  62. 'amplitude': { value: 1.0 },
  63. 'color': { value: new THREE.Color( 0xff2200 ) },
  64. 'colorTexture': { value: new THREE.TextureLoader().load( 'textures/water.jpg' ) }
  65. };
  66. uniforms[ 'colorTexture' ].value.wrapS = uniforms[ 'colorTexture' ].value.wrapT = THREE.RepeatWrapping;
  67. const shaderMaterial = new THREE.ShaderMaterial( {
  68. uniforms: uniforms,
  69. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  70. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  71. } );
  72. const radius = 50, segments = 128, rings = 64;
  73. const geometry = new THREE.SphereGeometry( radius, segments, rings );
  74. displacement = new Float32Array( geometry.attributes.position.count );
  75. noise = new Float32Array( geometry.attributes.position.count );
  76. for ( let i = 0; i < displacement.length; i ++ ) {
  77. noise[ i ] = Math.random() * 5;
  78. }
  79. geometry.setAttribute( 'displacement', new THREE.BufferAttribute( displacement, 1 ) );
  80. sphere = new THREE.Mesh( geometry, shaderMaterial );
  81. scene.add( sphere );
  82. renderer = new THREE.WebGLRenderer();
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. const container = document.getElementById( 'container' );
  86. container.appendChild( renderer.domElement );
  87. stats = new Stats();
  88. container.appendChild( stats.dom );
  89. //
  90. window.addEventListener( 'resize', onWindowResize );
  91. }
  92. function onWindowResize() {
  93. camera.aspect = window.innerWidth / window.innerHeight;
  94. camera.updateProjectionMatrix();
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. }
  97. function animate() {
  98. requestAnimationFrame( animate );
  99. render();
  100. stats.update();
  101. }
  102. function render() {
  103. const time = Date.now() * 0.01;
  104. sphere.rotation.y = sphere.rotation.z = 0.01 * time;
  105. uniforms[ 'amplitude' ].value = 2.5 * Math.sin( sphere.rotation.y * 0.125 );
  106. uniforms[ 'color' ].value.offsetHSL( 0.0005, 0, 0 );
  107. for ( let i = 0; i < displacement.length; i ++ ) {
  108. displacement[ i ] = Math.sin( 0.1 * i + time );
  109. noise[ i ] += 0.5 * ( 0.5 - Math.random() );
  110. noise[ i ] = THREE.MathUtils.clamp( noise[ i ], - 5, 5 );
  111. displacement[ i ] += noise[ i ];
  112. }
  113. sphere.geometry.attributes.displacement.needsUpdate = true;
  114. renderer.render( scene, camera );
  115. }
  116. </script>
  117. </body>
  118. </html>