1
0

webgpu_shadowmap_opacity.html 4.4 KB

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