webgl_lights_hemisphere.html 7.1 KB

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