materialDefines.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Manages the defines for the Material
  3. */
  4. export class MaterialDefines {
  5. /** @hidden */
  6. protected _keys: string[];
  7. private _isDirty = true;
  8. /** @hidden */
  9. public _renderId: number;
  10. /** @hidden */
  11. public _areLightsDirty = true;
  12. /** @hidden */
  13. public _areLightsDisposed = false;
  14. /** @hidden */
  15. public _areAttributesDirty = true;
  16. /** @hidden */
  17. public _areTexturesDirty = true;
  18. /** @hidden */
  19. public _areFresnelDirty = true;
  20. /** @hidden */
  21. public _areMiscDirty = true;
  22. /** @hidden */
  23. public _areImageProcessingDirty = true;
  24. /** @hidden */
  25. public _normals = false;
  26. /** @hidden */
  27. public _uvs = false;
  28. /** @hidden */
  29. public _needNormals = false;
  30. /** @hidden */
  31. public _needUVs = false;
  32. [id: string]: any;
  33. /**
  34. * Specifies if the material needs to be re-calculated
  35. */
  36. public get isDirty(): boolean {
  37. return this._isDirty;
  38. }
  39. /**
  40. * Marks the material to indicate that it has been re-calculated
  41. */
  42. public markAsProcessed() {
  43. this._isDirty = false;
  44. this._areAttributesDirty = false;
  45. this._areTexturesDirty = false;
  46. this._areFresnelDirty = false;
  47. this._areLightsDirty = false;
  48. this._areLightsDisposed = false;
  49. this._areMiscDirty = false;
  50. this._areImageProcessingDirty = false;
  51. }
  52. /**
  53. * Marks the material to indicate that it needs to be re-calculated
  54. */
  55. public markAsUnprocessed() {
  56. this._isDirty = true;
  57. }
  58. /**
  59. * Marks the material to indicate all of its defines need to be re-calculated
  60. */
  61. public markAllAsDirty() {
  62. this._areTexturesDirty = true;
  63. this._areAttributesDirty = true;
  64. this._areLightsDirty = true;
  65. this._areFresnelDirty = true;
  66. this._areMiscDirty = true;
  67. this._areImageProcessingDirty = true;
  68. this._isDirty = true;
  69. }
  70. /**
  71. * Marks the material to indicate that image processing needs to be re-calculated
  72. */
  73. public markAsImageProcessingDirty() {
  74. this._areImageProcessingDirty = true;
  75. this._isDirty = true;
  76. }
  77. /**
  78. * Marks the material to indicate the lights need to be re-calculated
  79. * @param disposed Defines whether the light is dirty due to dispose or not
  80. */
  81. public markAsLightDirty(disposed = false) {
  82. this._areLightsDirty = true;
  83. this._areLightsDisposed = this._areLightsDisposed || disposed;
  84. this._isDirty = true;
  85. }
  86. /**
  87. * Marks the attribute state as changed
  88. */
  89. public markAsAttributesDirty() {
  90. this._areAttributesDirty = true;
  91. this._isDirty = true;
  92. }
  93. /**
  94. * Marks the texture state as changed
  95. */
  96. public markAsTexturesDirty() {
  97. this._areTexturesDirty = true;
  98. this._isDirty = true;
  99. }
  100. /**
  101. * Marks the fresnel state as changed
  102. */
  103. public markAsFresnelDirty() {
  104. this._areFresnelDirty = true;
  105. this._isDirty = true;
  106. }
  107. /**
  108. * Marks the misc state as changed
  109. */
  110. public markAsMiscDirty() {
  111. this._areMiscDirty = true;
  112. this._isDirty = true;
  113. }
  114. /**
  115. * Rebuilds the material defines
  116. */
  117. public rebuild() {
  118. if (this._keys) {
  119. delete this._keys;
  120. }
  121. this._keys = [];
  122. for (var key of Object.keys(this)) {
  123. if (key[0] === "_") {
  124. continue;
  125. }
  126. this._keys.push(key);
  127. }
  128. }
  129. /**
  130. * Specifies if two material defines are equal
  131. * @param other - A material define instance to compare to
  132. * @returns - Boolean indicating if the material defines are equal (true) or not (false)
  133. */
  134. public isEqual(other: MaterialDefines): boolean {
  135. if (this._keys.length !== other._keys.length) {
  136. return false;
  137. }
  138. for (var index = 0; index < this._keys.length; index++) {
  139. var prop = this._keys[index];
  140. if ((<any>this)[prop] !== (<any>other)[prop]) {
  141. return false;
  142. }
  143. }
  144. return true;
  145. }
  146. /**
  147. * Clones this instance's defines to another instance
  148. * @param other - material defines to clone values to
  149. */
  150. public cloneTo(other: MaterialDefines): void {
  151. if (this._keys.length !== other._keys.length) {
  152. other._keys = this._keys.slice(0);
  153. }
  154. for (var index = 0; index < this._keys.length; index++) {
  155. var prop = this._keys[index];
  156. (<any>other)[prop] = (<any>this)[prop];
  157. }
  158. }
  159. /**
  160. * Resets the material define values
  161. */
  162. public reset(): void {
  163. for (var index = 0; index < this._keys.length; index++) {
  164. var prop = this._keys[index];
  165. var type = typeof (<any>this)[prop];
  166. switch (type) {
  167. case "number":
  168. (<any>this)[prop] = 0;
  169. break;
  170. case "string":
  171. (<any>this)[prop] = "";
  172. break;
  173. default:
  174. (<any>this)[prop] = false;
  175. break;
  176. }
  177. }
  178. }
  179. /**
  180. * Converts the material define values to a string
  181. * @returns - String of material define information
  182. */
  183. public toString(): string {
  184. var result = "";
  185. for (var index = 0; index < this._keys.length; index++) {
  186. var prop = this._keys[index];
  187. var value = (<any>this)[prop];
  188. var type = typeof value;
  189. switch (type) {
  190. case "number":
  191. case "string":
  192. result += "#define " + prop + " " + value + "\n";
  193. break;
  194. default:
  195. if (value) {
  196. result += "#define " + prop + "\n";
  197. }
  198. break;
  199. }
  200. }
  201. return result;
  202. }
  203. }