babylon.customMaterial.js 19 KB

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