webgl_lights_pointlights.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. font-family: Monospace;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #info {
  14. color: #fff;
  15. position: absolute;
  16. top: 10px;
  17. width: 100%;
  18. text-align: center;
  19. z-index: 100;
  20. display:block;
  21. }
  22. #info a {
  23. color: #ff0080;
  24. font-weight: bold;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="info">
  30. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - point lights WebGL demo.<br />
  31. Walt Disney head by <a href="http://davidoreilly.com/post/18087489343/disneyhead" target="_blank" rel="noopener">David OReilly</a>
  32. </div>
  33. <script src="../build/three.js"></script>
  34. <script src="js/loaders/OBJLoader.js"></script>
  35. <script src="js/Detector.js"></script>
  36. <script src="js/libs/stats.min.js"></script>
  37. <script>
  38. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  39. var camera, scene, renderer,
  40. light1, light2, light3, light4,
  41. object, stats;
  42. var clock = new THREE.Clock();
  43. init();
  44. animate();
  45. function init() {
  46. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  47. camera.position.z = 100;
  48. scene = new THREE.Scene();
  49. //model
  50. var loader = new THREE.OBJLoader();
  51. loader.load( 'models/obj/walt/WaltHead.obj', function ( obj ) {
  52. object = obj;
  53. object.scale.multiplyScalar( 0.8 );
  54. object.position.y = - 30;
  55. scene.add( object );
  56. } );
  57. var sphere = new THREE.SphereBufferGeometry( 0.5, 16, 8 );
  58. //lights
  59. light1 = new THREE.PointLight( 0xff0040, 2, 50 );
  60. light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xff0040 } ) ) );
  61. scene.add( light1 );
  62. light2 = new THREE.PointLight( 0x0040ff, 2, 50 );
  63. light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) ) );
  64. scene.add( light2 );
  65. light3 = new THREE.PointLight( 0x80ff80, 2, 50 );
  66. light3.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x80ff80 } ) ) );
  67. scene.add( light3 );
  68. light4 = new THREE.PointLight( 0xffaa00, 2, 50 );
  69. light4.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) ) );
  70. scene.add( light4 );
  71. //renderer
  72. renderer = new THREE.WebGLRenderer( { antialias: true } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. document.body.appendChild( renderer.domElement );
  76. //stats
  77. stats = new Stats();
  78. document.body.appendChild( stats.dom );
  79. window.addEventListener( 'resize', onWindowResize, false );
  80. }
  81. function onWindowResize() {
  82. camera.aspect = window.innerWidth / window.innerHeight;
  83. camera.updateProjectionMatrix();
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. }
  86. function animate() {
  87. requestAnimationFrame( animate );
  88. render();
  89. stats.update();
  90. }
  91. function render() {
  92. var time = Date.now() * 0.0005;
  93. var delta = clock.getDelta();
  94. if( object ) object.rotation.y -= 0.5 * delta;
  95. light1.position.x = Math.sin( time * 0.7 ) * 30;
  96. light1.position.y = Math.cos( time * 0.5 ) * 40;
  97. light1.position.z = Math.cos( time * 0.3 ) * 30;
  98. light2.position.x = Math.cos( time * 0.3 ) * 30;
  99. light2.position.y = Math.sin( time * 0.5 ) * 40;
  100. light2.position.z = Math.sin( time * 0.7 ) * 30;
  101. light3.position.x = Math.sin( time * 0.7 ) * 30;
  102. light3.position.y = Math.cos( time * 0.3 ) * 40;
  103. light3.position.z = Math.sin( time * 0.5 ) * 30;
  104. light4.position.x = Math.sin( time * 0.3 ) * 30;
  105. light4.position.y = Math.cos( time * 0.7 ) * 40;
  106. light4.position.z = Math.sin( time * 0.5 ) * 30;
  107. renderer.render( scene, camera );
  108. }
  109. </script>
  110. </body>
  111. </html>