WebGPUUniform.js 2.1 KB

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