WebGPUUniform.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { Color, Matrix3, Matrix4, Vector2, Vector3, Vector4 } from '../../../../build/three.module.js';
  2. class WebGPUUniform {
  3. constructor( name, value = null ) {
  4. this.name = name;
  5. this.value = value;
  6. this.boundary = 0; // used to build the uniform buffer according to the STD140 layout
  7. this.itemSize = 0;
  8. this.offset = 0; // this property is set by WebGPUUniformsGroup and marks the start position in the uniform buffer
  9. }
  10. setValue( value ) {
  11. this.value = value;
  12. }
  13. }
  14. class FloatUniform extends WebGPUUniform {
  15. constructor( name, value = 0 ) {
  16. super( name, value );
  17. this.boundary = 4;
  18. this.itemSize = 1;
  19. Object.defineProperty( this, 'isFloatUniform', { value: true } );
  20. }
  21. }
  22. class Vector2Uniform extends WebGPUUniform {
  23. constructor( name, value = new Vector2() ) {
  24. super( name, value );
  25. this.boundary = 8;
  26. this.itemSize = 2;
  27. Object.defineProperty( this, 'isVector2Uniform', { value: true } );
  28. }
  29. }
  30. class Vector3Uniform extends WebGPUUniform {
  31. constructor( name, value = new Vector3() ) {
  32. super( name, value );
  33. this.boundary = 16;
  34. this.itemSize = 3;
  35. Object.defineProperty( this, 'isVector3Uniform', { value: true } );
  36. }
  37. }
  38. class Vector4Uniform extends WebGPUUniform {
  39. constructor( name, value = new Vector4() ) {
  40. super( name, value );
  41. this.boundary = 16;
  42. this.itemSize = 4;
  43. Object.defineProperty( this, 'isVector4Uniform', { value: true } );
  44. }
  45. }
  46. class ColorUniform extends WebGPUUniform {
  47. constructor( name, value = new Color() ) {
  48. super( name, value );
  49. this.boundary = 16;
  50. this.itemSize = 3;
  51. Object.defineProperty( this, 'isColorUniform', { value: true } );
  52. }
  53. }
  54. class Matrix3Uniform extends WebGPUUniform {
  55. constructor( name, value = new Matrix3() ) {
  56. super( name, value );
  57. this.boundary = 48;
  58. this.itemSize = 12;
  59. Object.defineProperty( this, 'isMatrix3Uniform', { value: true } );
  60. }
  61. }
  62. class Matrix4Uniform extends WebGPUUniform {
  63. constructor( name, value = new Matrix4() ) {
  64. super( name, value );
  65. this.boundary = 64;
  66. this.itemSize = 16;
  67. Object.defineProperty( this, 'isMatrix4Uniform', { value: true } );
  68. }
  69. }
  70. export { FloatUniform, Vector2Uniform, Vector3Uniform, Vector4Uniform, ColorUniform, Matrix3Uniform, Matrix4Uniform };