webgpu_materials.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 { attribute, positionLocal, normalLocal, normalWorld, normalView, color, texture, ShaderNode, func, uv, vec3, triplanarTexture, viewportBottomLeft, MeshBasicNodeMaterial } 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 Stats from 'three/addons/libs/stats.module.js';
  30. let stats;
  31. let camera, scene, renderer;
  32. const objects = [], materials = [];
  33. init();
  34. 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 = attribute( 'color' );
  47. helper.position.y = - 75;
  48. scene.add( helper );
  49. // Materials
  50. const textureLoader = new THREE.TextureLoader();
  51. const uvTexture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
  52. uvTexture.wrapS = THREE.RepeatWrapping;
  53. uvTexture.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. // PositionLocal
  62. material = new MeshBasicNodeMaterial();
  63. material.colorNode = positionLocal;
  64. materials.push( material );
  65. // NormalLocal
  66. material = new MeshBasicNodeMaterial();
  67. material.colorNode = normalLocal;
  68. materials.push( material );
  69. // NormalWorld
  70. material = new MeshBasicNodeMaterial();
  71. material.colorNode = normalWorld;
  72. materials.push( material );
  73. // NormalView
  74. material = new MeshBasicNodeMaterial();
  75. material.colorNode = normalView;
  76. materials.push( material );
  77. // Texture
  78. material = new MeshBasicNodeMaterial();
  79. material.colorNode = texture( uvTexture );
  80. materials.push( material );
  81. // Opacity
  82. material = new MeshBasicNodeMaterial();
  83. material.colorNode = color( 0x0099FF );
  84. material.opacityNode = texture( uvTexture );
  85. material.transparent = true;
  86. materials.push( material );
  87. // AlphaTest
  88. material = new MeshBasicNodeMaterial();
  89. material.colorNode = texture( uvTexture );
  90. material.opacityNode = texture( opacityTexture );
  91. material.alphaTestNode = 0.5;
  92. materials.push( material );
  93. // Normal
  94. material = new THREE.MeshNormalMaterial();
  95. material.opacity = .5;
  96. material.transparent = true;
  97. materials.push( material );
  98. //
  99. // ADVANCED
  100. //
  101. // Custom ShaderNode ( desaturate filter )
  102. const desaturateShaderNode = new ShaderNode( ( input ) => {
  103. return vec3( 0.299, 0.587, 0.114 ).dot( input.color.xyz );
  104. } );
  105. material = new MeshBasicNodeMaterial();
  106. material.colorNode = desaturateShaderNode.call( { color: texture( uvTexture ) } );
  107. materials.push( material );
  108. // Custom ShaderNode(no inputs) > Approach 2
  109. const desaturateNoInputsShaderNode = new ShaderNode( () => {
  110. return vec3( 0.299, 0.587, 0.114 ).dot( texture( uvTexture ).xyz );
  111. } );
  112. material = new MeshBasicNodeMaterial();
  113. material.colorNode = desaturateNoInputsShaderNode;
  114. materials.push( material );
  115. // Custom WGSL ( desaturate filter )
  116. const desaturateWGSLNode = func( `
  117. fn desaturate( color:vec3<f32> ) -> vec3<f32> {
  118. let lum = vec3<f32>( 0.299, 0.587, 0.114 );
  119. return vec3<f32>( dot( lum, color ) );
  120. }
  121. ` );
  122. material = new MeshBasicNodeMaterial();
  123. material.colorNode = desaturateWGSLNode.call( { color: texture( uvTexture ) } );
  124. materials.push( material );
  125. // Custom WGSL ( get texture from keywords )
  126. const getWGSLTextureSample = func( `
  127. fn getWGSLTextureSample( tex: texture_2d<f32>, tex_sampler: sampler, uv:vec2<f32> ) -> vec4<f32> {
  128. return textureSample( tex, tex_sampler, uv ) * vec4<f32>( 0.0, 1.0, 0.0, 1.0 );
  129. }
  130. ` );
  131. const textureNode = texture( uvTexture );
  132. //getWGSLTextureSample.keywords = { tex: textureNode, tex_sampler: sampler( textureNode ) };
  133. material = new MeshBasicNodeMaterial();
  134. material.colorNode = getWGSLTextureSample.call( { tex: textureNode, tex_sampler: textureNode, uv: uv() } );
  135. materials.push( material );
  136. // Triplanar Texture Mapping
  137. material = new MeshBasicNodeMaterial();
  138. material.colorNode = triplanarTexture( texture( uvTexture ) );
  139. materials.push( material );
  140. // Screen Projection Texture
  141. material = new MeshBasicNodeMaterial();
  142. material.colorNode = texture( uvTexture, viewportBottomLeft );
  143. materials.push( material );
  144. //
  145. // Geometry
  146. //
  147. const geometry = new TeapotGeometry( 50, 18 );
  148. for ( let i = 0, l = materials.length; i < l; i ++ ) {
  149. addMesh( geometry, materials[ i ] );
  150. }
  151. //
  152. renderer = new WebGPURenderer();
  153. renderer.setPixelRatio( window.devicePixelRatio );
  154. renderer.setSize( window.innerWidth, window.innerHeight );
  155. renderer.setAnimationLoop( animate );
  156. container.appendChild( renderer.domElement );
  157. //
  158. stats = new Stats();
  159. container.appendChild( stats.dom );
  160. //
  161. window.addEventListener( 'resize', onWindowResize );
  162. }
  163. function addMesh( geometry, material ) {
  164. const mesh = new THREE.Mesh( geometry, material );
  165. mesh.position.x = ( objects.length % 4 ) * 200 - 400;
  166. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  167. mesh.rotation.x = Math.random() * 200 - 100;
  168. mesh.rotation.y = Math.random() * 200 - 100;
  169. mesh.rotation.z = Math.random() * 200 - 100;
  170. objects.push( mesh );
  171. scene.add( mesh );
  172. }
  173. function onWindowResize() {
  174. camera.aspect = window.innerWidth / window.innerHeight;
  175. camera.updateProjectionMatrix();
  176. renderer.setSize( window.innerWidth, window.innerHeight );
  177. }
  178. //
  179. function animate() {
  180. const timer = 0.0001 * Date.now();
  181. camera.position.x = Math.cos( timer ) * 1000;
  182. camera.position.z = Math.sin( timer ) * 1000;
  183. camera.lookAt( scene.position );
  184. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  185. const object = objects[ i ];
  186. object.rotation.x += 0.01;
  187. object.rotation.y += 0.005;
  188. }
  189. renderer.render( scene, camera );
  190. stats.update();
  191. }
  192. </script>
  193. </body>
  194. </html>