WebGPUUniform.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. getValue() {
  14. return this.value;
  15. }
  16. }
  17. class FloatUniform extends WebGPUUniform {
  18. constructor( name, value = 0 ) {
  19. super( name, value );
  20. this.boundary = 4;
  21. this.itemSize = 1;
  22. Object.defineProperty( this, 'isFloatUniform', { value: true } );
  23. }
  24. getValue() {
  25. return this.nodeUniform.value;
  26. }
  27. }
  28. class Vector2Uniform extends WebGPUUniform {
  29. constructor( name, value = new Vector2() ) {
  30. super( name, value );
  31. this.boundary = 8;
  32. this.itemSize = 2;
  33. Object.defineProperty( this, 'isVector2Uniform', { value: true } );
  34. }
  35. }
  36. class Vector3Uniform extends WebGPUUniform {
  37. constructor( name, value = new Vector3() ) {
  38. super( name, value );
  39. this.boundary = 16;
  40. this.itemSize = 3;
  41. Object.defineProperty( this, 'isVector3Uniform', { value: true } );
  42. }
  43. }
  44. class Vector4Uniform extends WebGPUUniform {
  45. constructor( name, value = new Vector4() ) {
  46. super( name, value );
  47. this.boundary = 16;
  48. this.itemSize = 4;
  49. Object.defineProperty( this, 'isVector4Uniform', { value: true } );
  50. }
  51. }
  52. class ColorUniform extends WebGPUUniform {
  53. constructor( name, value = new Color() ) {
  54. super( name, value );
  55. this.boundary = 16;
  56. this.itemSize = 3;
  57. Object.defineProperty( this, 'isColorUniform', { value: true } );
  58. }
  59. }
  60. class Matrix3Uniform extends WebGPUUniform {
  61. constructor( name, value = new Matrix3() ) {
  62. super( name, value );
  63. this.boundary = 48;
  64. this.itemSize = 12;
  65. Object.defineProperty( this, 'isMatrix3Uniform', { value: true } );
  66. }
  67. }
  68. class Matrix4Uniform extends WebGPUUniform {
  69. constructor( name, value = new Matrix4() ) {
  70. super( name, value );
  71. this.boundary = 64;
  72. this.itemSize = 16;
  73. Object.defineProperty( this, 'isMatrix4Uniform', { value: true } );
  74. }
  75. }
  76. export { FloatUniform, Vector2Uniform, Vector3Uniform, Vector4Uniform, ColorUniform, Matrix3Uniform, Matrix4Uniform };