2
0

webgpu_materials.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Materials
  12. </div>
  13. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/",
  19. "three/nodes": "./jsm/nodes/Nodes.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import * as Nodes from 'three/nodes';
  26. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  27. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  28. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  29. import { ShaderNode, vec3, dot, triplanarTexture, viewportBottomLeft } from 'three/nodes';
  30. import Stats from 'three/addons/libs/stats.module.js';
  31. let stats;
  32. let camera, scene, renderer;
  33. const objects = [], materials = [];
  34. init();
  35. function init() {
  36. if ( WebGPU.isAvailable() === false ) {
  37. document.body.appendChild( WebGPU.getErrorMessage() );
  38. throw new Error( 'No WebGPU support' );
  39. }
  40. const container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  43. camera.position.set( 0, 200, 800 );
  44. scene = new THREE.Scene();
  45. // Grid
  46. const helper = new THREE.GridHelper( 1000, 40, 0x303030, 0x303030 );
  47. helper.material.colorNode = new Nodes.AttributeNode( 'color' );
  48. helper.position.y = - 75;
  49. scene.add( helper );
  50. // Materials
  51. const textureLoader = new THREE.TextureLoader();
  52. const texture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
  53. texture.wrapS = THREE.RepeatWrapping;
  54. texture.wrapT = THREE.RepeatWrapping;
  55. const opacityTexture = textureLoader.load( './textures/alphaMap.jpg' );
  56. opacityTexture.wrapS = THREE.RepeatWrapping;
  57. opacityTexture.wrapT = THREE.RepeatWrapping;
  58. let material;
  59. //
  60. // BASIC
  61. //
  62. // PositionNode.LOCAL
  63. material = new Nodes.MeshBasicNodeMaterial();
  64. material.colorNode = new Nodes.PositionNode( Nodes.PositionNode.LOCAL );
  65. materials.push( material );
  66. // NormalNode.LOCAL
  67. material = new Nodes.MeshBasicNodeMaterial();
  68. material.colorNode = new Nodes.NormalNode( Nodes.NormalNode.LOCAL );
  69. materials.push( material );
  70. // NormalNode.WORLD
  71. material = new Nodes.MeshBasicNodeMaterial();
  72. material.colorNode = new Nodes.NormalNode( Nodes.NormalNode.WORLD );
  73. materials.push( material );
  74. // NormalNode.VIEW
  75. material = new Nodes.MeshBasicNodeMaterial();
  76. material.colorNode = new Nodes.NormalNode( Nodes.NormalNode.VIEW );
  77. materials.push( material );
  78. // TextureNode
  79. material = new Nodes.MeshBasicNodeMaterial();
  80. material.colorNode = new Nodes.TextureNode( texture );
  81. materials.push( material );
  82. // Opacity
  83. material = new Nodes.MeshBasicNodeMaterial();
  84. material.colorNode = new Nodes.UniformNode( new THREE.Color( 0x0099FF ) );
  85. material.opacityNode = new Nodes.TextureNode( texture );
  86. material.transparent = true;
  87. materials.push( material );
  88. // AlphaTest
  89. material = new Nodes.MeshBasicNodeMaterial();
  90. material.colorNode = new Nodes.TextureNode( texture );
  91. material.opacityNode = new Nodes.TextureNode( opacityTexture );
  92. material.alphaTestNode = new Nodes.UniformNode( 0.5 );
  93. materials.push( material );
  94. //
  95. // ADVANCED
  96. //
  97. // Custom ShaderNode ( desaturate filter )
  98. const desaturateShaderNode = new ShaderNode( ( input ) => {
  99. return dot( vec3( 0.299, 0.587, 0.114 ), input.color.xyz );
  100. } );
  101. material = new Nodes.MeshBasicNodeMaterial();
  102. material.colorNode = desaturateShaderNode.call( { color: new Nodes.TextureNode( texture ) } );
  103. materials.push( material );
  104. // Custom ShaderNode(no inputs) > Approach 2
  105. const desaturateNoInputsShaderNode = new ShaderNode( () => {
  106. return dot( vec3( 0.299, 0.587, 0.114 ), Nodes.texture( texture ).xyz );
  107. } );
  108. material = new Nodes.MeshBasicNodeMaterial();
  109. material.colorNode = desaturateNoInputsShaderNode;
  110. materials.push( material );
  111. // Custom WGSL ( desaturate filter )
  112. const desaturateWGSLNode = new Nodes.FunctionNode( `
  113. fn desaturate( color:vec3<f32> ) -> vec3<f32> {
  114. let lum = vec3<f32>( 0.299, 0.587, 0.114 );
  115. return vec3<f32>( dot( lum, color ) );
  116. }
  117. ` );
  118. material = new Nodes.MeshBasicNodeMaterial();
  119. material.colorNode = desaturateWGSLNode.call( { color: new Nodes.TextureNode( texture ) } );
  120. materials.push( material );
  121. // Custom WGSL ( get texture from keywords )
  122. const getWGSLTextureSample = new Nodes.FunctionNode( `
  123. fn getWGSLTextureSample( tex: texture_2d<f32>, tex_sampler: sampler, uv:vec2<f32> ) -> vec4<f32> {
  124. return textureSample( tex, tex_sampler, uv ) * vec4<f32>( 0.0, 1.0, 0.0, 1.0 );
  125. }
  126. ` );
  127. const textureNode = new Nodes.TextureNode( texture );
  128. //getWGSLTextureSample.keywords = { tex: textureNode, tex_sampler: sampler( textureNode ) };
  129. material = new Nodes.MeshBasicNodeMaterial();
  130. material.colorNode = getWGSLTextureSample.call( { tex: textureNode, tex_sampler: textureNode, uv: new Nodes.UVNode() } );
  131. materials.push( material );
  132. // Triplanar Texture Mapping
  133. material = new Nodes.MeshBasicNodeMaterial();
  134. material.colorNode = triplanarTexture( new Nodes.TextureNode( texture ) );
  135. materials.push( material );
  136. // Screen Projection Texture
  137. material = new Nodes.MeshBasicNodeMaterial();
  138. material.colorNode = Nodes.texture( texture, viewportBottomLeft );
  139. materials.push( material );
  140. //
  141. // Geometry
  142. //
  143. const geometry = new TeapotGeometry( 50, 18 );
  144. for ( let i = 0, l = materials.length; i < l; i ++ ) {
  145. addMesh( geometry, materials[ i ] );
  146. }
  147. //
  148. renderer = new WebGPURenderer();
  149. renderer.setPixelRatio( window.devicePixelRatio );
  150. renderer.setSize( window.innerWidth, window.innerHeight );
  151. renderer.setAnimationLoop( animate );
  152. container.appendChild( renderer.domElement );
  153. //
  154. stats = new Stats();
  155. container.appendChild( stats.dom );
  156. //
  157. window.addEventListener( 'resize', onWindowResize );
  158. }
  159. function addMesh( geometry, material ) {
  160. const mesh = new THREE.Mesh( geometry, material );
  161. mesh.position.x = ( objects.length % 4 ) * 200 - 400;
  162. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  163. mesh.rotation.x = Math.random() * 200 - 100;
  164. mesh.rotation.y = Math.random() * 200 - 100;
  165. mesh.rotation.z = Math.random() * 200 - 100;
  166. objects.push( mesh );
  167. scene.add( mesh );
  168. }
  169. function onWindowResize() {
  170. camera.aspect = window.innerWidth / window.innerHeight;
  171. camera.updateProjectionMatrix();
  172. renderer.setSize( window.innerWidth, window.innerHeight );
  173. }
  174. //
  175. function animate() {
  176. const timer = 0.0001 * Date.now();
  177. camera.position.x = Math.cos( timer ) * 1000;
  178. camera.position.z = Math.sin( timer ) * 1000;
  179. camera.lookAt( scene.position );
  180. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  181. const object = objects[ i ];
  182. object.rotation.x += 0.01;
  183. object.rotation.y += 0.005;
  184. }
  185. renderer.render( scene, camera );
  186. stats.update();
  187. }
  188. </script>
  189. </body>
  190. </html>