babylon.normalMaterial.ts 21 KB

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