babylon.terrainMaterial.ts 14 KB

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