webgpu_nodes.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <html lang="en">
  2. <head>
  3. <title>WebGPU Nodes</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <link type="text/css" rel="stylesheet" href="main.css">
  7. </head>
  8. <body>
  9. <div id="info">
  10. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - nodes<br/>(Chrome Canary with flag: --enable-unsafe-webgpu)
  11. </div>
  12. <script type="module">
  13. import * as THREE from '../build/three.module.js';
  14. import { GUI } from './jsm/libs/dat.gui.module.js';
  15. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  16. import WebGPU from './jsm/renderers/webgpu/WebGPU.js';
  17. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  18. import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.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. let gui;
  23. let controls, camera, scene, renderer;
  24. let teapot, material, texture;
  25. let rotateTeapot = false;
  26. const param = { example: new URL( window.location.href ).searchParams.get( 'e' ) || 'texture' };
  27. init().then( animate ).catch( error );
  28. function addGui( name, value, callback, isColor, min, max ) {
  29. let node;
  30. param[ name ] = value;
  31. if ( isColor ) {
  32. node = gui.addColor( param, name ).onChange( function () {
  33. callback( param[ name ] );
  34. } );
  35. } else if ( typeof value === 'object' ) {
  36. param[ name ] = value[ Object.keys( value )[ 0 ] ];
  37. node = gui.add( param, name, value ).onChange( function () {
  38. callback( param[ name ] );
  39. } );
  40. } else {
  41. node = gui.add( param, name, min, max ).onChange( function () {
  42. callback( param[ name ] );
  43. } );
  44. }
  45. return node;
  46. }
  47. function clearGui() {
  48. if ( gui ) gui.destroy();
  49. gui = new GUI();
  50. gui.add( param, 'example', {
  51. 'texture': 'texture',
  52. 'position': 'position',
  53. 'normal': 'normal',
  54. 'empty': 'empty'
  55. } ).onFinishChange( function () {
  56. updateMaterial();
  57. } );
  58. gui.open();
  59. }
  60. function updateMaterial() {
  61. clearGui();
  62. rotateTeapot = false;
  63. const name = param.example;
  64. switch ( name ) {
  65. case 'texture':
  66. material.colorNode = new TextureNode( texture );
  67. break;
  68. case 'position':
  69. const positionNode = new PositionNode( PositionNode.VIEW );
  70. material.colorNode = positionNode;
  71. addGui( 'scope', {
  72. local: PositionNode.LOCAL
  73. }, function ( val ) {
  74. positionNode.scope = val;
  75. material.dispose();
  76. } );
  77. break;
  78. case 'normal':
  79. const normalNode = new NormalNode( NormalNode.LOCAL );
  80. material.colorNode = normalNode;
  81. addGui( 'scope', {
  82. local: NormalNode.LOCAL,
  83. world: NormalNode.WORLD,
  84. view: NormalNode.VIEW
  85. }, function ( val ) {
  86. normalNode.scope = val;
  87. material.dispose();
  88. } );
  89. addGui( 'rotate', false, function ( val ) {
  90. rotateTeapot = val;
  91. } );
  92. break;
  93. case 'empty':
  94. material.colorNode = undefined;
  95. break;
  96. }
  97. material.dispose();
  98. }
  99. async function init() {
  100. if ( WebGPU.isAvailable() === false ) {
  101. document.body.appendChild( WebGPU.getErrorMessage() );
  102. throw 'No WebGPU support';
  103. }
  104. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  105. camera.position.y = 3;
  106. camera.position.z = 6;
  107. scene = new THREE.Scene();
  108. scene.background = new THREE.Color( 0x222222 );
  109. // textures
  110. const textureLoader = new THREE.TextureLoader();
  111. texture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
  112. texture.wrapS = THREE.RepeatWrapping;
  113. texture.wrapT = THREE.RepeatWrapping;
  114. texture.name = 'uv_grid';
  115. // teapot mesh
  116. const geometryTeapot = new TeapotGeometry( 1, 18 );
  117. material = new THREE.MeshBasicMaterial();
  118. teapot = new THREE.Mesh( geometryTeapot, material );
  119. scene.add( teapot );
  120. //
  121. renderer = new WebGPURenderer();
  122. renderer.setPixelRatio( window.devicePixelRatio );
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. document.body.appendChild( renderer.domElement );
  125. //
  126. controls = new OrbitControls( camera, renderer.domElement );
  127. controls.minDistance = 3;
  128. controls.maxDistance = 8;
  129. //
  130. updateMaterial();
  131. //
  132. window.addEventListener( 'resize', onWindowResize );
  133. return renderer.init();
  134. }
  135. function onWindowResize() {
  136. camera.aspect = window.innerWidth / window.innerHeight;
  137. camera.updateProjectionMatrix();
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. }
  140. function animate() {
  141. requestAnimationFrame( animate );
  142. if ( rotateTeapot ) {
  143. teapot.rotation.x += 0.01;
  144. } else {
  145. teapot.rotation.x = 0;
  146. }
  147. //teapot.rotation.y += 0.02;
  148. renderer.render( scene, camera );
  149. }
  150. function error( error ) {
  151. console.error( error );
  152. }
  153. </script>
  154. </body>
  155. </html>
粤ICP备19079148号