You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
earthquake_3d_viewer_front/three/examples/webgpu_occlusion.html

141 lines
3.4 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - occlusion</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - occlusion<br />
The plane is green when the sphere is completely occluded.
</div>
<script type="importmap">
{
"imports": {
"three": "../build/three.webgpu.js",
"three/webgpu": "../build/three.webgpu.js",
"three/tsl": "../build/three.tsl.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { nodeObject, uniform } from 'three/tsl';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
let camera, scene, renderer, controls;
class OcclusionNode extends THREE.Node {
constructor( testObject, normalColor, occludedColor ) {
super( 'vec3' );
this.updateType = THREE.NodeUpdateType.OBJECT;
this.uniformNode = uniform( new THREE.Color() );
this.testObject = testObject;
this.normalColor = normalColor;
this.occludedColor = occludedColor;
}
async update( frame ) {
const isOccluded = frame.renderer.isOccluded( this.testObject );
this.uniformNode.value.copy( isOccluded ? this.occludedColor : this.normalColor );
}
setup( /* builder */ ) {
return this.uniformNode;
}
}
init();
async function init() {
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
camera.position.z = 7;
scene = new THREE.Scene();
// lights
const ambientLight = new THREE.AmbientLight( 0xb0b0b0 );
const light = new THREE.DirectionalLight( 0xFFFFFF, 1.0 );
light.position.set( 0.32, 0.39, 0.7 );
scene.add( ambientLight );
scene.add( light );
// models
const planeGeometry = new THREE.PlaneGeometry( 2, 2 );
const sphereGeometry = new THREE.SphereGeometry( 0.5 );
const plane = new THREE.Mesh( planeGeometry, new THREE.MeshPhongNodeMaterial( { color: 0x00ff00, side: THREE.DoubleSide } ) );
const sphere = new THREE.Mesh( sphereGeometry, new THREE.MeshPhongNodeMaterial( { color: 0xffff00 } ) );
const instanceUniform = nodeObject( new OcclusionNode( sphere, new THREE.Color( 0x0000ff ), new THREE.Color( 0x00ff00 ) ) );
plane.material.colorNode = instanceUniform;
sphere.position.z = - 1;
sphere.occlusionTest = true;
scene.add( plane );
scene.add( sphere );
// renderer
renderer = new THREE.WebGPURenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( render );
document.body.appendChild( renderer.domElement );
// controls
controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 3;
controls.maxDistance = 25;
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>