1
0

webgpu_shadowmap_opacity.html 4.1 KB

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