webgpu_instance_uniform.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - instance uniform</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 - instance uniform
  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/webgpu';
  25. import { nodeObject, uniform, cubeTexture } from 'three/tsl';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. class InstanceUniformNode extends THREE.Node {
  30. constructor() {
  31. super( 'vec3' );
  32. this.updateType = THREE.NodeUpdateType.OBJECT;
  33. this.uniformNode = uniform( new THREE.Color() );
  34. }
  35. update( frame ) {
  36. const mesh = frame.object;
  37. const meshColor = mesh.color;
  38. this.uniformNode.value.copy( meshColor );
  39. }
  40. setup( /*builder*/ ) {
  41. return this.uniformNode;
  42. }
  43. }
  44. let stats;
  45. let camera, scene, renderer;
  46. let controls;
  47. const objects = [];
  48. init();
  49. function init() {
  50. const container = document.createElement( 'div' );
  51. document.body.appendChild( container );
  52. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 4000 );
  53. camera.position.set( 0, 200, 1200 );
  54. scene = new THREE.Scene();
  55. // Grid
  56. const helper = new THREE.GridHelper( 1000, 40, 0x303030, 0x303030 );
  57. helper.position.y = - 75;
  58. scene.add( helper );
  59. // CubeMap
  60. const path = 'textures/cube/SwedishRoyalCastle/';
  61. const format = '.jpg';
  62. const urls = [
  63. path + 'px' + format, path + 'nx' + format,
  64. path + 'py' + format, path + 'ny' + format,
  65. path + 'pz' + format, path + 'nz' + format
  66. ];
  67. const cTexture = new THREE.CubeTextureLoader().load( urls );
  68. // Materials
  69. const instanceUniform = nodeObject( new InstanceUniformNode() );
  70. const cubeTextureNode = cubeTexture( cTexture );
  71. const material = new THREE.MeshBasicNodeMaterial();
  72. material.colorNode = instanceUniform.add( cubeTextureNode );
  73. material.emissiveNode = instanceUniform.mul( cubeTextureNode );
  74. // Geometry
  75. const geometry = new TeapotGeometry( 50, 18 );
  76. for ( let i = 0, l = 12; i < l; i ++ ) {
  77. addMesh( geometry, material );
  78. }
  79. //
  80. renderer = new THREE.WebGPURenderer( { antialias: true } );
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. renderer.setAnimationLoop( animate );
  84. container.appendChild( renderer.domElement );
  85. //
  86. controls = new OrbitControls( camera, renderer.domElement );
  87. controls.minDistance = 400;
  88. controls.maxDistance = 2000;
  89. //
  90. stats = new Stats();
  91. container.appendChild( stats.dom );
  92. //
  93. window.addEventListener( 'resize', onWindowResize );
  94. }
  95. function addMesh( geometry, material ) {
  96. const mesh = new THREE.Mesh( geometry, material );
  97. mesh.color = new THREE.Color( Math.random() * 0xffffff );
  98. mesh.position.x = ( objects.length % 4 ) * 200 - 300;
  99. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  100. mesh.rotation.x = Math.random() * 200 - 100;
  101. mesh.rotation.y = Math.random() * 200 - 100;
  102. mesh.rotation.z = Math.random() * 200 - 100;
  103. objects.push( mesh );
  104. scene.add( mesh );
  105. }
  106. function onWindowResize() {
  107. camera.aspect = window.innerWidth / window.innerHeight;
  108. camera.updateProjectionMatrix();
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. }
  111. //
  112. function animate() {
  113. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  114. const object = objects[ i ];
  115. object.rotation.x += 0.01;
  116. object.rotation.y += 0.005;
  117. }
  118. renderer.render( scene, camera );
  119. stats.update();
  120. }
  121. </script>
  122. </body>
  123. </html>
粤ICP备19079148号