webgpu_materials.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import * as TSL from 'three/tsl';
  26. import { Fn, wgslFn, positionLocal, scriptable, positionWorld, normalLocal, normalWorld, normalView, color, texture, uv, float, vec2, vec3, vec4, oscSine, triplanarTexture, screenUV, js, string, Loop, cameraProjectionMatrix, ScriptableNodeResources } from 'three/tsl';
  27. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  28. import WebGPU from 'three/addons/capabilities/WebGPU.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.position.y = - 75;
  47. scene.add( helper );
  48. // Materials
  49. const textureLoader = new THREE.TextureLoader();
  50. const uvTexture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
  51. uvTexture.wrapS = THREE.RepeatWrapping;
  52. uvTexture.wrapT = THREE.RepeatWrapping;
  53. const opacityTexture = textureLoader.load( './textures/alphaMap.jpg' );
  54. opacityTexture.wrapS = THREE.RepeatWrapping;
  55. opacityTexture.wrapT = THREE.RepeatWrapping;
  56. let material;
  57. //
  58. // BASIC
  59. //
  60. // PositionLocal
  61. material = new THREE.MeshBasicNodeMaterial();
  62. material.colorNode = positionLocal;
  63. materials.push( material );
  64. // PositionWorld
  65. material = new THREE.MeshBasicNodeMaterial();
  66. material.colorNode = positionWorld;
  67. materials.push( material );
  68. // NormalLocal
  69. material = new THREE.MeshBasicNodeMaterial();
  70. material.colorNode = normalLocal;
  71. materials.push( material );
  72. // NormalWorld
  73. material = new THREE.MeshBasicNodeMaterial();
  74. material.colorNode = normalWorld;
  75. materials.push( material );
  76. // NormalView
  77. material = new THREE.MeshBasicNodeMaterial();
  78. material.colorNode = normalView;
  79. materials.push( material );
  80. // Texture
  81. material = new THREE.MeshBasicNodeMaterial();
  82. material.colorNode = texture( uvTexture );
  83. materials.push( material );
  84. // Opacity
  85. material = new THREE.MeshBasicNodeMaterial();
  86. material.colorNode = color( 0x0099FF );
  87. material.opacityNode = texture( uvTexture );
  88. material.transparent = true;
  89. materials.push( material );
  90. // AlphaTest
  91. material = new THREE.MeshBasicNodeMaterial();
  92. material.colorNode = texture( uvTexture );
  93. material.opacityNode = texture( opacityTexture );
  94. material.alphaTestNode = 0.5;
  95. materials.push( material );
  96. // camera
  97. material = new THREE.MeshBasicNodeMaterial();
  98. material.colorNode = cameraProjectionMatrix.mul( positionLocal );
  99. materials.push( material );
  100. // Normal
  101. material = new THREE.MeshNormalMaterial();
  102. material.opacity = .5;
  103. material.transparent = true;
  104. materials.push( material );
  105. //
  106. // ADVANCED
  107. //
  108. // Custom ShaderNode ( desaturate filter )
  109. const desaturateShaderNode = Fn( ( input ) => {
  110. return vec3( 0.299, 0.587, 0.114 ).dot( input.color.xyz );
  111. } );
  112. material = new THREE.MeshBasicNodeMaterial();
  113. material.colorNode = desaturateShaderNode( { color: texture( uvTexture ) } );
  114. materials.push( material );
  115. // Custom ShaderNode(no inputs) > Approach 2
  116. const desaturateNoInputsShaderNode = Fn( () => {
  117. return vec3( 0.299, 0.587, 0.114 ).dot( texture( uvTexture ).xyz );
  118. } );
  119. material = new THREE.MeshBasicNodeMaterial();
  120. material.colorNode = desaturateNoInputsShaderNode();
  121. materials.push( material );
  122. // Custom WGSL ( desaturate filter )
  123. const desaturateWGSLFn = wgslFn( `
  124. fn desaturate( color:vec3<f32> ) -> vec3<f32> {
  125. let lum = vec3<f32>( 0.299, 0.587, 0.114 );
  126. return vec3<f32>( dot( lum, color ) );
  127. }
  128. ` );
  129. // include example
  130. const someWGSLFn = wgslFn( `
  131. fn someFn( color:vec3<f32> ) -> vec3<f32> {
  132. return desaturate( color );
  133. }
  134. `, [ desaturateWGSLFn ] );
  135. material = new THREE.MeshBasicNodeMaterial();
  136. material.colorNode = someWGSLFn( { color: texture( uvTexture ) } );
  137. materials.push( material );
  138. // Custom WGSL
  139. const getWGSLTextureSample = wgslFn( `
  140. fn getWGSLTextureSample( tex: texture_2d<f32>, tex_sampler: sampler, uv:vec2<f32> ) -> vec4<f32> {
  141. return textureSample( tex, tex_sampler, uv ) * vec4<f32>( 0.0, 1.0, 0.0, 1.0 );
  142. }
  143. ` );
  144. const textureNode = texture( uvTexture );
  145. material = new THREE.MeshBasicNodeMaterial();
  146. material.colorNode = getWGSLTextureSample( { tex: textureNode, tex_sampler: textureNode, uv: uv() } );
  147. materials.push( material );
  148. // Triplanar Texture Mapping
  149. material = new THREE.MeshBasicNodeMaterial();
  150. material.colorNode = triplanarTexture( texture( uvTexture ), null, null, float( .01 ) );
  151. materials.push( material );
  152. // Screen Projection Texture
  153. material = new THREE.MeshBasicNodeMaterial();
  154. material.colorNode = texture( uvTexture, screenUV.flipY() );
  155. materials.push( material );
  156. // Loop
  157. material = new THREE.MeshBasicNodeMaterial();
  158. materials.push( material );
  159. const loopCount = 10;
  160. material.colorNode = Loop( loopCount, ( { i } ) => {
  161. const output = vec4().toVar();
  162. const scale = oscSine().mul( .09 ); // just a value to test
  163. const scaleI = scale.mul( i );
  164. const scaleINeg = scaleI.negate();
  165. const leftUV = uv().add( vec2( scaleI, 0 ) );
  166. const rightUV = uv().add( vec2( scaleINeg, 0 ) );
  167. const topUV = uv().add( vec2( 0, scaleI ) );
  168. const bottomUV = uv().add( vec2( 0, scaleINeg ) );
  169. output.assign( output.add( texture( uvTexture, leftUV ) ) );
  170. output.assign( output.add( texture( uvTexture, rightUV ) ) );
  171. output.assign( output.add( texture( uvTexture, topUV ) ) );
  172. output.assign( output.add( texture( uvTexture, bottomUV ) ) );
  173. return output.div( loopCount * 4 );
  174. } );
  175. // Scriptable
  176. ScriptableNodeResources.set( 'TSL', TSL );
  177. const asyncNode = scriptable( js( `
  178. layout = {
  179. outputType: 'node'
  180. };
  181. const { float } = TSL;
  182. function init() {
  183. setTimeout( () => {
  184. local.set( 'result', float( 1.0 ) );
  185. refresh(); // refresh the node
  186. }, 1000 );
  187. return float( 0.0 );
  188. }
  189. function main() {
  190. const result = local.get( 'result', init );
  191. //console.log( 'result', result );
  192. return result;
  193. }
  194. ` ) );
  195. const scriptableNode = scriptable( js( `
  196. layout = {
  197. outputType: 'node',
  198. elements: [
  199. { name: 'source', inputType: 'node' },
  200. { name: 'contrast', inputType: 'node' },
  201. { name: 'vector3', inputType: 'Vector3' },
  202. { name: 'message', inputType: 'string' },
  203. { name: 'binary', inputType: 'ArrayBuffer' },
  204. { name: 'object3d', inputType: 'Object3D' },
  205. { name: 'execFrom', inputType: 'string' }
  206. ]
  207. };
  208. const { saturation, float, oscSine, mul } = TSL;
  209. function helloWorld() {
  210. console.log( "Hello World!" );
  211. }
  212. function main() {
  213. const source = parameters.get( 'source' ) || float();
  214. const contrast = parameters.get( 'contrast' ) || float();
  215. const material = local.get( 'material' );
  216. //console.log( 'vector3', parameters.get( 'vector3' ) );
  217. if ( parameters.get( 'execFrom' ) === 'serialized' ) {
  218. //console.log( 'message', parameters.get( 'message' ).value );
  219. //console.log( 'binary', parameters.get( 'binary' ) );
  220. //console.log( 'object3d', parameters.get( 'object3d' ) ); // unserializable yet
  221. //console.log( global.get( 'renderer' ) );
  222. }
  223. if ( material ) material.needsUpdate = true;
  224. return mul( saturation( source, oscSine() ), contrast );
  225. }
  226. output = { helloWorld };
  227. ` ) );
  228. scriptableNode.setParameter( 'source', texture( uvTexture ).xyz );
  229. scriptableNode.setParameter( 'contrast', asyncNode );
  230. scriptableNode.setParameter( 'vector3', vec3( new THREE.Vector3( 1, 1, 1 ) ) );
  231. scriptableNode.setParameter( 'message', string( 'Hello World!' ) );
  232. scriptableNode.setParameter( 'binary', new ArrayBuffer( 4 ) );
  233. scriptableNode.setParameter( 'object3d', new THREE.Group() );
  234. scriptableNode.call( 'helloWorld' );
  235. material = new THREE.MeshBasicNodeMaterial();
  236. material.colorNode = scriptableNode;
  237. materials.push( material );
  238. scriptableNode.setLocal( 'material', material );
  239. //
  240. // Geometry
  241. //
  242. const geometry = new TeapotGeometry( 50, 18 );
  243. for ( let i = 0, l = materials.length; i < l; i ++ ) {
  244. addMesh( geometry, materials[ i ] );
  245. }
  246. const serializeMesh = scene.children[ scene.children.length - 1 ];
  247. //
  248. renderer = new THREE.WebGPURenderer( { antialias: true } );
  249. renderer.setPixelRatio( window.devicePixelRatio );
  250. renderer.setSize( window.innerWidth, window.innerHeight );
  251. renderer.setAnimationLoop( animate );
  252. container.appendChild( renderer.domElement );
  253. //
  254. stats = new Stats();
  255. container.appendChild( stats.dom );
  256. //
  257. window.addEventListener( 'resize', onWindowResize );
  258. //
  259. setTimeout( () => testSerialization( serializeMesh ), 1000 );
  260. }
  261. function addMesh( geometry, material ) {
  262. const mesh = new THREE.Mesh( geometry, material );
  263. mesh.position.x = ( objects.length % 4 ) * 200 - 400;
  264. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  265. mesh.rotation.x = Math.random() * 200 - 100;
  266. mesh.rotation.y = Math.random() * 200 - 100;
  267. mesh.rotation.z = Math.random() * 200 - 100;
  268. objects.push( mesh );
  269. scene.add( mesh );
  270. }
  271. function moduleToLib( module ) {
  272. const lib = {};
  273. for ( const nodeElement of Object.values( module ) ) {
  274. if ( typeof nodeElement === 'function' && nodeElement.type !== undefined ) {
  275. lib[ nodeElement.type ] = nodeElement;
  276. }
  277. }
  278. return lib;
  279. }
  280. function testSerialization( mesh ) {
  281. const json = mesh.toJSON();
  282. const loader = new THREE.NodeObjectLoader().setNodes( moduleToLib( THREE ) ).setNodeMaterials( moduleToLib( THREE ) );
  283. const serializedMesh = loader.parse( json );
  284. serializedMesh.position.x = ( objects.length % 4 ) * 200 - 400;
  285. serializedMesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  286. const scriptableNode = serializedMesh.material.colorNode;
  287. // it's because local.get( 'material' ) is used in the example ( local/global is unserializable )
  288. scriptableNode.setLocal( 'material', serializedMesh.material );
  289. scriptableNode.setParameter( 'execFrom', 'serialized' );
  290. objects.push( serializedMesh );
  291. scene.add( serializedMesh );
  292. }
  293. function onWindowResize() {
  294. camera.aspect = window.innerWidth / window.innerHeight;
  295. camera.updateProjectionMatrix();
  296. renderer.setSize( window.innerWidth, window.innerHeight );
  297. }
  298. //
  299. function animate() {
  300. const timer = 0.0001 * Date.now();
  301. camera.position.x = Math.cos( timer ) * 1000;
  302. camera.position.z = Math.sin( timer ) * 1000;
  303. camera.lookAt( scene.position );
  304. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  305. const object = objects[ i ];
  306. object.rotation.x += 0.01;
  307. object.rotation.y += 0.005;
  308. }
  309. renderer.render( scene, camera );
  310. stats.update();
  311. }
  312. </script>
  313. </body>
  314. </html>
粤ICP备19079148号