webgl_shaders_ocean.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. let container, stats;
  22. let camera, scene, renderer;
  23. let controls, water, sun, mesh;
  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. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  33. container.appendChild( renderer.domElement );
  34. //
  35. scene = new THREE.Scene();
  36. camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 1, 20000 );
  37. camera.position.set( 30, 30, 100 );
  38. //
  39. sun = new THREE.Vector3();
  40. // Water
  41. const waterGeometry = new THREE.PlaneGeometry( 10000, 10000 );
  42. water = new Water(
  43. waterGeometry,
  44. {
  45. textureWidth: 512,
  46. textureHeight: 512,
  47. waterNormals: new THREE.TextureLoader().load( 'textures/waternormals.jpg', function ( texture ) {
  48. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  49. } ),
  50. sunDirection: new THREE.Vector3(),
  51. sunColor: 0xffffff,
  52. waterColor: 0x001e0f,
  53. distortionScale: 3.7,
  54. fog: scene.fog !== undefined
  55. }
  56. );
  57. water.rotation.x = - Math.PI / 2;
  58. scene.add( water );
  59. // Skybox
  60. const sky = new Sky();
  61. sky.scale.setScalar( 10000 );
  62. scene.add( sky );
  63. const skyUniforms = sky.material.uniforms;
  64. skyUniforms[ 'turbidity' ].value = 10;
  65. skyUniforms[ 'rayleigh' ].value = 2;
  66. skyUniforms[ 'mieCoefficient' ].value = 0.005;
  67. skyUniforms[ 'mieDirectionalG' ].value = 0.8;
  68. const parameters = {
  69. inclination: 0.49,
  70. azimuth: 0.205
  71. };
  72. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  73. function updateSun() {
  74. const theta = Math.PI * ( parameters.inclination - 0.5 );
  75. const phi = 2 * Math.PI * ( parameters.azimuth - 0.5 );
  76. sun.x = Math.cos( phi );
  77. sun.y = Math.sin( phi ) * Math.sin( theta );
  78. sun.z = Math.sin( phi ) * Math.cos( theta );
  79. sky.material.uniforms[ 'sunPosition' ].value.copy( sun );
  80. water.material.uniforms[ 'sunDirection' ].value.copy( sun ).normalize();
  81. scene.environment = pmremGenerator.fromScene( sky ).texture;
  82. }
  83. updateSun();
  84. //
  85. const geometry = new THREE.BoxGeometry( 30, 30, 30 );
  86. const material = new THREE.MeshStandardMaterial( { roughness: 0 } );
  87. mesh = new THREE.Mesh( geometry, material );
  88. scene.add( mesh );
  89. //
  90. controls = new OrbitControls( camera, renderer.domElement );
  91. controls.maxPolarAngle = Math.PI * 0.495;
  92. controls.target.set( 0, 10, 0 );
  93. controls.minDistance = 40.0;
  94. controls.maxDistance = 200.0;
  95. controls.update();
  96. //
  97. stats = new Stats();
  98. container.appendChild( stats.dom );
  99. // GUI
  100. const gui = new GUI();
  101. const folderSky = gui.addFolder( 'Sky' );
  102. folderSky.add( parameters, 'inclination', 0, 0.5, 0.0001 ).onChange( updateSun );
  103. folderSky.add( parameters, 'azimuth', 0, 1, 0.0001 ).onChange( updateSun );
  104. folderSky.open();
  105. const waterUniforms = water.material.uniforms;
  106. const folderWater = gui.addFolder( 'Water' );
  107. folderWater.add( waterUniforms.distortionScale, 'value', 0, 8, 0.1 ).name( 'distortionScale' );
  108. folderWater.add( waterUniforms.size, 'value', 0.1, 10, 0.1 ).name( 'size' );
  109. folderWater.open();
  110. //
  111. window.addEventListener( 'resize', onWindowResize );
  112. }
  113. function onWindowResize() {
  114. camera.aspect = window.innerWidth / window.innerHeight;
  115. camera.updateProjectionMatrix();
  116. renderer.setSize( window.innerWidth, window.innerHeight );
  117. }
  118. function animate() {
  119. requestAnimationFrame( animate );
  120. render();
  121. stats.update();
  122. }
  123. function render() {
  124. const time = performance.now() * 0.001;
  125. mesh.position.y = Math.sin( time ) * 20 + 5;
  126. mesh.rotation.x = time * 0.5;
  127. mesh.rotation.z = time * 0.51;
  128. water.material.uniforms[ 'time' ].value += 1.0 / 60.0;
  129. renderer.render( scene, camera );
  130. }
  131. </script>
  132. </body>
  133. </html>