terrainMaterial.ts 16 KB

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