webgl_lights_pointlights2.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - point lights</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. <style>
  8. body {
  9. background-color: #000;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #info {
  14. position: absolute;
  15. top: 0px; width: 100%;
  16. color: #ffffff;
  17. padding: 5px;
  18. font-family: Monospace;
  19. font-size: 13px;
  20. text-align: center;
  21. }
  22. a {
  23. color: #ff0080;
  24. text-decoration: none;
  25. }
  26. a:hover {
  27. color: #0080ff;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="container"></div>
  33. <div id="info">
  34. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - point lights WebGL demo
  35. </div>
  36. <script src="../build/three.js"></script>
  37. <script src="js/controls/TrackballControls.js"></script>
  38. <script src="js/Detector.js"></script>
  39. <script src="js/libs/stats.min.js"></script>
  40. <script>
  41. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  42. var camera, scene, renderer, controls;
  43. var light1, light2, light3, light4, light5, light6;
  44. var clock = new THREE.Clock();
  45. var stats;
  46. init();
  47. animate();
  48. function init() {
  49. var container = document.getElementById( 'container' );
  50. // CAMERA
  51. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 300 );
  52. camera.position.set( 0, 15, 150 );
  53. camera.lookAt( new THREE.Vector3() );
  54. // SCENE
  55. scene = new THREE.Scene();
  56. scene.background = new THREE.Color( 0x040306 );
  57. scene.fog = new THREE.Fog( 0x040306, 10, 300 );
  58. // CONTROLS
  59. controls = new THREE.TrackballControls( camera );
  60. controls.rotateSpeed = 1.0;
  61. controls.zoomSpeed = 1.2;
  62. controls.panSpeed = 0.8;
  63. controls.noZoom = false;
  64. controls.noPan = false;
  65. controls.staticMoving = false;
  66. controls.dynamicDampingFactor = 0.15;
  67. controls.keys = [ 65, 83, 68 ];
  68. // TEXTURES
  69. var textureLoader = new THREE.TextureLoader();
  70. var texture = textureLoader.load( "textures/disturb.jpg" );
  71. texture.repeat.set( 20, 10 );
  72. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  73. texture.format = THREE.RGBFormat;
  74. // MATERIALS
  75. var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, map: texture } );
  76. var objectMaterial = new THREE.MeshStandardMaterial( { color: 0xffffff, roughness: 0.5, metalness: 1.0 } );
  77. // GROUND
  78. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 800, 400, 2, 2 ), groundMaterial );
  79. mesh.position.y = - 5;
  80. mesh.rotation.x = - Math.PI / 2;
  81. scene.add( mesh );
  82. // OBJECTS
  83. var objectGeometry = new THREE.TorusBufferGeometry( 1.5, 0.4, 8, 16 );
  84. for ( var i = 0; i < 5000; i ++ ) {
  85. var mesh = new THREE.Mesh( objectGeometry, objectMaterial );
  86. mesh.position.x = 400 * ( 0.5 - Math.random() );
  87. mesh.position.y = 50 * ( 0.5 - Math.random() ) + 25;
  88. mesh.position.z = 200 * ( 0.5 - Math.random() );
  89. mesh.rotation.y = 3.14 * ( 0.5 - Math.random() );
  90. mesh.rotation.x = 3.14 * ( 0.5 - Math.random() );
  91. mesh.matrixAutoUpdate = false;
  92. mesh.updateMatrix();
  93. scene.add( mesh );
  94. }
  95. // LIGHTS
  96. var intensity = 2.5;
  97. var distance = 100;
  98. var decay = 2.0;
  99. var c1 = 0xff0040, c2 = 0x0040ff, c3 = 0x80ff80, c4 = 0xffaa00, c5 = 0x00ffaa, c6 = 0xff1100;
  100. var sphere = new THREE.SphereBufferGeometry( 0.25, 16, 8 );
  101. light1 = new THREE.PointLight( c1, intensity, distance, decay );
  102. light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c1 } ) ) );
  103. scene.add( light1 );
  104. light2 = new THREE.PointLight( c2, intensity, distance, decay );
  105. light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c2 } ) ) );
  106. scene.add( light2 );
  107. light3 = new THREE.PointLight( c3, intensity, distance, decay );
  108. light3.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c3 } ) ) );
  109. scene.add( light3 );
  110. light4 = new THREE.PointLight( c4, intensity, distance, decay );
  111. light4.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c4 } ) ) );
  112. scene.add( light4 );
  113. light5 = new THREE.PointLight( c5, intensity, distance, decay );
  114. light5.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c5 } ) ) );
  115. scene.add( light5 );
  116. light6 = new THREE.PointLight( c6, intensity, distance, decay );
  117. light6.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c6 } ) ) );
  118. scene.add( light6 );
  119. var dlight = new THREE.DirectionalLight( 0xffffff, 0.05 );
  120. dlight.position.set( 0.5, 1, 0 ).normalize();
  121. scene.add( dlight );
  122. // RENDERER
  123. renderer = new THREE.WebGLRenderer( { antialias: true } );
  124. renderer.setPixelRatio( window.devicePixelRatio );
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. container.appendChild( renderer.domElement );
  127. renderer.gammaInput = true;
  128. renderer.gammaOutput = true;
  129. // STATS
  130. stats = new Stats();
  131. container.appendChild( stats.dom );
  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. controls.handleResize();
  140. }
  141. //
  142. function animate() {
  143. requestAnimationFrame( animate );
  144. render();
  145. stats.update();
  146. }
  147. function render() {
  148. var time = Date.now() * 0.00025;
  149. var z = 20, d = 150;
  150. light1.position.x = Math.sin( time * 0.7 ) * d;
  151. light1.position.z = Math.cos( time * 0.3 ) * d;
  152. light2.position.x = Math.cos( time * 0.3 ) * d;
  153. light2.position.z = Math.sin( time * 0.7 ) * d;
  154. light3.position.x = Math.sin( time * 0.7 ) * d;
  155. light3.position.z = Math.sin( time * 0.5 ) * d;
  156. light4.position.x = Math.sin( time * 0.3 ) * d;
  157. light4.position.z = Math.sin( time * 0.5 ) * d;
  158. light5.position.x = Math.cos( time * 0.3 ) * d;
  159. light5.position.z = Math.sin( time * 0.5 ) * d;
  160. light6.position.x = Math.cos( time * 0.7 ) * d;
  161. light6.position.z = Math.cos( time * 0.5 ) * d;
  162. controls.update( clock.getDelta() );
  163. renderer.render( scene, camera );
  164. }
  165. </script>
  166. </body>
  167. </html>