webgpu_materials.html 6.7 KB

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