ModelMaterial.js 4.0 KB

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