babylon.customMaterial.ts 84 KB

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