webgl_layers.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - layers</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. <style>
  9. body {
  10. background-color: #f0f0f0;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl layers
  21. </div>
  22. <script type="module">
  23. import * as THREE from '../build/three.module.js';
  24. import Stats from './jsm/libs/stats.module.js';
  25. import { GUI } from './jsm/libs/dat.gui.module.js';
  26. var container, stats;
  27. var camera, scene, renderer;
  28. var radius = 100, theta = 0;
  29. init();
  30. animate();
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  35. camera.layers.enable( 0 ); // enabled by default
  36. camera.layers.enable( 1 );
  37. camera.layers.enable( 2 );
  38. scene = new THREE.Scene();
  39. scene.background = new THREE.Color( 0xf0f0f0 );
  40. var light = new THREE.PointLight( 0xffffff, 1 );
  41. light.layers.enable( 0 );
  42. light.layers.enable( 1 );
  43. light.layers.enable( 2 );
  44. scene.add( camera );
  45. camera.add( light );
  46. var colors = [ 0xff0000, 0x00ff00, 0x0000ff ];
  47. var geometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
  48. var layer;
  49. for ( var i = 0; i < 300; i ++ ) {
  50. layer = ( i % 3 );
  51. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: colors[ layer ] } ) );
  52. object.position.x = Math.random() * 800 - 400;
  53. object.position.y = Math.random() * 800 - 400;
  54. object.position.z = Math.random() * 800 - 400;
  55. object.rotation.x = Math.random() * 2 * Math.PI;
  56. object.rotation.y = Math.random() * 2 * Math.PI;
  57. object.rotation.z = Math.random() * 2 * Math.PI;
  58. object.scale.x = Math.random() + 0.5;
  59. object.scale.y = Math.random() + 0.5;
  60. object.scale.z = Math.random() + 0.5;
  61. object.layers.set( layer );
  62. scene.add( object );
  63. }
  64. renderer = new THREE.WebGLRenderer();
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. container.appendChild( renderer.domElement );
  68. stats = new Stats();
  69. container.appendChild( stats.dom );
  70. var layers = {
  71. 'toggle red': function() {
  72. camera.layers.toggle( 0 );
  73. },
  74. 'toggle green': function() {
  75. camera.layers.toggle( 1 );
  76. },
  77. 'toggle blue': function() {
  78. camera.layers.toggle( 2 );
  79. },
  80. 'enable all': function() {
  81. camera.layers.enableAll();
  82. },
  83. 'disable all': function() {
  84. camera.layers.disableAll();
  85. }
  86. };
  87. //
  88. // Init gui
  89. var gui = new GUI();
  90. gui.add( layers, 'toggle red' );
  91. gui.add( layers, 'toggle green' );
  92. gui.add( layers, 'toggle blue' );
  93. gui.add( layers, 'enable all' );
  94. gui.add( layers, 'disable all' );
  95. window.addEventListener( 'resize', onWindowResize, false );
  96. }
  97. function onWindowResize() {
  98. camera.aspect = window.innerWidth / window.innerHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. }
  102. //
  103. function animate() {
  104. requestAnimationFrame( animate );
  105. render();
  106. stats.update();
  107. }
  108. function render() {
  109. theta += 0.1;
  110. camera.position.x = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
  111. camera.position.y = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
  112. camera.position.z = radius * Math.cos( THREE.MathUtils.degToRad( theta ) );
  113. camera.lookAt( scene.position );
  114. renderer.render( scene, camera );
  115. }
  116. </script>
  117. </body>
  118. </html>