babylon.terrainMaterial.ts 14 KB

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