1
0

webgpu_materials.html 8.7 KB

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