ModelMaterial.js 3.7 KB

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