babylon.customMaterial.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return function (d, b) {
  7. extendStatics(d, b);
  8. function __() { this.constructor = d; }
  9. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  10. };
  11. })();
  12. var BABYLON;
  13. (function (BABYLON) {
  14. var CustomShaderHelper = (function () {
  15. function CustomShaderHelper() {
  16. }
  17. return CustomShaderHelper;
  18. }());
  19. BABYLON.CustomShaderHelper = CustomShaderHelper;
  20. var CustomMaterial = (function (_super) {
  21. __extends(CustomMaterial, _super);
  22. function CustomMaterial(name, builder, scene) {
  23. var _this = _super.call(this, name, scene) || this;
  24. _this._mainPart = 'void main(void) {';
  25. _this._diffusePart = 'vec3 diffuseColor=vDiffuseColor.rgb;';
  26. _this._vertexPositionPart = 'gl_Position=viewProjection*finalWorld*vec4(position,1.0);';
  27. _this.builder = builder;
  28. return _this;
  29. }
  30. CustomMaterial.prototype.isReady = function (mesh, useInstances) {
  31. if (this.isFrozen) {
  32. if (this._wasPreviouslyReady) {
  33. return true;
  34. }
  35. }
  36. var scene = this.getScene();
  37. var engine = scene.getEngine();
  38. var needUVs = false;
  39. var needNormals = false;
  40. this._defines.reset();
  41. // Lights
  42. if (scene.lightsEnabled && !this.disableLighting) {
  43. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, this.maxSimultaneousLights);
  44. }
  45. if (!this.checkReadyOnEveryCall) {
  46. if (this._renderId === scene.getRenderId()) {
  47. if (this._checkCache(scene, mesh, useInstances)) {
  48. return true;
  49. }
  50. }
  51. }
  52. // Textures
  53. if (scene.texturesEnabled) {
  54. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  55. if (!this.diffuseTexture.isReady()) {
  56. return false;
  57. }
  58. else {
  59. needUVs = true;
  60. this._defines.DIFFUSE = true;
  61. }
  62. }
  63. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  64. if (!this.ambientTexture.isReady()) {
  65. return false;
  66. }
  67. else {
  68. needUVs = true;
  69. this._defines.AMBIENT = true;
  70. }
  71. }
  72. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  73. if (!this.opacityTexture.isReady()) {
  74. return false;
  75. }
  76. else {
  77. needUVs = true;
  78. this._defines.OPACITY = true;
  79. if (this.opacityTexture.getAlphaFromRGB) {
  80. this._defines.OPACITYRGB = true;
  81. }
  82. }
  83. }
  84. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  85. if (!this.reflectionTexture.isReady()) {
  86. return false;
  87. }
  88. else {
  89. needNormals = true;
  90. this._defines.REFLECTION = true;
  91. if (this.roughness > 0) {
  92. this._defines.ROUGHNESS = true;
  93. }
  94. if (this.useReflectionOverAlpha) {
  95. this._defines.REFLECTIONOVERALPHA = true;
  96. }
  97. if (this.reflectionTexture.coordinatesMode === BABYLON.Texture.INVCUBIC_MODE) {
  98. this._defines.INVERTCUBICMAP = true;
  99. }
  100. this._defines.REFLECTIONMAP_3D = this.reflectionTexture.isCube;
  101. switch (this.reflectionTexture.coordinatesMode) {
  102. case BABYLON.Texture.CUBIC_MODE:
  103. case BABYLON.Texture.INVCUBIC_MODE:
  104. this._defines.REFLECTIONMAP_CUBIC = true;
  105. break;
  106. case BABYLON.Texture.EXPLICIT_MODE:
  107. this._defines.REFLECTIONMAP_EXPLICIT = true;
  108. break;
  109. case BABYLON.Texture.PLANAR_MODE:
  110. this._defines.REFLECTIONMAP_PLANAR = true;
  111. break;
  112. case BABYLON.Texture.PROJECTION_MODE:
  113. this._defines.REFLECTIONMAP_PROJECTION = true;
  114. break;
  115. case BABYLON.Texture.SKYBOX_MODE:
  116. this._defines.REFLECTIONMAP_SKYBOX = true;
  117. break;
  118. case BABYLON.Texture.SPHERICAL_MODE:
  119. this._defines.REFLECTIONMAP_SPHERICAL = true;
  120. break;
  121. case BABYLON.Texture.EQUIRECTANGULAR_MODE:
  122. this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  123. break;
  124. case BABYLON.Texture.FIXED_EQUIRECTANGULAR_MODE:
  125. this._defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = true;
  126. break;
  127. }
  128. }
  129. }
  130. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  131. if (!this.emissiveTexture.isReady()) {
  132. return false;
  133. }
  134. else {
  135. needUVs = true;
  136. this._defines.EMISSIVE = true;
  137. }
  138. }
  139. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapTextureEnabled) {
  140. if (!this.lightmapTexture.isReady()) {
  141. return false;
  142. }
  143. else {
  144. needUVs = true;
  145. this._defines.LIGHTMAP = true;
  146. this._defines.USELIGHTMAPASSHADOWMAP = this.useLightmapAsShadowmap;
  147. }
  148. }
  149. if (this.specularTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  150. if (!this.specularTexture.isReady()) {
  151. return false;
  152. }
  153. else {
  154. needUVs = true;
  155. this._defines.SPECULAR = true;
  156. this._defines.GLOSSINESS = this.useGlossinessFromSpecularMapAlpha;
  157. }
  158. }
  159. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled) {
  160. if (!this.bumpTexture.isReady()) {
  161. return false;
  162. }
  163. else {
  164. needUVs = true;
  165. this._defines.BUMP = true;
  166. if (this.useParallax) {
  167. this._defines.PARALLAX = true;
  168. if (this.useParallaxOcclusion) {
  169. this._defines.PARALLAXOCCLUSION = true;
  170. }
  171. }
  172. if (this.invertNormalMapX) {
  173. this._defines.INVERTNORMALMAPX = true;
  174. }
  175. if (this.invertNormalMapY) {
  176. this._defines.INVERTNORMALMAPY = true;
  177. }
  178. if (scene._mirroredCameraPosition) {
  179. this._defines.INVERTNORMALMAPX = !this._defines.INVERTNORMALMAPX;
  180. this._defines.INVERTNORMALMAPY = !this._defines.INVERTNORMALMAPY;
  181. }
  182. }
  183. }
  184. if (this.refractionTexture && BABYLON.StandardMaterial.RefractionTextureEnabled) {
  185. if (!this.refractionTexture.isReady()) {
  186. return false;
  187. }
  188. else {
  189. needUVs = true;
  190. this._defines.REFRACTION = true;
  191. this._defines.REFRACTIONMAP_3D = this.refractionTexture.isCube;
  192. }
  193. }
  194. if (this.cameraColorGradingTexture && BABYLON.StandardMaterial.ColorGradingTextureEnabled) {
  195. if (!this.cameraColorGradingTexture.isReady()) {
  196. return false;
  197. }
  198. else {
  199. this._defines.CAMERACOLORGRADING = true;
  200. }
  201. }
  202. }
  203. // Effect
  204. if (scene.clipPlane) {
  205. this._defines.CLIPPLANE = true;
  206. }
  207. if (engine.getAlphaTesting()) {
  208. this._defines.ALPHATEST = true;
  209. }
  210. if (this._shouldUseAlphaFromDiffuseTexture()) {
  211. this._defines.ALPHAFROMDIFFUSE = true;
  212. }
  213. if (this.useEmissiveAsIllumination) {
  214. this._defines.EMISSIVEASILLUMINATION = true;
  215. }
  216. if (this.linkEmissiveWithDiffuse) {
  217. this._defines.LINKEMISSIVEWITHDIFFUSE = true;
  218. }
  219. if (this.useLogarithmicDepth) {
  220. this._defines.LOGARITHMICDEPTH = true;
  221. }
  222. if (this.cameraColorCurves) {
  223. this._defines.CAMERACOLORCURVES = true;
  224. }
  225. // Point size
  226. if (this.pointsCloud || scene.forcePointsCloud) {
  227. this._defines.POINTSIZE = true;
  228. }
  229. // Fog
  230. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  231. this._defines.FOG = true;
  232. }
  233. if (BABYLON.StandardMaterial.FresnelEnabled) {
  234. // Fresnel
  235. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled ||
  236. this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  237. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled ||
  238. this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled ||
  239. this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  240. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  241. this._defines.DIFFUSEFRESNEL = true;
  242. }
  243. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  244. this._defines.OPACITYFRESNEL = true;
  245. }
  246. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  247. this._defines.REFLECTIONFRESNEL = true;
  248. if (this.useReflectionFresnelFromSpecular) {
  249. this._defines.REFLECTIONFRESNELFROMSPECULAR = true;
  250. }
  251. }
  252. if (this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled) {
  253. this._defines.REFRACTIONFRESNEL = true;
  254. }
  255. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  256. this._defines.EMISSIVEFRESNEL = true;
  257. }
  258. needNormals = true;
  259. this._defines.FRESNEL = true;
  260. }
  261. }
  262. if (this._defines.SPECULARTERM && this.useSpecularOverAlpha) {
  263. this._defines.SPECULAROVERALPHA = true;
  264. }
  265. // Attribs
  266. if (mesh) {
  267. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  268. this._defines.NORMAL = true;
  269. }
  270. if (needUVs) {
  271. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  272. this._defines.UV1 = true;
  273. }
  274. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  275. this._defines.UV2 = true;
  276. }
  277. }
  278. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  279. this._defines.VERTEXCOLOR = true;
  280. if (mesh.hasVertexAlpha) {
  281. this._defines.VERTEXALPHA = true;
  282. }
  283. }
  284. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  285. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  286. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  287. }
  288. // Instances
  289. if (useInstances) {
  290. this._defines.INSTANCES = true;
  291. }
  292. }
  293. // Get correct effect
  294. if (!this._defines.isEqual(this._cachedDefines)) {
  295. this._defines.cloneTo(this._cachedDefines);
  296. scene.resetCachedMaterial();
  297. // Fallbacks
  298. var fallbacks = new BABYLON.EffectFallbacks();
  299. if (this._defines.REFLECTION) {
  300. fallbacks.addFallback(0, "REFLECTION");
  301. }
  302. if (this._defines.SPECULAR) {
  303. fallbacks.addFallback(0, "SPECULAR");
  304. }
  305. if (this._defines.BUMP) {
  306. fallbacks.addFallback(0, "BUMP");
  307. }
  308. if (this._defines.PARALLAX) {
  309. fallbacks.addFallback(1, "PARALLAX");
  310. }
  311. if (this._defines.PARALLAXOCCLUSION) {
  312. fallbacks.addFallback(0, "PARALLAXOCCLUSION");
  313. }
  314. if (this._defines.SPECULAROVERALPHA) {
  315. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  316. }
  317. if (this._defines.FOG) {
  318. fallbacks.addFallback(1, "FOG");
  319. }
  320. if (this._defines.POINTSIZE) {
  321. fallbacks.addFallback(0, "POINTSIZE");
  322. }
  323. if (this._defines.LOGARITHMICDEPTH) {
  324. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  325. }
  326. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
  327. if (this._defines.SPECULARTERM) {
  328. fallbacks.addFallback(0, "SPECULARTERM");
  329. }
  330. if (this._defines.DIFFUSEFRESNEL) {
  331. fallbacks.addFallback(1, "DIFFUSEFRESNEL");
  332. }
  333. if (this._defines.OPACITYFRESNEL) {
  334. fallbacks.addFallback(2, "OPACITYFRESNEL");
  335. }
  336. if (this._defines.REFLECTIONFRESNEL) {
  337. fallbacks.addFallback(3, "REFLECTIONFRESNEL");
  338. }
  339. if (this._defines.EMISSIVEFRESNEL) {
  340. fallbacks.addFallback(4, "EMISSIVEFRESNEL");
  341. }
  342. if (this._defines.FRESNEL) {
  343. fallbacks.addFallback(4, "FRESNEL");
  344. }
  345. //Attributes
  346. var attribs = [BABYLON.VertexBuffer.PositionKind];
  347. if (this._defines.NORMAL) {
  348. attribs.push(BABYLON.VertexBuffer.NormalKind);
  349. }
  350. if (this._defines.UV1) {
  351. attribs.push(BABYLON.VertexBuffer.UVKind);
  352. }
  353. if (this._defines.UV2) {
  354. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  355. }
  356. if (this._defines.VERTEXCOLOR) {
  357. attribs.push(BABYLON.VertexBuffer.ColorKind);
  358. }
  359. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  360. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  361. var shaderName = "default";
  362. if (this.builder) {
  363. shaderName = this.builder(new CustomShaderHelper(), shaderName, this._mainPart, this._diffusePart, this._vertexPositionPart);
  364. }
  365. var join = this._defines.toString();
  366. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vDiffuseColor", "vSpecularColor", "vEmissiveColor",
  367. "vFogInfos", "vFogColor", "pointSize",
  368. "vDiffuseInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vSpecularInfos", "vBumpInfos", "vLightmapInfos", "vRefractionInfos",
  369. "mBones",
  370. "vClipPlane", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix", "lightmapMatrix", "refractionMatrix",
  371. "depthValues",
  372. "diffuseLeftColor", "diffuseRightColor", "opacityParts", "reflectionLeftColor", "reflectionRightColor", "emissiveLeftColor", "emissiveRightColor", "refractionLeftColor", "refractionRightColor",
  373. "logarithmicDepthConstant"
  374. ];
  375. var samplers = ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler", "lightmapSampler", "refractionCubeSampler", "refraction2DSampler"];
  376. if (this._defines.CAMERACOLORCURVES) {
  377. BABYLON.ColorCurves.PrepareUniforms(uniforms);
  378. }
  379. if (this._defines.CAMERACOLORGRADING) {
  380. BABYLON.ColorGradingTexture.PrepareUniformsAndSamplers(uniforms, samplers);
  381. }
  382. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, this._defines, this.maxSimultaneousLights);
  383. this._effect = scene.getEngine().createEffect(shaderName, attribs, uniforms, samplers, join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: this.maxSimultaneousLights - 1 });
  384. }
  385. if (!this._effect.isReady()) {
  386. return false;
  387. }
  388. this._renderId = scene.getRenderId();
  389. this._wasPreviouslyReady = true;
  390. if (mesh) {
  391. if (!mesh._materialDefines) {
  392. mesh._materialDefines = new BABYLON.StandardMaterialDefines();
  393. }
  394. this._defines.cloneTo(mesh._materialDefines);
  395. }
  396. return true;
  397. };
  398. return CustomMaterial;
  399. }(BABYLON.StandardMaterial));
  400. BABYLON.CustomMaterial = CustomMaterial;
  401. })(BABYLON || (BABYLON = {}));
  402. //# sourceMappingURL=babylon.customMaterial.js.map