210 lines
4.9 KiB
HTML
210 lines
4.9 KiB
HTML
|
|
<!DOCTYPE html>
|
||
|
|
<html lang="en">
|
||
|
|
<head>
|
||
|
|
<title>three.js - WebGPU - Materials</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 - Materials<br/>(Chrome Canary with flag: --enable-unsafe-webgpu)
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<script type="importmap">
|
||
|
|
{
|
||
|
|
"imports": {
|
||
|
|
"three": "../build/three.module.js"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
</script>
|
||
|
|
<script type="module">
|
||
|
|
|
||
|
|
import * as THREE from 'three';
|
||
|
|
|
||
|
|
import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
|
||
|
|
import WebGPU from './jsm/renderers/webgpu/WebGPU.js';
|
||
|
|
|
||
|
|
import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.js';
|
||
|
|
|
||
|
|
import AttributeNode from './jsm/renderers/nodes/core/AttributeNode.js';
|
||
|
|
import PositionNode from './jsm/renderers/nodes/accessors/PositionNode.js';
|
||
|
|
import NormalNode from './jsm/renderers/nodes/accessors/NormalNode.js';
|
||
|
|
import TextureNode from './jsm/renderers/nodes/inputs/TextureNode.js';
|
||
|
|
|
||
|
|
import Stats from './jsm/libs/stats.module.js';
|
||
|
|
|
||
|
|
let stats;
|
||
|
|
|
||
|
|
let camera, scene, renderer;
|
||
|
|
|
||
|
|
const objects = [], materials = [];
|
||
|
|
|
||
|
|
init().then( animate ).catch( error );
|
||
|
|
|
||
|
|
async function init() {
|
||
|
|
|
||
|
|
if ( WebGPU.isAvailable() === false ) {
|
||
|
|
|
||
|
|
document.body.appendChild( WebGPU.getErrorMessage() );
|
||
|
|
|
||
|
|
throw 'No WebGPU support';
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
const container = document.createElement( 'div' );
|
||
|
|
document.body.appendChild( container );
|
||
|
|
|
||
|
|
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
|
||
|
|
camera.position.set( 0, 200, 800 );
|
||
|
|
|
||
|
|
scene = new THREE.Scene();
|
||
|
|
|
||
|
|
// Grid
|
||
|
|
|
||
|
|
const helper = new THREE.GridHelper( 1000, 40, 0x303030, 0x303030 );
|
||
|
|
helper.material.colorNode = new AttributeNode( 'color', 'vec3' );
|
||
|
|
helper.position.y = - 75;
|
||
|
|
scene.add( helper );
|
||
|
|
|
||
|
|
// Materials
|
||
|
|
|
||
|
|
const textureLoader = new THREE.TextureLoader();
|
||
|
|
|
||
|
|
const texture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
|
||
|
|
texture.wrapS = THREE.RepeatWrapping;
|
||
|
|
texture.wrapT = THREE.RepeatWrapping;
|
||
|
|
|
||
|
|
let material;
|
||
|
|
|
||
|
|
// PositionNode.LOCAL
|
||
|
|
material = new THREE.MeshBasicMaterial();
|
||
|
|
material.colorNode = new PositionNode( PositionNode.LOCAL );
|
||
|
|
materials.push( material );
|
||
|
|
|
||
|
|
// NormalNode.LOCAL
|
||
|
|
material = new THREE.MeshBasicMaterial();
|
||
|
|
material.colorNode = new NormalNode( NormalNode.LOCAL );
|
||
|
|
materials.push( material );
|
||
|
|
|
||
|
|
// NormalNode.WORLD
|
||
|
|
material = new THREE.MeshBasicMaterial();
|
||
|
|
material.colorNode = new NormalNode( NormalNode.WORLD );
|
||
|
|
materials.push( material );
|
||
|
|
|
||
|
|
// NormalNode.VIEW
|
||
|
|
material = new THREE.MeshBasicMaterial();
|
||
|
|
material.colorNode = new NormalNode( NormalNode.VIEW );
|
||
|
|
materials.push( material );
|
||
|
|
|
||
|
|
// TextureNode
|
||
|
|
material = new THREE.MeshBasicMaterial();
|
||
|
|
material.colorNode = new TextureNode( texture );
|
||
|
|
materials.push( material );
|
||
|
|
|
||
|
|
// Opacity
|
||
|
|
material = new THREE.MeshBasicMaterial();
|
||
|
|
material.opacityNode = new TextureNode( texture );
|
||
|
|
material.transparent = true;
|
||
|
|
materials.push( material );
|
||
|
|
|
||
|
|
// Geometry
|
||
|
|
|
||
|
|
const geometry = new TeapotGeometry( 50, 18 );
|
||
|
|
|
||
|
|
for ( let i = 0, l = materials.length; i < l; i ++ ) {
|
||
|
|
|
||
|
|
addMesh( geometry, materials[ i ] );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
//
|
||
|
|
|
||
|
|
renderer = new WebGPURenderer();
|
||
|
|
renderer.setPixelRatio( window.devicePixelRatio );
|
||
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
||
|
|
container.appendChild( renderer.domElement );
|
||
|
|
|
||
|
|
//
|
||
|
|
|
||
|
|
stats = new Stats();
|
||
|
|
container.appendChild( stats.dom );
|
||
|
|
|
||
|
|
//
|
||
|
|
|
||
|
|
window.addEventListener( 'resize', onWindowResize );
|
||
|
|
|
||
|
|
return renderer.init();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
function addMesh( geometry, material ) {
|
||
|
|
|
||
|
|
const mesh = new THREE.Mesh( geometry, material );
|
||
|
|
|
||
|
|
mesh.position.x = ( objects.length % 4 ) * 200 - 400;
|
||
|
|
mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
|
||
|
|
|
||
|
|
mesh.rotation.x = Math.random() * 200 - 100;
|
||
|
|
mesh.rotation.y = Math.random() * 200 - 100;
|
||
|
|
mesh.rotation.z = Math.random() * 200 - 100;
|
||
|
|
|
||
|
|
objects.push( mesh );
|
||
|
|
|
||
|
|
scene.add( mesh );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
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 timer = 0.0001 * Date.now();
|
||
|
|
|
||
|
|
camera.position.x = Math.cos( timer ) * 1000;
|
||
|
|
camera.position.z = Math.sin( timer ) * 1000;
|
||
|
|
|
||
|
|
camera.lookAt( scene.position );
|
||
|
|
|
||
|
|
for ( let i = 0, l = objects.length; i < l; i ++ ) {
|
||
|
|
|
||
|
|
const object = objects[ i ];
|
||
|
|
|
||
|
|
object.rotation.x += 0.01;
|
||
|
|
object.rotation.y += 0.005;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
renderer.render( scene, camera );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
function error( error ) {
|
||
|
|
|
||
|
|
console.error( error );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
</script>
|
||
|
|
|
||
|
|
</body>
|
||
|
|
</html>
|