webgpu_occlusion.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - occlusion</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. <meta property="og:title" content="three.js webgpu - occlusion">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_occlusion.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_occlusion.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Occlusion</span>
  18. </div>
  19. <small>
  20. The plane is green when the sphere is completely occluded.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { uniform } from 'three/tsl';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. let camera, scene, renderer, controls;
  38. class OcclusionNode extends THREE.Node {
  39. constructor( testObject, normalColor, occludedColor ) {
  40. super( 'vec3' );
  41. this.updateType = THREE.NodeUpdateType.OBJECT;
  42. this.uniformNode = uniform( new THREE.Color() );
  43. this.testObject = testObject;
  44. this.normalColor = normalColor;
  45. this.occludedColor = occludedColor;
  46. }
  47. async update( frame ) {
  48. const isOccluded = frame.renderer.isOccluded( this.testObject );
  49. this.uniformNode.value.copy( isOccluded ? this.occludedColor : this.normalColor );
  50. }
  51. setup( /* builder */ ) {
  52. return this.uniformNode;
  53. }
  54. }
  55. init();
  56. async function init() {
  57. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  58. camera.position.z = 7;
  59. scene = new THREE.Scene();
  60. // lights
  61. const ambientLight = new THREE.AmbientLight( 0xb0b0b0 );
  62. const light = new THREE.DirectionalLight( 0xFFFFFF, 1.0 );
  63. light.position.set( 0.32, 0.39, 0.7 );
  64. scene.add( ambientLight );
  65. scene.add( light );
  66. // models
  67. const planeGeometry = new THREE.PlaneGeometry( 2, 2 );
  68. const sphereGeometry = new THREE.SphereGeometry( 0.5 );
  69. const plane = new THREE.Mesh( planeGeometry, new THREE.MeshPhongNodeMaterial( { color: 0x00ff00, side: THREE.DoubleSide } ) );
  70. const sphere = new THREE.Mesh( sphereGeometry, new THREE.MeshPhongNodeMaterial( { color: 0xffff00 } ) );
  71. const instanceUniform = new OcclusionNode( sphere, new THREE.Color( 0x0000ff ), new THREE.Color( 0x00ff00 ) );
  72. plane.material.colorNode = instanceUniform;
  73. sphere.position.z = - 1;
  74. sphere.occlusionTest = true;
  75. scene.add( plane );
  76. scene.add( sphere );
  77. // renderer
  78. renderer = new THREE.WebGPURenderer( { antialias: true } );
  79. renderer.setPixelRatio( window.devicePixelRatio );
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. renderer.setAnimationLoop( render );
  82. document.body.appendChild( renderer.domElement );
  83. // controls
  84. controls = new OrbitControls( camera, renderer.domElement );
  85. controls.minDistance = 3;
  86. controls.maxDistance = 25;
  87. window.addEventListener( 'resize', onWindowResize );
  88. }
  89. function onWindowResize() {
  90. camera.aspect = window.innerWidth / window.innerHeight;
  91. camera.updateProjectionMatrix();
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. }
  94. function render() {
  95. renderer.render( scene, camera );
  96. }
  97. </script>
  98. </body>
  99. </html>
粤ICP备19079148号