1
0

webgpu_materials.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. <meta property="og:title" content="three.js webgpu - materials">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_materials.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_materials.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Materials</span>
  18. </div>
  19. <small>
  20. Examples of built-in materials and NodeMaterials.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { Fn, wgslFn, positionLocal, positionWorld, normalLocal, normalWorld, normalView, color, texture, uv, float, vec2, vec3, vec4, oscSine, triplanarTexture, screenUV, Loop, cameraProjectionMatrix } from 'three/tsl';
  36. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  37. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  38. import { Inspector } from 'three/addons/inspector/Inspector.js';
  39. let camera, scene, renderer;
  40. const objects = [], materials = [];
  41. init();
  42. function init() {
  43. if ( WebGPU.isAvailable() === false ) {
  44. document.body.appendChild( WebGPU.getErrorMessage() );
  45. throw new Error( 'No WebGPU support' );
  46. }
  47. const container = document.createElement( 'div' );
  48. document.body.appendChild( container );
  49. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  50. camera.position.set( 0, 200, 800 );
  51. scene = new THREE.Scene();
  52. scene.background = new THREE.Color( 0x000000 );
  53. // Grid
  54. const helper = new THREE.GridHelper( 1000, 40, 0x303030, 0x303030 );
  55. helper.position.y = - 75;
  56. scene.add( helper );
  57. // Materials
  58. const textureLoader = new THREE.TextureLoader();
  59. const uvTexture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
  60. uvTexture.wrapS = THREE.RepeatWrapping;
  61. uvTexture.wrapT = THREE.RepeatWrapping;
  62. const opacityTexture = textureLoader.load( './textures/alphaMap.jpg' );
  63. opacityTexture.wrapS = THREE.RepeatWrapping;
  64. opacityTexture.wrapT = THREE.RepeatWrapping;
  65. let material;
  66. //
  67. // BASIC
  68. //
  69. // PositionLocal
  70. material = new THREE.MeshBasicNodeMaterial();
  71. material.colorNode = positionLocal;
  72. materials.push( material );
  73. // PositionWorld
  74. material = new THREE.MeshBasicNodeMaterial();
  75. material.colorNode = positionWorld;
  76. materials.push( material );
  77. // NormalLocal
  78. material = new THREE.MeshBasicNodeMaterial();
  79. material.colorNode = normalLocal;
  80. materials.push( material );
  81. // NormalWorld
  82. material = new THREE.MeshBasicNodeMaterial();
  83. material.colorNode = normalWorld;
  84. materials.push( material );
  85. // NormalView
  86. material = new THREE.MeshBasicNodeMaterial();
  87. material.colorNode = normalView;
  88. materials.push( material );
  89. // Texture
  90. material = new THREE.MeshBasicNodeMaterial();
  91. material.colorNode = texture( uvTexture );
  92. materials.push( material );
  93. // Opacity
  94. material = new THREE.MeshBasicNodeMaterial();
  95. material.colorNode = color( 0x0099FF );
  96. material.opacityNode = texture( uvTexture );
  97. material.transparent = true;
  98. materials.push( material );
  99. // AlphaTest
  100. material = new THREE.MeshBasicNodeMaterial();
  101. material.colorNode = texture( uvTexture );
  102. material.opacityNode = texture( opacityTexture );
  103. material.alphaTestNode = 0.5;
  104. materials.push( material );
  105. // camera
  106. material = new THREE.MeshBasicNodeMaterial();
  107. material.colorNode = cameraProjectionMatrix.mul( positionLocal );
  108. materials.push( material );
  109. // Normal
  110. material = new THREE.MeshNormalMaterial();
  111. material.opacity = .5;
  112. material.transparent = true;
  113. materials.push( material );
  114. //
  115. // ADVANCED
  116. //
  117. // Custom ShaderNode ( desaturate filter )
  118. const desaturateShaderNode = Fn( ( input ) => {
  119. return vec3( 0.299, 0.587, 0.114 ).dot( input.color.xyz );
  120. } );
  121. material = new THREE.MeshBasicNodeMaterial();
  122. material.colorNode = desaturateShaderNode( { color: texture( uvTexture ) } );
  123. materials.push( material );
  124. // Custom ShaderNode(no inputs) > Approach 2
  125. const desaturateNoInputsShaderNode = Fn( () => {
  126. return vec3( 0.299, 0.587, 0.114 ).dot( texture( uvTexture ).xyz );
  127. } );
  128. material = new THREE.MeshBasicNodeMaterial();
  129. material.colorNode = desaturateNoInputsShaderNode();
  130. materials.push( material );
  131. // Custom WGSL ( desaturate filter )
  132. const desaturateWGSLFn = wgslFn( `
  133. fn desaturate( color:vec3<f32> ) -> vec3<f32> {
  134. let lum = vec3<f32>( 0.299, 0.587, 0.114 );
  135. return vec3<f32>( dot( lum, color ) );
  136. }
  137. ` );
  138. // include example
  139. const someWGSLFn = wgslFn( `
  140. fn someFn( color:vec3<f32> ) -> vec3<f32> {
  141. return desaturate( color );
  142. }
  143. `, [ desaturateWGSLFn ] );
  144. material = new THREE.MeshBasicNodeMaterial();
  145. material.colorNode = someWGSLFn( { color: texture( uvTexture ) } );
  146. materials.push( material );
  147. // Custom WGSL
  148. const getWGSLTextureSample = wgslFn( `
  149. fn getWGSLTextureSample( tex: texture_2d<f32>, tex_sampler: sampler, uv:vec2<f32> ) -> vec4<f32> {
  150. return textureSample( tex, tex_sampler, uv ) * vec4<f32>( 0.0, 1.0, 0.0, 1.0 );
  151. }
  152. ` );
  153. const textureNode = texture( uvTexture );
  154. material = new THREE.MeshBasicNodeMaterial();
  155. material.colorNode = getWGSLTextureSample( { tex: textureNode, tex_sampler: textureNode, uv: uv() } );
  156. materials.push( material );
  157. // Triplanar Texture Mapping
  158. material = new THREE.MeshBasicNodeMaterial();
  159. material.colorNode = triplanarTexture( texture( uvTexture ), null, null, float( .01 ) );
  160. materials.push( material );
  161. // Screen Projection Texture
  162. material = new THREE.MeshBasicNodeMaterial();
  163. material.colorNode = texture( uvTexture, screenUV.flipY() );
  164. materials.push( material );
  165. // Loop
  166. material = new THREE.MeshBasicNodeMaterial();
  167. materials.push( material );
  168. const loopCount = 10;
  169. material.colorNode = Loop( loopCount, ( { i } ) => {
  170. const output = vec4().toVar();
  171. const scale = oscSine().mul( .09 ); // just a value to test
  172. const scaleI = scale.mul( i );
  173. const scaleINeg = scaleI.negate();
  174. const leftUV = uv().add( vec2( scaleI, 0 ) );
  175. const rightUV = uv().add( vec2( scaleINeg, 0 ) );
  176. const topUV = uv().add( vec2( 0, scaleI ) );
  177. const bottomUV = uv().add( vec2( 0, scaleINeg ) );
  178. output.assign( output.add( texture( uvTexture, leftUV ) ) );
  179. output.assign( output.add( texture( uvTexture, rightUV ) ) );
  180. output.assign( output.add( texture( uvTexture, topUV ) ) );
  181. output.assign( output.add( texture( uvTexture, bottomUV ) ) );
  182. return output.div( loopCount * 4 );
  183. } );
  184. //
  185. // Geometry
  186. //
  187. const geometry = new TeapotGeometry( 50, 18 );
  188. for ( let i = 0, l = materials.length; i < l; i ++ ) {
  189. addMesh( geometry, materials[ i ] );
  190. }
  191. //
  192. renderer = new THREE.WebGPURenderer( { antialias: true } );
  193. renderer.setPixelRatio( window.devicePixelRatio );
  194. renderer.setSize( window.innerWidth, window.innerHeight );
  195. renderer.setAnimationLoop( animate );
  196. renderer.inspector = new Inspector();
  197. container.appendChild( renderer.domElement );
  198. //
  199. window.addEventListener( 'resize', onWindowResize );
  200. }
  201. function addMesh( geometry, material ) {
  202. const mesh = new THREE.Mesh( geometry, material );
  203. mesh.position.x = ( objects.length % 4 ) * 200 - 400;
  204. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  205. mesh.rotation.x = Math.random() * 200 - 100;
  206. mesh.rotation.y = Math.random() * 200 - 100;
  207. mesh.rotation.z = Math.random() * 200 - 100;
  208. objects.push( mesh );
  209. scene.add( mesh );
  210. }
  211. function onWindowResize() {
  212. camera.aspect = window.innerWidth / window.innerHeight;
  213. camera.updateProjectionMatrix();
  214. renderer.setSize( window.innerWidth, window.innerHeight );
  215. }
  216. //
  217. function animate() {
  218. const timer = 0.0001 * Date.now();
  219. camera.position.x = Math.cos( timer ) * 1000;
  220. camera.position.z = Math.sin( timer ) * 1000;
  221. camera.lookAt( scene.position );
  222. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  223. const object = objects[ i ];
  224. object.rotation.x += 0.01;
  225. object.rotation.y += 0.005;
  226. }
  227. renderer.render( scene, camera );
  228. }
  229. </script>
  230. </body>
  231. </html>
粤ICP备19079148号