babylon.terrainMaterial.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 LIGHT0 = false;
  12. public LIGHT1 = false;
  13. public LIGHT2 = false;
  14. public LIGHT3 = false;
  15. public SPOTLIGHT0 = false;
  16. public SPOTLIGHT1 = false;
  17. public SPOTLIGHT2 = false;
  18. public SPOTLIGHT3 = false;
  19. public HEMILIGHT0 = false;
  20. public HEMILIGHT1 = false;
  21. public HEMILIGHT2 = false;
  22. public HEMILIGHT3 = false;
  23. public DIRLIGHT0 = false;
  24. public DIRLIGHT1 = false;
  25. public DIRLIGHT2 = false;
  26. public DIRLIGHT3 = false;
  27. public POINTLIGHT0 = false;
  28. public POINTLIGHT1 = false;
  29. public POINTLIGHT2 = false;
  30. public POINTLIGHT3 = false;
  31. public SHADOW0 = false;
  32. public SHADOW1 = false;
  33. public SHADOW2 = false;
  34. public SHADOW3 = false;
  35. public SHADOWS = false;
  36. public SHADOWVSM0 = false;
  37. public SHADOWVSM1 = false;
  38. public SHADOWVSM2 = false;
  39. public SHADOWVSM3 = false;
  40. public SHADOWPCF0 = false;
  41. public SHADOWPCF1 = false;
  42. public SHADOWPCF2 = false;
  43. public SHADOWPCF3 = false;
  44. public SPECULARTERM = false;
  45. public NORMAL = false;
  46. public UV1 = false;
  47. public UV2 = false;
  48. public VERTEXCOLOR = false;
  49. public VERTEXALPHA = false;
  50. public NUM_BONE_INFLUENCERS = 0;
  51. public BonesPerMesh = 0;
  52. public INSTANCES = false;
  53. constructor() {
  54. super();
  55. this._keys = Object.keys(this);
  56. }
  57. }
  58. export class TerrainMaterial extends Material {
  59. public mixTexture: BaseTexture;
  60. public diffuseTexture1: Texture;
  61. public diffuseTexture2: Texture;
  62. public diffuseTexture3: Texture;
  63. public bumpTexture1: Texture;
  64. public bumpTexture2: Texture;
  65. public bumpTexture3: Texture;
  66. public diffuseColor = new Color3(1, 1, 1);
  67. public specularColor = new Color3(0, 0, 0);
  68. public specularPower = 64;
  69. public disableLighting = false;
  70. private _worldViewProjectionMatrix = Matrix.Zero();
  71. private _scaledDiffuse = new Color3();
  72. private _scaledSpecular = new Color3();
  73. private _renderId: number;
  74. private _defines = new TerrainMaterialDefines();
  75. private _cachedDefines = new TerrainMaterialDefines();
  76. constructor(name: string, scene: Scene) {
  77. super(name, scene);
  78. this._cachedDefines.BonesPerMesh = -1;
  79. }
  80. public needAlphaBlending(): boolean {
  81. return (this.alpha < 1.0);
  82. }
  83. public needAlphaTesting(): boolean {
  84. return false;
  85. }
  86. public getAlphaTestTexture(): BaseTexture {
  87. return null;
  88. }
  89. // Methods
  90. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  91. if (!mesh) {
  92. return true;
  93. }
  94. if (this._defines.INSTANCES !== useInstances) {
  95. return false;
  96. }
  97. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  98. return true;
  99. }
  100. return false;
  101. }
  102. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  103. if (this.checkReadyOnlyOnce) {
  104. if (this._wasPreviouslyReady) {
  105. return true;
  106. }
  107. }
  108. var scene = this.getScene();
  109. if (!this.checkReadyOnEveryCall) {
  110. if (this._renderId === scene.getRenderId()) {
  111. if (this._checkCache(scene, mesh, useInstances)) {
  112. return true;
  113. }
  114. }
  115. }
  116. var engine = scene.getEngine();
  117. var needNormals = false;
  118. var needUVs = false;
  119. this._defines.reset();
  120. // Textures
  121. if (scene.texturesEnabled) {
  122. if (this.mixTexture && StandardMaterial.DiffuseTextureEnabled) {
  123. if (!this.mixTexture.isReady()) {
  124. return false;
  125. } else {
  126. needUVs = true;
  127. this._defines.DIFFUSE = true;
  128. }
  129. }
  130. if ((this.bumpTexture1 || this.bumpTexture2 || this.bumpTexture3) && StandardMaterial.BumpTextureEnabled) {
  131. needUVs = true;
  132. needNormals = true;
  133. this._defines.BUMP = true;
  134. }
  135. }
  136. // Effect
  137. if (scene.clipPlane) {
  138. this._defines.CLIPPLANE = true;
  139. }
  140. if (engine.getAlphaTesting()) {
  141. this._defines.ALPHATEST = true;
  142. }
  143. // Point size
  144. if (this.pointsCloud || scene.forcePointsCloud) {
  145. this._defines.POINTSIZE = true;
  146. }
  147. // Fog
  148. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  149. this._defines.FOG = true;
  150. }
  151. var lightIndex = 0;
  152. if (scene.lightsEnabled && !this.disableLighting) {
  153. for (var index = 0; index < scene.lights.length; index++) {
  154. var light = scene.lights[index];
  155. if (!light.isEnabled()) {
  156. continue;
  157. }
  158. // Excluded check
  159. if (light._excludedMeshesIds.length > 0) {
  160. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  161. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  162. if (excludedMesh) {
  163. light.excludedMeshes.push(excludedMesh);
  164. }
  165. }
  166. light._excludedMeshesIds = [];
  167. }
  168. // Included check
  169. if (light._includedOnlyMeshesIds.length > 0) {
  170. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  171. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  172. if (includedOnlyMesh) {
  173. light.includedOnlyMeshes.push(includedOnlyMesh);
  174. }
  175. }
  176. light._includedOnlyMeshesIds = [];
  177. }
  178. if (!light.canAffectMesh(mesh)) {
  179. continue;
  180. }
  181. needNormals = true;
  182. this._defines["LIGHT" + lightIndex] = true;
  183. var type;
  184. if (light instanceof SpotLight) {
  185. type = "SPOTLIGHT" + lightIndex;
  186. } else if (light instanceof HemisphericLight) {
  187. type = "HEMILIGHT" + lightIndex;
  188. } else if (light instanceof PointLight) {
  189. type = "POINTLIGHT" + lightIndex;
  190. } else {
  191. type = "DIRLIGHT" + lightIndex;
  192. }
  193. this._defines[type] = true;
  194. // Specular
  195. if (!light.specular.equalsFloats(0, 0, 0)) {
  196. this._defines.SPECULARTERM = true;
  197. }
  198. // Shadows
  199. if (scene.shadowsEnabled) {
  200. var shadowGenerator = light.getShadowGenerator();
  201. if (mesh && mesh.receiveShadows && shadowGenerator) {
  202. this._defines["SHADOW" + lightIndex] = true;
  203. this._defines.SHADOWS = true;
  204. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  205. this._defines["SHADOWVSM" + lightIndex] = true;
  206. }
  207. if (shadowGenerator.usePoissonSampling) {
  208. this._defines["SHADOWPCF" + lightIndex] = true;
  209. }
  210. }
  211. }
  212. lightIndex++;
  213. if (lightIndex === maxSimultaneousLights)
  214. break;
  215. }
  216. }
  217. // Attribs
  218. if (mesh) {
  219. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  220. this._defines.NORMAL = true;
  221. }
  222. if (needUVs) {
  223. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  224. this._defines.UV1 = true;
  225. }
  226. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  227. this._defines.UV2 = true;
  228. }
  229. }
  230. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  231. this._defines.VERTEXCOLOR = true;
  232. if (mesh.hasVertexAlpha) {
  233. this._defines.VERTEXALPHA = true;
  234. }
  235. }
  236. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  237. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  238. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  239. }
  240. // Instances
  241. if (useInstances) {
  242. this._defines.INSTANCES = true;
  243. }
  244. }
  245. // Get correct effect
  246. if (!this._defines.isEqual(this._cachedDefines)) {
  247. this._defines.cloneTo(this._cachedDefines);
  248. scene.resetCachedMaterial();
  249. // Fallbacks
  250. var fallbacks = new EffectFallbacks();
  251. if (this._defines.FOG) {
  252. fallbacks.addFallback(1, "FOG");
  253. }
  254. for (lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  255. if (!this._defines["LIGHT" + lightIndex]) {
  256. continue;
  257. }
  258. if (lightIndex > 0) {
  259. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  260. }
  261. if (this._defines["SHADOW" + lightIndex]) {
  262. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  263. }
  264. if (this._defines["SHADOWPCF" + lightIndex]) {
  265. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  266. }
  267. if (this._defines["SHADOWVSM" + lightIndex]) {
  268. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  269. }
  270. }
  271. if (this._defines.NUM_BONE_INFLUENCERS > 0){
  272. fallbacks.addCPUSkinningFallback(0, mesh);
  273. }
  274. //Attributes
  275. var attribs = [VertexBuffer.PositionKind];
  276. if (this._defines.NORMAL) {
  277. attribs.push(VertexBuffer.NormalKind);
  278. }
  279. if (this._defines.UV1) {
  280. attribs.push(VertexBuffer.UVKind);
  281. }
  282. if (this._defines.UV2) {
  283. attribs.push(VertexBuffer.UV2Kind);
  284. }
  285. if (this._defines.VERTEXCOLOR) {
  286. attribs.push(VertexBuffer.ColorKind);
  287. }
  288. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  289. attribs.push(VertexBuffer.MatricesIndicesKind);
  290. attribs.push(VertexBuffer.MatricesWeightsKind);
  291. if (this._defines.NUM_BONE_INFLUENCERS > 4) {
  292. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  293. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  294. }
  295. }
  296. if (this._defines.INSTANCES) {
  297. attribs.push("world0");
  298. attribs.push("world1");
  299. attribs.push("world2");
  300. attribs.push("world3");
  301. }
  302. // Legacy browser patch
  303. var shaderName = "terrain";
  304. var join = this._defines.toString();
  305. this._effect = scene.getEngine().createEffect(shaderName,
  306. attribs,
  307. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  308. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  309. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  310. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  311. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  312. "vFogInfos", "vFogColor", "pointSize",
  313. "vTextureInfos",
  314. "mBones",
  315. "vClipPlane", "textureMatrix",
  316. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3",
  317. "diffuse1Infos", "diffuse2Infos", "diffuse3Infos"
  318. ],
  319. ["textureSampler", "diffuse1Sampler", "diffuse2Sampler", "diffuse3Sampler",
  320. "bump1Sampler", "bump2Sampler", "bump3Sampler",
  321. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  322. ],
  323. join, fallbacks, this.onCompiled, this.onError);
  324. }
  325. if (!this._effect.isReady()) {
  326. return false;
  327. }
  328. this._renderId = scene.getRenderId();
  329. this._wasPreviouslyReady = true;
  330. if (mesh) {
  331. if (!mesh._materialDefines) {
  332. mesh._materialDefines = new TerrainMaterialDefines();
  333. }
  334. this._defines.cloneTo(mesh._materialDefines);
  335. }
  336. return true;
  337. }
  338. public bindOnlyWorldMatrix(world: Matrix): void {
  339. this._effect.setMatrix("world", world);
  340. }
  341. public bind(world: Matrix, mesh?: Mesh): void {
  342. var scene = this.getScene();
  343. // Matrices
  344. this.bindOnlyWorldMatrix(world);
  345. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  346. // Bones
  347. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  348. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  349. }
  350. if (scene.getCachedMaterial() !== this) {
  351. // Textures
  352. if (this.mixTexture) {
  353. this._effect.setTexture("textureSampler", this.mixTexture);
  354. this._effect.setFloat2("vTextureInfos", this.mixTexture.coordinatesIndex, this.mixTexture.level);
  355. this._effect.setMatrix("textureMatrix", this.mixTexture.getTextureMatrix());
  356. if (StandardMaterial.DiffuseTextureEnabled) {
  357. if (this.diffuseTexture1) {
  358. this._effect.setTexture("diffuse1Sampler", this.diffuseTexture1);
  359. this._effect.setFloat2("diffuse1Infos", this.diffuseTexture1.uScale, this.diffuseTexture1.vScale);
  360. }
  361. if (this.diffuseTexture2) {
  362. this._effect.setTexture("diffuse2Sampler", this.diffuseTexture2);
  363. this._effect.setFloat2("diffuse2Infos", this.diffuseTexture2.uScale, this.diffuseTexture2.vScale);
  364. }
  365. if (this.diffuseTexture3) {
  366. this._effect.setTexture("diffuse3Sampler", this.diffuseTexture3);
  367. this._effect.setFloat2("diffuse3Infos", this.diffuseTexture3.uScale, this.diffuseTexture3.vScale);
  368. }
  369. }
  370. if (StandardMaterial.BumpTextureEnabled && scene.getEngine().getCaps().standardDerivatives) {
  371. if (this.bumpTexture1) {
  372. this._effect.setTexture("bump1Sampler", this.bumpTexture1);
  373. }
  374. if (this.bumpTexture2) {
  375. this._effect.setTexture("bump2Sampler", this.bumpTexture2);
  376. }
  377. if (this.bumpTexture3) {
  378. this._effect.setTexture("bump3Sampler", this.bumpTexture3);
  379. }
  380. }
  381. }
  382. // Clip plane
  383. if (scene.clipPlane) {
  384. var clipPlane = scene.clipPlane;
  385. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  386. }
  387. // Point size
  388. if (this.pointsCloud) {
  389. this._effect.setFloat("pointSize", this.pointSize);
  390. }
  391. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  392. }
  393. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  394. if (this._defines.SPECULARTERM) {
  395. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  396. }
  397. if (scene.lightsEnabled && !this.disableLighting) {
  398. var lightIndex = 0;
  399. for (var index = 0; index < scene.lights.length; index++) {
  400. var light = scene.lights[index];
  401. if (!light.isEnabled()) {
  402. continue;
  403. }
  404. if (!light.canAffectMesh(mesh)) {
  405. continue;
  406. }
  407. if (light instanceof PointLight) {
  408. // Point Light
  409. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  410. } else if (light instanceof DirectionalLight) {
  411. // Directional Light
  412. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  413. } else if (light instanceof SpotLight) {
  414. // Spot Light
  415. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  416. } else if (light instanceof HemisphericLight) {
  417. // Hemispheric Light
  418. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  419. }
  420. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  421. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  422. if (this._defines.SPECULARTERM) {
  423. light.specular.scaleToRef(light.intensity, this._scaledSpecular);
  424. this._effect.setColor3("vLightSpecular" + lightIndex, this._scaledSpecular);
  425. }
  426. // Shadows
  427. if (scene.shadowsEnabled) {
  428. var shadowGenerator = light.getShadowGenerator();
  429. if (mesh.receiveShadows && shadowGenerator) {
  430. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  431. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  432. this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  433. }
  434. }
  435. lightIndex++;
  436. if (lightIndex === maxSimultaneousLights)
  437. break;
  438. }
  439. }
  440. // View
  441. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  442. this._effect.setMatrix("view", scene.getViewMatrix());
  443. }
  444. // Fog
  445. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  446. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  447. this._effect.setColor3("vFogColor", scene.fogColor);
  448. }
  449. super.bind(world, mesh);
  450. }
  451. public getAnimatables(): IAnimatable[] {
  452. var results = [];
  453. if (this.mixTexture && this.mixTexture.animations && this.mixTexture.animations.length > 0) {
  454. results.push(this.mixTexture);
  455. }
  456. return results;
  457. }
  458. public dispose(forceDisposeEffect?: boolean): void {
  459. if (this.mixTexture) {
  460. this.mixTexture.dispose();
  461. }
  462. super.dispose(forceDisposeEffect);
  463. }
  464. public clone(name: string): TerrainMaterial {
  465. var newMaterial = new TerrainMaterial(name, this.getScene());
  466. // Base material
  467. this.copyTo(newMaterial);
  468. // Simple material
  469. if (this.mixTexture && this.mixTexture.clone) {
  470. newMaterial.mixTexture = this.mixTexture.clone();
  471. }
  472. newMaterial.diffuseColor = this.diffuseColor.clone();
  473. return newMaterial;
  474. }
  475. }
  476. }