Files
apt-nl-map/static/Magic4/js/three.js-dev/examples/webgpu_lights_selective.html
2024-12-04 10:21:04 +08:00

183 lines
5.3 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js - WebGPU - Selective Lights</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 - Selective Lights<br />
(Chrome Canary with flag: --enable-unsafe-webgpu)<br />
<b style="color:red">Left: Red lights</b> - <b>Center: All lights</b> - <b style="color:blue">Right: blue light</b>
</div>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from './jsm/libs/stats.module.js';
import { OrbitControls } from './jsm/controls/OrbitControls.js';
import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.js';
import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
import WebGPU from './jsm/renderers/webgpu/WebGPU.js';
import LightsNode from './jsm/renderers/nodes/lights/LightsNode.js';
let camera, scene, renderer,
light1, light2, light3, light4,
stats, controls;
init().then( animate ).catch( error );
async function init() {
if ( WebGPU.isAvailable() === false ) {
document.body.appendChild( WebGPU.getErrorMessage() );
throw 'No WebGPU support';
}
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.z = 70;
scene = new THREE.Scene();
const sphere = new THREE.SphereGeometry( 0.5, 16, 8 );
//lights
light1 = new THREE.PointLight( 0xff0040, 2, 100 );
light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xff0040 } ) ) );
scene.add( light1 );
light2 = new THREE.PointLight( 0x0040ff, 2, 100 );
light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) ) );
scene.add( light2 );
light3 = new THREE.PointLight( 0x80ff80, 2, 100 );
light3.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x80ff80 } ) ) );
scene.add( light3 );
light4 = new THREE.PointLight( 0xffaa00, 2, 100 );
light4.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) ) );
scene.add( light4 );
//light nodes ( selective lights )
const allLightsNode = LightsNode.fromLights( [ light1, light2, light3, light4 ] );
const redLightNode = LightsNode.fromLights( [ light1 ] );
const blueLightNode = LightsNode.fromLights( [ light2 ] );
//models
const geometryTeapot = new TeapotGeometry( 8, 18 );
const leftObject = new THREE.Mesh( geometryTeapot, new THREE.MeshPhongMaterial( { color: 0x555555 } ) );
leftObject.material.lightNode = redLightNode;
leftObject.position.x = - 30;
scene.add( leftObject );
const centerObject = new THREE.Mesh( geometryTeapot, new THREE.MeshPhongMaterial( { color: 0x555555 } ) );
centerObject.material.lightNode = allLightsNode;
scene.add( centerObject );
const rightObject = new THREE.Mesh( geometryTeapot, new THREE.MeshPhongMaterial( { color: 0x555555 } ) );
rightObject.material.lightNode = blueLightNode;
rightObject.position.x = 30;
scene.add( rightObject );
leftObject.material.shininess = centerObject.material.shininess = rightObject.material.shininess = 70;
leftObject.rotation.y = centerObject.rotation.y = rightObject.rotation.y = Math.PI * - 0.5;
leftObject.position.y = centerObject.position.y = rightObject.position.y = - 10;
//renderer
renderer = new WebGPURenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//controls
controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 30;
controls.maxDistance = 150;
//stats
stats = new Stats();
document.body.appendChild( stats.dom );
window.addEventListener( 'resize', onWindowResize );
return renderer.init();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
const time = Date.now() * 0.0005;
light1.position.x = Math.sin( time * 0.7 ) * 30;
light1.position.y = Math.cos( time * 0.5 ) * 40;
light1.position.z = Math.cos( time * 0.3 ) * 30;
light2.position.x = Math.cos( time * 0.3 ) * 30;
light2.position.y = Math.sin( time * 0.5 ) * 40;
light2.position.z = Math.sin( time * 0.7 ) * 30;
light3.position.x = Math.sin( time * 0.7 ) * 30;
light3.position.y = Math.cos( time * 0.3 ) * 40;
light3.position.z = Math.sin( time * 0.5 ) * 30;
light4.position.x = Math.sin( time * 0.3 ) * 30;
light4.position.y = Math.cos( time * 0.7 ) * 40;
light4.position.z = Math.sin( time * 0.5 ) * 30;
renderer.render( scene, camera );
}
function error( error ) {
console.error( error );
}
</script>
</body>
</html>