123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <!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
- </div>
- <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/",
- "three/nodes": "./jsm/nodes/Nodes.js"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { attribute, positionLocal, normalLocal, normalWorld, normalView, color, texture, ShaderNode, func, uv, vec3, triplanarTexture, viewportBottomLeft, MeshBasicNodeMaterial } from 'three/nodes';
- import WebGPU from 'three/addons/capabilities/WebGPU.js';
- import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
- import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
- import Stats from 'three/addons/libs/stats.module.js';
- let stats;
- let camera, scene, renderer;
- const objects = [], materials = [];
- init();
- 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 = attribute( 'color' );
- helper.position.y = - 75;
- scene.add( helper );
- // Materials
- const textureLoader = new THREE.TextureLoader();
- const uvTexture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
- uvTexture.wrapS = THREE.RepeatWrapping;
- uvTexture.wrapT = THREE.RepeatWrapping;
- const opacityTexture = textureLoader.load( './textures/alphaMap.jpg' );
- opacityTexture.wrapS = THREE.RepeatWrapping;
- opacityTexture.wrapT = THREE.RepeatWrapping;
- let material;
- //
- // BASIC
- //
- // PositionLocal
- material = new MeshBasicNodeMaterial();
- material.colorNode = positionLocal;
- materials.push( material );
- // NormalLocal
- material = new MeshBasicNodeMaterial();
- material.colorNode = normalLocal;
- materials.push( material );
- // NormalWorld
- material = new MeshBasicNodeMaterial();
- material.colorNode = normalWorld;
- materials.push( material );
- // NormalView
- material = new MeshBasicNodeMaterial();
- material.colorNode = normalView;
- materials.push( material );
- // Texture
- material = new MeshBasicNodeMaterial();
- material.colorNode = texture( uvTexture );
- materials.push( material );
- // Opacity
- material = new MeshBasicNodeMaterial();
- material.colorNode = color( 0x0099FF );
- material.opacityNode = texture( uvTexture );
- material.transparent = true;
- materials.push( material );
- // AlphaTest
- material = new MeshBasicNodeMaterial();
- material.colorNode = texture( uvTexture );
- material.opacityNode = texture( opacityTexture );
- material.alphaTestNode = 0.5;
- materials.push( material );
- // Normal
- material = new THREE.MeshNormalMaterial();
- material.opacity = .5;
- material.transparent = true;
- materials.push( material );
- //
- // ADVANCED
- //
- // Custom ShaderNode ( desaturate filter )
- const desaturateShaderNode = new ShaderNode( ( input ) => {
- return vec3( 0.299, 0.587, 0.114 ).dot( input.color.xyz );
- } );
- material = new MeshBasicNodeMaterial();
- material.colorNode = desaturateShaderNode.call( { color: texture( uvTexture ) } );
- materials.push( material );
- // Custom ShaderNode(no inputs) > Approach 2
- const desaturateNoInputsShaderNode = new ShaderNode( () => {
- return vec3( 0.299, 0.587, 0.114 ).dot( texture( uvTexture ).xyz );
- } );
- material = new MeshBasicNodeMaterial();
- material.colorNode = desaturateNoInputsShaderNode;
- materials.push( material );
- // Custom WGSL ( desaturate filter )
- const desaturateWGSLNode = func( `
- 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 MeshBasicNodeMaterial();
- material.colorNode = desaturateWGSLNode.call( { color: texture( uvTexture ) } );
- materials.push( material );
- // Custom WGSL ( get texture from keywords )
- const getWGSLTextureSample = func( `
- fn getWGSLTextureSample( tex: texture_2d<f32>, tex_sampler: sampler, uv:vec2<f32> ) -> vec4<f32> {
- return textureSample( tex, tex_sampler, uv ) * vec4<f32>( 0.0, 1.0, 0.0, 1.0 );
- }
- ` );
- const textureNode = texture( uvTexture );
- //getWGSLTextureSample.keywords = { tex: textureNode, tex_sampler: sampler( textureNode ) };
- material = new MeshBasicNodeMaterial();
- material.colorNode = getWGSLTextureSample.call( { tex: textureNode, tex_sampler: textureNode, uv: uv() } );
- materials.push( material );
- // Triplanar Texture Mapping
- material = new MeshBasicNodeMaterial();
- material.colorNode = triplanarTexture( texture( uvTexture ) );
- materials.push( material );
- // Screen Projection Texture
- material = new MeshBasicNodeMaterial();
- material.colorNode = texture( uvTexture, viewportBottomLeft );
- 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 );
- renderer.setAnimationLoop( animate );
- container.appendChild( renderer.domElement );
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize );
- }
- 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() {
- 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 );
- stats.update();
- }
- </script>
- </body>
- </html>
|