webgl_shaders_ocean.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - shaders - ocean</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="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl ocean
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import Stats from './jsm/libs/stats.module.js';
  17. import { GUI } from './jsm/libs/dat.gui.module.js';
  18. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  19. import { Water } from './jsm/objects/Water.js';
  20. import { Sky } from './jsm/objects/Sky.js';
  21. var container, stats;
  22. var camera, scene, renderer, light;
  23. var controls, water, sphere;
  24. init();
  25. animate();
  26. function init() {
  27. container = document.getElementById( 'container' );
  28. //
  29. renderer = new THREE.WebGLRenderer();
  30. renderer.setPixelRatio( window.devicePixelRatio );
  31. renderer.setSize( window.innerWidth, window.innerHeight );
  32. container.appendChild( renderer.domElement );
  33. //
  34. scene = new THREE.Scene();
  35. //
  36. camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 1, 20000 );
  37. camera.position.set( 30, 30, 100 );
  38. //
  39. light = new THREE.DirectionalLight( 0xffffff, 0.8 );
  40. scene.add( light );
  41. // Water
  42. var waterGeometry = new THREE.PlaneBufferGeometry( 10000, 10000 );
  43. water = new Water(
  44. waterGeometry,
  45. {
  46. textureWidth: 512,
  47. textureHeight: 512,
  48. waterNormals: new THREE.TextureLoader().load( 'textures/waternormals.jpg', function ( texture ) {
  49. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  50. } ),
  51. alpha: 1.0,
  52. sunDirection: light.position.clone().normalize(),
  53. sunColor: 0xffffff,
  54. waterColor: 0x001e0f,
  55. distortionScale: 3.7,
  56. fog: scene.fog !== undefined
  57. }
  58. );
  59. water.rotation.x = - Math.PI / 2;
  60. scene.add( water );
  61. // Skybox
  62. var sky = new Sky();
  63. var uniforms = sky.material.uniforms;
  64. uniforms[ 'turbidity' ].value = 10;
  65. uniforms[ 'rayleigh' ].value = 2;
  66. uniforms[ 'luminance' ].value = 1;
  67. uniforms[ 'mieCoefficient' ].value = 0.005;
  68. uniforms[ 'mieDirectionalG' ].value = 0.8;
  69. var parameters = {
  70. distance: 400,
  71. inclination: 0.49,
  72. azimuth: 0.205
  73. };
  74. var cubeCamera = new THREE.CubeCamera( 0.1, 1, 512 );
  75. cubeCamera.renderTarget.texture.generateMipmaps = true;
  76. cubeCamera.renderTarget.texture.minFilter = THREE.LinearMipmapLinearFilter;
  77. scene.background = cubeCamera.renderTarget;
  78. function updateSun() {
  79. var theta = Math.PI * ( parameters.inclination - 0.5 );
  80. var phi = 2 * Math.PI * ( parameters.azimuth - 0.5 );
  81. light.position.x = parameters.distance * Math.cos( phi );
  82. light.position.y = parameters.distance * Math.sin( phi ) * Math.sin( theta );
  83. light.position.z = parameters.distance * Math.sin( phi ) * Math.cos( theta );
  84. sky.material.uniforms[ 'sunPosition' ].value = light.position.copy( light.position );
  85. water.material.uniforms[ 'sunDirection' ].value.copy( light.position ).normalize();
  86. cubeCamera.update( renderer, sky );
  87. }
  88. updateSun();
  89. //
  90. var geometry = new THREE.IcosahedronBufferGeometry( 20, 1 );
  91. var count = geometry.attributes.position.count;
  92. var colors = [];
  93. var color = new THREE.Color();
  94. for ( var i = 0; i < count; i += 3 ) {
  95. color.setHex( Math.random() * 0xffffff );
  96. colors.push( color.r, color.g, color.b );
  97. colors.push( color.r, color.g, color.b );
  98. colors.push( color.r, color.g, color.b );
  99. }
  100. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  101. var material = new THREE.MeshStandardMaterial( {
  102. vertexColors: THREE.VertexColors,
  103. roughness: 0.0,
  104. flatShading: true,
  105. envMap: cubeCamera.renderTarget.texture,
  106. side: THREE.DoubleSide
  107. } );
  108. sphere = new THREE.Mesh( geometry, material );
  109. scene.add( sphere );
  110. //
  111. controls = new OrbitControls( camera, renderer.domElement );
  112. controls.maxPolarAngle = Math.PI * 0.495;
  113. controls.target.set( 0, 10, 0 );
  114. controls.minDistance = 40.0;
  115. controls.maxDistance = 200.0;
  116. controls.update();
  117. //
  118. stats = new Stats();
  119. container.appendChild( stats.dom );
  120. // GUI
  121. var gui = new GUI();
  122. var folder = gui.addFolder( 'Sky' );
  123. folder.add( parameters, 'inclination', 0, 0.5, 0.0001 ).onChange( updateSun );
  124. folder.add( parameters, 'azimuth', 0, 1, 0.0001 ).onChange( updateSun );
  125. folder.open();
  126. var uniforms = water.material.uniforms;
  127. var folder = gui.addFolder( 'Water' );
  128. folder.add( uniforms.distortionScale, 'value', 0, 8, 0.1 ).name( 'distortionScale' );
  129. folder.add( uniforms.size, 'value', 0.1, 10, 0.1 ).name( 'size' );
  130. folder.add( uniforms.alpha, 'value', 0.9, 1, .001 ).name( 'alpha' );
  131. folder.open();
  132. //
  133. window.addEventListener( 'resize', onWindowResize, false );
  134. }
  135. function onWindowResize() {
  136. camera.aspect = window.innerWidth / window.innerHeight;
  137. camera.updateProjectionMatrix();
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. }
  140. function animate() {
  141. requestAnimationFrame( animate );
  142. render();
  143. stats.update();
  144. }
  145. function render() {
  146. var time = performance.now() * 0.001;
  147. sphere.position.y = Math.sin( time ) * 20 + 5;
  148. sphere.rotation.x = time * 0.5;
  149. sphere.rotation.z = time * 0.51;
  150. water.material.uniforms[ 'time' ].value += 1.0 / 60.0;
  151. renderer.render( scene, camera );
  152. }
  153. </script>
  154. </body>
  155. </html>