babylon.customMaterial.ts 84 KB

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