WebGPUUniform.js 2.4 KB

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