webgpu_materials.html 7.0 KB

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