webgpu_materials.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - WebGPU - Materials</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <!-- WebGPU (For Chrome 94-101), expires 09/01/2022 -->
  9. <meta http-equiv="origin-trial" content="AoS1pSJwCV3KRe73TO0YgJkK9FZ/qhmvKeafztp0ofiE8uoGrnKzfxGVKKICvoBfL8dgE0zpkp2g/oEJNS0fDgkAAABeeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJHUFUiLCJleHBpcnkiOjE2NTI4MzE5OTksImlzU3ViZG9tYWluIjp0cnVlfQ==">
  10. </head>
  11. <body>
  12. <div id="info">
  13. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Materials
  14. </div>
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three-nodes/": "./jsm/nodes/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import * as Nodes from 'three-nodes/Nodes.js';
  27. import WebGPU from './jsm/capabilities/WebGPU.js';
  28. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  29. import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.js';
  30. import { ShaderNode, vec3, dot } from 'three-nodes/ShaderNode.js';
  31. import Stats from './jsm/libs/stats.module.js';
  32. let stats;
  33. let camera, scene, renderer;
  34. const objects = [], materials = [];
  35. init().then( animate ).catch( error );
  36. async function init() {
  37. if ( WebGPU.isAvailable() === false ) {
  38. document.body.appendChild( WebGPU.getErrorMessage() );
  39. throw new Error( 'No WebGPU support' );
  40. }
  41. const container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  44. camera.position.set( 0, 200, 800 );
  45. scene = new THREE.Scene();
  46. // Grid
  47. const helper = new THREE.GridHelper( 1000, 40, 0x303030, 0x303030 );
  48. helper.material.colorNode = new Nodes.AttributeNode( 'color', 'vec3' );
  49. helper.position.y = - 75;
  50. scene.add( helper );
  51. // Materials
  52. const textureLoader = new THREE.TextureLoader();
  53. const texture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
  54. texture.wrapS = THREE.RepeatWrapping;
  55. texture.wrapT = THREE.RepeatWrapping;
  56. const opacityTexture = textureLoader.load( './textures/alphaMap.jpg' );
  57. opacityTexture.wrapS = THREE.RepeatWrapping;
  58. opacityTexture.wrapT = THREE.RepeatWrapping;
  59. let material;
  60. //
  61. // BASIC
  62. //
  63. // PositionNode.LOCAL
  64. material = new Nodes.MeshBasicNodeMaterial();
  65. material.colorNode = new Nodes.PositionNode( Nodes.PositionNode.LOCAL );
  66. materials.push( material );
  67. // NormalNode.LOCAL
  68. material = new Nodes.MeshBasicNodeMaterial();
  69. material.colorNode = new Nodes.NormalNode( Nodes.NormalNode.LOCAL );
  70. materials.push( material );
  71. // NormalNode.WORLD
  72. material = new Nodes.MeshBasicNodeMaterial();
  73. material.colorNode = new Nodes.NormalNode( Nodes.NormalNode.WORLD );
  74. materials.push( material );
  75. // NormalNode.VIEW
  76. material = new Nodes.MeshBasicNodeMaterial();
  77. material.colorNode = new Nodes.NormalNode( Nodes.NormalNode.VIEW );
  78. materials.push( material );
  79. // TextureNode
  80. material = new Nodes.MeshBasicNodeMaterial();
  81. material.colorNode = new Nodes.TextureNode( texture );
  82. materials.push( material );
  83. // Opacity
  84. material = new Nodes.MeshBasicNodeMaterial();
  85. material.colorNode = new Nodes.ColorNode( new THREE.Color( 0x0099FF ) );
  86. material.opacityNode = new Nodes.TextureNode( texture );
  87. material.transparent = true;
  88. materials.push( material );
  89. // AlphaTest
  90. material = new Nodes.MeshBasicNodeMaterial();
  91. material.colorNode = new Nodes.TextureNode( texture );
  92. material.opacityNode = new Nodes.TextureNode( opacityTexture );
  93. material.alphaTestNode = new Nodes.FloatNode( 0.5 );
  94. materials.push( material );
  95. //
  96. // ADVANCED
  97. //
  98. // Custom ShaderNode ( desaturate filter )
  99. const desaturateShaderNode = new ShaderNode( ( input ) => {
  100. return dot( vec3( 0.299, 0.587, 0.114 ), input.color.xyz );
  101. } );
  102. material = new Nodes.MeshBasicNodeMaterial();
  103. material.colorNode = desaturateShaderNode( { color: new Nodes.TextureNode( texture ) } );
  104. materials.push( material );
  105. // Custom WGSL ( desaturate filter )
  106. const desaturateWGSLNode = new Nodes.FunctionNode( `
  107. fn desaturate( color:vec3<f32> ) -> vec3<f32> {
  108. let lum = vec3<f32>( 0.299, 0.587, 0.114 );
  109. return vec3<f32>( dot( lum, color ) );
  110. }
  111. ` );
  112. material = new Nodes.MeshBasicNodeMaterial();
  113. material.colorNode = desaturateWGSLNode.call( { color: new Nodes.TextureNode( texture ) } );
  114. materials.push( material );
  115. //
  116. // Geometry
  117. //
  118. const geometry = new TeapotGeometry( 50, 18 );
  119. for ( let i = 0, l = materials.length; i < l; i ++ ) {
  120. addMesh( geometry, materials[ i ] );
  121. }
  122. //
  123. renderer = new WebGPURenderer();
  124. renderer.setPixelRatio( window.devicePixelRatio );
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. container.appendChild( renderer.domElement );
  127. //
  128. stats = new Stats();
  129. container.appendChild( stats.dom );
  130. //
  131. window.addEventListener( 'resize', onWindowResize );
  132. return renderer.init();
  133. }
  134. function addMesh( geometry, material ) {
  135. const mesh = new THREE.Mesh( geometry, material );
  136. mesh.position.x = ( objects.length % 4 ) * 200 - 400;
  137. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  138. mesh.rotation.x = Math.random() * 200 - 100;
  139. mesh.rotation.y = Math.random() * 200 - 100;
  140. mesh.rotation.z = Math.random() * 200 - 100;
  141. objects.push( mesh );
  142. scene.add( mesh );
  143. }
  144. function onWindowResize() {
  145. camera.aspect = window.innerWidth / window.innerHeight;
  146. camera.updateProjectionMatrix();
  147. renderer.setSize( window.innerWidth, window.innerHeight );
  148. }
  149. //
  150. function animate() {
  151. requestAnimationFrame( animate );
  152. render();
  153. stats.update();
  154. }
  155. function render() {
  156. const timer = 0.0001 * Date.now();
  157. camera.position.x = Math.cos( timer ) * 1000;
  158. camera.position.z = Math.sin( timer ) * 1000;
  159. camera.lookAt( scene.position );
  160. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  161. const object = objects[ i ];
  162. object.rotation.x += 0.01;
  163. object.rotation.y += 0.005;
  164. }
  165. renderer.render( scene, camera );
  166. }
  167. function error( error ) {
  168. console.error( error );
  169. }
  170. </script>
  171. </body>
  172. </html>