webgl_lights_pointlights2.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #stats { position: absolute; top:0; left: 0 }
  30. #stats #fps { background: transparent !important }
  31. #stats #fps #fpsText { color: #aaa !important }
  32. #stats #fps #fpsGraph { display: none }
  33. </style>
  34. </head>
  35. <body>
  36. <div id="container"></div>
  37. <div id="info">
  38. <a href="http://threejs.org" target="_blank">three.js</a> - point lights WebGL demo
  39. </div>
  40. <script src="../build/three.min.js"></script>
  41. <script src="js/controls/TrackballControls.js"></script>
  42. <script src="js/Detector.js"></script>
  43. <script src="js/libs/stats.min.js"></script>
  44. <script>
  45. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  46. var camera, scene, renderer, controls,
  47. particle1, particle2, particle4, particle4, particle5, particle6,
  48. light1, light2, light3, light4, light5, light6;
  49. var FAR = 300;
  50. var clock = new THREE.Clock();
  51. init();
  52. animate();
  53. function init() {
  54. var container = document.getElementById( 'container' );
  55. // CAMERA
  56. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, FAR );
  57. camera.position.set( 0, 15, 150 );
  58. camera.lookAt( new THREE.Vector3() );
  59. // SCENE
  60. scene = new THREE.Scene();
  61. scene.fog = new THREE.Fog( 0x030303, 10, FAR );
  62. scene.fog.color.setHSV( 0.75, 0.5, 0.025 );
  63. // CONTROLS
  64. var fly = false;
  65. if ( !fly ) {
  66. controls = new THREE.TrackballControls( camera );
  67. controls.target.set( 0, 0, 0 );
  68. controls.rotateSpeed = 1.0;
  69. controls.zoomSpeed = 1.2;
  70. controls.panSpeed = 0.8;
  71. controls.noZoom = false;
  72. controls.noPan = false;
  73. controls.staticMoving = false;
  74. controls.dynamicDampingFactor = 0.15;
  75. controls.keys = [ 65, 83, 68 ];
  76. } else {
  77. controls = new THREE.FirstPersonControls( camera );
  78. controls.movementSpeed = 25;
  79. controls.lookSpeed = 0.05;
  80. controls.lookVertical = true;
  81. controls.lon = -90;
  82. }
  83. // TEXTURES
  84. var texture = THREE.ImageUtils.loadTexture( "textures/disturb.jpg" );
  85. texture.repeat.set( 20, 10 );
  86. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  87. texture.format = THREE.RGBFormat;
  88. var texture2 = THREE.ImageUtils.loadTexture( "textures/planets/moon_1024.jpg" );
  89. texture2.repeat.set( 2, 1 );
  90. texture2.wrapS = texture2.wrapT = THREE.RepeatWrapping;
  91. texture2.format = THREE.RGBFormat;
  92. // MATERIALS
  93. var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, ambient: 0x444444, map: texture } );
  94. var objectMaterial = new THREE.MeshPhongMaterial( { color: 0x000000, ambient: 0x111111, specular: 0xffffff, metal: true, map: texture2 } );
  95. // GROUND
  96. var mesh = new THREE.Mesh( new THREE.PlaneGeometry( 800, 400, 2, 2 ), groundMaterial );
  97. mesh.position.y = -5;
  98. mesh.rotation.x = - Math.PI / 2;
  99. scene.add( mesh );
  100. // OBJECTS
  101. //var objectGeometry = new THREE.CubeGeometry( 0.5, 1, 1 );
  102. //var objectGeometry = new THREE.SphereGeometry( 1.5, 16, 8 );
  103. var objectGeometry = new THREE.TorusGeometry( 1.5, 0.4, 8, 16 );
  104. for ( var i = 0; i < 5000; i ++ ) {
  105. var mesh = new THREE.Mesh( objectGeometry, objectMaterial );
  106. mesh.position.x = 400 * ( 0.5 - Math.random() );
  107. mesh.position.y = 50 * ( 0.5 - Math.random() ) + 25;
  108. mesh.position.z = 200 * ( 0.5 - Math.random() );
  109. mesh.rotation.y = 3.14 * ( 0.5 - Math.random() );
  110. mesh.rotation.x = 3.14 * ( 0.5 - Math.random() );
  111. mesh.matrixAutoUpdate = false;
  112. mesh.updateMatrix();
  113. scene.add( mesh );
  114. }
  115. // LIGHTS
  116. scene.add( new THREE.AmbientLight( 0x111111 ) );
  117. var intensity = 2.5;
  118. var distance = 100;
  119. var c1 = 0xff0040, c2 = 0x0040ff, c3 = 0x80ff80, c4 = 0xffaa00, c5 = 0x00ffaa, c6 = 0xff1100;
  120. //var c1 = 0xffffff, c2 = 0xffffff, c3 = 0xffffff, c4 = 0xffffff, c5 = 0xffffff, c6 = 0xffffff;
  121. light1 = new THREE.PointLight( c1, intensity, distance );
  122. scene.add( light1 );
  123. light2 = new THREE.PointLight( c2, intensity, distance );
  124. scene.add( light2 );
  125. light3 = new THREE.PointLight( c3, intensity, distance );
  126. scene.add( light3 );
  127. light4 = new THREE.PointLight( c4, intensity, distance );
  128. scene.add( light4 );
  129. light5 = new THREE.PointLight( c5, intensity, distance );
  130. scene.add( light5 );
  131. light6 = new THREE.PointLight( c6, intensity, distance );
  132. scene.add( light6 );
  133. var dlight = new THREE.DirectionalLight( 0xffffff, 0.1 );
  134. dlight.position.set( 0.5, -1, 0 ).normalize();
  135. scene.add( dlight );
  136. var sphere = new THREE.SphereGeometry( 0.25, 16, 8 );
  137. var l1 = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c1 } ) );
  138. l1.position = light1.position;
  139. scene.add( l1 );
  140. var l2 = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c2 } ) );
  141. l2.position = light2.position;
  142. scene.add( l2 );
  143. var l3 = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c3 } ) );
  144. l3.position = light3.position;
  145. scene.add( l3 );
  146. var l4 = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c4 } ) );
  147. l4.position = light4.position;
  148. scene.add( l4 );
  149. var l5 = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c5 } ) );
  150. l5.position = light5.position;
  151. scene.add( l5 );
  152. var l6 = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: c6 } ) );
  153. l6.position = light6.position;
  154. scene.add( l6 );
  155. // RENDERER
  156. renderer = new THREE.WebGLRenderer( { antialias: false, clearColor: 0x030303, clearAlpha: 1 } );
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. renderer.setClearColor( scene.fog.color, 1 );
  159. container.appendChild( renderer.domElement );
  160. renderer.gammaInput = true;
  161. renderer.gammaOutput = true;
  162. renderer.physicallyBasedShading = true;
  163. // STATS
  164. stats = new Stats();
  165. container.appendChild( stats.domElement );
  166. //
  167. window.addEventListener( 'resize', onWindowResize, false );
  168. }
  169. function onWindowResize() {
  170. camera.aspect = window.innerWidth / window.innerHeight;
  171. camera.updateProjectionMatrix();
  172. renderer.setSize( window.innerWidth, window.innerHeight );
  173. controls.handleResize();
  174. }
  175. //
  176. function animate() {
  177. requestAnimationFrame( animate );
  178. render();
  179. stats.update();
  180. }
  181. function render() {
  182. var time = Date.now() * 0.00025;
  183. var z = 20, d = 150;
  184. light1.position.x = Math.sin( time * 0.7 ) * d;
  185. light1.position.z = Math.cos( time * 0.3 ) * d;
  186. light2.position.x = Math.cos( time * 0.3 ) * d;
  187. light2.position.z = Math.sin( time * 0.7 ) * d;
  188. light3.position.x = Math.sin( time * 0.7 ) * d;
  189. light3.position.z = Math.sin( time * 0.5 ) * d;
  190. light4.position.x = Math.sin( time * 0.3 ) * d;
  191. light4.position.z = Math.sin( time * 0.5 ) * d;
  192. light5.position.x = Math.cos( time * 0.3 ) * d;
  193. light5.position.z = Math.sin( time * 0.5 ) * d;
  194. light6.position.x = Math.cos( time * 0.7 ) * d;
  195. light6.position.z = Math.cos( time * 0.5 ) * d;
  196. controls.update( clock.getDelta() );
  197. renderer.render( scene, camera );
  198. }
  199. </script>
  200. </body>
  201. </html>