babylon.terrainMaterial.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 POINTSIZE = false;
  9. public FOG = false;
  10. public SPECULARTERM = false;
  11. public NORMAL = false;
  12. public UV1 = false;
  13. public UV2 = false;
  14. public VERTEXCOLOR = false;
  15. public VERTEXALPHA = false;
  16. public NUM_BONE_INFLUENCERS = 0;
  17. public BonesPerMesh = 0;
  18. public INSTANCES = false;
  19. public USERIGHTHANDEDSYSTEM = 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 _worldViewProjectionMatrix = Matrix.Zero();
  69. private _renderId: number;
  70. constructor(name: string, scene: Scene) {
  71. super(name, scene);
  72. }
  73. public needAlphaBlending(): boolean {
  74. return (this.alpha < 1.0);
  75. }
  76. public needAlphaTesting(): boolean {
  77. return false;
  78. }
  79. public getAlphaTestTexture(): BaseTexture {
  80. return null;
  81. }
  82. // Methods
  83. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  84. if (this.isFrozen) {
  85. if (this._wasPreviouslyReady && subMesh.effect) {
  86. return true;
  87. }
  88. }
  89. if (!subMesh._materialDefines) {
  90. subMesh._materialDefines = new TerrainMaterialDefines();
  91. }
  92. var defines = <TerrainMaterialDefines>subMesh._materialDefines;
  93. var scene = this.getScene();
  94. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  95. if (this._renderId === scene.getRenderId()) {
  96. return true;
  97. }
  98. }
  99. var engine = scene.getEngine();
  100. // Textures
  101. if (scene.texturesEnabled) {
  102. if (this.mixTexture && StandardMaterial.DiffuseTextureEnabled) {
  103. if (!this.mixTexture.isReady()) {
  104. return false;
  105. } else {
  106. defines._needUVs = true;
  107. defines.DIFFUSE = true;
  108. }
  109. }
  110. if ((this.bumpTexture1 || this.bumpTexture2 || this.bumpTexture3) && StandardMaterial.BumpTextureEnabled) {
  111. defines._needUVs = true;
  112. defines._needNormals = true;
  113. defines.BUMP = true;
  114. }
  115. }
  116. // Misc.
  117. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, defines);
  118. // Lights
  119. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  120. // Values that need to be evaluated on every frame
  121. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances);
  122. // Attribs
  123. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  124. // Get correct effect
  125. if (defines.isDirty) {
  126. defines.markAsProcessed();
  127. scene.resetCachedMaterial();
  128. // Fallbacks
  129. var fallbacks = new EffectFallbacks();
  130. if (defines.FOG) {
  131. fallbacks.addFallback(1, "FOG");
  132. }
  133. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this.maxSimultaneousLights);
  134. if (defines.NUM_BONE_INFLUENCERS > 0) {
  135. fallbacks.addCPUSkinningFallback(0, mesh);
  136. }
  137. //Attributes
  138. var attribs = [VertexBuffer.PositionKind];
  139. if (defines.NORMAL) {
  140. attribs.push(VertexBuffer.NormalKind);
  141. }
  142. if (defines.UV1) {
  143. attribs.push(VertexBuffer.UVKind);
  144. }
  145. if (defines.UV2) {
  146. attribs.push(VertexBuffer.UV2Kind);
  147. }
  148. if (defines.VERTEXCOLOR) {
  149. attribs.push(VertexBuffer.ColorKind);
  150. }
  151. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  152. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  153. // Legacy browser patch
  154. var shaderName = "terrain";
  155. var join = defines.toString();
  156. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  157. "vFogInfos", "vFogColor", "pointSize",
  158. "vTextureInfos",
  159. "mBones",
  160. "vClipPlane", "textureMatrix",
  161. "diffuse1Infos", "diffuse2Infos", "diffuse3Infos"
  162. ];
  163. var samplers = ["textureSampler", "diffuse1Sampler", "diffuse2Sampler", "diffuse3Sampler",
  164. "bump1Sampler", "bump2Sampler", "bump3Sampler"
  165. ];
  166. var uniformBuffers = [];
  167. MaterialHelper.PrepareUniformsAndSamplersList(<EffectCreationOptions>{
  168. uniformsNames: uniforms,
  169. uniformBuffersNames: uniformBuffers,
  170. samplers: samplers,
  171. defines: defines,
  172. maxSimultaneousLights: this.maxSimultaneousLights
  173. });
  174. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  175. <EffectCreationOptions>{
  176. attributes: attribs,
  177. uniformsNames: uniforms,
  178. uniformBuffersNames: uniformBuffers,
  179. samplers: samplers,
  180. defines: join,
  181. fallbacks: fallbacks,
  182. onCompiled: this.onCompiled,
  183. onError: this.onError,
  184. indexParameters: { maxSimultaneousLights: this.maxSimultaneousLights }
  185. }, engine), defines);
  186. }
  187. if (!subMesh.effect.isReady()) {
  188. return false;
  189. }
  190. this._renderId = scene.getRenderId();
  191. this._wasPreviouslyReady = true;
  192. return true;
  193. }
  194. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  195. var scene = this.getScene();
  196. var defines = <TerrainMaterialDefines>subMesh._materialDefines;
  197. if (!defines) {
  198. return;
  199. }
  200. var effect = subMesh.effect;
  201. this._activeEffect = effect;
  202. // Matrices
  203. this.bindOnlyWorldMatrix(world);
  204. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  205. // Bones
  206. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  207. if (this._mustRebind(scene, effect)) {
  208. // Textures
  209. if (this.mixTexture) {
  210. this._activeEffect.setTexture("textureSampler", this._mixTexture);
  211. this._activeEffect.setFloat2("vTextureInfos", this._mixTexture.coordinatesIndex, this._mixTexture.level);
  212. this._activeEffect.setMatrix("textureMatrix", this._mixTexture.getTextureMatrix());
  213. if (StandardMaterial.DiffuseTextureEnabled) {
  214. if (this._diffuseTexture1) {
  215. this._activeEffect.setTexture("diffuse1Sampler", this._diffuseTexture1);
  216. this._activeEffect.setFloat2("diffuse1Infos", this._diffuseTexture1.uScale, this._diffuseTexture1.vScale);
  217. }
  218. if (this._diffuseTexture2) {
  219. this._activeEffect.setTexture("diffuse2Sampler", this._diffuseTexture2);
  220. this._activeEffect.setFloat2("diffuse2Infos", this._diffuseTexture2.uScale, this._diffuseTexture2.vScale);
  221. }
  222. if (this._diffuseTexture3) {
  223. this._activeEffect.setTexture("diffuse3Sampler", this._diffuseTexture3);
  224. this._activeEffect.setFloat2("diffuse3Infos", this._diffuseTexture3.uScale, this._diffuseTexture3.vScale);
  225. }
  226. }
  227. if (StandardMaterial.BumpTextureEnabled && scene.getEngine().getCaps().standardDerivatives) {
  228. if (this._bumpTexture1) {
  229. this._activeEffect.setTexture("bump1Sampler", this._bumpTexture1);
  230. }
  231. if (this._bumpTexture2) {
  232. this._activeEffect.setTexture("bump2Sampler", this._bumpTexture2);
  233. }
  234. if (this._bumpTexture3) {
  235. this._activeEffect.setTexture("bump3Sampler", this._bumpTexture3);
  236. }
  237. }
  238. }
  239. // Clip plane
  240. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  241. // Point size
  242. if (this.pointsCloud) {
  243. this._activeEffect.setFloat("pointSize", this.pointSize);
  244. }
  245. this._activeEffect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  246. }
  247. this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  248. if (defines.SPECULARTERM) {
  249. this._activeEffect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  250. }
  251. if (scene.lightsEnabled && !this.disableLighting) {
  252. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this.maxSimultaneousLights);
  253. }
  254. // View
  255. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  256. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  257. }
  258. // Fog
  259. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  260. this._afterBind(mesh, this._activeEffect);
  261. }
  262. public getAnimatables(): IAnimatable[] {
  263. var results = [];
  264. if (this.mixTexture && this.mixTexture.animations && this.mixTexture.animations.length > 0) {
  265. results.push(this.mixTexture);
  266. }
  267. return results;
  268. }
  269. public getActiveTextures(): BaseTexture[] {
  270. var activeTextures = super.getActiveTextures();
  271. if (this._mixTexture) {
  272. activeTextures.push(this._mixTexture);
  273. }
  274. if (this._diffuseTexture1) {
  275. activeTextures.push(this._diffuseTexture1);
  276. }
  277. if (this._diffuseTexture2) {
  278. activeTextures.push(this._diffuseTexture2);
  279. }
  280. if (this._diffuseTexture3) {
  281. activeTextures.push(this._diffuseTexture3);
  282. }
  283. if (this._bumpTexture1) {
  284. activeTextures.push(this._bumpTexture1);
  285. }
  286. if (this._bumpTexture2) {
  287. activeTextures.push(this._bumpTexture2);
  288. }
  289. if (this._bumpTexture3) {
  290. activeTextures.push(this._bumpTexture3);
  291. }
  292. return activeTextures;
  293. }
  294. public dispose(forceDisposeEffect?: boolean): void {
  295. if (this.mixTexture) {
  296. this.mixTexture.dispose();
  297. }
  298. super.dispose(forceDisposeEffect);
  299. }
  300. public clone(name: string): TerrainMaterial {
  301. return SerializationHelper.Clone(() => new TerrainMaterial(name, this.getScene()), this);
  302. }
  303. public serialize(): any {
  304. var serializationObject = SerializationHelper.Serialize(this);
  305. serializationObject.customType = "BABYLON.TerrainMaterial";
  306. return serializationObject;
  307. }
  308. // Statics
  309. public static Parse(source: any, scene: Scene, rootUrl: string): TerrainMaterial {
  310. return SerializationHelper.Parse(() => new TerrainMaterial(source.name, scene), source, scene, rootUrl);
  311. }
  312. }
  313. }