babylon.customMaterial.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. export class CustomShaderStructure {
  4. public FragmentStore : string;
  5. public VertexStore : string;
  6. constructor(){
  7. }
  8. }
  9. export class ShaderSpecialParts{
  10. constructor(){}
  11. public Fragment_Begin:string;
  12. public Fragment_Definitions:string;
  13. public Fragment_MainBegin: string;
  14. // diffuseColor
  15. public Fragment_Custom_Diffuse: string;
  16. // alpha
  17. public Fragment_Custom_Alpha : string;
  18. public Fragment_Before_FragColor: string;
  19. public Vertex_Begin:string;
  20. public Vertex_Definitions:string;
  21. public Vertex_MainBegin: string;
  22. // positionUpdated
  23. public Vertex_Before_PositionUpdated:string;
  24. // normalUpdated
  25. public Vertex_Before_NormalUpdated : string;
  26. }
  27. export class ShaderForVer3_0 extends CustomShaderStructure {
  28. constructor() {
  29. super();
  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<shadowsVertexDeclaration>[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. }
  522. }
  523. export class StandardShaderVersions{
  524. public static Ver3_0;
  525. }
  526. export class CustomMaterial extends StandardMaterial {
  527. public static ShaderIndexer = 1;
  528. public CustomParts : ShaderSpecialParts;
  529. public ShaderVersion : CustomShaderStructure ;
  530. _customUniform : string[];
  531. _newUniforms : string[];
  532. _newUniformInstances : any[];
  533. _newSamplerInstances : Texture[];
  534. public AttachAfterBind(mesh:Mesh,effect:Effect){
  535. for(var el in this._newUniformInstances){
  536. var ea = el.toString().split('-');
  537. if(ea[0] == 'vec2') effect.setVector2(ea[1],this._newUniformInstances[el]);
  538. else if(ea[0] == 'vec3') effect.setVector3(ea[1],this._newUniformInstances[el]);
  539. else if(ea[0] == 'vec4') effect.setVector4(ea[1],this._newUniformInstances[el]);
  540. else if(ea[0] == 'mat4') effect.setMatrix(ea[1],this._newUniformInstances[el]);
  541. else if(ea[0] == 'float') effect.setFloat(ea[1],this._newUniformInstances[el]);
  542. }
  543. for(var el in this._newSamplerInstances){
  544. var ea = el.toString().split('-');
  545. if(ea[0] == 'sampler2D' && this._newSamplerInstances[el].isReady && this._newSamplerInstances[el].isReady())
  546. effect.setTexture(ea[1],this._newSamplerInstances[el]);
  547. }
  548. }
  549. public ReviewUniform(name:string, arr : string[] ) : string[]{
  550. if(name == "uniform")
  551. {
  552. for(var ind in this._newUniforms)
  553. if(this._customUniform[ind].indexOf('sampler')== -1)
  554. arr.push(this._newUniforms[ind]);
  555. }
  556. if(name == "sampler")
  557. {
  558. for(var ind in this._newUniforms)
  559. if(this._customUniform[ind].indexOf('sampler')!= -1)
  560. arr.push(this._newUniforms[ind]);
  561. }
  562. return arr;
  563. }
  564. public Builder(shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: StandardMaterialDefines) : string {
  565. CustomMaterial.ShaderIndexer++;
  566. var name = name+"custom_"+CustomMaterial.ShaderIndexer;
  567. this.ReviewUniform("uniform",uniforms);
  568. this.ReviewUniform("sampler",samplers);
  569. var fn_afterBind = this._afterBind;
  570. this._afterBind = function(m,e){
  571. this.AttachAfterBind(m,e);
  572. try{fn_afterBind(m,e);}catch(e){};
  573. } ;
  574. BABYLON.Effect.ShadersStore[name+"VertexShader"] = this.ShaderVersion.VertexStore
  575. .replace('#[Vertex_Begin]',(this.CustomParts.Vertex_Begin ? this.CustomParts.Vertex_Begin : ""))
  576. .replace('#[Vertex_Definitions]',(this._customUniform? this._customUniform.join("\n"):"")+ (this.CustomParts.Vertex_Definitions ? this.CustomParts.Vertex_Definitions : ""))
  577. .replace('#[Vertex_MainBegin]',(this.CustomParts.Vertex_MainBegin ? this.CustomParts.Vertex_MainBegin : ""))
  578. .replace('#[Vertex_Before_PositionUpdated]',(this.CustomParts.Vertex_Before_PositionUpdated ? this.CustomParts.Vertex_Before_PositionUpdated : ""))
  579. .replace('#[Vertex_Before_NormalUpdated]',(this.CustomParts.Vertex_Before_NormalUpdated ? this.CustomParts.Vertex_Before_NormalUpdated : "")) ;
  580. BABYLON.Effect.ShadersStore[name+"PixelShader"] = this.ShaderVersion.FragmentStore
  581. .replace('#[Fragment_Begin]',(this.CustomParts.Fragment_Begin ? this.CustomParts.Fragment_Begin : ""))
  582. .replace('#[Fragment_MainBegin]',(this.CustomParts.Fragment_MainBegin ? this.CustomParts.Fragment_MainBegin : ""))
  583. .replace('#[Fragment_Definitions]',(this._customUniform? this._customUniform.join("\n"):"")+(this.CustomParts.Fragment_Definitions ? this.CustomParts.Fragment_Definitions : ""))
  584. .replace('#[Fragment_Custom_Diffuse]',(this.CustomParts.Fragment_Custom_Diffuse ? this.CustomParts.Fragment_Custom_Diffuse : ""))
  585. .replace('#[Fragment_Custom_Alpha]',(this.CustomParts.Fragment_Custom_Alpha ? this.CustomParts.Fragment_Custom_Alpha : ""))
  586. .replace('#[Fragment_Before_FragColor]',(this.CustomParts.Fragment_Before_FragColor ? this.CustomParts.Fragment_Before_FragColor : "")) ;
  587. return name ;
  588. }
  589. public SelectVersion(ver:string){
  590. switch(ver){
  591. case "3.0.0" : this.ShaderVersion = new ShaderForVer3_0();break;
  592. }
  593. }
  594. constructor(name:string,scene:Scene ){
  595. super(name,scene);
  596. this.CustomParts = new ShaderSpecialParts();
  597. this.customShaderNameResolve = this.Builder;
  598. this.SelectVersion("3.0.0");
  599. }
  600. public AddUniform(name:string,kind:string,param:any):CustomMaterial{
  601. if(!this._customUniform)
  602. {
  603. this._customUniform = new Array();
  604. this._newUniforms = new Array();
  605. this._newSamplerInstances = new Array();
  606. this._newUniformInstances = new Array();
  607. }
  608. if(param){
  609. if(kind.indexOf("sampler") == -1) {
  610. this._newUniformInstances[kind+"-"+name] = param;
  611. }
  612. else{
  613. this._newSamplerInstances[kind+"-"+name] = param;
  614. }
  615. }
  616. this._customUniform.push("uniform "+kind+" "+name+";");
  617. this._newUniforms.push(name);
  618. return this;
  619. }
  620. public Fragment_Begin(shaderPart:string):CustomMaterial{
  621. this.CustomParts.Fragment_Begin = shaderPart;
  622. return this;
  623. }
  624. public Fragment_Definitions(shaderPart:string):CustomMaterial{
  625. this.CustomParts.Fragment_Definitions = shaderPart;
  626. return this;
  627. }
  628. public Fragment_MainBegin(shaderPart:string):CustomMaterial{
  629. this.CustomParts.Fragment_MainBegin = shaderPart;
  630. return this;
  631. }
  632. public Fragment_Custom_Diffuse(shaderPart:string):CustomMaterial{
  633. this.CustomParts.Fragment_Custom_Diffuse = shaderPart.replace("result","diffuseColor");
  634. return this;
  635. }
  636. public Fragment_Custom_Alpha(shaderPart:string):CustomMaterial{
  637. this.CustomParts.Fragment_Custom_Alpha = shaderPart.replace("result","alpha");
  638. return this;
  639. }
  640. public Fragment_Before_FragColor(shaderPart:string):CustomMaterial{
  641. this.CustomParts.Fragment_Before_FragColor = shaderPart.replace("result","color");
  642. return this;
  643. }
  644. public Vertex_Begin(shaderPart:string):CustomMaterial{
  645. this.CustomParts.Vertex_Begin = shaderPart;
  646. return this;
  647. }
  648. public Vertex_Definitions(shaderPart:string):CustomMaterial{
  649. this.CustomParts.Vertex_Definitions = shaderPart;
  650. return this;
  651. }
  652. public Vertex_MainBegin(shaderPart:string):CustomMaterial{
  653. this.CustomParts.Vertex_MainBegin = shaderPart;
  654. return this;
  655. }
  656. public Vertex_Before_PositionUpdated(shaderPart:string):CustomMaterial{
  657. this.CustomParts.Vertex_Before_PositionUpdated = shaderPart.replace("result","positionUpdated");
  658. return this;
  659. }
  660. public Vertex_Before_NormalUpdated(shaderPart:string):CustomMaterial{
  661. this.CustomParts.Vertex_Before_NormalUpdated = shaderPart.replace("result","normalUpdated");
  662. return this;
  663. }
  664. }
  665. }