materialDefines.ts 5.9 KB

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