webgpu_shadowmap_opacity.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - shadowmap + opacity</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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - shadowmap + opacity
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.webgpu.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { Fn, vec4 } from 'three/tsl';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  27. let camera, scene, renderer;
  28. init();
  29. async function init() {
  30. const container = document.createElement( 'div' );
  31. document.body.appendChild( container );
  32. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 40 );
  33. camera.position.set( - 4, 2, 6 );
  34. renderer = new THREE.WebGPURenderer( { antialias: true } );
  35. renderer.setPixelRatio( window.devicePixelRatio );
  36. renderer.setSize( window.innerWidth, window.innerHeight );
  37. renderer.setAnimationLoop( render );
  38. renderer.toneMapping = THREE.AgXToneMapping;
  39. renderer.toneMappingExposure = 1.5;
  40. container.appendChild( renderer.domElement );
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0x9e9eff );
  43. // light + shadow
  44. const hemi = new THREE.AmbientLight( 0xffffff, .5 );
  45. scene.add( hemi );
  46. const dirLight = new THREE.DirectionalLight( 0x6666ff, 10 );
  47. dirLight.position.set( 3, 5, 17 );
  48. dirLight.castShadow = true;
  49. dirLight.shadow.camera.near = 0.1;
  50. dirLight.shadow.camera.far = 50;
  51. dirLight.shadow.camera.right = 5;
  52. dirLight.shadow.camera.left = - 5;
  53. dirLight.shadow.camera.top = 5;
  54. dirLight.shadow.camera.bottom = - 5;
  55. dirLight.shadow.mapSize.width = 2048;
  56. dirLight.shadow.mapSize.height = 2048;
  57. dirLight.shadow.radius = 4;
  58. dirLight.shadow.bias = - 0.0005;
  59. dirLight.shadow.autoUpdate = false;
  60. dirLight.shadow.needsUpdate = true;
  61. scene.add( dirLight );
  62. //
  63. const loader = new GLTFLoader();
  64. const gltf = await loader.loadAsync( 'models/gltf/DragonAttenuation.glb' );
  65. gltf.scene.position.set( 0, 0, - .5 );
  66. const floor = gltf.scene.children[ 0 ];
  67. floor.scale.x += 4;
  68. floor.scale.y += 4;
  69. const dragon = gltf.scene.children[ 1 ];
  70. dragon.position.set( - 1.5, - 0.8, 1 );
  71. const dragon2 = dragon.clone();
  72. dragon2.material = dragon.material.clone();
  73. dragon2.material.attenuationColor = new THREE.Color( 0xff0000 );
  74. dragon2.position.x += 4;
  75. gltf.scene.add( dragon2 );
  76. // shadow node
  77. const customShadow = Fn( ( [ color, opacity = .8 ] ) => {
  78. return vec4( color, opacity );
  79. } );
  80. // apply shadow
  81. floor.receiveShadow = true;
  82. dragon.castShadow = dragon2.castShadow = true;
  83. dragon.receiveShadow = dragon2.receiveShadow = true;
  84. dragon.material.shadowNode = customShadow( dragon.material.attenuationColor );
  85. dragon2.material.shadowNode = customShadow( dragon2.material.attenuationColor );
  86. //
  87. scene.add( gltf.scene );
  88. const controls = new OrbitControls( camera, renderer.domElement );
  89. controls.minDistance = 0.1;
  90. controls.maxDistance = 10;
  91. controls.target.set( 0, 0, 0 );
  92. controls.update();
  93. window.addEventListener( 'resize', onWindowResize );
  94. }
  95. function onWindowResize() {
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. //
  101. function render() {
  102. renderer.render( scene, camera );
  103. }
  104. </script>
  105. </body>
  106. </html>