webgl_layers.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. <!-- Import maps polyfill -->
  23. <!-- Remove this when import maps will be widely supported -->
  24. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.module.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import Stats from 'three/addons/libs/stats.module.js';
  36. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  37. let container, stats;
  38. let camera, scene, renderer;
  39. let theta = 0;
  40. const radius = 5;
  41. init();
  42. animate();
  43. function init() {
  44. container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
  47. camera.layers.enable( 0 ); // enabled by default
  48. camera.layers.enable( 1 );
  49. camera.layers.enable( 2 );
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0xf0f0f0 );
  52. const light = new THREE.PointLight( 0xffffff, 3, 0, 0 );
  53. light.layers.enable( 0 );
  54. light.layers.enable( 1 );
  55. light.layers.enable( 2 );
  56. scene.add( camera );
  57. camera.add( light );
  58. const colors = [ 0xff0000, 0x00ff00, 0x0000ff ];
  59. const geometry = new THREE.BoxGeometry();
  60. for ( let i = 0; i < 300; i ++ ) {
  61. const layer = ( i % 3 );
  62. const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: colors[ layer ] } ) );
  63. object.position.x = Math.random() * 40 - 20;
  64. object.position.y = Math.random() * 40 - 20;
  65. object.position.z = Math.random() * 40 - 20;
  66. object.rotation.x = Math.random() * 2 * Math.PI;
  67. object.rotation.y = Math.random() * 2 * Math.PI;
  68. object.rotation.z = Math.random() * 2 * Math.PI;
  69. object.scale.x = Math.random() + 0.5;
  70. object.scale.y = Math.random() + 0.5;
  71. object.scale.z = Math.random() + 0.5;
  72. object.layers.set( layer );
  73. scene.add( object );
  74. }
  75. renderer = new THREE.WebGLRenderer( { antialias: true } );
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. renderer.useLegacyLights = false;
  79. container.appendChild( renderer.domElement );
  80. stats = new Stats();
  81. container.appendChild( stats.dom );
  82. const layers = {
  83. 'toggle red': function () {
  84. camera.layers.toggle( 0 );
  85. },
  86. 'toggle green': function () {
  87. camera.layers.toggle( 1 );
  88. },
  89. 'toggle blue': function () {
  90. camera.layers.toggle( 2 );
  91. },
  92. 'enable all': function () {
  93. camera.layers.enableAll();
  94. },
  95. 'disable all': function () {
  96. camera.layers.disableAll();
  97. }
  98. };
  99. //
  100. // Init gui
  101. const gui = new GUI();
  102. gui.add( layers, 'toggle red' );
  103. gui.add( layers, 'toggle green' );
  104. gui.add( layers, 'toggle blue' );
  105. gui.add( layers, 'enable all' );
  106. gui.add( layers, 'disable all' );
  107. window.addEventListener( 'resize', onWindowResize );
  108. }
  109. function onWindowResize() {
  110. camera.aspect = window.innerWidth / window.innerHeight;
  111. camera.updateProjectionMatrix();
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. }
  114. //
  115. function animate() {
  116. requestAnimationFrame( animate );
  117. render();
  118. stats.update();
  119. }
  120. function render() {
  121. theta += 0.1;
  122. camera.position.x = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
  123. camera.position.y = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
  124. camera.position.z = radius * Math.cos( THREE.MathUtils.degToRad( theta ) );
  125. camera.lookAt( scene.position );
  126. renderer.render( scene, camera );
  127. }
  128. </script>
  129. </body>
  130. </html>