babylon.customMaterial.js 22 KB

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