webgl_lights_pointlights2.html 5.8 KB

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