WebGPUUniformsGroup.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import WebGPUBinding from './WebGPUBinding.js';
  2. class WebGPUUniformsGroup extends WebGPUBinding {
  3. constructor() {
  4. super();
  5. this.name = '';
  6. this.uniforms = new Map();
  7. this.update = function () {};
  8. this.type = 'uniform-buffer';
  9. this.visibility = GPUShaderStage.VERTEX;
  10. this.usage = GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST;
  11. this.array = null; // set by the renderer
  12. this.bufferGPU = null; // set by the renderer
  13. Object.defineProperty( this, 'isUniformsGroup', { value: true } );
  14. }
  15. setName( name ) {
  16. this.name = name;
  17. }
  18. setUniform( name, uniform ) {
  19. this.uniforms.set( name, uniform );
  20. return this;
  21. }
  22. removeUniform( name ) {
  23. this.uniforms.delete( name );
  24. return this;
  25. }
  26. setUpdateCallback( callback ) {
  27. this.update = callback;
  28. return this;
  29. }
  30. copy( source ) {
  31. this.name = source.name;
  32. const uniformsSource = source.uniforms;
  33. this.uniforms.clear();
  34. for ( const entry of uniformsSource ) {
  35. this.uniforms.set( ...entry );
  36. }
  37. return this;
  38. }
  39. clone() {
  40. return new this.constructor().copy( this );
  41. }
  42. getByteLength() {
  43. let size = 0;
  44. for ( const uniform of this.uniforms.values() ) {
  45. size += this.getUniformByteLength( uniform );
  46. }
  47. return size;
  48. }
  49. getUniformByteLength( uniform ) {
  50. let size;
  51. if ( typeof uniform === 'number' ) {
  52. size = 4;
  53. } else if ( uniform.isVector2 ) {
  54. size = 8;
  55. } else if ( uniform.isVector3 || uniform.isColor ) {
  56. size = 12;
  57. } else if ( uniform.isVector4 ) {
  58. size = 16;
  59. } else if ( uniform.isMatrix3 ) {
  60. size = 48; // (3 * 4) * 4 bytes
  61. } else if ( uniform.isMatrix4 ) {
  62. size = 64;
  63. } else if ( uniform.isTexture ) {
  64. console.error( 'THREE.UniformsGroup: Texture samplers can not be part of an uniforms group.' );
  65. } else {
  66. console.error( 'THREE.UniformsGroup: Unsupported uniform value type.', uniform );
  67. }
  68. return size;
  69. }
  70. }
  71. export default WebGPUUniformsGroup;