babylon.terrainMaterial.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. class TerrainMaterialDefines extends MaterialDefines {
  4. public DIFFUSE = false;
  5. public BUMP = false;
  6. public CLIPPLANE = false;
  7. public ALPHATEST = false;
  8. public DEPTHPREPASS = false;
  9. public POINTSIZE = false;
  10. public FOG = false;
  11. public SPECULARTERM = false;
  12. public NORMAL = false;
  13. public UV1 = false;
  14. public UV2 = false;
  15. public VERTEXCOLOR = false;
  16. public VERTEXALPHA = false;
  17. public NUM_BONE_INFLUENCERS = 0;
  18. public BonesPerMesh = 0;
  19. public INSTANCES = false;
  20. constructor() {
  21. super();
  22. this.rebuild();
  23. }
  24. }
  25. export class TerrainMaterial extends PushMaterial {
  26. @serializeAsTexture("mixTexture")
  27. private _mixTexture: BaseTexture;
  28. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  29. public mixTexture: BaseTexture;
  30. @serializeAsTexture("diffuseTexture1")
  31. private _diffuseTexture1: Texture;
  32. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  33. public diffuseTexture1: Texture;
  34. @serializeAsTexture("diffuseTexture2")
  35. private _diffuseTexture2: Texture;
  36. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  37. public diffuseTexture2: Texture;
  38. @serializeAsTexture("diffuseTexture3")
  39. private _diffuseTexture3: Texture;
  40. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  41. public diffuseTexture3: Texture;
  42. @serializeAsTexture("bumpTexture1")
  43. private _bumpTexture1: Texture;
  44. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  45. public bumpTexture1: Texture;
  46. @serializeAsTexture("bumpTexture2")
  47. private _bumpTexture2: Texture;
  48. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  49. public bumpTexture2: Texture;
  50. @serializeAsTexture("bumpTexture3")
  51. private _bumpTexture3: Texture;
  52. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  53. public bumpTexture3: Texture;
  54. @serializeAsColor3()
  55. public diffuseColor = new Color3(1, 1, 1);
  56. @serializeAsColor3()
  57. public specularColor = new Color3(0, 0, 0);
  58. @serialize()
  59. public specularPower = 64;
  60. @serialize("disableLighting")
  61. private _disableLighting = false;
  62. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  63. public disableLighting: boolean;
  64. @serialize("maxSimultaneousLights")
  65. private _maxSimultaneousLights = 4;
  66. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  67. public maxSimultaneousLights: number;
  68. private _renderId: number;
  69. constructor(name: string, scene: Scene) {
  70. super(name, scene);
  71. }
  72. public needAlphaBlending(): boolean {
  73. return (this.alpha < 1.0);
  74. }
  75. public needAlphaTesting(): boolean {
  76. return false;
  77. }
  78. public getAlphaTestTexture(): Nullable<BaseTexture> {
  79. return null;
  80. }
  81. // Methods
  82. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  83. if (this.isFrozen) {
  84. if (this._wasPreviouslyReady && subMesh.effect) {
  85. return true;
  86. }
  87. }
  88. if (!subMesh._materialDefines) {
  89. subMesh._materialDefines = new TerrainMaterialDefines();
  90. }
  91. var defines = <TerrainMaterialDefines>subMesh._materialDefines;
  92. var scene = this.getScene();
  93. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  94. if (this._renderId === scene.getRenderId()) {
  95. return true;
  96. }
  97. }
  98. var engine = scene.getEngine();
  99. // Textures
  100. if (scene.texturesEnabled) {
  101. if (this.mixTexture && StandardMaterial.DiffuseTextureEnabled) {
  102. if (!this.mixTexture.isReady()) {
  103. return false;
  104. } else {
  105. defines._needUVs = true;
  106. defines.DIFFUSE = true;
  107. }
  108. }
  109. if ((this.bumpTexture1 || this.bumpTexture2 || this.bumpTexture3) && StandardMaterial.BumpTextureEnabled) {
  110. defines._needUVs = true;
  111. defines._needNormals = true;
  112. defines.BUMP = true;
  113. }
  114. }
  115. // Misc.
  116. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, defines);
  117. // Lights
  118. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  119. // Values that need to be evaluated on every frame
  120. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false, this._shouldTurnAlphaTestOn(mesh));
  121. // Attribs
  122. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  123. // Get correct effect
  124. if (defines.isDirty) {
  125. defines.markAsProcessed();
  126. scene.resetCachedMaterial();
  127. // Fallbacks
  128. var fallbacks = new EffectFallbacks();
  129. if (defines.FOG) {
  130. fallbacks.addFallback(1, "FOG");
  131. }
  132. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this.maxSimultaneousLights);
  133. if (defines.NUM_BONE_INFLUENCERS > 0) {
  134. fallbacks.addCPUSkinningFallback(0, mesh);
  135. }
  136. //Attributes
  137. var attribs = [VertexBuffer.PositionKind];
  138. if (defines.NORMAL) {
  139. attribs.push(VertexBuffer.NormalKind);
  140. }
  141. if (defines.UV1) {
  142. attribs.push(VertexBuffer.UVKind);
  143. }
  144. if (defines.UV2) {
  145. attribs.push(VertexBuffer.UV2Kind);
  146. }
  147. if (defines.VERTEXCOLOR) {
  148. attribs.push(VertexBuffer.ColorKind);
  149. }
  150. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  151. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  152. // Legacy browser patch
  153. var shaderName = "terrain";
  154. var join = defines.toString();
  155. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  156. "vFogInfos", "vFogColor", "pointSize",
  157. "vTextureInfos",
  158. "mBones",
  159. "vClipPlane", "textureMatrix",
  160. "diffuse1Infos", "diffuse2Infos", "diffuse3Infos"
  161. ];
  162. var samplers = ["textureSampler", "diffuse1Sampler", "diffuse2Sampler", "diffuse3Sampler",
  163. "bump1Sampler", "bump2Sampler", "bump3Sampler"
  164. ];
  165. var uniformBuffers = new Array<string>()
  166. MaterialHelper.PrepareUniformsAndSamplersList(<EffectCreationOptions>{
  167. uniformsNames: uniforms,
  168. uniformBuffersNames: uniformBuffers,
  169. samplers: samplers,
  170. defines: defines,
  171. maxSimultaneousLights: this.maxSimultaneousLights
  172. });
  173. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  174. <EffectCreationOptions>{
  175. attributes: attribs,
  176. uniformsNames: uniforms,
  177. uniformBuffersNames: uniformBuffers,
  178. samplers: samplers,
  179. defines: join,
  180. fallbacks: fallbacks,
  181. onCompiled: this.onCompiled,
  182. onError: this.onError,
  183. indexParameters: { maxSimultaneousLights: this.maxSimultaneousLights }
  184. }, engine), defines);
  185. }
  186. if (!subMesh.effect || !subMesh.effect.isReady()) {
  187. return false;
  188. }
  189. this._renderId = scene.getRenderId();
  190. this._wasPreviouslyReady = true;
  191. return true;
  192. }
  193. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  194. var scene = this.getScene();
  195. var defines = <TerrainMaterialDefines>subMesh._materialDefines;
  196. if (!defines) {
  197. return;
  198. }
  199. var effect = subMesh.effect;
  200. if (!effect) {
  201. return;
  202. }
  203. this._activeEffect = effect;
  204. // Matrices
  205. this.bindOnlyWorldMatrix(world);
  206. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  207. // Bones
  208. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  209. if (this._mustRebind(scene, effect)) {
  210. // Textures
  211. if (this.mixTexture) {
  212. this._activeEffect.setTexture("textureSampler", this._mixTexture);
  213. this._activeEffect.setFloat2("vTextureInfos", this._mixTexture.coordinatesIndex, this._mixTexture.level);
  214. this._activeEffect.setMatrix("textureMatrix", this._mixTexture.getTextureMatrix());
  215. if (StandardMaterial.DiffuseTextureEnabled) {
  216. if (this._diffuseTexture1) {
  217. this._activeEffect.setTexture("diffuse1Sampler", this._diffuseTexture1);
  218. this._activeEffect.setFloat2("diffuse1Infos", this._diffuseTexture1.uScale, this._diffuseTexture1.vScale);
  219. }
  220. if (this._diffuseTexture2) {
  221. this._activeEffect.setTexture("diffuse2Sampler", this._diffuseTexture2);
  222. this._activeEffect.setFloat2("diffuse2Infos", this._diffuseTexture2.uScale, this._diffuseTexture2.vScale);
  223. }
  224. if (this._diffuseTexture3) {
  225. this._activeEffect.setTexture("diffuse3Sampler", this._diffuseTexture3);
  226. this._activeEffect.setFloat2("diffuse3Infos", this._diffuseTexture3.uScale, this._diffuseTexture3.vScale);
  227. }
  228. }
  229. if (StandardMaterial.BumpTextureEnabled && scene.getEngine().getCaps().standardDerivatives) {
  230. if (this._bumpTexture1) {
  231. this._activeEffect.setTexture("bump1Sampler", this._bumpTexture1);
  232. }
  233. if (this._bumpTexture2) {
  234. this._activeEffect.setTexture("bump2Sampler", this._bumpTexture2);
  235. }
  236. if (this._bumpTexture3) {
  237. this._activeEffect.setTexture("bump3Sampler", this._bumpTexture3);
  238. }
  239. }
  240. }
  241. // Clip plane
  242. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  243. // Point size
  244. if (this.pointsCloud) {
  245. this._activeEffect.setFloat("pointSize", this.pointSize);
  246. }
  247. MaterialHelper.BindEyePosition(effect, scene);
  248. }
  249. this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  250. if (defines.SPECULARTERM) {
  251. this._activeEffect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  252. }
  253. if (scene.lightsEnabled && !this.disableLighting) {
  254. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this.maxSimultaneousLights);
  255. }
  256. // View
  257. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  258. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  259. }
  260. // Fog
  261. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  262. this._afterBind(mesh, this._activeEffect);
  263. }
  264. public getAnimatables(): IAnimatable[] {
  265. var results = [];
  266. if (this.mixTexture && this.mixTexture.animations && this.mixTexture.animations.length > 0) {
  267. results.push(this.mixTexture);
  268. }
  269. return results;
  270. }
  271. public getActiveTextures(): BaseTexture[] {
  272. var activeTextures = super.getActiveTextures();
  273. if (this._mixTexture) {
  274. activeTextures.push(this._mixTexture);
  275. }
  276. if (this._diffuseTexture1) {
  277. activeTextures.push(this._diffuseTexture1);
  278. }
  279. if (this._diffuseTexture2) {
  280. activeTextures.push(this._diffuseTexture2);
  281. }
  282. if (this._diffuseTexture3) {
  283. activeTextures.push(this._diffuseTexture3);
  284. }
  285. if (this._bumpTexture1) {
  286. activeTextures.push(this._bumpTexture1);
  287. }
  288. if (this._bumpTexture2) {
  289. activeTextures.push(this._bumpTexture2);
  290. }
  291. if (this._bumpTexture3) {
  292. activeTextures.push(this._bumpTexture3);
  293. }
  294. return activeTextures;
  295. }
  296. public hasTexture(texture: BaseTexture): boolean {
  297. if (super.hasTexture(texture)) {
  298. return true;
  299. }
  300. if (this._mixTexture === texture) {
  301. return true;
  302. }
  303. if (this._diffuseTexture1 === texture) {
  304. return true;
  305. }
  306. if (this._diffuseTexture2 === texture) {
  307. return true;
  308. }
  309. if (this._diffuseTexture3 === texture) {
  310. return true;
  311. }
  312. if (this._bumpTexture1 === texture) {
  313. return true;
  314. }
  315. if (this._bumpTexture2 === texture) {
  316. return true;
  317. }
  318. if (this._bumpTexture3 === texture) {
  319. return true;
  320. }
  321. return false;
  322. }
  323. public dispose(forceDisposeEffect?: boolean): void {
  324. if (this.mixTexture) {
  325. this.mixTexture.dispose();
  326. }
  327. super.dispose(forceDisposeEffect);
  328. }
  329. public clone(name: string): TerrainMaterial {
  330. return SerializationHelper.Clone(() => new TerrainMaterial(name, this.getScene()), this);
  331. }
  332. public serialize(): any {
  333. var serializationObject = SerializationHelper.Serialize(this);
  334. serializationObject.customType = "BABYLON.TerrainMaterial";
  335. return serializationObject;
  336. }
  337. public getClassName(): string {
  338. return "TerrainMaterial";
  339. }
  340. // Statics
  341. public static Parse(source: any, scene: Scene, rootUrl: string): TerrainMaterial {
  342. return SerializationHelper.Parse(() => new TerrainMaterial(source.name, scene), source, scene, rootUrl);
  343. }
  344. }
  345. }