WebGPUUniform.js 2.1 KB

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