webgpu_shadowmap_opacity.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.inspector = new Inspector();
  48. container.appendChild( renderer.domElement );
  49. scene = new THREE.Scene();
  50. scene.background = new THREE.Color( 0x9e9eff );
  51. // light + shadow
  52. const hemi = new THREE.AmbientLight( 0xffffff, .5 );
  53. scene.add( hemi );
  54. const dirLight = new THREE.DirectionalLight( 0x6666ff, 10 );
  55. dirLight.position.set( 3, 5, 17 );
  56. dirLight.castShadow = true;
  57. dirLight.shadow.camera.near = 0.1;
  58. dirLight.shadow.camera.far = 50;
  59. dirLight.shadow.camera.right = 5;
  60. dirLight.shadow.camera.left = - 5;
  61. dirLight.shadow.camera.top = 5;
  62. dirLight.shadow.camera.bottom = - 5;
  63. dirLight.shadow.mapSize.width = 2048;
  64. dirLight.shadow.mapSize.height = 2048;
  65. dirLight.shadow.radius = 4;
  66. dirLight.shadow.bias = - 0.0005;
  67. dirLight.shadow.autoUpdate = false;
  68. dirLight.shadow.needsUpdate = true;
  69. scene.add( dirLight );
  70. //
  71. const loader = new GLTFLoader();
  72. const gltf = await loader.loadAsync( 'models/gltf/DragonAttenuation.glb' );
  73. gltf.scene.position.set( 0, 0, - .5 );
  74. const floor = gltf.scene.children[ 0 ];
  75. floor.scale.x += 4;
  76. floor.scale.y += 4;
  77. const dragon = gltf.scene.children[ 1 ];
  78. dragon.position.set( - 1.5, - 0.8, 1 );
  79. const dragon2 = dragon.clone();
  80. dragon2.material = dragon.material.clone();
  81. dragon2.material.attenuationColor = new THREE.Color( 0xff0000 );
  82. dragon2.position.x += 4;
  83. gltf.scene.add( dragon2 );
  84. // shadow node
  85. const customShadow = Fn( ( [ color, opacity = 1 ] ) => {
  86. //return vec4( color, opacity ); // opacity by blending
  87. return mix( 1, color, opacity ); // opacity by color
  88. } );
  89. // apply shadow
  90. floor.receiveShadow = true;
  91. dragon.castShadow = dragon2.castShadow = true;
  92. dragon.receiveShadow = dragon2.receiveShadow = true;
  93. dragon.material.castShadowNode = customShadow( dragon.material.attenuationColor );
  94. dragon2.material.castShadowNode = customShadow( dragon2.material.attenuationColor );
  95. //
  96. scene.add( gltf.scene );
  97. const controls = new OrbitControls( camera, renderer.domElement );
  98. controls.minDistance = 0.1;
  99. controls.maxDistance = 10;
  100. controls.target.set( 0, 0, 0 );
  101. controls.update();
  102. window.addEventListener( 'resize', onWindowResize );
  103. }
  104. function onWindowResize() {
  105. camera.aspect = window.innerWidth / window.innerHeight;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. }
  109. //
  110. function render() {
  111. renderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>
粤ICP备19079148号