babylon.customMaterial.ts 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. // old version of standard material updated every 3 months
  4. export class StandardMaterialDefines_OldVer extends MaterialDefines implements IImageProcessingConfigurationDefines {
  5. public DIFFUSE = false;
  6. public AMBIENT = false;
  7. public OPACITY = false;
  8. public OPACITYRGB = false;
  9. public REFLECTION = false;
  10. public EMISSIVE = false;
  11. public SPECULAR = false;
  12. public BUMP = false;
  13. public PARALLAX = false;
  14. public PARALLAXOCCLUSION = false;
  15. public SPECULAROVERALPHA = false;
  16. public CLIPPLANE = false;
  17. public ALPHATEST = false;
  18. public ALPHAFROMDIFFUSE = false;
  19. public POINTSIZE = false;
  20. public FOG = false;
  21. public SPECULARTERM = false;
  22. public DIFFUSEFRESNEL = false;
  23. public OPACITYFRESNEL = false;
  24. public REFLECTIONFRESNEL = false;
  25. public REFRACTIONFRESNEL = false;
  26. public EMISSIVEFRESNEL = false;
  27. public FRESNEL = false;
  28. public NORMAL = false;
  29. public UV1 = false;
  30. public UV2 = false;
  31. public VERTEXCOLOR = false;
  32. public VERTEXALPHA = false;
  33. public NUM_BONE_INFLUENCERS = 0;
  34. public BonesPerMesh = 0;
  35. public INSTANCES = false;
  36. public GLOSSINESS = false;
  37. public ROUGHNESS = false;
  38. public EMISSIVEASILLUMINATION = false;
  39. public LINKEMISSIVEWITHDIFFUSE = false;
  40. public REFLECTIONFRESNELFROMSPECULAR = false;
  41. public LIGHTMAP = false;
  42. public USELIGHTMAPASSHADOWMAP = false;
  43. public REFLECTIONMAP_3D = false;
  44. public REFLECTIONMAP_SPHERICAL = false;
  45. public REFLECTIONMAP_PLANAR = false;
  46. public REFLECTIONMAP_CUBIC = false;
  47. public REFLECTIONMAP_PROJECTION = false;
  48. public REFLECTIONMAP_SKYBOX = false;
  49. public REFLECTIONMAP_EXPLICIT = false;
  50. public REFLECTIONMAP_EQUIRECTANGULAR = false;
  51. public REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
  52. public REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
  53. public INVERTCUBICMAP = false;
  54. public LOGARITHMICDEPTH = false;
  55. public REFRACTION = false;
  56. public REFRACTIONMAP_3D = false;
  57. public REFLECTIONOVERALPHA = false;
  58. public TWOSIDEDLIGHTING = false;
  59. public SHADOWFLOAT = false;
  60. public MORPHTARGETS = false;
  61. public MORPHTARGETS_NORMAL = false;
  62. public MORPHTARGETS_TANGENT = false;
  63. public NUM_MORPH_INFLUENCERS = 0;
  64. public USERIGHTHANDEDSYSTEM = false;
  65. public IMAGEPROCESSING = false;
  66. public VIGNETTE = false;
  67. public VIGNETTEBLENDMODEMULTIPLY = false;
  68. public VIGNETTEBLENDMODEOPAQUE = false;
  69. public TONEMAPPING = false;
  70. public CONTRAST = false;
  71. public COLORCURVES = false;
  72. public COLORGRADING = false;
  73. public SAMPLER3DGREENDEPTH = false;
  74. public SAMPLER3DBGRMAP = false;
  75. public IMAGEPROCESSINGPOSTPROCESS = false;
  76. public EXPOSURE = false;
  77. constructor() {
  78. super();
  79. this.rebuild();
  80. }
  81. public setReflectionMode(modeToEnable: string) {
  82. var modes = [
  83. "REFLECTIONMAP_CUBIC", "REFLECTIONMAP_EXPLICIT", "REFLECTIONMAP_PLANAR",
  84. "REFLECTIONMAP_PROJECTION", "REFLECTIONMAP_PROJECTION", "REFLECTIONMAP_SKYBOX",
  85. "REFLECTIONMAP_SPHERICAL", "REFLECTIONMAP_EQUIRECTANGULAR", "REFLECTIONMAP_EQUIRECTANGULAR_FIXED",
  86. "REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED"
  87. ];
  88. for (var mode of modes) {
  89. this[mode] = (mode === modeToEnable);
  90. }
  91. }
  92. }
  93. export class StandardMaterial_OldVer extends PushMaterial {
  94. @serializeAsTexture("diffuseTexture")
  95. private _diffuseTexture: BaseTexture;
  96. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  97. public diffuseTexture: BaseTexture;
  98. @serializeAsTexture("ambientTexture")
  99. private _ambientTexture: BaseTexture;
  100. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  101. public ambientTexture: BaseTexture;
  102. @serializeAsTexture("opacityTexture")
  103. private _opacityTexture: BaseTexture;
  104. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  105. public opacityTexture: BaseTexture;
  106. @serializeAsTexture("reflectionTexture")
  107. private _reflectionTexture: BaseTexture;
  108. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  109. public reflectionTexture: BaseTexture;
  110. @serializeAsTexture("emissiveTexture")
  111. private _emissiveTexture: BaseTexture;
  112. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  113. public emissiveTexture: BaseTexture;
  114. @serializeAsTexture("specularTexture")
  115. private _specularTexture: BaseTexture;
  116. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  117. public specularTexture: BaseTexture;
  118. @serializeAsTexture("bumpTexture")
  119. private _bumpTexture: BaseTexture;
  120. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  121. public bumpTexture: BaseTexture;
  122. @serializeAsTexture("lightmapTexture")
  123. private _lightmapTexture: BaseTexture;
  124. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  125. public lightmapTexture: BaseTexture;
  126. @serializeAsTexture("refractionTexture")
  127. private _refractionTexture: BaseTexture;
  128. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  129. public refractionTexture: BaseTexture;
  130. @serializeAsColor3("ambient")
  131. public ambientColor = new Color3(0, 0, 0);
  132. @serializeAsColor3("diffuse")
  133. public diffuseColor = new Color3(1, 1, 1);
  134. @serializeAsColor3("specular")
  135. public specularColor = new Color3(1, 1, 1);
  136. @serializeAsColor3("emissive")
  137. public emissiveColor = new Color3(0, 0, 0);
  138. @serialize()
  139. public specularPower = 64;
  140. @serialize("useAlphaFromDiffuseTexture")
  141. private _useAlphaFromDiffuseTexture = false;
  142. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  143. public useAlphaFromDiffuseTexture: boolean;
  144. @serialize("useEmissiveAsIllumination")
  145. private _useEmissiveAsIllumination = false;
  146. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  147. public useEmissiveAsIllumination: boolean;
  148. @serialize("linkEmissiveWithDiffuse")
  149. private _linkEmissiveWithDiffuse = false;
  150. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  151. public linkEmissiveWithDiffuse: boolean;
  152. @serialize("useSpecularOverAlpha")
  153. private _useSpecularOverAlpha = false;
  154. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  155. public useSpecularOverAlpha: boolean;
  156. @serialize("useReflectionOverAlpha")
  157. private _useReflectionOverAlpha = false;
  158. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  159. public useReflectionOverAlpha: boolean;
  160. @serialize("disableLighting")
  161. private _disableLighting = false;
  162. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  163. public disableLighting: boolean;
  164. @serialize("useParallax")
  165. private _useParallax = false;
  166. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  167. public useParallax: boolean;
  168. @serialize("useParallaxOcclusion")
  169. private _useParallaxOcclusion = false;
  170. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  171. public useParallaxOcclusion: boolean;
  172. @serialize()
  173. public parallaxScaleBias = 0.05;
  174. @serialize("roughness")
  175. private _roughness = 0;
  176. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  177. public roughness: number;
  178. @serialize()
  179. public indexOfRefraction = 0.98;
  180. @serialize()
  181. public invertRefractionY = true;
  182. @serialize("useLightmapAsShadowmap")
  183. private _useLightmapAsShadowmap = false;
  184. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  185. public useLightmapAsShadowmap: boolean;
  186. // Fresnel
  187. @serializeAsFresnelParameters("diffuseFresnelParameters")
  188. private _diffuseFresnelParameters: FresnelParameters;
  189. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  190. public diffuseFresnelParameters: FresnelParameters;
  191. @serializeAsFresnelParameters("opacityFresnelParameters")
  192. private _opacityFresnelParameters: FresnelParameters;
  193. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  194. public opacityFresnelParameters: FresnelParameters;
  195. @serializeAsFresnelParameters("reflectionFresnelParameters")
  196. private _reflectionFresnelParameters: FresnelParameters;
  197. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  198. public reflectionFresnelParameters: FresnelParameters;
  199. @serializeAsFresnelParameters("refractionFresnelParameters")
  200. private _refractionFresnelParameters: FresnelParameters;
  201. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  202. public refractionFresnelParameters: FresnelParameters;
  203. @serializeAsFresnelParameters("emissiveFresnelParameters")
  204. private _emissiveFresnelParameters: FresnelParameters;
  205. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  206. public emissiveFresnelParameters: FresnelParameters;
  207. @serialize("useReflectionFresnelFromSpecular")
  208. private _useReflectionFresnelFromSpecular = false;
  209. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  210. public useReflectionFresnelFromSpecular: boolean;
  211. @serialize("useGlossinessFromSpecularMapAlpha")
  212. private _useGlossinessFromSpecularMapAlpha = false;
  213. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  214. public useGlossinessFromSpecularMapAlpha: boolean;
  215. @serialize("maxSimultaneousLights")
  216. private _maxSimultaneousLights = 4;
  217. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  218. public maxSimultaneousLights: number;
  219. /**
  220. * If sets to true, x component of normal map value will invert (x = 1.0 - x).
  221. */
  222. @serialize("invertNormalMapX")
  223. private _invertNormalMapX = false;
  224. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  225. public invertNormalMapX: boolean;
  226. /**
  227. * If sets to true, y component of normal map value will invert (y = 1.0 - y).
  228. */
  229. @serialize("invertNormalMapY")
  230. private _invertNormalMapY = false;
  231. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  232. public invertNormalMapY: boolean;
  233. /**
  234. * If sets to true and backfaceCulling is false, normals will be flipped on the backside.
  235. */
  236. @serialize("twoSidedLighting")
  237. private _twoSidedLighting = false;
  238. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  239. public twoSidedLighting: boolean;
  240. /**
  241. * Default configuration related to image processing available in the standard Material.
  242. */
  243. protected _imageProcessingConfiguration: ImageProcessingConfiguration;
  244. /**
  245. * Gets the image processing configuration used either in this material.
  246. */
  247. public get imageProcessingConfiguration(): ImageProcessingConfiguration {
  248. return this._imageProcessingConfiguration;
  249. }
  250. /**
  251. * Sets the Default image processing configuration used either in the this material.
  252. *
  253. * If sets to null, the scene one is in use.
  254. */
  255. public set imageProcessingConfiguration(value: ImageProcessingConfiguration) {
  256. this._attachImageProcessingConfiguration(value);
  257. // Ensure the effect will be rebuilt.
  258. this._markAllSubMeshesAsTexturesDirty();
  259. }
  260. /**
  261. * Keep track of the image processing observer to allow dispose and replace.
  262. */
  263. private _imageProcessingObserver: Observer<ImageProcessingConfiguration>;
  264. /**
  265. * Attaches a new image processing configuration to the Standard Material.
  266. * @param configuration
  267. */
  268. protected _attachImageProcessingConfiguration(configuration: ImageProcessingConfiguration): void {
  269. if (configuration === this._imageProcessingConfiguration) {
  270. return;
  271. }
  272. // Detaches observer.
  273. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  274. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  275. }
  276. // Pick the scene configuration if needed.
  277. if (!configuration) {
  278. this._imageProcessingConfiguration = this.getScene().imageProcessingConfiguration;
  279. }
  280. else {
  281. this._imageProcessingConfiguration = configuration;
  282. }
  283. // Attaches observer.
  284. this._imageProcessingObserver = this._imageProcessingConfiguration.onUpdateParameters.add(conf => {
  285. this._markAllSubMeshesAsImageProcessingDirty();
  286. });
  287. }
  288. /**
  289. * Gets wether the color curves effect is enabled.
  290. */
  291. public get cameraColorCurvesEnabled(): boolean {
  292. return this.imageProcessingConfiguration.colorCurvesEnabled;
  293. }
  294. /**
  295. * Sets wether the color curves effect is enabled.
  296. */
  297. public set cameraColorCurvesEnabled(value: boolean) {
  298. this.imageProcessingConfiguration.colorCurvesEnabled = value;
  299. }
  300. /**
  301. * Gets wether the color grading effect is enabled.
  302. */
  303. public get cameraColorGradingEnabled(): boolean {
  304. return this.imageProcessingConfiguration.colorGradingEnabled;
  305. }
  306. /**
  307. * Gets wether the color grading effect is enabled.
  308. */
  309. public set cameraColorGradingEnabled(value: boolean) {
  310. this.imageProcessingConfiguration.colorGradingEnabled = value;
  311. }
  312. /**
  313. * Gets wether tonemapping is enabled or not.
  314. */
  315. public get cameraToneMappingEnabled(): boolean {
  316. return this._imageProcessingConfiguration.toneMappingEnabled;
  317. };
  318. /**
  319. * Sets wether tonemapping is enabled or not
  320. */
  321. public set cameraToneMappingEnabled(value: boolean) {
  322. this._imageProcessingConfiguration.toneMappingEnabled = value;
  323. };
  324. /**
  325. * The camera exposure used on this material.
  326. * This property is here and not in the camera to allow controlling exposure without full screen post process.
  327. * This corresponds to a photographic exposure.
  328. */
  329. public get cameraExposure(): number {
  330. return this._imageProcessingConfiguration.exposure;
  331. };
  332. /**
  333. * The camera exposure used on this material.
  334. * This property is here and not in the camera to allow controlling exposure without full screen post process.
  335. * This corresponds to a photographic exposure.
  336. */
  337. public set cameraExposure(value: number) {
  338. this._imageProcessingConfiguration.exposure = value;
  339. };
  340. /**
  341. * Gets The camera contrast used on this material.
  342. */
  343. public get cameraContrast(): number {
  344. return this._imageProcessingConfiguration.contrast;
  345. }
  346. /**
  347. * Sets The camera contrast used on this material.
  348. */
  349. public set cameraContrast(value: number) {
  350. this._imageProcessingConfiguration.contrast = value;
  351. }
  352. /**
  353. * Gets the Color Grading 2D Lookup Texture.
  354. */
  355. public get cameraColorGradingTexture(): BaseTexture {
  356. return this._imageProcessingConfiguration.colorGradingTexture;
  357. }
  358. /**
  359. * Sets the Color Grading 2D Lookup Texture.
  360. */
  361. public set cameraColorGradingTexture(value: BaseTexture) {
  362. this._imageProcessingConfiguration.colorGradingTexture = value;
  363. }
  364. public customShaderNameResolve: (shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: StandardMaterialDefines_OldVer) => string;
  365. protected _renderTargets = new SmartArray<RenderTargetTexture>(16);
  366. protected _worldViewProjectionMatrix = Matrix.Zero();
  367. protected _globalAmbientColor = new Color3(0, 0, 0);
  368. protected _useLogarithmicDepth: boolean;
  369. constructor(name: string, scene: Scene) {
  370. super(name, scene);
  371. // Setup the default processing configuration to the scene.
  372. this._attachImageProcessingConfiguration(null);
  373. this.getRenderTargetTextures = (): SmartArray<RenderTargetTexture> => {
  374. this._renderTargets.reset();
  375. if (StandardMaterial_OldVer.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  376. this._renderTargets.push(this._reflectionTexture);
  377. }
  378. if (StandardMaterial_OldVer.RefractionTextureEnabled && this._refractionTexture && this._refractionTexture.isRenderTarget) {
  379. this._renderTargets.push(this._refractionTexture);
  380. }
  381. return this._renderTargets;
  382. }
  383. }
  384. public getClassName(): string {
  385. return "StandardMaterial_OldVer";
  386. }
  387. @serialize()
  388. public get useLogarithmicDepth(): boolean {
  389. return this._useLogarithmicDepth;
  390. }
  391. public set useLogarithmicDepth(value: boolean) {
  392. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  393. this._markAllSubMeshesAsMiscDirty();
  394. }
  395. public needAlphaBlending(): boolean {
  396. return (this.alpha < 1.0) || (this._opacityTexture != null) || this._shouldUseAlphaFromDiffuseTexture() || this._opacityFresnelParameters && this._opacityFresnelParameters.isEnabled;
  397. }
  398. public needAlphaTesting(): boolean {
  399. return this._diffuseTexture != null && this._diffuseTexture.hasAlpha;
  400. }
  401. protected _shouldUseAlphaFromDiffuseTexture(): boolean {
  402. return this._diffuseTexture != null && this._diffuseTexture.hasAlpha && this._useAlphaFromDiffuseTexture;
  403. }
  404. public getAlphaTestTexture(): BaseTexture {
  405. return this._diffuseTexture;
  406. }
  407. /**
  408. * Child classes can use it to update shaders
  409. */
  410. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  411. if (this.isFrozen) {
  412. if (this._wasPreviouslyReady && subMesh.effect) {
  413. return true;
  414. }
  415. }
  416. if (!subMesh._materialDefines) {
  417. subMesh._materialDefines = new StandardMaterialDefines_OldVer();
  418. }
  419. var scene = this.getScene();
  420. var defines = <StandardMaterialDefines_OldVer>subMesh._materialDefines;
  421. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  422. if (defines._renderId === scene.getRenderId()) {
  423. return true;
  424. }
  425. }
  426. var engine = scene.getEngine();
  427. // Lights
  428. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, true, this._maxSimultaneousLights, this._disableLighting);
  429. // Textures
  430. if (defines._areTexturesDirty) {
  431. defines._needUVs = false;
  432. if (scene.texturesEnabled) {
  433. if (this._diffuseTexture && StandardMaterial_OldVer.DiffuseTextureEnabled) {
  434. if (!this._diffuseTexture.isReadyOrNotBlocking()) {
  435. return false;
  436. } else {
  437. defines._needUVs = true;
  438. defines.DIFFUSE = true;
  439. }
  440. } else {
  441. defines.DIFFUSE = false;
  442. }
  443. if (this._ambientTexture && StandardMaterial_OldVer.AmbientTextureEnabled) {
  444. if (!this._ambientTexture.isReadyOrNotBlocking()) {
  445. return false;
  446. } else {
  447. defines._needUVs = true;
  448. defines.AMBIENT = true;
  449. }
  450. } else {
  451. defines.AMBIENT = false;
  452. }
  453. if (this._opacityTexture && StandardMaterial_OldVer.OpacityTextureEnabled) {
  454. if (!this._opacityTexture.isReadyOrNotBlocking()) {
  455. return false;
  456. } else {
  457. defines._needUVs = true;
  458. defines.OPACITY = true;
  459. defines.OPACITYRGB = this._opacityTexture.getAlphaFromRGB;
  460. }
  461. } else {
  462. defines.OPACITY = false;
  463. }
  464. if (this._reflectionTexture && StandardMaterial_OldVer.ReflectionTextureEnabled) {
  465. if (!this._reflectionTexture.isReadyOrNotBlocking()) {
  466. return false;
  467. } else {
  468. defines._needNormals = true;
  469. defines.REFLECTION = true;
  470. defines.ROUGHNESS = (this._roughness > 0);
  471. defines.REFLECTIONOVERALPHA = this._useReflectionOverAlpha;
  472. defines.INVERTCUBICMAP = (this._reflectionTexture.coordinatesMode === Texture.INVCUBIC_MODE);
  473. defines.REFLECTIONMAP_3D = this._reflectionTexture.isCube;
  474. switch (this._reflectionTexture.coordinatesMode) {
  475. case Texture.CUBIC_MODE:
  476. case Texture.INVCUBIC_MODE:
  477. defines.setReflectionMode("REFLECTIONMAP_CUBIC");
  478. break;
  479. case Texture.EXPLICIT_MODE:
  480. defines.setReflectionMode("REFLECTIONMAP_EXPLICIT");
  481. break;
  482. case Texture.PLANAR_MODE:
  483. defines.setReflectionMode("REFLECTIONMAP_PLANAR");
  484. break;
  485. case Texture.PROJECTION_MODE:
  486. defines.setReflectionMode("REFLECTIONMAP_PROJECTION");
  487. break;
  488. case Texture.SKYBOX_MODE:
  489. defines.setReflectionMode("REFLECTIONMAP_SKYBOX");
  490. break;
  491. case Texture.SPHERICAL_MODE:
  492. defines.setReflectionMode("REFLECTIONMAP_SPHERICAL");
  493. break;
  494. case Texture.EQUIRECTANGULAR_MODE:
  495. defines.setReflectionMode("REFLECTIONMAP_EQUIRECTANGULAR");
  496. break;
  497. case Texture.FIXED_EQUIRECTANGULAR_MODE:
  498. defines.setReflectionMode("REFLECTIONMAP_EQUIRECTANGULAR_FIXED");
  499. break;
  500. case Texture.FIXED_EQUIRECTANGULAR_MIRRORED_MODE:
  501. defines.setReflectionMode("REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED");
  502. break;
  503. }
  504. }
  505. } else {
  506. defines.REFLECTION = false;
  507. }
  508. if (this._emissiveTexture && StandardMaterial_OldVer.EmissiveTextureEnabled) {
  509. if (!this._emissiveTexture.isReadyOrNotBlocking()) {
  510. return false;
  511. } else {
  512. defines._needUVs = true;
  513. defines.EMISSIVE = true;
  514. }
  515. } else {
  516. defines.EMISSIVE = false;
  517. }
  518. if (this._lightmapTexture && StandardMaterial_OldVer.LightmapTextureEnabled) {
  519. if (!this._lightmapTexture.isReadyOrNotBlocking()) {
  520. return false;
  521. } else {
  522. defines._needUVs = true;
  523. defines.LIGHTMAP = true;
  524. defines.USELIGHTMAPASSHADOWMAP = this._useLightmapAsShadowmap;
  525. }
  526. } else {
  527. defines.LIGHTMAP = false;
  528. }
  529. if (this._specularTexture && StandardMaterial_OldVer.SpecularTextureEnabled) {
  530. if (!this._specularTexture.isReadyOrNotBlocking()) {
  531. return false;
  532. } else {
  533. defines._needUVs = true;
  534. defines.SPECULAR = true;
  535. defines.GLOSSINESS = this._useGlossinessFromSpecularMapAlpha;
  536. }
  537. } else {
  538. defines.SPECULAR = false;
  539. }
  540. if (scene.getEngine().getCaps().standardDerivatives && this._bumpTexture && StandardMaterial_OldVer.BumpTextureEnabled) {
  541. // Bump texure can not be not blocking.
  542. if (!this._bumpTexture.isReady()) {
  543. return false;
  544. } else {
  545. defines._needUVs = true;
  546. defines.BUMP = true;
  547. defines.PARALLAX = this._useParallax;
  548. defines.PARALLAXOCCLUSION = this._useParallaxOcclusion;
  549. }
  550. } else {
  551. defines.BUMP = false;
  552. }
  553. if (this._refractionTexture && StandardMaterial_OldVer.RefractionTextureEnabled) {
  554. if (!this._refractionTexture.isReadyOrNotBlocking()) {
  555. return false;
  556. } else {
  557. defines._needUVs = true;
  558. defines.REFRACTION = true;
  559. defines.REFRACTIONMAP_3D = this._refractionTexture.isCube;
  560. }
  561. } else {
  562. defines.REFRACTION = false;
  563. }
  564. defines.TWOSIDEDLIGHTING = !this._backFaceCulling && this._twoSidedLighting;
  565. } else {
  566. defines.DIFFUSE = false;
  567. defines.AMBIENT = false;
  568. defines.OPACITY = false;
  569. defines.REFLECTION = false;
  570. defines.EMISSIVE = false;
  571. defines.LIGHTMAP = false;
  572. defines.BUMP = false;
  573. defines.REFRACTION = false;
  574. }
  575. defines.ALPHAFROMDIFFUSE = this._shouldUseAlphaFromDiffuseTexture();
  576. defines.EMISSIVEASILLUMINATION = this._useEmissiveAsIllumination;
  577. defines.LINKEMISSIVEWITHDIFFUSE = this._linkEmissiveWithDiffuse;
  578. defines.SPECULAROVERALPHA = this._useSpecularOverAlpha;
  579. }
  580. if (defines._areImageProcessingDirty) {
  581. if (!this._imageProcessingConfiguration.isReady()) {
  582. return false;
  583. }
  584. this._imageProcessingConfiguration.prepareDefines(defines);
  585. }
  586. if (defines._areFresnelDirty) {
  587. if (StandardMaterial_OldVer.FresnelEnabled) {
  588. // Fresnel
  589. if (this._diffuseFresnelParameters || this._opacityFresnelParameters ||
  590. this._emissiveFresnelParameters || this._refractionFresnelParameters ||
  591. this._reflectionFresnelParameters) {
  592. defines.DIFFUSEFRESNEL = (this._diffuseFresnelParameters && this._diffuseFresnelParameters.isEnabled);
  593. defines.OPACITYFRESNEL = (this._opacityFresnelParameters && this._opacityFresnelParameters.isEnabled);
  594. defines.REFLECTIONFRESNEL = (this._reflectionFresnelParameters && this._reflectionFresnelParameters.isEnabled);
  595. defines.REFLECTIONFRESNELFROMSPECULAR = this._useReflectionFresnelFromSpecular;
  596. defines.REFRACTIONFRESNEL = (this._refractionFresnelParameters && this._refractionFresnelParameters.isEnabled) ;
  597. defines.EMISSIVEFRESNEL = (this._emissiveFresnelParameters && this._emissiveFresnelParameters.isEnabled) ;
  598. defines._needNormals = true;
  599. defines.FRESNEL = true;
  600. }
  601. } else {
  602. defines.FRESNEL = false;
  603. }
  604. }
  605. // Misc.
  606. MaterialHelper.PrepareDefinesForMisc(mesh, scene, this._useLogarithmicDepth, this.pointsCloud, this.fogEnabled, defines);
  607. // Attribs
  608. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true, true);
  609. // Values that need to be evaluated on every frame
  610. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances);
  611. // Get correct effect
  612. if (defines.isDirty) {
  613. defines.markAsProcessed();
  614. scene.resetCachedMaterial();
  615. // Fallbacks
  616. var fallbacks = new EffectFallbacks();
  617. if (defines.REFLECTION) {
  618. fallbacks.addFallback(0, "REFLECTION");
  619. }
  620. if (defines.SPECULAR) {
  621. fallbacks.addFallback(0, "SPECULAR");
  622. }
  623. if (defines.BUMP) {
  624. fallbacks.addFallback(0, "BUMP");
  625. }
  626. if (defines.PARALLAX) {
  627. fallbacks.addFallback(1, "PARALLAX");
  628. }
  629. if (defines.PARALLAXOCCLUSION) {
  630. fallbacks.addFallback(0, "PARALLAXOCCLUSION");
  631. }
  632. if (defines.SPECULAROVERALPHA) {
  633. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  634. }
  635. if (defines.FOG) {
  636. fallbacks.addFallback(1, "FOG");
  637. }
  638. if (defines.POINTSIZE) {
  639. fallbacks.addFallback(0, "POINTSIZE");
  640. }
  641. if (defines.LOGARITHMICDEPTH) {
  642. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  643. }
  644. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this._maxSimultaneousLights);
  645. if (defines.SPECULARTERM) {
  646. fallbacks.addFallback(0, "SPECULARTERM");
  647. }
  648. if (defines.DIFFUSEFRESNEL) {
  649. fallbacks.addFallback(1, "DIFFUSEFRESNEL");
  650. }
  651. if (defines.OPACITYFRESNEL) {
  652. fallbacks.addFallback(2, "OPACITYFRESNEL");
  653. }
  654. if (defines.REFLECTIONFRESNEL) {
  655. fallbacks.addFallback(3, "REFLECTIONFRESNEL");
  656. }
  657. if (defines.EMISSIVEFRESNEL) {
  658. fallbacks.addFallback(4, "EMISSIVEFRESNEL");
  659. }
  660. if (defines.FRESNEL) {
  661. fallbacks.addFallback(4, "FRESNEL");
  662. }
  663. //Attributes
  664. var attribs = [VertexBuffer.PositionKind];
  665. if (defines.NORMAL) {
  666. attribs.push(VertexBuffer.NormalKind);
  667. }
  668. if (defines.UV1) {
  669. attribs.push(VertexBuffer.UVKind);
  670. }
  671. if (defines.UV2) {
  672. attribs.push(VertexBuffer.UV2Kind);
  673. }
  674. if (defines.VERTEXCOLOR) {
  675. attribs.push(VertexBuffer.ColorKind);
  676. }
  677. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  678. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  679. MaterialHelper.PrepareAttributesForMorphTargets(attribs, mesh, defines);
  680. var shaderName = "default";
  681. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vDiffuseColor", "vSpecularColor", "vEmissiveColor",
  682. "vFogInfos", "vFogColor", "pointSize",
  683. "vDiffuseInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vSpecularInfos", "vBumpInfos", "vLightmapInfos", "vRefractionInfos",
  684. "mBones",
  685. "vClipPlane", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix", "lightmapMatrix", "refractionMatrix",
  686. "diffuseLeftColor", "diffuseRightColor", "opacityParts", "reflectionLeftColor", "reflectionRightColor", "emissiveLeftColor", "emissiveRightColor", "refractionLeftColor", "refractionRightColor",
  687. "logarithmicDepthConstant", "vNormalReorderParams"
  688. ];
  689. var samplers = ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler", "lightmapSampler", "refractionCubeSampler", "refraction2DSampler"]
  690. var uniformBuffers = ["Material", "Scene"];
  691. ImageProcessingConfiguration.PrepareUniforms(uniforms, defines);
  692. ImageProcessingConfiguration.PrepareSamplers(samplers, defines);
  693. MaterialHelper.PrepareUniformsAndSamplersList(<EffectCreationOptions>{
  694. uniformsNames: uniforms,
  695. uniformBuffersNames: uniformBuffers,
  696. samplers: samplers,
  697. defines: defines,
  698. maxSimultaneousLights: this._maxSimultaneousLights
  699. });
  700. if (this.customShaderNameResolve) {
  701. shaderName = this.customShaderNameResolve(shaderName, uniforms, uniformBuffers, samplers, defines);
  702. }
  703. var join = defines.toString();
  704. subMesh.setEffect(scene.getEngine().createEffect(shaderName, <EffectCreationOptions>{
  705. attributes: attribs,
  706. uniformsNames: uniforms,
  707. uniformBuffersNames: uniformBuffers,
  708. samplers: samplers,
  709. defines: join,
  710. fallbacks: fallbacks,
  711. onCompiled: this.onCompiled,
  712. onError: this.onError,
  713. indexParameters: { maxSimultaneousLights: this._maxSimultaneousLights, maxSimultaneousMorphTargets: defines.NUM_MORPH_INFLUENCERS }
  714. }, engine), defines);
  715. this.buildUniformLayout();
  716. }
  717. if (!subMesh.effect.isReady()) {
  718. return false;
  719. }
  720. defines._renderId = scene.getRenderId();
  721. this._wasPreviouslyReady = true;
  722. return true;
  723. }
  724. public buildUniformLayout(): void {
  725. // Order is important !
  726. this._uniformBuffer.addUniform("diffuseLeftColor", 4);
  727. this._uniformBuffer.addUniform("diffuseRightColor", 4);
  728. this._uniformBuffer.addUniform("opacityParts", 4);
  729. this._uniformBuffer.addUniform("reflectionLeftColor", 4);
  730. this._uniformBuffer.addUniform("reflectionRightColor", 4);
  731. this._uniformBuffer.addUniform("refractionLeftColor", 4);
  732. this._uniformBuffer.addUniform("refractionRightColor", 4);
  733. this._uniformBuffer.addUniform("emissiveLeftColor", 4);
  734. this._uniformBuffer.addUniform("emissiveRightColor", 4);
  735. this._uniformBuffer.addUniform("vDiffuseInfos", 2);
  736. this._uniformBuffer.addUniform("vAmbientInfos", 2);
  737. this._uniformBuffer.addUniform("vOpacityInfos", 2);
  738. this._uniformBuffer.addUniform("vReflectionInfos", 2);
  739. this._uniformBuffer.addUniform("vEmissiveInfos", 2);
  740. this._uniformBuffer.addUniform("vLightmapInfos", 2);
  741. this._uniformBuffer.addUniform("vSpecularInfos", 2);
  742. this._uniformBuffer.addUniform("vBumpInfos", 3);
  743. this._uniformBuffer.addUniform("diffuseMatrix", 16);
  744. this._uniformBuffer.addUniform("ambientMatrix", 16);
  745. this._uniformBuffer.addUniform("opacityMatrix", 16);
  746. this._uniformBuffer.addUniform("reflectionMatrix", 16);
  747. this._uniformBuffer.addUniform("emissiveMatrix", 16);
  748. this._uniformBuffer.addUniform("lightmapMatrix", 16);
  749. this._uniformBuffer.addUniform("specularMatrix", 16);
  750. this._uniformBuffer.addUniform("bumpMatrix", 16);
  751. this._uniformBuffer.addUniform("vNormalReorderParams", 4);
  752. this._uniformBuffer.addUniform("refractionMatrix", 16);
  753. this._uniformBuffer.addUniform("vRefractionInfos", 4);
  754. this._uniformBuffer.addUniform("vSpecularColor", 4);
  755. this._uniformBuffer.addUniform("vEmissiveColor", 3);
  756. this._uniformBuffer.addUniform("vDiffuseColor", 4);
  757. this._uniformBuffer.addUniform("pointSize", 1);
  758. this._uniformBuffer.create();
  759. }
  760. public unbind(): void {
  761. if (this._activeEffect) {
  762. if (this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  763. this._activeEffect.setTexture("reflection2DSampler", null);
  764. }
  765. if (this._refractionTexture && this._refractionTexture.isRenderTarget) {
  766. this._activeEffect.setTexture("refraction2DSampler", null);
  767. }
  768. }
  769. super.unbind();
  770. }
  771. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  772. var scene = this.getScene();
  773. var defines = <StandardMaterialDefines_OldVer>subMesh._materialDefines;
  774. if (!defines) {
  775. return;
  776. }
  777. var effect = subMesh.effect;
  778. this._activeEffect = effect;
  779. // Matrices
  780. this.bindOnlyWorldMatrix(world);
  781. // Bones
  782. MaterialHelper.BindBonesParameters(mesh, effect);
  783. if (this._mustRebind(scene, effect, mesh.visibility)) {
  784. this._uniformBuffer.bindToEffect(effect, "Material");
  785. this.bindViewProjection(effect);
  786. if (!this._uniformBuffer.useUbo || !this.isFrozen || !this._uniformBuffer.isSync) {
  787. if (StandardMaterial_OldVer.FresnelEnabled && defines.FRESNEL) {
  788. // Fresnel
  789. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  790. this._uniformBuffer.updateColor4("diffuseLeftColor", this.diffuseFresnelParameters.leftColor, this.diffuseFresnelParameters.power);
  791. this._uniformBuffer.updateColor4("diffuseRightColor", this.diffuseFresnelParameters.rightColor, this.diffuseFresnelParameters.bias);
  792. }
  793. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  794. this._uniformBuffer.updateColor4("opacityParts", new Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  795. }
  796. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  797. this._uniformBuffer.updateColor4("reflectionLeftColor", this.reflectionFresnelParameters.leftColor, this.reflectionFresnelParameters.power);
  798. this._uniformBuffer.updateColor4("reflectionRightColor", this.reflectionFresnelParameters.rightColor, this.reflectionFresnelParameters.bias);
  799. }
  800. if (this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled) {
  801. this._uniformBuffer.updateColor4("refractionLeftColor", this.refractionFresnelParameters.leftColor, this.refractionFresnelParameters.power);
  802. this._uniformBuffer.updateColor4("refractionRightColor", this.refractionFresnelParameters.rightColor, this.refractionFresnelParameters.bias);
  803. }
  804. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  805. this._uniformBuffer.updateColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  806. this._uniformBuffer.updateColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  807. }
  808. }
  809. // Textures
  810. if (scene.texturesEnabled) {
  811. if (this._diffuseTexture && StandardMaterial_OldVer.DiffuseTextureEnabled) {
  812. this._uniformBuffer.updateFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  813. this._uniformBuffer.updateMatrix("diffuseMatrix", this._diffuseTexture.getTextureMatrix());
  814. }
  815. if (this._ambientTexture && StandardMaterial_OldVer.AmbientTextureEnabled) {
  816. this._uniformBuffer.updateFloat2("vAmbientInfos", this._ambientTexture.coordinatesIndex, this._ambientTexture.level);
  817. this._uniformBuffer.updateMatrix("ambientMatrix", this._ambientTexture.getTextureMatrix());
  818. }
  819. if (this._opacityTexture && StandardMaterial_OldVer.OpacityTextureEnabled) {
  820. this._uniformBuffer.updateFloat2("vOpacityInfos", this._opacityTexture.coordinatesIndex, this._opacityTexture.level);
  821. this._uniformBuffer.updateMatrix("opacityMatrix", this._opacityTexture.getTextureMatrix());
  822. }
  823. if (this._reflectionTexture && StandardMaterial_OldVer.ReflectionTextureEnabled) {
  824. this._uniformBuffer.updateFloat2("vReflectionInfos", this._reflectionTexture.level, this.roughness);
  825. this._uniformBuffer.updateMatrix("reflectionMatrix", this._reflectionTexture.getReflectionTextureMatrix());
  826. }
  827. if (this._emissiveTexture && StandardMaterial_OldVer.EmissiveTextureEnabled) {
  828. this._uniformBuffer.updateFloat2("vEmissiveInfos", this._emissiveTexture.coordinatesIndex, this._emissiveTexture.level);
  829. this._uniformBuffer.updateMatrix("emissiveMatrix", this._emissiveTexture.getTextureMatrix());
  830. }
  831. if (this._lightmapTexture && StandardMaterial_OldVer.LightmapTextureEnabled) {
  832. this._uniformBuffer.updateFloat2("vLightmapInfos", this._lightmapTexture.coordinatesIndex, this._lightmapTexture.level);
  833. this._uniformBuffer.updateMatrix("lightmapMatrix", this._lightmapTexture.getTextureMatrix());
  834. }
  835. if (this._specularTexture && StandardMaterial_OldVer.SpecularTextureEnabled) {
  836. this._uniformBuffer.updateFloat2("vSpecularInfos", this._specularTexture.coordinatesIndex, this._specularTexture.level);
  837. this._uniformBuffer.updateMatrix("specularMatrix", this._specularTexture.getTextureMatrix());
  838. }
  839. if (this._bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial_OldVer.BumpTextureEnabled) {
  840. this._uniformBuffer.updateFloat3("vBumpInfos", this._bumpTexture.coordinatesIndex, 1.0 / this._bumpTexture.level, this.parallaxScaleBias);
  841. this._uniformBuffer.updateMatrix("bumpMatrix", this._bumpTexture.getTextureMatrix());
  842. if (scene._mirroredCameraPosition) {
  843. this._uniformBuffer.updateFloat4("vNormalReorderParams", this.invertNormalMapX ? 0 : 1.0, this.invertNormalMapX ? 1.0 : -1.0, this.invertNormalMapY ? 0 : 1.0, this.invertNormalMapY ? 1.0 : -1.0);
  844. } else {
  845. this._uniformBuffer.updateFloat4("vNormalReorderParams", this.invertNormalMapX ? 1.0 : 0, this.invertNormalMapX ? -1.0 : 1.0, this.invertNormalMapY ? 1.0 : 0, this.invertNormalMapY ? -1.0 : 1.0);
  846. }
  847. }
  848. if (this._refractionTexture && StandardMaterial_OldVer.RefractionTextureEnabled) {
  849. var depth = 1.0;
  850. if (!this._refractionTexture.isCube) {
  851. this._uniformBuffer.updateMatrix("refractionMatrix", this._refractionTexture.getReflectionTextureMatrix());
  852. if ((<any>this._refractionTexture).depth) {
  853. depth = (<any>this._refractionTexture).depth;
  854. }
  855. }
  856. this._uniformBuffer.updateFloat4("vRefractionInfos", this._refractionTexture.level, this.indexOfRefraction, depth, this.invertRefractionY ? -1 : 1);
  857. }
  858. }
  859. // Point size
  860. if (this.pointsCloud) {
  861. this._uniformBuffer.updateFloat("pointSize", this.pointSize);
  862. }
  863. if (defines.SPECULARTERM) {
  864. this._uniformBuffer.updateColor4("vSpecularColor", this.specularColor, this.specularPower);
  865. }
  866. this._uniformBuffer.updateColor3("vEmissiveColor", this.emissiveColor);
  867. // Diffuse
  868. this._uniformBuffer.updateColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  869. }
  870. // Textures
  871. if (scene.texturesEnabled) {
  872. if (this._diffuseTexture && StandardMaterial_OldVer.DiffuseTextureEnabled) {
  873. effect.setTexture("diffuseSampler", this._diffuseTexture);
  874. }
  875. if (this._ambientTexture && StandardMaterial_OldVer.AmbientTextureEnabled) {
  876. effect.setTexture("ambientSampler", this._ambientTexture);
  877. }
  878. if (this._opacityTexture && StandardMaterial_OldVer.OpacityTextureEnabled) {
  879. effect.setTexture("opacitySampler", this._opacityTexture);
  880. }
  881. if (this._reflectionTexture && StandardMaterial_OldVer.ReflectionTextureEnabled) {
  882. if (this._reflectionTexture.isCube) {
  883. effect.setTexture("reflectionCubeSampler", this._reflectionTexture);
  884. } else {
  885. effect.setTexture("reflection2DSampler", this._reflectionTexture);
  886. }
  887. }
  888. if (this._emissiveTexture && StandardMaterial_OldVer.EmissiveTextureEnabled) {
  889. effect.setTexture("emissiveSampler", this._emissiveTexture);
  890. }
  891. if (this._lightmapTexture && StandardMaterial_OldVer.LightmapTextureEnabled) {
  892. effect.setTexture("lightmapSampler", this._lightmapTexture);
  893. }
  894. if (this._specularTexture && StandardMaterial_OldVer.SpecularTextureEnabled) {
  895. effect.setTexture("specularSampler", this._specularTexture);
  896. }
  897. if (this._bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial_OldVer.BumpTextureEnabled) {
  898. effect.setTexture("bumpSampler", this._bumpTexture);
  899. }
  900. if (this._refractionTexture && StandardMaterial_OldVer.RefractionTextureEnabled) {
  901. var depth = 1.0;
  902. if (this._refractionTexture.isCube) {
  903. effect.setTexture("refractionCubeSampler", this._refractionTexture);
  904. } else {
  905. effect.setTexture("refraction2DSampler", this._refractionTexture);
  906. }
  907. }
  908. }
  909. // Clip plane
  910. MaterialHelper.BindClipPlane(effect, scene);
  911. // Colors
  912. scene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  913. effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  914. effect.setColor3("vAmbientColor", this._globalAmbientColor);
  915. }
  916. if (this._mustRebind(scene, effect) || !this.isFrozen) {
  917. // Lights
  918. if (scene.lightsEnabled && !this._disableLighting) {
  919. MaterialHelper.BindLights(scene, mesh, effect, defines, this._maxSimultaneousLights);
  920. }
  921. // View
  922. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE || this._reflectionTexture || this._refractionTexture) {
  923. this.bindView(effect);
  924. }
  925. // Fog
  926. MaterialHelper.BindFogParameters(scene, mesh, effect);
  927. // Morph targets
  928. if (defines.NUM_MORPH_INFLUENCERS) {
  929. MaterialHelper.BindMorphTargetParameters(mesh, effect);
  930. }
  931. // Log. depth
  932. MaterialHelper.BindLogDepth(defines, effect, scene);
  933. // image processing
  934. this._imageProcessingConfiguration.bind(this._activeEffect);
  935. }
  936. this._uniformBuffer.update();
  937. this._afterBind(mesh, this._activeEffect);
  938. }
  939. public getAnimatables(): IAnimatable[] {
  940. var results = [];
  941. if (this._diffuseTexture && this._diffuseTexture.animations && this._diffuseTexture.animations.length > 0) {
  942. results.push(this._diffuseTexture);
  943. }
  944. if (this._ambientTexture && this._ambientTexture.animations && this._ambientTexture.animations.length > 0) {
  945. results.push(this._ambientTexture);
  946. }
  947. if (this._opacityTexture && this._opacityTexture.animations && this._opacityTexture.animations.length > 0) {
  948. results.push(this._opacityTexture);
  949. }
  950. if (this._reflectionTexture && this._reflectionTexture.animations && this._reflectionTexture.animations.length > 0) {
  951. results.push(this._reflectionTexture);
  952. }
  953. if (this._emissiveTexture && this._emissiveTexture.animations && this._emissiveTexture.animations.length > 0) {
  954. results.push(this._emissiveTexture);
  955. }
  956. if (this._specularTexture && this._specularTexture.animations && this._specularTexture.animations.length > 0) {
  957. results.push(this._specularTexture);
  958. }
  959. if (this._bumpTexture && this._bumpTexture.animations && this._bumpTexture.animations.length > 0) {
  960. results.push(this._bumpTexture);
  961. }
  962. if (this._lightmapTexture && this._lightmapTexture.animations && this._lightmapTexture.animations.length > 0) {
  963. results.push(this._lightmapTexture);
  964. }
  965. if (this._refractionTexture && this._refractionTexture.animations && this._refractionTexture.animations.length > 0) {
  966. results.push(this._refractionTexture);
  967. }
  968. return results;
  969. }
  970. public getActiveTextures(): BaseTexture[] {
  971. var activeTextures = super.getActiveTextures();
  972. if (this._diffuseTexture) {
  973. activeTextures.push(this._diffuseTexture);
  974. }
  975. if (this._ambientTexture) {
  976. activeTextures.push(this._ambientTexture);
  977. }
  978. if (this._opacityTexture) {
  979. activeTextures.push(this._opacityTexture);
  980. }
  981. if (this._reflectionTexture) {
  982. activeTextures.push(this._reflectionTexture);
  983. }
  984. if (this._emissiveTexture) {
  985. activeTextures.push(this._emissiveTexture);
  986. }
  987. if (this._specularTexture) {
  988. activeTextures.push(this._specularTexture);
  989. }
  990. if (this._bumpTexture) {
  991. activeTextures.push(this._bumpTexture);
  992. }
  993. if (this._lightmapTexture) {
  994. activeTextures.push(this._lightmapTexture);
  995. }
  996. if (this._refractionTexture) {
  997. activeTextures.push(this._refractionTexture);
  998. }
  999. return activeTextures;
  1000. }
  1001. public dispose(forceDisposeEffect?: boolean, forceDisposeTextures?: boolean): void {
  1002. if (forceDisposeTextures) {
  1003. if (this._diffuseTexture) {
  1004. this._diffuseTexture.dispose();
  1005. }
  1006. if (this._ambientTexture) {
  1007. this._ambientTexture.dispose();
  1008. }
  1009. if (this._opacityTexture) {
  1010. this._opacityTexture.dispose();
  1011. }
  1012. if (this._reflectionTexture) {
  1013. this._reflectionTexture.dispose();
  1014. }
  1015. if (this._emissiveTexture) {
  1016. this._emissiveTexture.dispose();
  1017. }
  1018. if (this._specularTexture) {
  1019. this._specularTexture.dispose();
  1020. }
  1021. if (this._bumpTexture) {
  1022. this._bumpTexture.dispose();
  1023. }
  1024. if (this._lightmapTexture) {
  1025. this._lightmapTexture.dispose();
  1026. }
  1027. if (this._refractionTexture) {
  1028. this._refractionTexture.dispose();
  1029. }
  1030. }
  1031. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  1032. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  1033. }
  1034. super.dispose(forceDisposeEffect, forceDisposeTextures);
  1035. }
  1036. public clone(name: string): StandardMaterial_OldVer {
  1037. var result = SerializationHelper.Clone(() => new StandardMaterial_OldVer(name, this.getScene()), this);
  1038. result.name = name;
  1039. result.id = name;
  1040. return result;
  1041. }
  1042. public serialize(): any {
  1043. return SerializationHelper.Serialize(this);
  1044. }
  1045. // Statics
  1046. public static Parse(source: any, scene: Scene, rootUrl: string): StandardMaterial_OldVer {
  1047. return SerializationHelper.Parse(() => new StandardMaterial_OldVer(source.name, scene), source, scene, rootUrl);
  1048. }
  1049. // Flags used to enable or disable a type of texture for all Standard Materials
  1050. static _DiffuseTextureEnabled = true;
  1051. public static get DiffuseTextureEnabled(): boolean {
  1052. return StandardMaterial_OldVer._DiffuseTextureEnabled;
  1053. }
  1054. public static set DiffuseTextureEnabled(value: boolean) {
  1055. if (StandardMaterial_OldVer._DiffuseTextureEnabled === value) {
  1056. return;
  1057. }
  1058. StandardMaterial_OldVer._DiffuseTextureEnabled = value;
  1059. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1060. }
  1061. static _AmbientTextureEnabled = true;
  1062. public static get AmbientTextureEnabled(): boolean {
  1063. return StandardMaterial_OldVer._AmbientTextureEnabled;
  1064. }
  1065. public static set AmbientTextureEnabled(value: boolean) {
  1066. if (StandardMaterial_OldVer._AmbientTextureEnabled === value) {
  1067. return;
  1068. }
  1069. StandardMaterial_OldVer._AmbientTextureEnabled = value;
  1070. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1071. }
  1072. static _OpacityTextureEnabled = true;
  1073. public static get OpacityTextureEnabled(): boolean {
  1074. return StandardMaterial_OldVer._OpacityTextureEnabled;
  1075. }
  1076. public static set OpacityTextureEnabled(value: boolean) {
  1077. if (StandardMaterial_OldVer._OpacityTextureEnabled === value) {
  1078. return;
  1079. }
  1080. StandardMaterial_OldVer._OpacityTextureEnabled = value;
  1081. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1082. }
  1083. static _ReflectionTextureEnabled = true;
  1084. public static get ReflectionTextureEnabled(): boolean {
  1085. return StandardMaterial_OldVer._ReflectionTextureEnabled;
  1086. }
  1087. public static set ReflectionTextureEnabled(value: boolean) {
  1088. if (StandardMaterial_OldVer._ReflectionTextureEnabled === value) {
  1089. return;
  1090. }
  1091. StandardMaterial_OldVer._ReflectionTextureEnabled = value;
  1092. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1093. }
  1094. static _EmissiveTextureEnabled = true;
  1095. public static get EmissiveTextureEnabled(): boolean {
  1096. return StandardMaterial_OldVer._EmissiveTextureEnabled;
  1097. }
  1098. public static set EmissiveTextureEnabled(value: boolean) {
  1099. if (StandardMaterial_OldVer._EmissiveTextureEnabled === value) {
  1100. return;
  1101. }
  1102. StandardMaterial_OldVer._EmissiveTextureEnabled = value;
  1103. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1104. }
  1105. static _SpecularTextureEnabled = true;
  1106. public static get SpecularTextureEnabled(): boolean {
  1107. return StandardMaterial_OldVer._SpecularTextureEnabled;
  1108. }
  1109. public static set SpecularTextureEnabled(value: boolean) {
  1110. if (StandardMaterial_OldVer._SpecularTextureEnabled === value) {
  1111. return;
  1112. }
  1113. StandardMaterial_OldVer._SpecularTextureEnabled = value;
  1114. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1115. }
  1116. static _BumpTextureEnabled = true;
  1117. public static get BumpTextureEnabled(): boolean {
  1118. return StandardMaterial_OldVer._BumpTextureEnabled;
  1119. }
  1120. public static set BumpTextureEnabled(value: boolean) {
  1121. if (StandardMaterial_OldVer._BumpTextureEnabled === value) {
  1122. return;
  1123. }
  1124. StandardMaterial_OldVer._BumpTextureEnabled = value;
  1125. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1126. }
  1127. static _LightmapTextureEnabled = true;
  1128. public static get LightmapTextureEnabled(): boolean {
  1129. return StandardMaterial_OldVer._LightmapTextureEnabled;
  1130. }
  1131. public static set LightmapTextureEnabled(value: boolean) {
  1132. if (StandardMaterial_OldVer._LightmapTextureEnabled === value) {
  1133. return;
  1134. }
  1135. StandardMaterial_OldVer._LightmapTextureEnabled = value;
  1136. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1137. }
  1138. static _RefractionTextureEnabled = true;
  1139. public static get RefractionTextureEnabled(): boolean {
  1140. return StandardMaterial_OldVer._RefractionTextureEnabled;
  1141. }
  1142. public static set RefractionTextureEnabled(value: boolean) {
  1143. if (StandardMaterial_OldVer._RefractionTextureEnabled === value) {
  1144. return;
  1145. }
  1146. StandardMaterial_OldVer._RefractionTextureEnabled = value;
  1147. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1148. }
  1149. static _ColorGradingTextureEnabled = true;
  1150. public static get ColorGradingTextureEnabled(): boolean {
  1151. return StandardMaterial_OldVer._ColorGradingTextureEnabled;
  1152. }
  1153. public static set ColorGradingTextureEnabled(value: boolean) {
  1154. if (StandardMaterial_OldVer._ColorGradingTextureEnabled === value) {
  1155. return;
  1156. }
  1157. StandardMaterial_OldVer._ColorGradingTextureEnabled = value;
  1158. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1159. }
  1160. static _FresnelEnabled = true;
  1161. public static get FresnelEnabled(): boolean {
  1162. return StandardMaterial_OldVer._FresnelEnabled;
  1163. }
  1164. public static set FresnelEnabled(value: boolean) {
  1165. if (StandardMaterial_OldVer._FresnelEnabled === value) {
  1166. return;
  1167. }
  1168. StandardMaterial_OldVer._FresnelEnabled = value;
  1169. Engine.MarkAllMaterialsAsDirty(Material.FresnelDirtyFlag);
  1170. }
  1171. }
  1172. export class CustomShaderStructure {
  1173. public FragmentStore : string;
  1174. public VertexStore : string;
  1175. constructor(){
  1176. }
  1177. }
  1178. export class ShaderSpecialParts{
  1179. constructor(){}
  1180. public Fragment_Begin:string;
  1181. public Fragment_Definitions:string;
  1182. public Fragment_MainBegin: string;
  1183. // diffuseColor
  1184. public Fragment_Custom_Diffuse: string;
  1185. // alpha
  1186. public Fragment_Custom_Alpha : string;
  1187. public Fragment_Before_FragColor: string;
  1188. public Vertex_Begin:string;
  1189. public Vertex_Definitions:string;
  1190. public Vertex_MainBegin: string;
  1191. // positionUpdated
  1192. public Vertex_Before_PositionUpdated:string;
  1193. // normalUpdated
  1194. public Vertex_Before_NormalUpdated : string;
  1195. }
  1196. export class ShaderForVer3_0 extends CustomShaderStructure {
  1197. constructor() {
  1198. super();
  1199. this.VertexStore = "";
  1200. this.FragmentStore = "#include<__decl__defaultFragment>\n\
  1201. #[Fragment_Begin]\n\
  1202. #extension GL_OES_standard_derivatives : enable\n\
  1203. #ifdef LOGARITHMICDEPTH\n\
  1204. #extension GL_EXT_frag_depth : enable\n\
  1205. #endif\n\
  1206. \n\
  1207. #define RECIPROCAL_PI2 0.15915494\n\
  1208. uniform vec3 vEyePosition;\n\
  1209. uniform vec3 vAmbientColor;\n\
  1210. \n\
  1211. varying vec3 vPositionW;\n\
  1212. #ifdef NORMAL\n\
  1213. varying vec3 vNormalW_helper;\n\
  1214. varying vec3 localNormal;\n\
  1215. varying vec3 localPosition;\n\
  1216. vec3 vNormalW;\n\
  1217. #endif\n\
  1218. #ifdef VERTEXCOLOR\n\
  1219. varying vec4 vColor;\n\
  1220. #endif\n\
  1221. \n\
  1222. #include<helperFunctions>\n\
  1223. \n\
  1224. #include<__decl__lightFragment>[0..maxSimultaneousLights]\n\
  1225. #include<lightsFragmentFunctions>\n\
  1226. #include<shadowsFragmentFunctions>\n\
  1227. \n\
  1228. #ifdef DIFFUSE\n\
  1229. varying vec2 vDiffuseUV;\n\
  1230. uniform sampler2D diffuseSampler;\n\
  1231. #endif\n\
  1232. #ifdef AMBIENT\n\
  1233. varying vec2 vAmbientUV;\n\
  1234. uniform sampler2D ambientSampler;\n\
  1235. #endif\n\
  1236. #ifdef OPACITY\n\
  1237. varying vec2 vOpacityUV;\n\
  1238. uniform sampler2D opacitySampler;\n\
  1239. #endif\n\
  1240. #ifdef EMISSIVE\n\
  1241. varying vec2 vEmissiveUV;\n\
  1242. uniform sampler2D emissiveSampler;\n\
  1243. #endif\n\
  1244. #ifdef LIGHTMAP\n\
  1245. varying vec2 vLightmapUV;\n\
  1246. uniform sampler2D lightmapSampler;\n\
  1247. #endif\n\
  1248. #ifdef REFRACTION\n\
  1249. #ifdef REFRACTIONMAP_3D\n\
  1250. uniform samplerCube refractionCubeSampler;\n\
  1251. #else\n\
  1252. uniform sampler2D refraction2DSampler;\n\
  1253. #endif\n\
  1254. #endif\n\
  1255. #if defined(SPECULAR) && defined(SPECULARTERM)\n\
  1256. varying vec2 vSpecularUV;\n\
  1257. uniform sampler2D specularSampler;\n\
  1258. #endif\n\
  1259. \n\
  1260. #include<fresnelFunction>\n\
  1261. \n\
  1262. #ifdef REFLECTION\n\
  1263. #ifdef REFLECTIONMAP_3D\n\
  1264. uniform samplerCube reflectionCubeSampler;\n\
  1265. #else\n\
  1266. uniform sampler2D reflection2DSampler;\n\
  1267. #endif\n\
  1268. #ifdef REFLECTIONMAP_SKYBOX\n\
  1269. varying vec3 vPositionUVW;\n\
  1270. #else\n\
  1271. #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)\n\
  1272. varying vec3 vDirectionW;\n\
  1273. #endif\n\
  1274. #endif\n\
  1275. #include<reflectionFunction>\n\
  1276. #endif\n\
  1277. #include<imageProcessingDeclaration>\n\
  1278. #include<imageProcessingFunctions>\n\
  1279. \n\
  1280. #include<bumpFragmentFunctions>\n\
  1281. #include<clipPlaneFragmentDeclaration>\n\
  1282. #include<logDepthDeclaration>\n\
  1283. #include<fogFragmentDeclaration>\n\
  1284. \n\
  1285. #[Fragment_Definitions]\n\
  1286. \n\
  1287. void main(void) {\n\
  1288. \n\
  1289. vNormalW = vNormalW_helper;\n\
  1290. #[Fragment_MainBegin]\n\
  1291. \n\
  1292. #include<clipPlaneFragment>\n\
  1293. vec3 viewDirectionW=normalize(vEyePosition-vPositionW);\n\
  1294. \n\
  1295. vec4 baseColor=vec4(1.,1.,1.,1.);\n\
  1296. vec3 diffuseColor=vDiffuseColor.rgb;\n\
  1297. #[Fragment_Custom_Diffuse]\n\
  1298. \n\
  1299. float alpha=vDiffuseColor.a;\n\
  1300. #[Fragment_Custom_Alpha]\n\
  1301. \n\
  1302. #ifdef NORMAL\n\
  1303. vec3 normalW=normalize(vNormalW);\n\
  1304. #else\n\
  1305. vec3 normalW=vec3(1.0,1.0,1.0);\n\
  1306. #endif\n\
  1307. #include<bumpFragment>\n\
  1308. #ifdef TWOSIDEDLIGHTING\n\
  1309. normalW=gl_FrontFacing ? normalW : -normalW;\n\
  1310. #endif\n\
  1311. #ifdef DIFFUSE\n\
  1312. baseColor=texture2D(diffuseSampler,vDiffuseUV+uvOffset);\n\
  1313. #ifdef ALPHATEST\n\
  1314. if (baseColor.a<0.4)\n\
  1315. discard;\n\
  1316. #endif\n\
  1317. #ifdef ALPHAFROMDIFFUSE\n\
  1318. alpha*=baseColor.a;\n\
  1319. #endif\n\
  1320. baseColor.rgb*=vDiffuseInfos.y;\n\
  1321. #endif\n\
  1322. #ifdef VERTEXCOLOR\n\
  1323. baseColor.rgb*=vColor.rgb;\n\
  1324. #endif\n\
  1325. \n\
  1326. vec3 baseAmbientColor=vec3(1.,1.,1.);\n\
  1327. #ifdef AMBIENT\n\
  1328. baseAmbientColor=texture2D(ambientSampler,vAmbientUV+uvOffset).rgb*vAmbientInfos.y;\n\
  1329. #endif\n\
  1330. \n\
  1331. #ifdef SPECULARTERM\n\
  1332. float glossiness=vSpecularColor.a;\n\
  1333. vec3 specularColor=vSpecularColor.rgb;\n\
  1334. #ifdef SPECULAR\n\
  1335. vec4 specularMapColor=texture2D(specularSampler,vSpecularUV+uvOffset);\n\
  1336. specularColor=specularMapColor.rgb;\n\
  1337. #ifdef GLOSSINESS\n\
  1338. glossiness=glossiness*specularMapColor.a;\n\
  1339. #endif\n\
  1340. #endif\n\
  1341. #else\n\
  1342. float glossiness=0.;\n\
  1343. #endif\n\
  1344. \n\
  1345. vec3 diffuseBase=vec3(0.,0.,0.);\n\
  1346. lightingInfo info;\n\
  1347. #ifdef SPECULARTERM\n\
  1348. vec3 specularBase=vec3(0.,0.,0.);\n\
  1349. #endif\n\
  1350. float shadow=1.;\n\
  1351. #ifdef LIGHTMAP\n\
  1352. vec3 lightmapColor=texture2D(lightmapSampler,vLightmapUV+uvOffset).rgb*vLightmapInfos.y;\n\
  1353. #endif\n\
  1354. #include<lightFragment>[0..maxSimultaneousLights]\n\
  1355. \n\
  1356. vec3 refractionColor=vec3(0.,0.,0.);\n\
  1357. #ifdef REFRACTION\n\
  1358. vec3 refractionVector=normalize(refract(-viewDirectionW,normalW,vRefractionInfos.y));\n\
  1359. #ifdef REFRACTIONMAP_3D\n\
  1360. refractionVector.y=refractionVector.y*vRefractionInfos.w;\n\
  1361. if (dot(refractionVector,viewDirectionW)<1.0)\n\
  1362. {\n\
  1363. refractionColor=textureCube(refractionCubeSampler,refractionVector).rgb*vRefractionInfos.x;\n\
  1364. }\n\
  1365. #else\n\
  1366. vec3 vRefractionUVW=vec3(refractionMatrix*(view*vec4(vPositionW+refractionVector*vRefractionInfos.z,1.0)));\n\
  1367. vec2 refractionCoords=vRefractionUVW.xy/vRefractionUVW.z;\n\
  1368. refractionCoords.y=1.0-refractionCoords.y;\n\
  1369. refractionColor=texture2D(refraction2DSampler,refractionCoords).rgb*vRefractionInfos.x;\n\
  1370. #endif\n\
  1371. #endif\n\
  1372. \n\
  1373. vec3 reflectionColor=vec3(0.,0.,0.);\n\
  1374. #ifdef REFLECTION\n\
  1375. vec3 vReflectionUVW=computeReflectionCoords(vec4(vPositionW,1.0),normalW);\n\
  1376. #ifdef REFLECTIONMAP_3D\n\
  1377. #ifdef ROUGHNESS\n\
  1378. float bias=vReflectionInfos.y;\n\
  1379. #ifdef SPECULARTERM\n\
  1380. #ifdef SPECULAR\n\
  1381. #ifdef GLOSSINESS\n\
  1382. bias*=(1.0-specularMapColor.a);\n\
  1383. #endif\n\
  1384. #endif\n\
  1385. #endif\n\
  1386. reflectionColor=textureCube(reflectionCubeSampler,vReflectionUVW,bias).rgb*vReflectionInfos.x;\n\
  1387. #else\n\
  1388. reflectionColor=textureCube(reflectionCubeSampler,vReflectionUVW).rgb*vReflectionInfos.x;\n\
  1389. #endif\n\
  1390. #else\n\
  1391. vec2 coords=vReflectionUVW.xy;\n\
  1392. #ifdef REFLECTIONMAP_PROJECTION\n\
  1393. coords/=vReflectionUVW.z;\n\
  1394. #endif\n\
  1395. coords.y=1.0-coords.y;\n\
  1396. reflectionColor=texture2D(reflection2DSampler,coords).rgb*vReflectionInfos.x;\n\
  1397. #endif\n\
  1398. #ifdef REFLECTIONFRESNEL\n\
  1399. float reflectionFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,reflectionRightColor.a,reflectionLeftColor.a);\n\
  1400. #ifdef REFLECTIONFRESNELFROMSPECULAR\n\
  1401. #ifdef SPECULARTERM\n\
  1402. reflectionColor*=specularColor.rgb*(1.0-reflectionFresnelTerm)+reflectionFresnelTerm*reflectionRightColor.rgb;\n\
  1403. #else\n\
  1404. reflectionColor*=reflectionLeftColor.rgb*(1.0-reflectionFresnelTerm)+reflectionFresnelTerm*reflectionRightColor.rgb;\n\
  1405. #endif\n\
  1406. #else\n\
  1407. reflectionColor*=reflectionLeftColor.rgb*(1.0-reflectionFresnelTerm)+reflectionFresnelTerm*reflectionRightColor.rgb;\n\
  1408. #endif\n\
  1409. #endif\n\
  1410. #endif\n\
  1411. #ifdef REFRACTIONFRESNEL\n\
  1412. float refractionFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,refractionRightColor.a,refractionLeftColor.a);\n\
  1413. refractionColor*=refractionLeftColor.rgb*(1.0-refractionFresnelTerm)+refractionFresnelTerm*refractionRightColor.rgb;\n\
  1414. #endif\n\
  1415. #ifdef OPACITY\n\
  1416. vec4 opacityMap=texture2D(opacitySampler,vOpacityUV+uvOffset);\n\
  1417. #ifdef OPACITYRGB\n\
  1418. opacityMap.rgb=opacityMap.rgb*vec3(0.3,0.59,0.11);\n\
  1419. alpha*=(opacityMap.x+opacityMap.y+opacityMap.z)* vOpacityInfos.y;\n\
  1420. #else\n\
  1421. alpha*=opacityMap.a*vOpacityInfos.y;\n\
  1422. #endif\n\
  1423. #endif\n\
  1424. #ifdef VERTEXALPHA\n\
  1425. alpha*=vColor.a;\n\
  1426. #endif\n\
  1427. #ifdef OPACITYFRESNEL\n\
  1428. float opacityFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,opacityParts.z,opacityParts.w);\n\
  1429. alpha+=opacityParts.x*(1.0-opacityFresnelTerm)+opacityFresnelTerm*opacityParts.y;\n\
  1430. #endif\n\
  1431. \n\
  1432. vec3 emissiveColor=vEmissiveColor;\n\
  1433. #ifdef EMISSIVE\n\
  1434. emissiveColor+=texture2D(emissiveSampler,vEmissiveUV+uvOffset).rgb*vEmissiveInfos.y;\n\
  1435. #endif\n\
  1436. #ifdef EMISSIVEFRESNEL\n\
  1437. float emissiveFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,emissiveRightColor.a,emissiveLeftColor.a);\n\
  1438. emissiveColor*=emissiveLeftColor.rgb*(1.0-emissiveFresnelTerm)+emissiveFresnelTerm*emissiveRightColor.rgb;\n\
  1439. #endif\n\
  1440. \n\
  1441. #ifdef DIFFUSEFRESNEL\n\
  1442. float diffuseFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,diffuseRightColor.a,diffuseLeftColor.a);\n\
  1443. diffuseBase*=diffuseLeftColor.rgb*(1.0-diffuseFresnelTerm)+diffuseFresnelTerm*diffuseRightColor.rgb;\n\
  1444. #endif\n\
  1445. \n\
  1446. #ifdef EMISSIVEASILLUMINATION\n\
  1447. vec3 finalDiffuse=clamp(diffuseBase*diffuseColor+vAmbientColor,0.0,1.0)*baseColor.rgb;\n\
  1448. #else\n\
  1449. #ifdef LINKEMISSIVEWITHDIFFUSE\n\
  1450. vec3 finalDiffuse=clamp((diffuseBase+emissiveColor)*diffuseColor+vAmbientColor,0.0,1.0)*baseColor.rgb;\n\
  1451. #else\n\
  1452. vec3 finalDiffuse=clamp(diffuseBase*diffuseColor+emissiveColor+vAmbientColor,0.0,1.0)*baseColor.rgb;\n\
  1453. #endif\n\
  1454. #endif\n\
  1455. #ifdef SPECULARTERM\n\
  1456. vec3 finalSpecular=specularBase*specularColor;\n\
  1457. #ifdef SPECULAROVERALPHA\n\
  1458. alpha=clamp(alpha+dot(finalSpecular,vec3(0.3,0.59,0.11)),0.,1.);\n\
  1459. #endif\n\
  1460. #else\n\
  1461. vec3 finalSpecular=vec3(0.0);\n\
  1462. #endif\n\
  1463. #ifdef REFLECTIONOVERALPHA\n\
  1464. alpha=clamp(alpha+dot(reflectionColor,vec3(0.3,0.59,0.11)),0.,1.);\n\
  1465. #endif\n\
  1466. \n\
  1467. #ifdef EMISSIVEASILLUMINATION\n\
  1468. vec4 color=vec4(clamp(finalDiffuse*baseAmbientColor+finalSpecular+reflectionColor+emissiveColor+refractionColor,0.0,1.0),alpha);\n\
  1469. #else\n\
  1470. vec4 color=vec4(finalDiffuse*baseAmbientColor+finalSpecular+reflectionColor+refractionColor,alpha);\n\
  1471. #endif\n\
  1472. \n\
  1473. #ifdef LIGHTMAP\n\
  1474. #ifndef LIGHTMAPEXCLUDED\n\
  1475. #ifdef USELIGHTMAPASSHADOWMAP\n\
  1476. color.rgb*=lightmapColor;\n\
  1477. #else\n\
  1478. color.rgb+=lightmapColor;\n\
  1479. #endif\n\
  1480. #endif\n\
  1481. #endif\n\
  1482. #include<logDepthFragment>\n\
  1483. #include<fogFragment>\n\
  1484. \n\
  1485. // Apply image processing if relevant. As this applies in linear space, \n\
  1486. // We first move from gamma to linear.\n\
  1487. #ifdef IMAGEPROCESSINGPOSTPROCESS\n\
  1488. color.rgb = toLinearSpace(color.rgb);\n\
  1489. #else\n\
  1490. #ifdef IMAGEPROCESSING\n\
  1491. color.rgb = toLinearSpace(color.rgb);\n\
  1492. color = applyImageProcessing(color);\n\
  1493. #endif\n\
  1494. #endif\n\
  1495. \n\
  1496. #[Fragment_Before_FragColor]\n\
  1497. gl_FragColor=color;\n\
  1498. }";
  1499. this.VertexStore = "#include<__decl__defaultVertex>\n\
  1500. \n\
  1501. #[Vertex_Begin]\n\
  1502. \n\
  1503. attribute vec3 position;\n\
  1504. #ifdef NORMAL\n\
  1505. attribute vec3 normal;\n\
  1506. #endif\n\
  1507. #ifdef TANGENT\n\
  1508. attribute vec4 tangent;\n\
  1509. #endif\n\
  1510. #ifdef UV1\n\
  1511. attribute vec2 uv;\n\
  1512. #endif\n\
  1513. #ifdef UV2\n\
  1514. attribute vec2 uv2;\n\
  1515. #endif\n\
  1516. #ifdef VERTEXCOLOR\n\
  1517. attribute vec4 color;\n\
  1518. #endif\n\
  1519. #include<bonesDeclaration>\n\
  1520. \n\
  1521. #include<instancesDeclaration>\n\
  1522. #ifdef DIFFUSE\n\
  1523. varying vec2 vDiffuseUV;\n\
  1524. #endif\n\
  1525. #ifdef AMBIENT\n\
  1526. varying vec2 vAmbientUV;\n\
  1527. #endif\n\
  1528. #ifdef OPACITY\n\
  1529. varying vec2 vOpacityUV;\n\
  1530. #endif\n\
  1531. #ifdef EMISSIVE\n\
  1532. varying vec2 vEmissiveUV;\n\
  1533. #endif\n\
  1534. #ifdef LIGHTMAP\n\
  1535. varying vec2 vLightmapUV;\n\
  1536. #endif\n\
  1537. #if defined(SPECULAR) && defined(SPECULARTERM)\n\
  1538. varying vec2 vSpecularUV;\n\
  1539. #endif\n\
  1540. #ifdef BUMP\n\
  1541. varying vec2 vBumpUV;\n\
  1542. #endif\n\
  1543. \n\
  1544. varying vec3 localPosition;\n\
  1545. varying vec3 vPositionW;\n\
  1546. #ifdef NORMAL\n\
  1547. varying vec3 vNormalW_helper;\n\
  1548. varying vec3 localNormal;\n\
  1549. #endif\n\
  1550. #ifdef VERTEXCOLOR\n\
  1551. varying vec4 vColor;\n\
  1552. #endif\n\
  1553. #include<bumpVertexDeclaration>\n\
  1554. #include<clipPlaneVertexDeclaration>\n\
  1555. #include<fogVertexDeclaration>\n\
  1556. #include<__decl__lightFragment>[0..maxSimultaneousLights]\n\
  1557. #include<morphTargetsVertexGlobalDeclaration>\n\
  1558. #include<morphTargetsVertexDeclaration>[0..maxSimultaneousMorphTargets]\n\
  1559. #ifdef REFLECTIONMAP_SKYBOX\n\
  1560. varying vec3 vPositionUVW;\n\
  1561. #endif\n\
  1562. #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)\n\
  1563. varying vec3 vDirectionW;\n\
  1564. #endif\n\
  1565. #include<logDepthDeclaration>\n\
  1566. \n\
  1567. #[Vertex_Definitions]\n\
  1568. \n\
  1569. void main(void) {\n\
  1570. \n\
  1571. #[Vertex_MainBegin]\n\
  1572. \n\
  1573. vec3 positionUpdated=position;\n\
  1574. #ifdef NORMAL \n\
  1575. vec3 normalUpdated=normal;\n\
  1576. #endif\n\
  1577. #ifdef TANGENT\n\
  1578. vec4 tangentUpdated=tangent;\n\
  1579. #endif\n\
  1580. #include<morphTargetsVertex>[0..maxSimultaneousMorphTargets]\n\
  1581. #ifdef REFLECTIONMAP_SKYBOX\n\
  1582. vPositionUVW=positionUpdated;\n\
  1583. #endif \n\
  1584. #include<instancesVertex>\n\
  1585. #include<bonesVertex>\n\
  1586. \n\
  1587. localPosition = positionUpdated;\n\
  1588. #[Vertex_Before_PositionUpdated]\n\
  1589. \n\
  1590. gl_Position=viewProjection*finalWorld*vec4(positionUpdated,1.0);\n\
  1591. vec4 worldPos=finalWorld*vec4(positionUpdated,1.0);\n\
  1592. vPositionW=vec3(worldPos);\n\
  1593. #ifdef NORMAL\n\
  1594. \n\
  1595. #[Vertex_Before_NormalUpdated]\n\
  1596. \n\
  1597. localNormal = normalUpdated;\n\
  1598. vNormalW_helper=normalize(vec3(finalWorld*vec4(normalUpdated,0.0)));\n\
  1599. #endif\n\
  1600. #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)\n\
  1601. vDirectionW=normalize(vec3(finalWorld*vec4(positionUpdated,0.0)));\n\
  1602. #endif\n\
  1603. \n\
  1604. #ifndef UV1\n\
  1605. vec2 uv=vec2(0.,0.);\n\
  1606. #endif\n\
  1607. #ifndef UV2\n\
  1608. vec2 uv2=vec2(0.,0.);\n\
  1609. #endif\n\
  1610. #ifdef DIFFUSE\n\
  1611. if (vDiffuseInfos.x == 0.)\n\
  1612. {\n\
  1613. vDiffuseUV=vec2(diffuseMatrix*vec4(uv,1.0,0.0));\n\
  1614. }\n\
  1615. else\n\
  1616. {\n\
  1617. vDiffuseUV=vec2(diffuseMatrix*vec4(uv2,1.0,0.0));\n\
  1618. }\n\
  1619. #endif\n\
  1620. #ifdef AMBIENT\n\
  1621. if (vAmbientInfos.x == 0.)\n\
  1622. {\n\
  1623. vAmbientUV=vec2(ambientMatrix*vec4(uv,1.0,0.0));\n\
  1624. }\n\
  1625. else\n\
  1626. {\n\
  1627. vAmbientUV=vec2(ambientMatrix*vec4(uv2,1.0,0.0));\n\
  1628. }\n\
  1629. #endif\n\
  1630. #ifdef OPACITY\n\
  1631. if (vOpacityInfos.x == 0.)\n\
  1632. {\n\
  1633. vOpacityUV=vec2(opacityMatrix*vec4(uv,1.0,0.0));\n\
  1634. }\n\
  1635. else\n\
  1636. {\n\
  1637. vOpacityUV=vec2(opacityMatrix*vec4(uv2,1.0,0.0));\n\
  1638. }\n\
  1639. #endif\n\
  1640. #ifdef EMISSIVE\n\
  1641. if (vEmissiveInfos.x == 0.)\n\
  1642. {\n\
  1643. vEmissiveUV=vec2(emissiveMatrix*vec4(uv,1.0,0.0));\n\
  1644. }\n\
  1645. else\n\
  1646. {\n\
  1647. vEmissiveUV=vec2(emissiveMatrix*vec4(uv2,1.0,0.0));\n\
  1648. }\n\
  1649. #endif\n\
  1650. #ifdef LIGHTMAP\n\
  1651. if (vLightmapInfos.x == 0.)\n\
  1652. {\n\
  1653. vLightmapUV=vec2(lightmapMatrix*vec4(uv,1.0,0.0));\n\
  1654. }\n\
  1655. else\n\
  1656. {\n\
  1657. vLightmapUV=vec2(lightmapMatrix*vec4(uv2,1.0,0.0));\n\
  1658. }\n\
  1659. #endif\n\
  1660. #if defined(SPECULAR) && defined(SPECULARTERM)\n\
  1661. if (vSpecularInfos.x == 0.)\n\
  1662. {\n\
  1663. vSpecularUV=vec2(specularMatrix*vec4(uv,1.0,0.0));\n\
  1664. }\n\
  1665. else\n\
  1666. {\n\
  1667. vSpecularUV=vec2(specularMatrix*vec4(uv2,1.0,0.0));\n\
  1668. }\n\
  1669. #endif\n\
  1670. #ifdef BUMP\n\
  1671. if (vBumpInfos.x == 0.)\n\
  1672. {\n\
  1673. vBumpUV=vec2(bumpMatrix*vec4(uv,1.0,0.0));\n\
  1674. }\n\
  1675. else\n\
  1676. {\n\
  1677. vBumpUV=vec2(bumpMatrix*vec4(uv2,1.0,0.0));\n\
  1678. }\n\
  1679. #endif\n\
  1680. #include<bumpVertex>\n\
  1681. #include<clipPlaneVertex>\n\
  1682. #include<fogVertex>\n\
  1683. #include<shadowsVertex>[0..maxSimultaneousLights]\n\
  1684. #ifdef VERTEXCOLOR\n\
  1685. \n\
  1686. vColor=color;\n\
  1687. #endif\n\
  1688. #include<pointCloudVertex>\n\
  1689. #include<logDepthVertex>\n\
  1690. }";
  1691. }
  1692. }
  1693. export class StandardShaderVersions{
  1694. public static Ver3_0;
  1695. }
  1696. export class CustomMaterial extends StandardMaterial_OldVer {
  1697. public static ShaderIndexer = 1;
  1698. public CustomParts : ShaderSpecialParts;
  1699. public ShaderVersion : CustomShaderStructure ;
  1700. _isCreatedShader : boolean;
  1701. _createdShaderName : string;
  1702. _customUniform : string[];
  1703. _newUniforms : string[];
  1704. _newUniformInstances : any[];
  1705. _newSamplerInstances : Texture[];
  1706. public AttachAfterBind(mesh:Mesh,effect:Effect){
  1707. for(var el in this._newUniformInstances){
  1708. var ea = el.toString().split('-');
  1709. if(ea[0] == 'vec2') effect.setVector2(ea[1],this._newUniformInstances[el]);
  1710. else if(ea[0] == 'vec3') effect.setVector3(ea[1],this._newUniformInstances[el]);
  1711. else if(ea[0] == 'vec4') effect.setVector4(ea[1],this._newUniformInstances[el]);
  1712. else if(ea[0] == 'mat4') effect.setMatrix(ea[1],this._newUniformInstances[el]);
  1713. else if(ea[0] == 'float') effect.setFloat(ea[1],this._newUniformInstances[el]);
  1714. }
  1715. for(var el in this._newSamplerInstances){
  1716. var ea = el.toString().split('-');
  1717. if(ea[0] == 'sampler2D' && this._newSamplerInstances[el].isReady && this._newSamplerInstances[el].isReady())
  1718. effect.setTexture(ea[1],this._newSamplerInstances[el]);
  1719. }
  1720. }
  1721. public ReviewUniform(name:string, arr : string[] ) : string[]{
  1722. if(name == "uniform")
  1723. {
  1724. for(var ind in this._newUniforms)
  1725. if(this._customUniform[ind].indexOf('sampler')== -1)
  1726. arr.push(this._newUniforms[ind]);
  1727. }
  1728. if(name == "sampler")
  1729. {
  1730. for(var ind in this._newUniforms)
  1731. if(this._customUniform[ind].indexOf('sampler')!= -1)
  1732. arr.push(this._newUniforms[ind]);
  1733. }
  1734. return arr;
  1735. }
  1736. public Builder(shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: StandardMaterialDefines_OldVer) : string {
  1737. if(this._isCreatedShader) return this._createdShaderName;
  1738. this._isCreatedShader = false;
  1739. CustomMaterial.ShaderIndexer++;
  1740. var name = name+"custom_"+CustomMaterial.ShaderIndexer;
  1741. this.ReviewUniform("uniform",uniforms);
  1742. this.ReviewUniform("sampler",samplers);
  1743. var fn_afterBind = this._afterBind;
  1744. this._afterBind = function(m,e){
  1745. this.AttachAfterBind(m,e);
  1746. try{fn_afterBind(m,e);}catch(e){};
  1747. } ;
  1748. BABYLON.Effect.ShadersStore[name+"VertexShader"] = this.ShaderVersion.VertexStore
  1749. .replace('#[Vertex_Begin]',(this.CustomParts.Vertex_Begin ? this.CustomParts.Vertex_Begin : ""))
  1750. .replace('#[Vertex_Definitions]',(this._customUniform? this._customUniform.join("\n"):"")+ (this.CustomParts.Vertex_Definitions ? this.CustomParts.Vertex_Definitions : ""))
  1751. .replace('#[Vertex_MainBegin]',(this.CustomParts.Vertex_MainBegin ? this.CustomParts.Vertex_MainBegin : ""))
  1752. .replace('#[Vertex_Before_PositionUpdated]',(this.CustomParts.Vertex_Before_PositionUpdated ? this.CustomParts.Vertex_Before_PositionUpdated : ""))
  1753. .replace('#[Vertex_Before_NormalUpdated]',(this.CustomParts.Vertex_Before_NormalUpdated ? this.CustomParts.Vertex_Before_NormalUpdated : "")) ;
  1754. BABYLON.Effect.ShadersStore[name+"PixelShader"] = this.ShaderVersion.FragmentStore
  1755. .replace('#[Fragment_Begin]',(this.CustomParts.Fragment_Begin ? this.CustomParts.Fragment_Begin : ""))
  1756. .replace('#[Fragment_MainBegin]',(this.CustomParts.Fragment_MainBegin ? this.CustomParts.Fragment_MainBegin : ""))
  1757. .replace('#[Fragment_Definitions]',(this._customUniform? this._customUniform.join("\n"):"")+(this.CustomParts.Fragment_Definitions ? this.CustomParts.Fragment_Definitions : ""))
  1758. .replace('#[Fragment_Custom_Diffuse]',(this.CustomParts.Fragment_Custom_Diffuse ? this.CustomParts.Fragment_Custom_Diffuse : ""))
  1759. .replace('#[Fragment_Custom_Alpha]',(this.CustomParts.Fragment_Custom_Alpha ? this.CustomParts.Fragment_Custom_Alpha : ""))
  1760. .replace('#[Fragment_Before_FragColor]',(this.CustomParts.Fragment_Before_FragColor ? this.CustomParts.Fragment_Before_FragColor : "")) ;
  1761. this._isCreatedShader = true;
  1762. this._createdShaderName = name;
  1763. return name ;
  1764. }
  1765. public SelectVersion(ver:string){
  1766. switch(ver){
  1767. case "3.0.0" : this.ShaderVersion = new ShaderForVer3_0();break;
  1768. }
  1769. }
  1770. constructor(name:string,scene:Scene ){
  1771. super(name,scene);
  1772. this.CustomParts = new ShaderSpecialParts();
  1773. this.customShaderNameResolve = this.Builder;
  1774. this.SelectVersion("3.0.0");
  1775. }
  1776. public AddUniform(name:string,kind:string,param:any):CustomMaterial{
  1777. if(!this._customUniform)
  1778. {
  1779. this._customUniform = new Array();
  1780. this._newUniforms = new Array();
  1781. this._newSamplerInstances = new Array();
  1782. this._newUniformInstances = new Array();
  1783. }
  1784. if(param){
  1785. if(kind.indexOf("sampler") == -1) {
  1786. this._newUniformInstances[kind+"-"+name] = param;
  1787. }
  1788. else{
  1789. this._newSamplerInstances[kind+"-"+name] = param;
  1790. }
  1791. }
  1792. this._customUniform.push("uniform "+kind+" "+name+";");
  1793. this._newUniforms.push(name);
  1794. return this;
  1795. }
  1796. public Fragment_Begin(shaderPart:string):CustomMaterial{
  1797. this.CustomParts.Fragment_Begin = shaderPart;
  1798. return this;
  1799. }
  1800. public Fragment_Definitions(shaderPart:string):CustomMaterial{
  1801. this.CustomParts.Fragment_Definitions = shaderPart;
  1802. return this;
  1803. }
  1804. public Fragment_MainBegin(shaderPart:string):CustomMaterial{
  1805. this.CustomParts.Fragment_MainBegin = shaderPart;
  1806. return this;
  1807. }
  1808. public Fragment_Custom_Diffuse(shaderPart:string):CustomMaterial{
  1809. this.CustomParts.Fragment_Custom_Diffuse = shaderPart.replace("result","diffuseColor");
  1810. return this;
  1811. }
  1812. public Fragment_Custom_Alpha(shaderPart:string):CustomMaterial{
  1813. this.CustomParts.Fragment_Custom_Alpha = shaderPart.replace("result","alpha");
  1814. return this;
  1815. }
  1816. public Fragment_Before_FragColor(shaderPart:string):CustomMaterial{
  1817. this.CustomParts.Fragment_Before_FragColor = shaderPart.replace("result","color");
  1818. return this;
  1819. }
  1820. public Vertex_Begin(shaderPart:string):CustomMaterial{
  1821. this.CustomParts.Vertex_Begin = shaderPart;
  1822. return this;
  1823. }
  1824. public Vertex_Definitions(shaderPart:string):CustomMaterial{
  1825. this.CustomParts.Vertex_Definitions = shaderPart;
  1826. return this;
  1827. }
  1828. public Vertex_MainBegin(shaderPart:string):CustomMaterial{
  1829. this.CustomParts.Vertex_MainBegin = shaderPart;
  1830. return this;
  1831. }
  1832. public Vertex_Before_PositionUpdated(shaderPart:string):CustomMaterial{
  1833. this.CustomParts.Vertex_Before_PositionUpdated = shaderPart.replace("result","positionUpdated");
  1834. return this;
  1835. }
  1836. public Vertex_Before_NormalUpdated(shaderPart:string):CustomMaterial{
  1837. this.CustomParts.Vertex_Before_NormalUpdated = shaderPart.replace("result","normalUpdated");
  1838. return this;
  1839. }
  1840. }
  1841. }