webgl_lights_pointlights.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/WebGL.js"></script>
  36. <script src="js/libs/stats.min.js"></script>
  37. <script>
  38. if ( WEBGL.isWebGLAvailable() === false ) {
  39. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  40. }
  41. var camera, scene, renderer,
  42. light1, light2, light3, light4,
  43. object, stats;
  44. var clock = new THREE.Clock();
  45. init();
  46. animate();
  47. function init() {
  48. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  49. camera.position.z = 100;
  50. scene = new THREE.Scene();
  51. //model
  52. var loader = new THREE.OBJLoader();
  53. loader.load( 'models/obj/walt/WaltHead.obj', function ( obj ) {
  54. object = obj;
  55. object.scale.multiplyScalar( 0.8 );
  56. object.position.y = - 30;
  57. scene.add( object );
  58. } );
  59. var sphere = new THREE.SphereBufferGeometry( 0.5, 16, 8 );
  60. //lights
  61. light1 = new THREE.PointLight( 0xff0040, 2, 50 );
  62. light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xff0040 } ) ) );
  63. scene.add( light1 );
  64. light2 = new THREE.PointLight( 0x0040ff, 2, 50 );
  65. light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) ) );
  66. scene.add( light2 );
  67. light3 = new THREE.PointLight( 0x80ff80, 2, 50 );
  68. light3.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x80ff80 } ) ) );
  69. scene.add( light3 );
  70. light4 = new THREE.PointLight( 0xffaa00, 2, 50 );
  71. light4.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) ) );
  72. scene.add( light4 );
  73. //renderer
  74. renderer = new THREE.WebGLRenderer( { antialias: true } );
  75. renderer.setPixelRatio( window.devicePixelRatio );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. document.body.appendChild( renderer.domElement );
  78. //stats
  79. stats = new Stats();
  80. document.body.appendChild( stats.dom );
  81. window.addEventListener( 'resize', onWindowResize, false );
  82. }
  83. function onWindowResize() {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. }
  88. function animate() {
  89. requestAnimationFrame( animate );
  90. render();
  91. stats.update();
  92. }
  93. function render() {
  94. var time = Date.now() * 0.0005;
  95. var delta = clock.getDelta();
  96. if ( object ) object.rotation.y -= 0.5 * delta;
  97. light1.position.x = Math.sin( time * 0.7 ) * 30;
  98. light1.position.y = Math.cos( time * 0.5 ) * 40;
  99. light1.position.z = Math.cos( time * 0.3 ) * 30;
  100. light2.position.x = Math.cos( time * 0.3 ) * 30;
  101. light2.position.y = Math.sin( time * 0.5 ) * 40;
  102. light2.position.z = Math.sin( time * 0.7 ) * 30;
  103. light3.position.x = Math.sin( time * 0.7 ) * 30;
  104. light3.position.y = Math.cos( time * 0.3 ) * 40;
  105. light3.position.z = Math.sin( time * 0.5 ) * 30;
  106. light4.position.x = Math.sin( time * 0.3 ) * 30;
  107. light4.position.y = Math.cos( time * 0.7 ) * 40;
  108. light4.position.z = Math.sin( time * 0.5 ) * 30;
  109. renderer.render( scene, camera );
  110. }
  111. </script>
  112. </body>
  113. </html>