babylon.customMaterial.js 23 KB

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