transparency-intersecting-planes.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Transparency - Intersecting Planes</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../../build/three.module.js",
  27. "three/addons/": "../../examples/jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. function main() {
  35. const canvas = document.querySelector( '#c' );
  36. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  37. const fov = 75;
  38. const aspect = 2; // the canvas default
  39. const near = 0.1;
  40. const far = 25;
  41. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  42. camera.position.set( 0.5, 1, 0.5 );
  43. const controls = new OrbitControls( camera, canvas );
  44. controls.target.set( 0, 0, 0 );
  45. controls.update();
  46. const scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 'white' );
  48. function addLight( x, y, z ) {
  49. const color = 0xFFFFFF;
  50. const intensity = 3;
  51. const light = new THREE.DirectionalLight( color, intensity );
  52. light.position.set( x, y, z );
  53. scene.add( light );
  54. }
  55. addLight( - 1, 2, 4 );
  56. addLight( 1, - 1, - 2 );
  57. const planeWidth = 1;
  58. const planeHeight = 1;
  59. const geometry = new THREE.PlaneGeometry( planeWidth, planeHeight );
  60. const loader = new THREE.TextureLoader();
  61. function makeInstance( geometry, color, rotY, url ) {
  62. const texture = loader.load( url, render );
  63. texture.colorSpace = THREE.SRGBColorSpace;
  64. const material = new THREE.MeshPhongMaterial( {
  65. color,
  66. map: texture,
  67. opacity: 0.5,
  68. transparent: true,
  69. side: THREE.DoubleSide,
  70. } );
  71. const mesh = new THREE.Mesh( geometry, material );
  72. scene.add( mesh );
  73. mesh.rotation.y = rotY;
  74. }
  75. makeInstance( geometry, 'pink', 0, 'resources/images/happyface.png' ); /* threejs.org: url */
  76. makeInstance( geometry, 'lightblue', Math.PI * 0.5, 'resources/images/hmmmface.png' ); /* threejs.org: url */
  77. function resizeRendererToDisplaySize( renderer ) {
  78. const canvas = renderer.domElement;
  79. const width = canvas.clientWidth;
  80. const height = canvas.clientHeight;
  81. const needResize = canvas.width !== width || canvas.height !== height;
  82. if ( needResize ) {
  83. renderer.setSize( width, height, false );
  84. }
  85. return needResize;
  86. }
  87. let renderRequested = false;
  88. function render() {
  89. renderRequested = undefined;
  90. if ( resizeRendererToDisplaySize( renderer ) ) {
  91. const canvas = renderer.domElement;
  92. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  93. camera.updateProjectionMatrix();
  94. }
  95. renderer.render( scene, camera );
  96. }
  97. render();
  98. function requestRenderIfNotRequested() {
  99. if ( ! renderRequested ) {
  100. renderRequested = true;
  101. requestAnimationFrame( render );
  102. }
  103. }
  104. controls.addEventListener( 'change', requestRenderIfNotRequested );
  105. window.addEventListener( 'resize', requestRenderIfNotRequested );
  106. }
  107. main();
  108. </script>
  109. </html>