webgl_shaders_ocean.html 5.8 KB

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