123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <!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">
- <!-- WebGPU (For Chrome 94-101), expires 09/01/2022 -->
- <meta http-equiv="origin-trial" content="AoS1pSJwCV3KRe73TO0YgJkK9FZ/qhmvKeafztp0ofiE8uoGrnKzfxGVKKICvoBfL8dgE0zpkp2g/oEJNS0fDgkAAABeeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJHUFUiLCJleHBpcnkiOjE2NTI4MzE5OTksImlzU3ViZG9tYWluIjp0cnVlfQ==">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Materials
- </div>
- <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import WebGPU from './jsm/capabilities/WebGPU.js';
- import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
- import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.js';
- import * as Nodes from './jsm/renderers/nodes/Nodes.js';
- import { ShaderNode, vec3, dot } from './jsm/renderers/nodes/ShaderNode.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 new Error( '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 Nodes.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;
- const opacityTexture = textureLoader.load( './textures/alphaMap.jpg' );
- opacityTexture.wrapS = THREE.RepeatWrapping;
- opacityTexture.wrapT = THREE.RepeatWrapping;
- let material;
- //
- // BASIC
- //
- // PositionNode.LOCAL
- material = new Nodes.MeshBasicNodeMaterial();
- material.colorNode = new Nodes.PositionNode( Nodes.PositionNode.LOCAL );
- materials.push( material );
- // NormalNode.LOCAL
- material = new Nodes.MeshBasicNodeMaterial();
- material.colorNode = new Nodes.NormalNode( Nodes.NormalNode.LOCAL );
- materials.push( material );
- // NormalNode.WORLD
- material = new Nodes.MeshBasicNodeMaterial();
- material.colorNode = new Nodes.NormalNode( Nodes.NormalNode.WORLD );
- materials.push( material );
- // NormalNode.VIEW
- material = new Nodes.MeshBasicNodeMaterial();
- material.colorNode = new Nodes.NormalNode( Nodes.NormalNode.VIEW );
- materials.push( material );
- // TextureNode
- material = new Nodes.MeshBasicNodeMaterial();
- material.colorNode = new Nodes.TextureNode( texture );
- materials.push( material );
- // Opacity
- material = new Nodes.MeshBasicNodeMaterial();
- material.colorNode = new Nodes.ColorNode( new THREE.Color( 0x0099FF ) );
- material.opacityNode = new Nodes.TextureNode( texture );
- material.transparent = true;
- materials.push( material );
- // AlphaTest
- material = new Nodes.MeshBasicNodeMaterial();
- material.colorNode = new Nodes.TextureNode( texture );
- material.opacityNode = new Nodes.TextureNode( opacityTexture );
- material.alphaTestNode = new Nodes.FloatNode( 0.5 );
- materials.push( material );
- //
- // ADVANCED
- //
- // Custom ShaderNode ( desaturate filter )
- const desaturateShaderNode = new ShaderNode( ( input ) => {
- return dot( vec3( 0.299, 0.587, 0.114 ), input.color.xyz );
- } );
- material = new Nodes.MeshBasicNodeMaterial();
- material.colorNode = desaturateShaderNode( { color: new Nodes.TextureNode( texture ) } );
- materials.push( material );
- // Custom WGSL ( desaturate filter )
- const desaturateWGSLNode = new Nodes.FunctionNode( `
- fn desaturate( color:vec3<f32> ) -> vec3<f32> {
- let lum = vec3<f32>( 0.299, 0.587, 0.114 );
- return vec3<f32>( dot( lum, color ) );
- }
- ` );
- material = new Nodes.MeshBasicNodeMaterial();
- material.colorNode = desaturateWGSLNode.call( { color: new Nodes.TextureNode( texture ) } );
- 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>
|