ModelMaterial.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import defined from "../Core/defined.js";
  2. import DeveloperError from "../Core/DeveloperError.js";
  3. /**
  4. * A model's material with modifiable parameters. A glTF material
  5. * contains parameters defined by the material's technique with values
  6. * defined by the technique and potentially overridden by the material.
  7. * This class allows changing these values at runtime.
  8. * <p>
  9. * Use {@link Model#getMaterial} to create an instance.
  10. * </p>
  11. *
  12. * @alias ModelMaterial
  13. * @internalConstructor
  14. * @class
  15. *
  16. * @see Model#getMaterial
  17. */
  18. function ModelMaterial(model, material, id) {
  19. this._name = material.name;
  20. this._id = id;
  21. this._uniformMap = model._uniformMaps[id];
  22. this._technique = undefined;
  23. this._program = undefined;
  24. this._values = undefined;
  25. }
  26. Object.defineProperties(ModelMaterial.prototype, {
  27. /**
  28. * The value of the <code>name</code> property of this material.
  29. *
  30. * @memberof ModelMaterial.prototype
  31. *
  32. * @type {String}
  33. * @readonly
  34. */
  35. name: {
  36. get: function () {
  37. return this._name;
  38. },
  39. },
  40. /**
  41. * The index of the material.
  42. *
  43. * @memberof ModelMaterial.prototype
  44. *
  45. * @type {String}
  46. * @readonly
  47. */
  48. id: {
  49. get: function () {
  50. return this._id;
  51. },
  52. },
  53. });
  54. /**
  55. * Assigns a value to a material parameter. The type for <code>value</code>
  56. * depends on the glTF type of the parameter. It will be a floating-point
  57. * number, Cartesian, or matrix.
  58. *
  59. * @param {String} name The name of the parameter.
  60. * @param {*} [value] The value to assign to the parameter.
  61. *
  62. * @exception {DeveloperError} name must match a parameter name in the material's technique that is targetable and not optimized out.
  63. *
  64. * @example
  65. * material.setValue('diffuse', new Cesium.Cartesian4(1.0, 0.0, 0.0, 1.0)); // vec4
  66. * material.setValue('shininess', 256.0); // scalar
  67. */
  68. ModelMaterial.prototype.setValue = function (name, value) {
  69. //>>includeStart('debug', pragmas.debug);
  70. if (!defined(name)) {
  71. throw new DeveloperError("name is required.");
  72. }
  73. //>>includeEnd('debug');
  74. const uniformName = `u_${name}`;
  75. const v = this._uniformMap.values[uniformName];
  76. //>>includeStart('debug', pragmas.debug);
  77. if (!defined(v)) {
  78. throw new DeveloperError(
  79. "name must match a parameter name in the material's technique that is targetable and not optimized out."
  80. );
  81. }
  82. //>>includeEnd('debug');
  83. v.value = v.clone(value, v.value);
  84. };
  85. /**
  86. * Returns the value of the parameter with the given <code>name</code>. The type of the
  87. * returned object depends on the glTF type of the parameter. It will be a floating-point
  88. * number, Cartesian, or matrix.
  89. *
  90. * @param {String} name The name of the parameter.
  91. * @returns {*} The value of the parameter or <code>undefined</code> if the parameter does not exist.
  92. */
  93. ModelMaterial.prototype.getValue = function (name) {
  94. //>>includeStart('debug', pragmas.debug);
  95. if (!defined(name)) {
  96. throw new DeveloperError("name is required.");
  97. }
  98. //>>includeEnd('debug');
  99. const uniformName = `u_${name}`;
  100. const v = this._uniformMap.values[uniformName];
  101. if (!defined(v)) {
  102. return undefined;
  103. }
  104. return v.value;
  105. };
  106. export default ModelMaterial;