babylon.terrainMaterial.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. var maxSimultaneousLights = 4;
  4. class TerrainMaterialDefines extends MaterialDefines {
  5. public DIFFUSE = false;
  6. public BUMP = false;
  7. public CLIPPLANE = false;
  8. public ALPHATEST = 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 Material {
  26. @serializeAsTexture()
  27. public mixTexture: BaseTexture;
  28. @serializeAsTexture()
  29. public diffuseTexture1: Texture;
  30. @serializeAsTexture()
  31. public diffuseTexture2: Texture;
  32. @serializeAsTexture()
  33. public diffuseTexture3: Texture;
  34. @serializeAsTexture()
  35. public bumpTexture1: Texture;
  36. @serializeAsTexture()
  37. public bumpTexture2: Texture;
  38. @serializeAsTexture()
  39. public bumpTexture3: Texture;
  40. @serializeAsColor3()
  41. public diffuseColor = new Color3(1, 1, 1);
  42. @serializeAsColor3()
  43. public specularColor = new Color3(0, 0, 0);
  44. @serialize()
  45. public specularPower = 64;
  46. @serialize()
  47. public disableLighting = false;
  48. @serialize()
  49. public maxSimultaneousLights = 4;
  50. private _worldViewProjectionMatrix = Matrix.Zero();
  51. private _renderId: number;
  52. private _defines = new TerrainMaterialDefines();
  53. private _cachedDefines = new TerrainMaterialDefines();
  54. constructor(name: string, scene: Scene) {
  55. super(name, scene);
  56. this._cachedDefines.BonesPerMesh = -1;
  57. }
  58. public needAlphaBlending(): boolean {
  59. return (this.alpha < 1.0);
  60. }
  61. public needAlphaTesting(): boolean {
  62. return false;
  63. }
  64. public getAlphaTestTexture(): BaseTexture {
  65. return null;
  66. }
  67. // Methods
  68. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  69. if (!mesh) {
  70. return true;
  71. }
  72. if (this._defines.INSTANCES !== useInstances) {
  73. return false;
  74. }
  75. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  76. return true;
  77. }
  78. return false;
  79. }
  80. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  81. if (this.checkReadyOnlyOnce) {
  82. if (this._wasPreviouslyReady) {
  83. return true;
  84. }
  85. }
  86. var scene = this.getScene();
  87. if (!this.checkReadyOnEveryCall) {
  88. if (this._renderId === scene.getRenderId()) {
  89. if (this._checkCache(scene, mesh, useInstances)) {
  90. return true;
  91. }
  92. }
  93. }
  94. var engine = scene.getEngine();
  95. var needNormals = false;
  96. var needUVs = false;
  97. this._defines.reset();
  98. // Effect
  99. if (scene.clipPlane) {
  100. this._defines.CLIPPLANE = true;
  101. }
  102. if (engine.getAlphaTesting()) {
  103. this._defines.ALPHATEST = true;
  104. }
  105. // Point size
  106. if (this.pointsCloud || scene.forcePointsCloud) {
  107. this._defines.POINTSIZE = true;
  108. }
  109. // Fog
  110. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  111. this._defines.FOG = true;
  112. }
  113. // Lights
  114. if (scene.lightsEnabled && !this.disableLighting) {
  115. needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, this.maxSimultaneousLights);
  116. }
  117. // Textures
  118. if (scene.texturesEnabled) {
  119. if (this.mixTexture && StandardMaterial.DiffuseTextureEnabled) {
  120. if (!this.mixTexture.isReady()) {
  121. return false;
  122. } else {
  123. needUVs = true;
  124. this._defines.DIFFUSE = true;
  125. }
  126. }
  127. if ((this.bumpTexture1 || this.bumpTexture2 || this.bumpTexture3) && StandardMaterial.BumpTextureEnabled) {
  128. needUVs = true;
  129. needNormals = true;
  130. this._defines.BUMP = true;
  131. }
  132. }
  133. // Attribs
  134. if (mesh) {
  135. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  136. this._defines.NORMAL = true;
  137. }
  138. if (needUVs) {
  139. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  140. this._defines.UV1 = true;
  141. }
  142. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  143. this._defines.UV2 = true;
  144. }
  145. }
  146. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  147. this._defines.VERTEXCOLOR = true;
  148. if (mesh.hasVertexAlpha) {
  149. this._defines.VERTEXALPHA = true;
  150. }
  151. }
  152. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  153. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  154. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  155. }
  156. // Instances
  157. if (useInstances) {
  158. this._defines.INSTANCES = true;
  159. }
  160. }
  161. // Get correct effect
  162. if (!this._defines.isEqual(this._cachedDefines)) {
  163. this._defines.cloneTo(this._cachedDefines);
  164. scene.resetCachedMaterial();
  165. // Fallbacks
  166. var fallbacks = new EffectFallbacks();
  167. if (this._defines.FOG) {
  168. fallbacks.addFallback(1, "FOG");
  169. }
  170. MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
  171. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  172. fallbacks.addCPUSkinningFallback(0, mesh);
  173. }
  174. //Attributes
  175. var attribs = [VertexBuffer.PositionKind];
  176. if (this._defines.NORMAL) {
  177. attribs.push(VertexBuffer.NormalKind);
  178. }
  179. if (this._defines.UV1) {
  180. attribs.push(VertexBuffer.UVKind);
  181. }
  182. if (this._defines.UV2) {
  183. attribs.push(VertexBuffer.UV2Kind);
  184. }
  185. if (this._defines.VERTEXCOLOR) {
  186. attribs.push(VertexBuffer.ColorKind);
  187. }
  188. MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  189. MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  190. // Legacy browser patch
  191. var shaderName = "terrain";
  192. var join = this._defines.toString();
  193. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  194. "vFogInfos", "vFogColor", "pointSize",
  195. "vTextureInfos",
  196. "mBones",
  197. "vClipPlane", "textureMatrix",
  198. "diffuse1Infos", "diffuse2Infos", "diffuse3Infos"
  199. ];
  200. var samplers = ["textureSampler", "diffuse1Sampler", "diffuse2Sampler", "diffuse3Sampler",
  201. "bump1Sampler", "bump2Sampler", "bump3Sampler"
  202. ];
  203. MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, this._defines, this.maxSimultaneousLights);
  204. this._effect = scene.getEngine().createEffect(shaderName,
  205. attribs, uniforms, samplers,
  206. join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: this.maxSimultaneousLights });
  207. }
  208. if (!this._effect.isReady()) {
  209. return false;
  210. }
  211. this._renderId = scene.getRenderId();
  212. this._wasPreviouslyReady = true;
  213. if (mesh) {
  214. if (!mesh._materialDefines) {
  215. mesh._materialDefines = new TerrainMaterialDefines();
  216. }
  217. this._defines.cloneTo(mesh._materialDefines);
  218. }
  219. return true;
  220. }
  221. public bindOnlyWorldMatrix(world: Matrix): void {
  222. this._effect.setMatrix("world", world);
  223. }
  224. public bind(world: Matrix, mesh?: Mesh): void {
  225. var scene = this.getScene();
  226. // Matrices
  227. this.bindOnlyWorldMatrix(world);
  228. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  229. // Bones
  230. MaterialHelper.BindBonesParameters(mesh, this._effect);
  231. if (scene.getCachedMaterial() !== this) {
  232. // Textures
  233. if (this.mixTexture) {
  234. this._effect.setTexture("textureSampler", this.mixTexture);
  235. this._effect.setFloat2("vTextureInfos", this.mixTexture.coordinatesIndex, this.mixTexture.level);
  236. this._effect.setMatrix("textureMatrix", this.mixTexture.getTextureMatrix());
  237. if (StandardMaterial.DiffuseTextureEnabled) {
  238. if (this.diffuseTexture1) {
  239. this._effect.setTexture("diffuse1Sampler", this.diffuseTexture1);
  240. this._effect.setFloat2("diffuse1Infos", this.diffuseTexture1.uScale, this.diffuseTexture1.vScale);
  241. }
  242. if (this.diffuseTexture2) {
  243. this._effect.setTexture("diffuse2Sampler", this.diffuseTexture2);
  244. this._effect.setFloat2("diffuse2Infos", this.diffuseTexture2.uScale, this.diffuseTexture2.vScale);
  245. }
  246. if (this.diffuseTexture3) {
  247. this._effect.setTexture("diffuse3Sampler", this.diffuseTexture3);
  248. this._effect.setFloat2("diffuse3Infos", this.diffuseTexture3.uScale, this.diffuseTexture3.vScale);
  249. }
  250. }
  251. if (StandardMaterial.BumpTextureEnabled && scene.getEngine().getCaps().standardDerivatives) {
  252. if (this.bumpTexture1) {
  253. this._effect.setTexture("bump1Sampler", this.bumpTexture1);
  254. }
  255. if (this.bumpTexture2) {
  256. this._effect.setTexture("bump2Sampler", this.bumpTexture2);
  257. }
  258. if (this.bumpTexture3) {
  259. this._effect.setTexture("bump3Sampler", this.bumpTexture3);
  260. }
  261. }
  262. }
  263. // Clip plane
  264. MaterialHelper.BindClipPlane(this._effect, scene);
  265. // Point size
  266. if (this.pointsCloud) {
  267. this._effect.setFloat("pointSize", this.pointSize);
  268. }
  269. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  270. }
  271. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  272. if (this._defines.SPECULARTERM) {
  273. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  274. }
  275. if (scene.lightsEnabled && !this.disableLighting) {
  276. MaterialHelper.BindLights(scene, mesh, this._effect, this._defines, this.maxSimultaneousLights);
  277. }
  278. // View
  279. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  280. this._effect.setMatrix("view", scene.getViewMatrix());
  281. }
  282. // Fog
  283. MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  284. super.bind(world, mesh);
  285. }
  286. public getAnimatables(): IAnimatable[] {
  287. var results = [];
  288. if (this.mixTexture && this.mixTexture.animations && this.mixTexture.animations.length > 0) {
  289. results.push(this.mixTexture);
  290. }
  291. return results;
  292. }
  293. public dispose(forceDisposeEffect?: boolean): void {
  294. if (this.mixTexture) {
  295. this.mixTexture.dispose();
  296. }
  297. super.dispose(forceDisposeEffect);
  298. }
  299. public clone(name: string): TerrainMaterial {
  300. return SerializationHelper.Clone(() => new TerrainMaterial(name, this.getScene()), this);
  301. }
  302. public serialize(): any {
  303. var serializationObject = SerializationHelper.Serialize(this);
  304. serializationObject.customType = "BABYLON.TerrainMaterial";
  305. return serializationObject;
  306. }
  307. // Statics
  308. public static Parse(source: any, scene: Scene, rootUrl: string): TerrainMaterial {
  309. return SerializationHelper.Parse(() => new TerrainMaterial(source.name, scene), source, scene, rootUrl);
  310. }
  311. }
  312. }