webgpu_materials.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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="module">
  14. import * as THREE from '../build/three.module.js';
  15. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  16. import WebGPU from './jsm/renderers/webgpu/WebGPU.js';
  17. import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.js';
  18. import ColorNode from './jsm/renderers/nodes/inputs/ColorNode.js';
  19. import PositionNode from './jsm/renderers/nodes/accessors/PositionNode.js';
  20. import NormalNode from './jsm/renderers/nodes/accessors/NormalNode.js';
  21. import TextureNode from './jsm/renderers/nodes/inputs/TextureNode.js';
  22. import Stats from './jsm/libs/stats.module.js';
  23. let stats;
  24. let camera, scene, renderer;
  25. const objects = [], materials = [];
  26. init().then( animate ).catch( error );
  27. async function init() {
  28. if ( WebGPU.isAvailable() === false ) {
  29. document.body.appendChild( WebGPU.getErrorMessage() );
  30. throw 'No WebGPU support';
  31. }
  32. const container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  35. camera.position.set( 0, 200, 800 );
  36. scene = new THREE.Scene();
  37. // Grid
  38. const helper = new THREE.GridHelper( 1000, 40 );
  39. helper.material.colorNode = new ColorNode( new THREE.Color( 0x303030 ) );
  40. helper.position.y = - 75;
  41. scene.add( helper );
  42. // Materials
  43. const textureLoader = new THREE.TextureLoader();
  44. const texture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
  45. texture.wrapS = THREE.RepeatWrapping;
  46. texture.wrapT = THREE.RepeatWrapping;
  47. let material;
  48. // PositionNode.LOCAL
  49. material = new THREE.MeshBasicMaterial();
  50. material.colorNode = new PositionNode( PositionNode.LOCAL );
  51. materials.push( material );
  52. // NormalNode.LOCAL
  53. material = new THREE.MeshBasicMaterial();
  54. material.colorNode = new NormalNode( NormalNode.LOCAL );
  55. materials.push( material );
  56. // NormalNode.WORLD
  57. material = new THREE.MeshBasicMaterial();
  58. material.colorNode = new NormalNode( NormalNode.WORLD );
  59. materials.push( material );
  60. // NormalNode.VIEW
  61. material = new THREE.MeshBasicMaterial();
  62. material.colorNode = new NormalNode( NormalNode.VIEW );
  63. materials.push( material );
  64. // TextureNode
  65. material = new THREE.MeshBasicMaterial();
  66. material.colorNode = new TextureNode( texture );
  67. materials.push( material );
  68. // Opacity
  69. material = new THREE.MeshBasicMaterial();
  70. material.opacityNode = new TextureNode( texture );
  71. material.transparent = true;
  72. materials.push( material );
  73. // Spheres geometry
  74. //const geometry = new THREE.SphereGeometry( 70, 32, 16 );
  75. const geometry = new TeapotGeometry( 50, 18 );
  76. for ( let i = 0, l = materials.length; i < l; i ++ ) {
  77. addMesh( geometry, materials[ i ] );
  78. }
  79. //
  80. renderer = new WebGPURenderer();
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. container.appendChild( renderer.domElement );
  84. //
  85. stats = new Stats();
  86. container.appendChild( stats.dom );
  87. //
  88. window.addEventListener( 'resize', onWindowResize );
  89. return renderer.init();
  90. }
  91. function addMesh( geometry, material ) {
  92. const mesh = new THREE.Mesh( geometry, material );
  93. mesh.position.x = ( objects.length % 4 ) * 200 - 400;
  94. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  95. mesh.rotation.x = Math.random() * 200 - 100;
  96. mesh.rotation.y = Math.random() * 200 - 100;
  97. mesh.rotation.z = Math.random() * 200 - 100;
  98. objects.push( mesh );
  99. scene.add( mesh );
  100. }
  101. function onWindowResize() {
  102. camera.aspect = window.innerWidth / window.innerHeight;
  103. camera.updateProjectionMatrix();
  104. renderer.setSize( window.innerWidth, window.innerHeight );
  105. }
  106. function generateTexture() {
  107. const canvas = document.createElement( 'canvas' );
  108. canvas.width = 256;
  109. canvas.height = 256;
  110. const context = canvas.getContext( '2d' );
  111. const image = context.getImageData( 0, 0, 256, 256 );
  112. let x = 0, y = 0;
  113. for ( let i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  114. x = j % 256;
  115. y = ( x === 0 ) ? y + 1 : y;
  116. image.data[ i ] = 255;
  117. image.data[ i + 1 ] = 255;
  118. image.data[ i + 2 ] = 255;
  119. image.data[ i + 3 ] = Math.floor( x ^ y );
  120. }
  121. context.putImageData( image, 0, 0 );
  122. return canvas;
  123. }
  124. //
  125. function animate() {
  126. requestAnimationFrame( animate );
  127. render();
  128. stats.update();
  129. }
  130. function render() {
  131. const timer = 0.0001 * Date.now();
  132. camera.position.x = Math.cos( timer ) * 1000;
  133. camera.position.z = Math.sin( timer ) * 1000;
  134. camera.lookAt( scene.position );
  135. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  136. const object = objects[ i ];
  137. object.rotation.x += 0.01;
  138. object.rotation.y += 0.005;
  139. }
  140. renderer.render( scene, camera );
  141. }
  142. function error( error ) {
  143. console.error( error );
  144. }
  145. </script>
  146. </body>
  147. </html>
粤ICP备19079148号