webgl_lights_pointlights2.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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">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. particle1, particle2, particle4, particle4, particle5, particle6,
  44. light1, light2, light3, light4, light5, light6;
  45. var FAR = 300;
  46. var clock = new THREE.Clock();
  47. init();
  48. animate();
  49. function init() {
  50. var container = document.getElementById( 'container' );
  51. // CAMERA
  52. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, FAR );
  53. camera.position.set( 0, 15, 150 );
  54. camera.lookAt( new THREE.Vector3() );
  55. // SCENE
  56. scene = new THREE.Scene();
  57. scene.fog = new THREE.Fog( 0x040306, 10, FAR );
  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.BoxGeometry( 0.5, 1, 1 );
  84. //var objectGeometry = new THREE.SphereGeometry( 1.5, 16, 8 );
  85. var objectGeometry = new THREE.TorusGeometry( 1.5, 0.4, 8, 16 );
  86. for ( var i = 0; i < 5000; i ++ ) {
  87. var mesh = new THREE.Mesh( objectGeometry, objectMaterial );
  88. mesh.position.x = 400 * ( 0.5 - Math.random() );
  89. mesh.position.y = 50 * ( 0.5 - Math.random() ) + 25;
  90. mesh.position.z = 200 * ( 0.5 - Math.random() );
  91. mesh.rotation.y = 3.14 * ( 0.5 - Math.random() );
  92. mesh.rotation.x = 3.14 * ( 0.5 - Math.random() );
  93. mesh.matrixAutoUpdate = false;
  94. mesh.updateMatrix();
  95. scene.add( mesh );
  96. }
  97. // LIGHTS
  98. var intensity = 2.5;
  99. var distance = 100;
  100. var decay = 2.0;
  101. var c1 = 0xff0040, c2 = 0x0040ff, c3 = 0x80ff80, c4 = 0xffaa00, c5 = 0x00ffaa, c6 = 0xff1100;
  102. var sphere = new THREE.SphereGeometry( 0.25, 16, 8 );
  103. light1 = new THREE.PointLight( c1, intensity, distance, decay );
  104. light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c1 } ) ) );
  105. scene.add( light1 );
  106. light2 = new THREE.PointLight( c2, intensity, distance, decay );
  107. light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c2 } ) ) );
  108. scene.add( light2 );
  109. light3 = new THREE.PointLight( c3, intensity, distance, decay );
  110. light3.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c3 } ) ) );
  111. scene.add( light3 );
  112. light4 = new THREE.PointLight( c4, intensity, distance, decay );
  113. light4.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c4 } ) ) );
  114. scene.add( light4 );
  115. light5 = new THREE.PointLight( c5, intensity, distance, decay );
  116. light5.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c5 } ) ) );
  117. scene.add( light5 );
  118. light6 = new THREE.PointLight( c6, intensity, distance, decay );
  119. light6.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c6 } ) ) );
  120. scene.add( light6 );
  121. var dlight = new THREE.DirectionalLight( 0xffffff, 0.05 );
  122. dlight.position.set( 0.5, 1, 0 ).normalize();
  123. scene.add( dlight );
  124. // RENDERER
  125. renderer = new THREE.WebGLRenderer( { antialias: false } );
  126. renderer.setClearColor( scene.fog.color );
  127. renderer.setPixelRatio( window.devicePixelRatio );
  128. renderer.setSize( window.innerWidth, window.innerHeight );
  129. container.appendChild( renderer.domElement );
  130. renderer.gammaInput = true;
  131. renderer.gammaOutput = true;
  132. // STATS
  133. stats = new Stats();
  134. container.appendChild( stats.dom );
  135. //
  136. window.addEventListener( 'resize', onWindowResize, false );
  137. }
  138. function onWindowResize() {
  139. camera.aspect = window.innerWidth / window.innerHeight;
  140. camera.updateProjectionMatrix();
  141. renderer.setSize( window.innerWidth, window.innerHeight );
  142. controls.handleResize();
  143. }
  144. //
  145. function animate() {
  146. requestAnimationFrame( animate );
  147. render();
  148. stats.update();
  149. }
  150. function render() {
  151. var time = Date.now() * 0.00025;
  152. var z = 20, d = 150;
  153. light1.position.x = Math.sin( time * 0.7 ) * d;
  154. light1.position.z = Math.cos( time * 0.3 ) * d;
  155. light2.position.x = Math.cos( time * 0.3 ) * d;
  156. light2.position.z = Math.sin( time * 0.7 ) * d;
  157. light3.position.x = Math.sin( time * 0.7 ) * d;
  158. light3.position.z = Math.sin( time * 0.5 ) * d;
  159. light4.position.x = Math.sin( time * 0.3 ) * d;
  160. light4.position.z = Math.sin( time * 0.5 ) * d;
  161. light5.position.x = Math.cos( time * 0.3 ) * d;
  162. light5.position.z = Math.sin( time * 0.5 ) * d;
  163. light6.position.x = Math.cos( time * 0.7 ) * d;
  164. light6.position.z = Math.cos( time * 0.5 ) * d;
  165. controls.update( clock.getDelta() );
  166. renderer.render( scene, camera );
  167. }
  168. </script>
  169. </body>
  170. </html>