babylon.gradientMaterial.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. var maxSimultaneousLights = 4;
  4. class GradientMaterialDefines extends MaterialDefines {
  5. public DIFFUSE = false;
  6. public CLIPPLANE = false;
  7. public ALPHATEST = false;
  8. public POINTSIZE = false;
  9. public FOG = false;
  10. public LIGHT0 = false;
  11. public LIGHT1 = false;
  12. public LIGHT2 = false;
  13. public LIGHT3 = false;
  14. public SPOTLIGHT0 = false;
  15. public SPOTLIGHT1 = false;
  16. public SPOTLIGHT2 = false;
  17. public SPOTLIGHT3 = false;
  18. public HEMILIGHT0 = false;
  19. public HEMILIGHT1 = false;
  20. public HEMILIGHT2 = false;
  21. public HEMILIGHT3 = false;
  22. public DIRLIGHT0 = false;
  23. public DIRLIGHT1 = false;
  24. public DIRLIGHT2 = false;
  25. public DIRLIGHT3 = false;
  26. public POINTLIGHT0 = false;
  27. public POINTLIGHT1 = false;
  28. public POINTLIGHT2 = false;
  29. public POINTLIGHT3 = false;
  30. public SHADOW0 = false;
  31. public SHADOW1 = false;
  32. public SHADOW2 = false;
  33. public SHADOW3 = false;
  34. public SHADOWS = false;
  35. public SHADOWVSM0 = false;
  36. public SHADOWVSM1 = false;
  37. public SHADOWVSM2 = false;
  38. public SHADOWVSM3 = false;
  39. public SHADOWPCF0 = false;
  40. public SHADOWPCF1 = false;
  41. public SHADOWPCF2 = false;
  42. public SHADOWPCF3 = false;
  43. public NORMAL = false;
  44. public UV1 = false;
  45. public UV2 = false;
  46. public VERTEXCOLOR = false;
  47. public VERTEXALPHA = false;
  48. public NUM_BONE_INFLUENCERS = 0;
  49. public BonesPerMesh = 0;
  50. public INSTANCES = false;
  51. constructor() {
  52. super();
  53. this._keys = Object.keys(this);
  54. }
  55. }
  56. export class GradientMaterial extends Material {
  57. // The gradient top color, red by default
  58. public topColor = new Color3(1, 0, 0);
  59. public topColorAlpha = 1.0;
  60. // The gradient top color, blue by default
  61. public bottomColor = new Color3(0, 0, 1);
  62. public bottomColorAlpha = 1.0;
  63. // Gradient offset
  64. public offset = 0;
  65. public smoothness = 1.0;
  66. public disableLighting = false;
  67. private _worldViewProjectionMatrix = Matrix.Zero();
  68. private _scaledDiffuse = new Color3();
  69. private _renderId: number;
  70. private _defines = new GradientMaterialDefines();
  71. private _cachedDefines = new GradientMaterialDefines();
  72. constructor(name: string, scene: Scene) {
  73. super(name, scene);
  74. this._cachedDefines.BonesPerMesh = -1;
  75. }
  76. public needAlphaBlending(): boolean {
  77. return (this.alpha < 1.0 || this.topColorAlpha < 1.0 || this.bottomColorAlpha < 1.0);
  78. }
  79. public needAlphaTesting(): boolean {
  80. return true;
  81. }
  82. public getAlphaTestTexture(): BaseTexture {
  83. return null;
  84. }
  85. // Methods
  86. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  87. if (!mesh) {
  88. return true;
  89. }
  90. if (this._defines.INSTANCES !== useInstances) {
  91. return false;
  92. }
  93. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  94. return true;
  95. }
  96. return false;
  97. }
  98. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  99. if (this.checkReadyOnlyOnce) {
  100. if (this._wasPreviouslyReady) {
  101. return true;
  102. }
  103. }
  104. var scene = this.getScene();
  105. if (!this.checkReadyOnEveryCall) {
  106. if (this._renderId === scene.getRenderId()) {
  107. if (this._checkCache(scene, mesh, useInstances)) {
  108. return true;
  109. }
  110. }
  111. }
  112. var engine = scene.getEngine();
  113. var needNormals = false;
  114. var needUVs = false;
  115. this._defines.reset();
  116. // No textures
  117. // Effect
  118. if (scene.clipPlane) {
  119. this._defines.CLIPPLANE = true;
  120. }
  121. if (engine.getAlphaTesting()) {
  122. this._defines.ALPHATEST = true;
  123. }
  124. // Point size
  125. if (this.pointsCloud || scene.forcePointsCloud) {
  126. this._defines.POINTSIZE = true;
  127. }
  128. // Fog
  129. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  130. this._defines.FOG = true;
  131. }
  132. var lightIndex = 0;
  133. if (scene.lightsEnabled && !this.disableLighting) {
  134. needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines);
  135. }
  136. // Attribs
  137. if (mesh) {
  138. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  139. this._defines.NORMAL = true;
  140. }
  141. if (needUVs) {
  142. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  143. this._defines.UV1 = true;
  144. }
  145. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  146. this._defines.UV2 = true;
  147. }
  148. }
  149. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  150. this._defines.VERTEXCOLOR = true;
  151. if (mesh.hasVertexAlpha) {
  152. this._defines.VERTEXALPHA = true;
  153. }
  154. }
  155. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  156. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  157. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  158. }
  159. // Instances
  160. if (useInstances) {
  161. this._defines.INSTANCES = true;
  162. }
  163. }
  164. // Get correct effect
  165. if (!this._defines.isEqual(this._cachedDefines)) {
  166. this._defines.cloneTo(this._cachedDefines);
  167. scene.resetCachedMaterial();
  168. // Fallbacks
  169. var fallbacks = new EffectFallbacks();
  170. if (this._defines.FOG) {
  171. fallbacks.addFallback(1, "FOG");
  172. }
  173. MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
  174. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  175. fallbacks.addCPUSkinningFallback(0, mesh);
  176. }
  177. //Attributes
  178. var attribs = [VertexBuffer.PositionKind];
  179. if (this._defines.NORMAL) {
  180. attribs.push(VertexBuffer.NormalKind);
  181. }
  182. if (this._defines.UV1) {
  183. attribs.push(VertexBuffer.UVKind);
  184. }
  185. if (this._defines.UV2) {
  186. attribs.push(VertexBuffer.UV2Kind);
  187. }
  188. if (this._defines.VERTEXCOLOR) {
  189. attribs.push(VertexBuffer.ColorKind);
  190. }
  191. MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  192. MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  193. // Legacy browser patch
  194. var shaderName = "gradient";
  195. var join = this._defines.toString();
  196. this._effect = scene.getEngine().createEffect(shaderName,
  197. attribs,
  198. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  199. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  200. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  201. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  202. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  203. "vFogInfos", "vFogColor", "pointSize",
  204. "vDiffuseInfos",
  205. "mBones",
  206. "vClipPlane", "diffuseMatrix",
  207. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues", "topColor", "bottomColor", "offset", "smoothness"
  208. ],
  209. ["diffuseSampler",
  210. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  211. ],
  212. join, fallbacks, this.onCompiled, this.onError);
  213. }
  214. if (!this._effect.isReady()) {
  215. return false;
  216. }
  217. this._renderId = scene.getRenderId();
  218. this._wasPreviouslyReady = true;
  219. if (mesh) {
  220. if (!mesh._materialDefines) {
  221. mesh._materialDefines = new GradientMaterialDefines();
  222. }
  223. this._defines.cloneTo(mesh._materialDefines);
  224. }
  225. return true;
  226. }
  227. public bindOnlyWorldMatrix(world: Matrix): void {
  228. this._effect.setMatrix("world", world);
  229. }
  230. public bind(world: Matrix, mesh?: Mesh): void {
  231. var scene = this.getScene();
  232. // Matrices
  233. this.bindOnlyWorldMatrix(world);
  234. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  235. // Bones
  236. MaterialHelper.BindBonesParameters(mesh, this._effect);
  237. if (scene.getCachedMaterial() !== this) {
  238. // Clip plane
  239. MaterialHelper.BindClipPlane(this._effect, scene);
  240. // Point size
  241. if (this.pointsCloud) {
  242. this._effect.setFloat("pointSize", this.pointSize);
  243. }
  244. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  245. }
  246. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  247. if (scene.lightsEnabled && !this.disableLighting) {
  248. MaterialHelper.BindLights(scene, mesh, this._effect, this._defines);
  249. }
  250. // View
  251. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  252. this._effect.setMatrix("view", scene.getViewMatrix());
  253. }
  254. // Fog
  255. MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  256. this._effect.setColor4("topColor", this.topColor, this.topColorAlpha);
  257. this._effect.setColor4("bottomColor", this.bottomColor, this.bottomColorAlpha);
  258. this._effect.setFloat("offset", this.offset);
  259. this._effect.setFloat("smoothness", this.smoothness);
  260. super.bind(world, mesh);
  261. }
  262. public getAnimatables(): IAnimatable[] {
  263. return [];
  264. }
  265. public dispose(forceDisposeEffect?: boolean): void {
  266. super.dispose(forceDisposeEffect);
  267. }
  268. public clone(name: string): GradientMaterial {
  269. var newMaterial = new GradientMaterial(name, this.getScene());
  270. // Base material
  271. this.copyTo(newMaterial);
  272. // Gradient material
  273. newMaterial.topColor = this.topColor.clone();
  274. newMaterial.bottomColor = this.bottomColor.clone();
  275. newMaterial.offset = this.offset;
  276. return newMaterial;
  277. }
  278. public serialize(): any {
  279. var serializationObject = super.serialize();
  280. serializationObject.customType = "BABYLON.GradientMaterial";
  281. serializationObject.topColor = this.topColor.asArray();
  282. serializationObject.bottomColor = this.bottomColor.asArray();
  283. serializationObject.offset = this.offset;
  284. serializationObject.disableLighting = this.disableLighting;
  285. return serializationObject;
  286. }
  287. public static Parse(source: any, scene: Scene, rootUrl: string): GradientMaterial {
  288. var material = new GradientMaterial(source.name, scene);
  289. material.topColor = Color3.FromArray(source.topColor);
  290. material.bottomColor = Color3.FromArray(source.bottomColor);
  291. material.offset = source.offset;
  292. material.disableLighting = source.disableLighting;
  293. material.alpha = source.alpha;
  294. material.id = source.id;
  295. Tags.AddTagsTo(material, source.tags);
  296. material.backFaceCulling = source.backFaceCulling;
  297. material.wireframe = source.wireframe;
  298. if (source.checkReadyOnlyOnce) {
  299. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  300. }
  301. return material;
  302. }
  303. }
  304. }