webgpu_occlusion.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. <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 - occlusion<br />
  12. The plane is green when the sphere is completely occluded.
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.webgpu.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { nodeObject, uniform } from 'three/tsl';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. let camera, scene, renderer, controls;
  28. class OcclusionNode extends THREE.Node {
  29. constructor( testObject, normalColor, occludedColor ) {
  30. super( 'vec3' );
  31. this.updateType = THREE.NodeUpdateType.OBJECT;
  32. this.uniformNode = uniform( new THREE.Color() );
  33. this.testObject = testObject;
  34. this.normalColor = normalColor;
  35. this.occludedColor = occludedColor;
  36. }
  37. async update( frame ) {
  38. const isOccluded = frame.renderer.isOccluded( this.testObject );
  39. this.uniformNode.value.copy( isOccluded ? this.occludedColor : this.normalColor );
  40. }
  41. setup( /* builder */ ) {
  42. return this.uniformNode;
  43. }
  44. }
  45. init();
  46. async function init() {
  47. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  48. camera.position.z = 7;
  49. scene = new THREE.Scene();
  50. // lights
  51. const ambientLight = new THREE.AmbientLight( 0xb0b0b0 );
  52. const light = new THREE.DirectionalLight( 0xFFFFFF, 1.0 );
  53. light.position.set( 0.32, 0.39, 0.7 );
  54. scene.add( ambientLight );
  55. scene.add( light );
  56. // models
  57. const planeGeometry = new THREE.PlaneGeometry( 2, 2 );
  58. const sphereGeometry = new THREE.SphereGeometry( 0.5 );
  59. const plane = new THREE.Mesh( planeGeometry, new THREE.MeshPhongNodeMaterial( { color: 0x00ff00, side: THREE.DoubleSide } ) );
  60. const sphere = new THREE.Mesh( sphereGeometry, new THREE.MeshPhongNodeMaterial( { color: 0xffff00 } ) );
  61. const instanceUniform = nodeObject( new OcclusionNode( sphere, new THREE.Color( 0x00ff00 ), new THREE.Color( 0x0000ff ) ) );
  62. plane.material.colorNode = instanceUniform;
  63. sphere.position.z = - 1;
  64. sphere.occlusionTest = true;
  65. scene.add( plane );
  66. scene.add( sphere );
  67. // renderer
  68. renderer = new THREE.WebGPURenderer( { antialias: true } );
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. // ensure shaders/pipelines are all complete before rendering
  72. await renderer.compileAsync( scene, camera );
  73. renderer.setAnimationLoop( render );
  74. document.body.appendChild( renderer.domElement );
  75. // controls
  76. controls = new OrbitControls( camera, renderer.domElement );
  77. controls.minDistance = 3;
  78. controls.maxDistance = 25;
  79. window.addEventListener( 'resize', onWindowResize );
  80. }
  81. function onWindowResize() {
  82. camera.aspect = window.innerWidth / window.innerHeight;
  83. camera.updateProjectionMatrix();
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. }
  86. function render() {
  87. renderer.render( scene, camera );
  88. }
  89. </script>
  90. </body>
  91. </html>