webgpu_occlusion.html 3.6 KB

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