transparency-doubleside.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 - DoubleSide</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.z = 4;
  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( ...pos ) {
  49. const color = 0xFFFFFF;
  50. const intensity = 3;
  51. const light = new THREE.DirectionalLight( color, intensity );
  52. light.position.set( ...pos );
  53. scene.add( light );
  54. }
  55. addLight( - 1, 2, 4 );
  56. addLight( 1, - 1, - 2 );
  57. const boxWidth = 1;
  58. const boxHeight = 1;
  59. const boxDepth = 1;
  60. const geometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  61. function makeInstance( geometry, color, x, y, z ) {
  62. const material = new THREE.MeshPhongMaterial( {
  63. color,
  64. opacity: 0.5,
  65. transparent: true,
  66. side: THREE.DoubleSide,
  67. } );
  68. const cube = new THREE.Mesh( geometry, material );
  69. scene.add( cube );
  70. cube.position.set( x, y, z );
  71. return cube;
  72. }
  73. function hsl( h, s, l ) {
  74. return ( new THREE.Color() ).setHSL( h, s, l );
  75. }
  76. {
  77. const d = 0.8;
  78. makeInstance( geometry, hsl( 0 / 8, 1, .5 ), - d, - d, - d );
  79. makeInstance( geometry, hsl( 1 / 8, 1, .5 ), d, - d, - d );
  80. makeInstance( geometry, hsl( 2 / 8, 1, .5 ), - d, d, - d );
  81. makeInstance( geometry, hsl( 3 / 8, 1, .5 ), d, d, - d );
  82. makeInstance( geometry, hsl( 4 / 8, 1, .5 ), - d, - d, d );
  83. makeInstance( geometry, hsl( 5 / 8, 1, .5 ), d, - d, d );
  84. makeInstance( geometry, hsl( 6 / 8, 1, .5 ), - d, d, d );
  85. makeInstance( geometry, hsl( 7 / 8, 1, .5 ), d, d, d );
  86. }
  87. function resizeRendererToDisplaySize( renderer ) {
  88. const canvas = renderer.domElement;
  89. const width = canvas.clientWidth;
  90. const height = canvas.clientHeight;
  91. const needResize = canvas.width !== width || canvas.height !== height;
  92. if ( needResize ) {
  93. renderer.setSize( width, height, false );
  94. }
  95. return needResize;
  96. }
  97. let renderRequested = false;
  98. function render() {
  99. renderRequested = undefined;
  100. if ( resizeRendererToDisplaySize( renderer ) ) {
  101. const canvas = renderer.domElement;
  102. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  103. camera.updateProjectionMatrix();
  104. }
  105. renderer.render( scene, camera );
  106. }
  107. render();
  108. function requestRenderIfNotRequested() {
  109. if ( ! renderRequested ) {
  110. renderRequested = true;
  111. requestAnimationFrame( render );
  112. }
  113. }
  114. controls.addEventListener( 'change', requestRenderIfNotRequested );
  115. window.addEventListener( 'resize', requestRenderIfNotRequested );
  116. }
  117. main();
  118. </script>
  119. </html>