webgl_lights_pointlights2.html 6.0 KB

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