webgl_lights_pointlights2.html 6.0 KB

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