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