babylon.standardMaterial.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. module BABYLON {
  2. var maxSimultaneousLights = 4;
  3. export class StandardMaterial extends Material {
  4. public diffuseTexture: Texture;
  5. public ambientTexture: Texture;
  6. public opacityTexture: Texture;
  7. public reflectionTexture: Texture;
  8. public emissiveTexture: Texture;
  9. public specularTexture: Texture;
  10. public bumpTexture: Texture;
  11. public ambientColor = new BABYLON.Color3(0, 0, 0);
  12. public diffuseColor = new BABYLON.Color3(1, 1, 1);
  13. public specularColor = new BABYLON.Color3(1, 1, 1);
  14. public specularPower = 64;
  15. public emissiveColor = new BABYLON.Color3(0, 0, 0);
  16. public useAlphaFromDiffuseTexture = false;
  17. private _cachedDefines = null;
  18. private _renderTargets = new BABYLON.SmartArray(16);
  19. private _worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  20. private _lightMatrix = BABYLON.Matrix.Zero();
  21. private _globalAmbientColor = new BABYLON.Color3(0, 0, 0);
  22. private _baseColor = new BABYLON.Color3();
  23. private _scaledDiffuse = new BABYLON.Color3();
  24. private _scaledSpecular = new BABYLON.Color3();
  25. private _renderId: number;
  26. constructor(name: string, scene: Scene) {
  27. super(name, scene);
  28. this.getRenderTargetTextures = (): SmartArray => {
  29. this._renderTargets.reset();
  30. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  31. this._renderTargets.push(this.reflectionTexture);
  32. }
  33. return this._renderTargets;
  34. }
  35. }
  36. public needAlphaBlending(): boolean {
  37. return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromDiffuseTexture();
  38. }
  39. public needAlphaTesting(): boolean {
  40. return this.diffuseTexture != null && this.diffuseTexture.hasAlpha;
  41. }
  42. private _shouldUseAlphaFromDiffuseTexture(): boolean {
  43. return this.diffuseTexture != null && this.diffuseTexture.hasAlpha && this.useAlphaFromDiffuseTexture;
  44. }
  45. // Methods
  46. public isReady(mesh?: Mesh): boolean {
  47. if (this.checkReadyOnlyOnce) {
  48. if (this._wasPreviouslyReady) {
  49. return true;
  50. }
  51. }
  52. var scene = this.getScene();
  53. if (!this.checkReadyOnEveryCall) {
  54. if (this._renderId === scene.getRenderId()) {
  55. return true;
  56. }
  57. }
  58. var engine = scene.getEngine();
  59. var defines = [];
  60. var optionalDefines = new Array<string>();
  61. // Textures
  62. if (scene.texturesEnabled) {
  63. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  64. if (!this.diffuseTexture.isReady()) {
  65. return false;
  66. } else {
  67. defines.push("#define DIFFUSE");
  68. }
  69. }
  70. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  71. if (!this.ambientTexture.isReady()) {
  72. return false;
  73. } else {
  74. defines.push("#define AMBIENT");
  75. }
  76. }
  77. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  78. if (!this.opacityTexture.isReady()) {
  79. return false;
  80. } else {
  81. defines.push("#define OPACITY");
  82. }
  83. }
  84. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  85. if (!this.reflectionTexture.isReady()) {
  86. return false;
  87. } else {
  88. defines.push("#define REFLECTION");
  89. }
  90. }
  91. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  92. if (!this.emissiveTexture.isReady()) {
  93. return false;
  94. } else {
  95. defines.push("#define EMISSIVE");
  96. }
  97. }
  98. if (this.specularTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  99. if (!this.specularTexture.isReady()) {
  100. return false;
  101. } else {
  102. defines.push("#define SPECULAR");
  103. optionalDefines.push(defines[defines.length - 1]);
  104. }
  105. }
  106. }
  107. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled) {
  108. if (!this.bumpTexture.isReady()) {
  109. return false;
  110. } else {
  111. defines.push("#define BUMP");
  112. optionalDefines.push(defines[defines.length - 1]);
  113. }
  114. }
  115. // Effect
  116. if (scene.clipPlane) {
  117. defines.push("#define CLIPPLANE");
  118. }
  119. if (engine.getAlphaTesting()) {
  120. defines.push("#define ALPHATEST");
  121. }
  122. if (this._shouldUseAlphaFromDiffuseTexture()) {
  123. defines.push("#define ALPHAFROMDIFFUSE");
  124. }
  125. // Fog
  126. if (scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  127. defines.push("#define FOG");
  128. optionalDefines.push(defines[defines.length - 1]);
  129. }
  130. var shadowsActivated = false;
  131. var lightIndex = 0;
  132. if (scene.lightsEnabled) {
  133. for (var index = 0; index < scene.lights.length; index++) {
  134. var light = scene.lights[index];
  135. if (!light.isEnabled()) {
  136. continue;
  137. }
  138. if (mesh && light.excludedMeshes.indexOf(mesh) !== -1) {
  139. continue;
  140. }
  141. defines.push("#define LIGHT" + lightIndex);
  142. if (lightIndex > 0) {
  143. optionalDefines.push(defines[defines.length - 1]);
  144. }
  145. var type;
  146. if (light instanceof BABYLON.SpotLight) {
  147. type = "#define SPOTLIGHT" + lightIndex;
  148. } else if (light instanceof BABYLON.HemisphericLight) {
  149. type = "#define HEMILIGHT" + lightIndex;
  150. } else {
  151. type = "#define POINTDIRLIGHT" + lightIndex;
  152. }
  153. defines.push(type);
  154. if (lightIndex > 0) {
  155. optionalDefines.push(defines[defines.length - 1]);
  156. }
  157. // Shadows
  158. var shadowGenerator = light.getShadowGenerator();
  159. if (mesh && mesh.receiveShadows && shadowGenerator) {
  160. defines.push("#define SHADOW" + lightIndex);
  161. if (lightIndex > 0) {
  162. optionalDefines.push(defines[defines.length - 1]);
  163. }
  164. if (!shadowsActivated) {
  165. defines.push("#define SHADOWS");
  166. shadowsActivated = true;
  167. }
  168. if (shadowGenerator.useVarianceShadowMap) {
  169. defines.push("#define SHADOWVSM" + lightIndex);
  170. if (lightIndex > 0) {
  171. optionalDefines.push(defines[defines.length - 1]);
  172. }
  173. }
  174. }
  175. lightIndex++;
  176. if (lightIndex == maxSimultaneousLights)
  177. break;
  178. }
  179. }
  180. var attribs = [BABYLON.VertexBuffer.PositionKind, BABYLON.VertexBuffer.NormalKind];
  181. if (mesh) {
  182. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  183. attribs.push(BABYLON.VertexBuffer.UVKind);
  184. defines.push("#define UV1");
  185. }
  186. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  187. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  188. defines.push("#define UV2");
  189. }
  190. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  191. attribs.push(BABYLON.VertexBuffer.ColorKind);
  192. defines.push("#define VERTEXCOLOR");
  193. }
  194. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  195. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  196. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  197. defines.push("#define BONES");
  198. defines.push("#define BonesPerMesh " + mesh.skeleton.bones.length);
  199. defines.push("#define BONES4");
  200. optionalDefines.push(defines[defines.length - 1]);
  201. }
  202. }
  203. // Get correct effect
  204. var join = defines.join("\n");
  205. if (this._cachedDefines != join) {
  206. this._cachedDefines = join;
  207. // Legacy browser patch
  208. var shaderName = "default";
  209. if (!scene.getEngine().getCaps().standardDerivatives) {
  210. shaderName = "legacydefault";
  211. }
  212. this._effect = scene.getEngine().createEffect(shaderName,
  213. attribs,
  214. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vDiffuseColor", "vSpecularColor", "vEmissiveColor",
  215. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  216. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  217. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  218. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  219. "vFogInfos", "vFogColor",
  220. "vDiffuseInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vSpecularInfos", "vBumpInfos",
  221. "mBones",
  222. "vClipPlane", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix",
  223. "darkness0", "darkness1", "darkness2", "darkness3"],
  224. ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler",
  225. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  226. ],
  227. join, optionalDefines, this.onCompiled, this.onError);
  228. }
  229. if (!this._effect.isReady()) {
  230. return false;
  231. }
  232. this._renderId = scene.getRenderId();
  233. this._wasPreviouslyReady = true;
  234. return true;
  235. }
  236. public unbind(): void {
  237. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  238. this._effect.setTexture("reflection2DSampler", null);
  239. }
  240. }
  241. public bind(world: Matrix, mesh: Mesh): void {
  242. var scene = this.getScene();
  243. this._baseColor.copyFrom(this.diffuseColor);
  244. // Matrices
  245. this._effect.setMatrix("world", world);
  246. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  247. // Bones
  248. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  249. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  250. }
  251. // Textures
  252. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  253. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  254. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  255. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture._computeTextureMatrix());
  256. this._baseColor.copyFromFloats(1, 1, 1);
  257. }
  258. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  259. this._effect.setTexture("ambientSampler", this.ambientTexture);
  260. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  261. this._effect.setMatrix("ambientMatrix", this.ambientTexture._computeTextureMatrix());
  262. }
  263. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  264. this._effect.setTexture("opacitySampler", this.opacityTexture);
  265. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  266. this._effect.setMatrix("opacityMatrix", this.opacityTexture._computeTextureMatrix());
  267. }
  268. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  269. if (this.reflectionTexture.isCube) {
  270. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  271. } else {
  272. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  273. }
  274. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture._computeReflectionTextureMatrix());
  275. this._effect.setFloat3("vReflectionInfos", this.reflectionTexture.coordinatesMode, this.reflectionTexture.level, this.reflectionTexture.isCube ? 1 : 0);
  276. }
  277. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  278. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  279. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  280. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture._computeTextureMatrix());
  281. }
  282. if (this.specularTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  283. this._effect.setTexture("specularSampler", this.specularTexture);
  284. this._effect.setFloat2("vSpecularInfos", this.specularTexture.coordinatesIndex, this.specularTexture.level);
  285. this._effect.setMatrix("specularMatrix", this.specularTexture._computeTextureMatrix());
  286. }
  287. if (this.bumpTexture && scene.getEngine().getCaps().standardDerivatives && BABYLON.StandardMaterial.BumpTextureEnabled) {
  288. this._effect.setTexture("bumpSampler", this.bumpTexture);
  289. this._effect.setFloat2("vBumpInfos", this.bumpTexture.coordinatesIndex, this.bumpTexture.level);
  290. this._effect.setMatrix("bumpMatrix", this.bumpTexture._computeTextureMatrix());
  291. }
  292. // Colors
  293. scene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  294. this._effect.setVector3("vEyePosition", scene.activeCamera.position);
  295. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  296. this._effect.setColor4("vDiffuseColor", this._baseColor, this.alpha * mesh.visibility);
  297. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  298. this._effect.setColor3("vEmissiveColor", this.emissiveColor);
  299. if (scene.lightsEnabled) {
  300. var lightIndex = 0;
  301. for (var index = 0; index < scene.lights.length; index++) {
  302. var light = scene.lights[index];
  303. if (!light.isEnabled()) {
  304. continue;
  305. }
  306. if (mesh && light.excludedMeshes.indexOf(mesh) !== -1) {
  307. continue;
  308. }
  309. if (light instanceof BABYLON.PointLight) {
  310. // Point Light
  311. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  312. } else if (light instanceof BABYLON.DirectionalLight) {
  313. // Directional Light
  314. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  315. } else if (light instanceof BABYLON.SpotLight) {
  316. // Spot Light
  317. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  318. } else if (light instanceof BABYLON.HemisphericLight) {
  319. // Hemispheric Light
  320. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  321. }
  322. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  323. light.specular.scaleToRef(light.intensity, this._scaledSpecular);
  324. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  325. this._effect.setColor3("vLightSpecular" + lightIndex, this._scaledSpecular);
  326. // Shadows
  327. var shadowGenerator = light.getShadowGenerator();
  328. if (mesh.receiveShadows && shadowGenerator) {
  329. world.multiplyToRef(shadowGenerator.getTransformMatrix(), this._lightMatrix);
  330. this._effect.setMatrix("lightMatrix" + lightIndex, this._lightMatrix);
  331. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMap());
  332. this._effect.setFloat("darkness" + lightIndex, shadowGenerator.getDarkness());
  333. }
  334. lightIndex++;
  335. if (lightIndex == maxSimultaneousLights)
  336. break;
  337. }
  338. }
  339. if (scene.clipPlane) {
  340. var clipPlane = scene.clipPlane;
  341. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  342. }
  343. // View
  344. if (scene.fogMode !== BABYLON.Scene.FOGMODE_NONE || this.reflectionTexture) {
  345. this._effect.setMatrix("view", scene.getViewMatrix());
  346. }
  347. // Fog
  348. if (scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  349. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  350. this._effect.setColor3("vFogColor", scene.fogColor);
  351. }
  352. }
  353. public getAnimatables(): IAnimatable[] {
  354. var results = [];
  355. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  356. results.push(this.diffuseTexture);
  357. }
  358. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  359. results.push(this.ambientTexture);
  360. }
  361. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  362. results.push(this.opacityTexture);
  363. }
  364. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  365. results.push(this.reflectionTexture);
  366. }
  367. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  368. results.push(this.emissiveTexture);
  369. }
  370. if (this.specularTexture && this.specularTexture.animations && this.specularTexture.animations.length > 0) {
  371. results.push(this.specularTexture);
  372. }
  373. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  374. results.push(this.bumpTexture);
  375. }
  376. return results;
  377. }
  378. public dispose(forceDisposeEffect?: boolean): void {
  379. if (this.diffuseTexture) {
  380. this.diffuseTexture.dispose();
  381. }
  382. if (this.ambientTexture) {
  383. this.ambientTexture.dispose();
  384. }
  385. if (this.opacityTexture) {
  386. this.opacityTexture.dispose();
  387. }
  388. if (this.reflectionTexture) {
  389. this.reflectionTexture.dispose();
  390. }
  391. if (this.emissiveTexture) {
  392. this.emissiveTexture.dispose();
  393. }
  394. if (this.specularTexture) {
  395. this.specularTexture.dispose();
  396. }
  397. if (this.bumpTexture) {
  398. this.bumpTexture.dispose();
  399. }
  400. super.dispose(forceDisposeEffect);
  401. }
  402. public clone(name: string): StandardMaterial {
  403. var newStandardMaterial = new BABYLON.StandardMaterial(name, this.getScene());
  404. // Base material
  405. newStandardMaterial.checkReadyOnEveryCall = this.checkReadyOnEveryCall;
  406. newStandardMaterial.alpha = this.alpha;
  407. newStandardMaterial.wireframe = this.wireframe;
  408. newStandardMaterial.backFaceCulling = this.backFaceCulling;
  409. // Standard material
  410. if (this.diffuseTexture && this.diffuseTexture.clone) {
  411. newStandardMaterial.diffuseTexture = this.diffuseTexture.clone();
  412. }
  413. if (this.ambientTexture && this.ambientTexture.clone) {
  414. newStandardMaterial.ambientTexture = this.ambientTexture.clone();
  415. }
  416. if (this.opacityTexture && this.opacityTexture.clone) {
  417. newStandardMaterial.opacityTexture = this.opacityTexture.clone();
  418. }
  419. if (this.reflectionTexture && this.reflectionTexture.clone) {
  420. newStandardMaterial.reflectionTexture = this.reflectionTexture.clone();
  421. }
  422. if (this.emissiveTexture && this.emissiveTexture.clone) {
  423. newStandardMaterial.emissiveTexture = this.emissiveTexture.clone();
  424. }
  425. if (this.specularTexture && this.specularTexture.clone) {
  426. newStandardMaterial.specularTexture = this.specularTexture.clone();
  427. }
  428. if (this.bumpTexture && this.bumpTexture.clone) {
  429. newStandardMaterial.bumpTexture = this.bumpTexture.clone();
  430. }
  431. newStandardMaterial.ambientColor = this.ambientColor.clone();
  432. newStandardMaterial.diffuseColor = this.diffuseColor.clone();
  433. newStandardMaterial.specularColor = this.specularColor.clone();
  434. newStandardMaterial.specularPower = this.specularPower;
  435. newStandardMaterial.emissiveColor = this.emissiveColor.clone();
  436. return newStandardMaterial;
  437. }
  438. // Statics
  439. // Flags used to enable or disable a type of texture for all Standard Materials
  440. public static DiffuseTextureEnabled = true;
  441. public static AmbientTextureEnabled = true;
  442. public static OpacityTextureEnabled = true;
  443. public static ReflectionTextureEnabled = true;
  444. public static EmissiveTextureEnabled = true;
  445. public static SpecularTextureEnabled = true;
  446. public static BumpTextureEnabled = true;
  447. }
  448. }