babylon.simpleMaterial.ts 19 KB

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