webgl_lights_pointlights2.html 7.7 KB

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