webgl_lights_hemisphere.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - hemisphere light</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. <style>
  9. body {
  10. color: #444;
  11. }
  12. a {
  13. color: #08f;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="container"></div>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl hemisphere light example<br/>
  21. flamingo by <a href="http://mirada.com/">mirada</a> from <a href="http://ro.me">rome</a><br/><br/>
  22. <button id="hemisphereButton">toggle hemisphere light</button>
  23. <button id="directionalButton">toggle directional light</button>
  24. </div>
  25. <script type="x-shader/x-vertex" id="vertexShader">
  26. varying vec3 vWorldPosition;
  27. void main() {
  28. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  29. vWorldPosition = worldPosition.xyz;
  30. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  31. }
  32. </script>
  33. <script type="x-shader/x-fragment" id="fragmentShader">
  34. uniform vec3 topColor;
  35. uniform vec3 bottomColor;
  36. uniform float offset;
  37. uniform float exponent;
  38. varying vec3 vWorldPosition;
  39. void main() {
  40. float h = normalize( vWorldPosition + offset ).y;
  41. gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( max( h , 0.0), exponent ), 0.0 ) ), 1.0 );
  42. }
  43. </script>
  44. <script type="module">
  45. import * as THREE from '../build/three.module.js';
  46. import Stats from './jsm/libs/stats.module.js';
  47. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  48. var camera, scene, renderer, dirLight, dirLightHeper, hemiLight, hemiLightHelper;
  49. var mixers = [];
  50. var stats;
  51. var clock = new THREE.Clock();
  52. init();
  53. animate();
  54. function init() {
  55. var container = document.getElementById( 'container' );
  56. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 5000 );
  57. camera.position.set( 0, 0, 250 );
  58. scene = new THREE.Scene();
  59. scene.background = new THREE.Color().setHSL( 0.6, 0, 1 );
  60. scene.fog = new THREE.Fog( scene.background, 1, 5000 );
  61. // LIGHTS
  62. hemiLight = new THREE.HemisphereLight( 0xffffff, 0xffffff, 0.6 );
  63. hemiLight.color.setHSL( 0.6, 1, 0.6 );
  64. hemiLight.groundColor.setHSL( 0.095, 1, 0.75 );
  65. hemiLight.position.set( 0, 50, 0 );
  66. scene.add( hemiLight );
  67. hemiLightHelper = new THREE.HemisphereLightHelper( hemiLight, 10 );
  68. scene.add( hemiLightHelper );
  69. //
  70. dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  71. dirLight.color.setHSL( 0.1, 1, 0.95 );
  72. dirLight.position.set( - 1, 1.75, 1 );
  73. dirLight.position.multiplyScalar( 30 );
  74. scene.add( dirLight );
  75. dirLight.castShadow = true;
  76. dirLight.shadow.mapSize.width = 2048;
  77. dirLight.shadow.mapSize.height = 2048;
  78. var d = 50;
  79. dirLight.shadow.camera.left = - d;
  80. dirLight.shadow.camera.right = d;
  81. dirLight.shadow.camera.top = d;
  82. dirLight.shadow.camera.bottom = - d;
  83. dirLight.shadow.camera.far = 3500;
  84. dirLight.shadow.bias = - 0.0001;
  85. dirLightHeper = new THREE.DirectionalLightHelper( dirLight, 10 );
  86. scene.add( dirLightHeper );
  87. // GROUND
  88. var groundGeo = new THREE.PlaneBufferGeometry( 10000, 10000 );
  89. var groundMat = new THREE.MeshLambertMaterial( { color: 0xffffff } );
  90. groundMat.color.setHSL( 0.095, 1, 0.75 );
  91. var ground = new THREE.Mesh( groundGeo, groundMat );
  92. ground.position.y = - 33;
  93. ground.rotation.x = - Math.PI / 2;
  94. ground.receiveShadow = true;
  95. scene.add( ground );
  96. // SKYDOME
  97. var vertexShader = document.getElementById( 'vertexShader' ).textContent;
  98. var fragmentShader = document.getElementById( 'fragmentShader' ).textContent;
  99. var uniforms = {
  100. "topColor": { value: new THREE.Color( 0x0077ff ) },
  101. "bottomColor": { value: new THREE.Color( 0xffffff ) },
  102. "offset": { value: 33 },
  103. "exponent": { value: 0.6 }
  104. };
  105. uniforms[ "topColor" ].value.copy( hemiLight.color );
  106. scene.fog.color.copy( uniforms[ "bottomColor" ].value );
  107. var skyGeo = new THREE.SphereBufferGeometry( 4000, 32, 15 );
  108. var skyMat = new THREE.ShaderMaterial( {
  109. uniforms: uniforms,
  110. vertexShader: vertexShader,
  111. fragmentShader: fragmentShader,
  112. side: THREE.BackSide
  113. } );
  114. var sky = new THREE.Mesh( skyGeo, skyMat );
  115. scene.add( sky );
  116. // MODEL
  117. var loader = new GLTFLoader();
  118. loader.load( 'models/gltf/Flamingo.glb', function ( gltf ) {
  119. var mesh = gltf.scene.children[ 0 ];
  120. var s = 0.35;
  121. mesh.scale.set( s, s, s );
  122. mesh.position.y = 15;
  123. mesh.rotation.y = - 1;
  124. mesh.castShadow = true;
  125. mesh.receiveShadow = true;
  126. scene.add( mesh );
  127. var mixer = new THREE.AnimationMixer( mesh );
  128. mixer.clipAction( gltf.animations[ 0 ] ).setDuration( 1 ).play();
  129. mixers.push( mixer );
  130. } );
  131. // RENDERER
  132. renderer = new THREE.WebGLRenderer( { antialias: true } );
  133. renderer.setPixelRatio( window.devicePixelRatio );
  134. renderer.setSize( window.innerWidth, window.innerHeight );
  135. container.appendChild( renderer.domElement );
  136. renderer.outputEncoding = THREE.sRGBEncoding;
  137. renderer.shadowMap.enabled = true;
  138. // STATS
  139. stats = new Stats();
  140. container.appendChild( stats.dom );
  141. //
  142. window.addEventListener( 'resize', onWindowResize, false );
  143. var hemisphereButton = document.getElementById( 'hemisphereButton' );
  144. hemisphereButton.addEventListener( 'click', function () {
  145. hemiLight.visible = ! hemiLight.visible;
  146. hemiLightHelper.visible = ! hemiLightHelper.visible;
  147. } );
  148. var directionalButton = document.getElementById( 'directionalButton' );
  149. directionalButton.addEventListener( 'click', function () {
  150. dirLight.visible = ! dirLight.visible;
  151. dirLightHeper.visible = ! dirLightHeper.visible;
  152. } );
  153. }
  154. function onWindowResize() {
  155. camera.aspect = window.innerWidth / window.innerHeight;
  156. camera.updateProjectionMatrix();
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. }
  159. //
  160. function animate() {
  161. requestAnimationFrame( animate );
  162. render();
  163. stats.update();
  164. }
  165. function render() {
  166. var delta = clock.getDelta();
  167. for ( var i = 0; i < mixers.length; i ++ ) {
  168. mixers[ i ].update( delta );
  169. }
  170. renderer.render( scene, camera );
  171. }
  172. </script>
  173. </body>
  174. </html>