babylon.customMaterial.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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 CustomShaderStructure = (function () {
  15. function CustomShaderStructure() {
  16. }
  17. return CustomShaderStructure;
  18. }());
  19. BABYLON.CustomShaderStructure = CustomShaderStructure;
  20. var ShaderSpecialParts = (function () {
  21. function ShaderSpecialParts() {
  22. }
  23. return ShaderSpecialParts;
  24. }());
  25. BABYLON.ShaderSpecialParts = ShaderSpecialParts;
  26. var ShaderForVer3_0 = (function (_super) {
  27. __extends(ShaderForVer3_0, _super);
  28. function ShaderForVer3_0() {
  29. var _this = _super.call(this) || this;
  30. _this.VertexStore = "";
  31. _this.FragmentStore = "#include<__decl__defaultFragment>\n\
  32. #[Fragment_Begin]\n\
  33. #extension GL_OES_standard_derivatives : enable\n\
  34. #ifdef LOGARITHMICDEPTH\n\
  35. #extension GL_EXT_frag_depth : enable\n\
  36. #endif\n\
  37. \n\
  38. #define RECIPROCAL_PI2 0.15915494\n\
  39. uniform vec3 vEyePosition;\n\
  40. uniform vec3 vAmbientColor;\n\
  41. \n\
  42. varying vec3 vPositionW;\n\
  43. #ifdef NORMAL\n\
  44. varying vec3 vNormalW_helper;\n\
  45. varying vec3 localNormal;\n\
  46. varying vec3 localPosition;\n\
  47. vec3 vNormalW;\n\
  48. #endif\n\
  49. #ifdef VERTEXCOLOR\n\
  50. varying vec4 vColor;\n\
  51. #endif\n\
  52. \n\
  53. #include<helperFunctions>\n\
  54. \n\
  55. #include<__decl__lightFragment>[0..maxSimultaneousLights]\n\
  56. #include<lightsFragmentFunctions>\n\
  57. #include<shadowsFragmentFunctions>\n\
  58. \n\
  59. #ifdef DIFFUSE\n\
  60. varying vec2 vDiffuseUV;\n\
  61. uniform sampler2D diffuseSampler;\n\
  62. #endif\n\
  63. #ifdef AMBIENT\n\
  64. varying vec2 vAmbientUV;\n\
  65. uniform sampler2D ambientSampler;\n\
  66. #endif\n\
  67. #ifdef OPACITY\n\
  68. varying vec2 vOpacityUV;\n\
  69. uniform sampler2D opacitySampler;\n\
  70. #endif\n\
  71. #ifdef EMISSIVE\n\
  72. varying vec2 vEmissiveUV;\n\
  73. uniform sampler2D emissiveSampler;\n\
  74. #endif\n\
  75. #ifdef LIGHTMAP\n\
  76. varying vec2 vLightmapUV;\n\
  77. uniform sampler2D lightmapSampler;\n\
  78. #endif\n\
  79. #ifdef REFRACTION\n\
  80. #ifdef REFRACTIONMAP_3D\n\
  81. uniform samplerCube refractionCubeSampler;\n\
  82. #else\n\
  83. uniform sampler2D refraction2DSampler;\n\
  84. #endif\n\
  85. #endif\n\
  86. #if defined(SPECULAR) && defined(SPECULARTERM)\n\
  87. varying vec2 vSpecularUV;\n\
  88. uniform sampler2D specularSampler;\n\
  89. #endif\n\
  90. \n\
  91. #include<fresnelFunction>\n\
  92. \n\
  93. #ifdef REFLECTION\n\
  94. #ifdef REFLECTIONMAP_3D\n\
  95. uniform samplerCube reflectionCubeSampler;\n\
  96. #else\n\
  97. uniform sampler2D reflection2DSampler;\n\
  98. #endif\n\
  99. #ifdef REFLECTIONMAP_SKYBOX\n\
  100. varying vec3 vPositionUVW;\n\
  101. #else\n\
  102. #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)\n\
  103. varying vec3 vDirectionW;\n\
  104. #endif\n\
  105. #endif\n\
  106. #include<reflectionFunction>\n\
  107. #endif\n\
  108. #include<imageProcessingDeclaration>\n\
  109. #include<imageProcessingFunctions>\n\
  110. \n\
  111. #include<bumpFragmentFunctions>\n\
  112. #include<clipPlaneFragmentDeclaration>\n\
  113. #include<logDepthDeclaration>\n\
  114. #include<fogFragmentDeclaration>\n\
  115. \n\
  116. #[Fragment_Definitions]\n\
  117. \n\
  118. void main(void) {\n\
  119. \n\
  120. vNormalW = vNormalW_helper;\n\
  121. #[Fragment_MainBegin]\n\
  122. \n\
  123. #include<clipPlaneFragment>\n\
  124. vec3 viewDirectionW=normalize(vEyePosition-vPositionW);\n\
  125. \n\
  126. vec4 baseColor=vec4(1.,1.,1.,1.);\n\
  127. vec3 diffuseColor=vDiffuseColor.rgb;\n\
  128. #[Fragment_Custom_Diffuse]\n\
  129. \n\
  130. float alpha=vDiffuseColor.a;\n\
  131. #[Fragment_Custom_Alpha]\n\
  132. \n\
  133. #ifdef NORMAL\n\
  134. vec3 normalW=normalize(vNormalW);\n\
  135. #else\n\
  136. vec3 normalW=vec3(1.0,1.0,1.0);\n\
  137. #endif\n\
  138. #include<bumpFragment>\n\
  139. #ifdef TWOSIDEDLIGHTING\n\
  140. normalW=gl_FrontFacing ? normalW : -normalW;\n\
  141. #endif\n\
  142. #ifdef DIFFUSE\n\
  143. baseColor=texture2D(diffuseSampler,vDiffuseUV+uvOffset);\n\
  144. #ifdef ALPHATEST\n\
  145. if (baseColor.a<0.4)\n\
  146. discard;\n\
  147. #endif\n\
  148. #ifdef ALPHAFROMDIFFUSE\n\
  149. alpha*=baseColor.a;\n\
  150. #endif\n\
  151. baseColor.rgb*=vDiffuseInfos.y;\n\
  152. #endif\n\
  153. #ifdef VERTEXCOLOR\n\
  154. baseColor.rgb*=vColor.rgb;\n\
  155. #endif\n\
  156. \n\
  157. vec3 baseAmbientColor=vec3(1.,1.,1.);\n\
  158. #ifdef AMBIENT\n\
  159. baseAmbientColor=texture2D(ambientSampler,vAmbientUV+uvOffset).rgb*vAmbientInfos.y;\n\
  160. #endif\n\
  161. \n\
  162. #ifdef SPECULARTERM\n\
  163. float glossiness=vSpecularColor.a;\n\
  164. vec3 specularColor=vSpecularColor.rgb;\n\
  165. #ifdef SPECULAR\n\
  166. vec4 specularMapColor=texture2D(specularSampler,vSpecularUV+uvOffset);\n\
  167. specularColor=specularMapColor.rgb;\n\
  168. #ifdef GLOSSINESS\n\
  169. glossiness=glossiness*specularMapColor.a;\n\
  170. #endif\n\
  171. #endif\n\
  172. #else\n\
  173. float glossiness=0.;\n\
  174. #endif\n\
  175. \n\
  176. vec3 diffuseBase=vec3(0.,0.,0.);\n\
  177. lightingInfo info;\n\
  178. #ifdef SPECULARTERM\n\
  179. vec3 specularBase=vec3(0.,0.,0.);\n\
  180. #endif\n\
  181. float shadow=1.;\n\
  182. #ifdef LIGHTMAP\n\
  183. vec3 lightmapColor=texture2D(lightmapSampler,vLightmapUV+uvOffset).rgb*vLightmapInfos.y;\n\
  184. #endif\n\
  185. #include<lightFragment>[0..maxSimultaneousLights]\n\
  186. \n\
  187. vec3 refractionColor=vec3(0.,0.,0.);\n\
  188. #ifdef REFRACTION\n\
  189. vec3 refractionVector=normalize(refract(-viewDirectionW,normalW,vRefractionInfos.y));\n\
  190. #ifdef REFRACTIONMAP_3D\n\
  191. refractionVector.y=refractionVector.y*vRefractionInfos.w;\n\
  192. if (dot(refractionVector,viewDirectionW)<1.0)\n\
  193. {\n\
  194. refractionColor=textureCube(refractionCubeSampler,refractionVector).rgb*vRefractionInfos.x;\n\
  195. }\n\
  196. #else\n\
  197. vec3 vRefractionUVW=vec3(refractionMatrix*(view*vec4(vPositionW+refractionVector*vRefractionInfos.z,1.0)));\n\
  198. vec2 refractionCoords=vRefractionUVW.xy/vRefractionUVW.z;\n\
  199. refractionCoords.y=1.0-refractionCoords.y;\n\
  200. refractionColor=texture2D(refraction2DSampler,refractionCoords).rgb*vRefractionInfos.x;\n\
  201. #endif\n\
  202. #endif\n\
  203. \n\
  204. vec3 reflectionColor=vec3(0.,0.,0.);\n\
  205. #ifdef REFLECTION\n\
  206. vec3 vReflectionUVW=computeReflectionCoords(vec4(vPositionW,1.0),normalW);\n\
  207. #ifdef REFLECTIONMAP_3D\n\
  208. #ifdef ROUGHNESS\n\
  209. float bias=vReflectionInfos.y;\n\
  210. #ifdef SPECULARTERM\n\
  211. #ifdef SPECULAR\n\
  212. #ifdef GLOSSINESS\n\
  213. bias*=(1.0-specularMapColor.a);\n\
  214. #endif\n\
  215. #endif\n\
  216. #endif\n\
  217. reflectionColor=textureCube(reflectionCubeSampler,vReflectionUVW,bias).rgb*vReflectionInfos.x;\n\
  218. #else\n\
  219. reflectionColor=textureCube(reflectionCubeSampler,vReflectionUVW).rgb*vReflectionInfos.x;\n\
  220. #endif\n\
  221. #else\n\
  222. vec2 coords=vReflectionUVW.xy;\n\
  223. #ifdef REFLECTIONMAP_PROJECTION\n\
  224. coords/=vReflectionUVW.z;\n\
  225. #endif\n\
  226. coords.y=1.0-coords.y;\n\
  227. reflectionColor=texture2D(reflection2DSampler,coords).rgb*vReflectionInfos.x;\n\
  228. #endif\n\
  229. #ifdef REFLECTIONFRESNEL\n\
  230. float reflectionFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,reflectionRightColor.a,reflectionLeftColor.a);\n\
  231. #ifdef REFLECTIONFRESNELFROMSPECULAR\n\
  232. #ifdef SPECULARTERM\n\
  233. reflectionColor*=specularColor.rgb*(1.0-reflectionFresnelTerm)+reflectionFresnelTerm*reflectionRightColor.rgb;\n\
  234. #else\n\
  235. reflectionColor*=reflectionLeftColor.rgb*(1.0-reflectionFresnelTerm)+reflectionFresnelTerm*reflectionRightColor.rgb;\n\
  236. #endif\n\
  237. #else\n\
  238. reflectionColor*=reflectionLeftColor.rgb*(1.0-reflectionFresnelTerm)+reflectionFresnelTerm*reflectionRightColor.rgb;\n\
  239. #endif\n\
  240. #endif\n\
  241. #endif\n\
  242. #ifdef REFRACTIONFRESNEL\n\
  243. float refractionFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,refractionRightColor.a,refractionLeftColor.a);\n\
  244. refractionColor*=refractionLeftColor.rgb*(1.0-refractionFresnelTerm)+refractionFresnelTerm*refractionRightColor.rgb;\n\
  245. #endif\n\
  246. #ifdef OPACITY\n\
  247. vec4 opacityMap=texture2D(opacitySampler,vOpacityUV+uvOffset);\n\
  248. #ifdef OPACITYRGB\n\
  249. opacityMap.rgb=opacityMap.rgb*vec3(0.3,0.59,0.11);\n\
  250. alpha*=(opacityMap.x+opacityMap.y+opacityMap.z)* vOpacityInfos.y;\n\
  251. #else\n\
  252. alpha*=opacityMap.a*vOpacityInfos.y;\n\
  253. #endif\n\
  254. #endif\n\
  255. #ifdef VERTEXALPHA\n\
  256. alpha*=vColor.a;\n\
  257. #endif\n\
  258. #ifdef OPACITYFRESNEL\n\
  259. float opacityFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,opacityParts.z,opacityParts.w);\n\
  260. alpha+=opacityParts.x*(1.0-opacityFresnelTerm)+opacityFresnelTerm*opacityParts.y;\n\
  261. #endif\n\
  262. \n\
  263. vec3 emissiveColor=vEmissiveColor;\n\
  264. #ifdef EMISSIVE\n\
  265. emissiveColor+=texture2D(emissiveSampler,vEmissiveUV+uvOffset).rgb*vEmissiveInfos.y;\n\
  266. #endif\n\
  267. #ifdef EMISSIVEFRESNEL\n\
  268. float emissiveFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,emissiveRightColor.a,emissiveLeftColor.a);\n\
  269. emissiveColor*=emissiveLeftColor.rgb*(1.0-emissiveFresnelTerm)+emissiveFresnelTerm*emissiveRightColor.rgb;\n\
  270. #endif\n\
  271. \n\
  272. #ifdef DIFFUSEFRESNEL\n\
  273. float diffuseFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,diffuseRightColor.a,diffuseLeftColor.a);\n\
  274. diffuseBase*=diffuseLeftColor.rgb*(1.0-diffuseFresnelTerm)+diffuseFresnelTerm*diffuseRightColor.rgb;\n\
  275. #endif\n\
  276. \n\
  277. #ifdef EMISSIVEASILLUMINATION\n\
  278. vec3 finalDiffuse=clamp(diffuseBase*diffuseColor+vAmbientColor,0.0,1.0)*baseColor.rgb;\n\
  279. #else\n\
  280. #ifdef LINKEMISSIVEWITHDIFFUSE\n\
  281. vec3 finalDiffuse=clamp((diffuseBase+emissiveColor)*diffuseColor+vAmbientColor,0.0,1.0)*baseColor.rgb;\n\
  282. #else\n\
  283. vec3 finalDiffuse=clamp(diffuseBase*diffuseColor+emissiveColor+vAmbientColor,0.0,1.0)*baseColor.rgb;\n\
  284. #endif\n\
  285. #endif\n\
  286. #ifdef SPECULARTERM\n\
  287. vec3 finalSpecular=specularBase*specularColor;\n\
  288. #ifdef SPECULAROVERALPHA\n\
  289. alpha=clamp(alpha+dot(finalSpecular,vec3(0.3,0.59,0.11)),0.,1.);\n\
  290. #endif\n\
  291. #else\n\
  292. vec3 finalSpecular=vec3(0.0);\n\
  293. #endif\n\
  294. #ifdef REFLECTIONOVERALPHA\n\
  295. alpha=clamp(alpha+dot(reflectionColor,vec3(0.3,0.59,0.11)),0.,1.);\n\
  296. #endif\n\
  297. \n\
  298. #ifdef EMISSIVEASILLUMINATION\n\
  299. vec4 color=vec4(clamp(finalDiffuse*baseAmbientColor+finalSpecular+reflectionColor+emissiveColor+refractionColor,0.0,1.0),alpha);\n\
  300. #else\n\
  301. vec4 color=vec4(finalDiffuse*baseAmbientColor+finalSpecular+reflectionColor+refractionColor,alpha);\n\
  302. #endif\n\
  303. \n\
  304. #ifdef LIGHTMAP\n\
  305. #ifndef LIGHTMAPEXCLUDED\n\
  306. #ifdef USELIGHTMAPASSHADOWMAP\n\
  307. color.rgb*=lightmapColor;\n\
  308. #else\n\
  309. color.rgb+=lightmapColor;\n\
  310. #endif\n\
  311. #endif\n\
  312. #endif\n\
  313. #include<logDepthFragment>\n\
  314. #include<fogFragment>\n\
  315. \n\
  316. // Apply image processing if relevant. As this applies in linear space, \n\
  317. // We first move from gamma to linear.\n\
  318. #ifdef IMAGEPROCESSINGPOSTPROCESS\n\
  319. color.rgb = toLinearSpace(color.rgb);\n\
  320. #else\n\
  321. #ifdef IMAGEPROCESSING\n\
  322. color.rgb = toLinearSpace(color.rgb);\n\
  323. color = applyImageProcessing(color);\n\
  324. #endif\n\
  325. #endif\n\
  326. \n\
  327. #[Fragment_Before_FragColor]\n\
  328. gl_FragColor=color;\n\
  329. }";
  330. _this.VertexStore = "#include<__decl__defaultVertex>\n\
  331. \n\
  332. #[Vertex_Begin]\n\
  333. \n\
  334. attribute vec3 position;\n\
  335. #ifdef NORMAL\n\
  336. attribute vec3 normal;\n\
  337. #endif\n\
  338. #ifdef TANGENT\n\
  339. attribute vec4 tangent;\n\
  340. #endif\n\
  341. #ifdef UV1\n\
  342. attribute vec2 uv;\n\
  343. #endif\n\
  344. #ifdef UV2\n\
  345. attribute vec2 uv2;\n\
  346. #endif\n\
  347. #ifdef VERTEXCOLOR\n\
  348. attribute vec4 color;\n\
  349. #endif\n\
  350. #include<bonesDeclaration>\n\
  351. \n\
  352. #include<instancesDeclaration>\n\
  353. #ifdef DIFFUSE\n\
  354. varying vec2 vDiffuseUV;\n\
  355. #endif\n\
  356. #ifdef AMBIENT\n\
  357. varying vec2 vAmbientUV;\n\
  358. #endif\n\
  359. #ifdef OPACITY\n\
  360. varying vec2 vOpacityUV;\n\
  361. #endif\n\
  362. #ifdef EMISSIVE\n\
  363. varying vec2 vEmissiveUV;\n\
  364. #endif\n\
  365. #ifdef LIGHTMAP\n\
  366. varying vec2 vLightmapUV;\n\
  367. #endif\n\
  368. #if defined(SPECULAR) && defined(SPECULARTERM)\n\
  369. varying vec2 vSpecularUV;\n\
  370. #endif\n\
  371. #ifdef BUMP\n\
  372. varying vec2 vBumpUV;\n\
  373. #endif\n\
  374. \n\
  375. varying vec3 localPosition;\n\
  376. varying vec3 vPositionW;\n\
  377. #ifdef NORMAL\n\
  378. varying vec3 vNormalW_helper;\n\
  379. varying vec3 localNormal;\n\
  380. #endif\n\
  381. #ifdef VERTEXCOLOR\n\
  382. varying vec4 vColor;\n\
  383. #endif\n\
  384. #include<bumpVertexDeclaration>\n\
  385. #include<clipPlaneVertexDeclaration>\n\
  386. #include<fogVertexDeclaration>\n\
  387. #include<__decl__lightFragment>[0..maxSimultaneousLights]\n\
  388. #include<morphTargetsVertexGlobalDeclaration>\n\
  389. #include<morphTargetsVertexDeclaration>[0..maxSimultaneousMorphTargets]\n\
  390. #ifdef REFLECTIONMAP_SKYBOX\n\
  391. varying vec3 vPositionUVW;\n\
  392. #endif\n\
  393. #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)\n\
  394. varying vec3 vDirectionW;\n\
  395. #endif\n\
  396. #include<logDepthDeclaration>\n\
  397. \n\
  398. #[Vertex_Definitions]\n\
  399. \n\
  400. void main(void) {\n\
  401. \n\
  402. #[Vertex_MainBegin]\n\
  403. \n\
  404. vec3 positionUpdated=position;\n\
  405. #ifdef NORMAL \n\
  406. vec3 normalUpdated=normal;\n\
  407. #endif\n\
  408. #ifdef TANGENT\n\
  409. vec4 tangentUpdated=tangent;\n\
  410. #endif\n\
  411. #include<morphTargetsVertex>[0..maxSimultaneousMorphTargets]\n\
  412. #ifdef REFLECTIONMAP_SKYBOX\n\
  413. vPositionUVW=positionUpdated;\n\
  414. #endif \n\
  415. #include<instancesVertex>\n\
  416. #include<bonesVertex>\n\
  417. \n\
  418. localPosition = positionUpdated;\n\
  419. #[Vertex_Before_PositionUpdated]\n\
  420. \n\
  421. gl_Position=viewProjection*finalWorld*vec4(positionUpdated,1.0);\n\
  422. vec4 worldPos=finalWorld*vec4(positionUpdated,1.0);\n\
  423. vPositionW=vec3(worldPos);\n\
  424. #ifdef NORMAL\n\
  425. \n\
  426. #[Vertex_Before_NormalUpdated]\n\
  427. \n\
  428. localNormal = normalUpdated;\n\
  429. vNormalW_helper=normalize(vec3(finalWorld*vec4(normalUpdated,0.0)));\n\
  430. #endif\n\
  431. #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)\n\
  432. vDirectionW=normalize(vec3(finalWorld*vec4(positionUpdated,0.0)));\n\
  433. #endif\n\
  434. \n\
  435. #ifndef UV1\n\
  436. vec2 uv=vec2(0.,0.);\n\
  437. #endif\n\
  438. #ifndef UV2\n\
  439. vec2 uv2=vec2(0.,0.);\n\
  440. #endif\n\
  441. #ifdef DIFFUSE\n\
  442. if (vDiffuseInfos.x == 0.)\n\
  443. {\n\
  444. vDiffuseUV=vec2(diffuseMatrix*vec4(uv,1.0,0.0));\n\
  445. }\n\
  446. else\n\
  447. {\n\
  448. vDiffuseUV=vec2(diffuseMatrix*vec4(uv2,1.0,0.0));\n\
  449. }\n\
  450. #endif\n\
  451. #ifdef AMBIENT\n\
  452. if (vAmbientInfos.x == 0.)\n\
  453. {\n\
  454. vAmbientUV=vec2(ambientMatrix*vec4(uv,1.0,0.0));\n\
  455. }\n\
  456. else\n\
  457. {\n\
  458. vAmbientUV=vec2(ambientMatrix*vec4(uv2,1.0,0.0));\n\
  459. }\n\
  460. #endif\n\
  461. #ifdef OPACITY\n\
  462. if (vOpacityInfos.x == 0.)\n\
  463. {\n\
  464. vOpacityUV=vec2(opacityMatrix*vec4(uv,1.0,0.0));\n\
  465. }\n\
  466. else\n\
  467. {\n\
  468. vOpacityUV=vec2(opacityMatrix*vec4(uv2,1.0,0.0));\n\
  469. }\n\
  470. #endif\n\
  471. #ifdef EMISSIVE\n\
  472. if (vEmissiveInfos.x == 0.)\n\
  473. {\n\
  474. vEmissiveUV=vec2(emissiveMatrix*vec4(uv,1.0,0.0));\n\
  475. }\n\
  476. else\n\
  477. {\n\
  478. vEmissiveUV=vec2(emissiveMatrix*vec4(uv2,1.0,0.0));\n\
  479. }\n\
  480. #endif\n\
  481. #ifdef LIGHTMAP\n\
  482. if (vLightmapInfos.x == 0.)\n\
  483. {\n\
  484. vLightmapUV=vec2(lightmapMatrix*vec4(uv,1.0,0.0));\n\
  485. }\n\
  486. else\n\
  487. {\n\
  488. vLightmapUV=vec2(lightmapMatrix*vec4(uv2,1.0,0.0));\n\
  489. }\n\
  490. #endif\n\
  491. #if defined(SPECULAR) && defined(SPECULARTERM)\n\
  492. if (vSpecularInfos.x == 0.)\n\
  493. {\n\
  494. vSpecularUV=vec2(specularMatrix*vec4(uv,1.0,0.0));\n\
  495. }\n\
  496. else\n\
  497. {\n\
  498. vSpecularUV=vec2(specularMatrix*vec4(uv2,1.0,0.0));\n\
  499. }\n\
  500. #endif\n\
  501. #ifdef BUMP\n\
  502. if (vBumpInfos.x == 0.)\n\
  503. {\n\
  504. vBumpUV=vec2(bumpMatrix*vec4(uv,1.0,0.0));\n\
  505. }\n\
  506. else\n\
  507. {\n\
  508. vBumpUV=vec2(bumpMatrix*vec4(uv2,1.0,0.0));\n\
  509. }\n\
  510. #endif\n\
  511. #include<bumpVertex>\n\
  512. #include<clipPlaneVertex>\n\
  513. #include<fogVertex>\n\
  514. #include<shadowsVertex>[0..maxSimultaneousLights]\n\
  515. #ifdef VERTEXCOLOR\n\
  516. \n\
  517. vColor=color;\n\
  518. #endif\n\
  519. #include<pointCloudVertex>\n\
  520. #include<logDepthVertex>\n\
  521. }";
  522. return _this;
  523. }
  524. return ShaderForVer3_0;
  525. }(CustomShaderStructure));
  526. BABYLON.ShaderForVer3_0 = ShaderForVer3_0;
  527. var StandardShaderVersions = (function () {
  528. function StandardShaderVersions() {
  529. }
  530. return StandardShaderVersions;
  531. }());
  532. BABYLON.StandardShaderVersions = StandardShaderVersions;
  533. var CustomMaterial = (function (_super) {
  534. __extends(CustomMaterial, _super);
  535. function CustomMaterial(name, scene) {
  536. var _this = _super.call(this, name, scene) || this;
  537. _this.CustomParts = new ShaderSpecialParts();
  538. _this.customShaderNameResolve = _this.Builder;
  539. _this.SelectVersion("3.0.0");
  540. return _this;
  541. }
  542. CustomMaterial.prototype.AttachAfterBind = function (mesh, effect) {
  543. for (var el in this._newUniformInstances) {
  544. var ea = el.toString().split('-');
  545. if (ea[0] == 'vec2')
  546. effect.setVector2(ea[1], this._newUniformInstances[el]);
  547. else if (ea[0] == 'vec3')
  548. effect.setVector3(ea[1], this._newUniformInstances[el]);
  549. else if (ea[0] == 'vec4')
  550. effect.setVector4(ea[1], this._newUniformInstances[el]);
  551. else if (ea[0] == 'mat4')
  552. effect.setMatrix(ea[1], this._newUniformInstances[el]);
  553. else if (ea[0] == 'float')
  554. effect.setFloat(ea[1], this._newUniformInstances[el]);
  555. }
  556. for (var el in this._newSamplerInstances) {
  557. var ea = el.toString().split('-');
  558. if (ea[0] == 'sampler2D' && this._newSamplerInstances[el].isReady && this._newSamplerInstances[el].isReady())
  559. effect.setTexture(ea[1], this._newSamplerInstances[el]);
  560. }
  561. };
  562. CustomMaterial.prototype.ReviewUniform = function (name, arr) {
  563. if (name == "uniform") {
  564. for (var ind in this._newUniforms)
  565. if (this._customUniform[ind].indexOf('sampler') == -1)
  566. arr.push(this._newUniforms[ind]);
  567. }
  568. if (name == "sampler") {
  569. for (var ind in this._newUniforms)
  570. if (this._customUniform[ind].indexOf('sampler') != -1)
  571. arr.push(this._newUniforms[ind]);
  572. }
  573. return arr;
  574. };
  575. CustomMaterial.prototype.Builder = function (shaderName, uniforms, uniformBuffers, samplers, defines) {
  576. if (this._isCreatedShader)
  577. return this._createdShaderName;
  578. this._isCreatedShader = false;
  579. CustomMaterial.ShaderIndexer++;
  580. var name = name + "custom_" + CustomMaterial.ShaderIndexer;
  581. this.ReviewUniform("uniform", uniforms);
  582. this.ReviewUniform("sampler", samplers);
  583. var fn_afterBind = this._afterBind;
  584. this._afterBind = function (m, e) {
  585. this.AttachAfterBind(m, e);
  586. try {
  587. fn_afterBind(m, e);
  588. }
  589. catch (e) { }
  590. ;
  591. };
  592. BABYLON.Effect.ShadersStore[name + "VertexShader"] = this.ShaderVersion.VertexStore
  593. .replace('#[Vertex_Begin]', (this.CustomParts.Vertex_Begin ? this.CustomParts.Vertex_Begin : ""))
  594. .replace('#[Vertex_Definitions]', (this._customUniform ? this._customUniform.join("\n") : "") + (this.CustomParts.Vertex_Definitions ? this.CustomParts.Vertex_Definitions : ""))
  595. .replace('#[Vertex_MainBegin]', (this.CustomParts.Vertex_MainBegin ? this.CustomParts.Vertex_MainBegin : ""))
  596. .replace('#[Vertex_Before_PositionUpdated]', (this.CustomParts.Vertex_Before_PositionUpdated ? this.CustomParts.Vertex_Before_PositionUpdated : ""))
  597. .replace('#[Vertex_Before_NormalUpdated]', (this.CustomParts.Vertex_Before_NormalUpdated ? this.CustomParts.Vertex_Before_NormalUpdated : ""));
  598. BABYLON.Effect.ShadersStore[name + "PixelShader"] = this.ShaderVersion.FragmentStore
  599. .replace('#[Fragment_Begin]', (this.CustomParts.Fragment_Begin ? this.CustomParts.Fragment_Begin : ""))
  600. .replace('#[Fragment_MainBegin]', (this.CustomParts.Fragment_MainBegin ? this.CustomParts.Fragment_MainBegin : ""))
  601. .replace('#[Fragment_Definitions]', (this._customUniform ? this._customUniform.join("\n") : "") + (this.CustomParts.Fragment_Definitions ? this.CustomParts.Fragment_Definitions : ""))
  602. .replace('#[Fragment_Custom_Diffuse]', (this.CustomParts.Fragment_Custom_Diffuse ? this.CustomParts.Fragment_Custom_Diffuse : ""))
  603. .replace('#[Fragment_Custom_Alpha]', (this.CustomParts.Fragment_Custom_Alpha ? this.CustomParts.Fragment_Custom_Alpha : ""))
  604. .replace('#[Fragment_Before_FragColor]', (this.CustomParts.Fragment_Before_FragColor ? this.CustomParts.Fragment_Before_FragColor : ""));
  605. this._isCreatedShader = true;
  606. this._createdShaderName = name;
  607. return name;
  608. };
  609. CustomMaterial.prototype.SelectVersion = function (ver) {
  610. switch (ver) {
  611. case "3.0.0":
  612. this.ShaderVersion = new ShaderForVer3_0();
  613. break;
  614. }
  615. };
  616. CustomMaterial.prototype.AddUniform = function (name, kind, param) {
  617. if (!this._customUniform) {
  618. this._customUniform = new Array();
  619. this._newUniforms = new Array();
  620. this._newSamplerInstances = new Array();
  621. this._newUniformInstances = new Array();
  622. }
  623. if (param) {
  624. if (kind.indexOf("sampler") == -1) {
  625. this._newUniformInstances[kind + "-" + name] = param;
  626. }
  627. else {
  628. this._newSamplerInstances[kind + "-" + name] = param;
  629. }
  630. }
  631. this._customUniform.push("uniform " + kind + " " + name + ";");
  632. this._newUniforms.push(name);
  633. return this;
  634. };
  635. CustomMaterial.prototype.Fragment_Begin = function (shaderPart) {
  636. this.CustomParts.Fragment_Begin = shaderPart;
  637. return this;
  638. };
  639. CustomMaterial.prototype.Fragment_Definitions = function (shaderPart) {
  640. this.CustomParts.Fragment_Definitions = shaderPart;
  641. return this;
  642. };
  643. CustomMaterial.prototype.Fragment_MainBegin = function (shaderPart) {
  644. this.CustomParts.Fragment_MainBegin = shaderPart;
  645. return this;
  646. };
  647. CustomMaterial.prototype.Fragment_Custom_Diffuse = function (shaderPart) {
  648. this.CustomParts.Fragment_Custom_Diffuse = shaderPart.replace("result", "diffuseColor");
  649. return this;
  650. };
  651. CustomMaterial.prototype.Fragment_Custom_Alpha = function (shaderPart) {
  652. this.CustomParts.Fragment_Custom_Alpha = shaderPart.replace("result", "alpha");
  653. return this;
  654. };
  655. CustomMaterial.prototype.Fragment_Before_FragColor = function (shaderPart) {
  656. this.CustomParts.Fragment_Before_FragColor = shaderPart.replace("result", "color");
  657. return this;
  658. };
  659. CustomMaterial.prototype.Vertex_Begin = function (shaderPart) {
  660. this.CustomParts.Vertex_Begin = shaderPart;
  661. return this;
  662. };
  663. CustomMaterial.prototype.Vertex_Definitions = function (shaderPart) {
  664. this.CustomParts.Vertex_Definitions = shaderPart;
  665. return this;
  666. };
  667. CustomMaterial.prototype.Vertex_MainBegin = function (shaderPart) {
  668. this.CustomParts.Vertex_MainBegin = shaderPart;
  669. return this;
  670. };
  671. CustomMaterial.prototype.Vertex_Before_PositionUpdated = function (shaderPart) {
  672. this.CustomParts.Vertex_Before_PositionUpdated = shaderPart.replace("result", "positionUpdated");
  673. return this;
  674. };
  675. CustomMaterial.prototype.Vertex_Before_NormalUpdated = function (shaderPart) {
  676. this.CustomParts.Vertex_Before_NormalUpdated = shaderPart.replace("result", "normalUpdated");
  677. return this;
  678. };
  679. return CustomMaterial;
  680. }(BABYLON.StandardMaterial));
  681. CustomMaterial.ShaderIndexer = 1;
  682. BABYLON.CustomMaterial = CustomMaterial;
  683. })(BABYLON || (BABYLON = {}));
  684. //# sourceMappingURL=babylon.customMaterial.js.map