webgpu_instance_uniform.html 4.2 KB

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