webgl_lights_pointlights2.html 5.8 KB

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