GXSL.ShapeShaders.pas 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924
  1. //
  2. // The graphics engine GXScene https://github.com/glscene
  3. //
  4. unit GXSL.ShapeShaders;
  5. (*
  6. Erosion shader Erode surface object and render with Anisotropic Specular Reflection
  7. At this time one light source is supported
  8. *)
  9. interface
  10. uses
  11. Winapi.OpenGL,
  12. Winapi.OpenGLext,
  13. System.Classes,
  14. System.SysUtils,
  15. Stage.OpenGLTokens,
  16. GXS.Scene,
  17. GXS.BaseClasses,
  18. GXS.State,
  19. GXS.Context,
  20. GXS.Graphics,
  21. GXS.RenderContextInfo,
  22. GXS.Coordinates,
  23. Stage.VectorGeometry,
  24. Stage.VectorTypes,
  25. Stage.TextureFormat,
  26. GXS.Color,
  27. GXS.Texture,
  28. GXS.Material,
  29. GXSL.Shader,
  30. GXSL.CustomShader;
  31. //------------------ Cel Shader --------------------------
  32. type
  33. (* A shader that applies cel shading through a vertex program
  34. and shade definition texture.
  35. Cel shading options:
  36. csoOutlines: Render a second outline pass.
  37. csoTextured: Allows for a primary texture that the cel shading
  38. is modulated with and forces the shade definition
  39. to render as a second texture. *)
  40. TgxCelShaderOption = (csoOutlines, csoTextured, csoNoBuildShadeTexture);
  41. TgxCelShaderOptions = set of TgxCelShaderOption;
  42. // An event for user defined cel intensity.
  43. TgxCelShaderGetIntensity = procedure(Sender: TObject; var intensity: Byte) of
  44. object;
  45. // A generic cel shader.
  46. TgxCelShader = class(TgxShader)
  47. private
  48. FOutlineWidth: Single;
  49. FCelShaderOptions: TgxCelShaderOptions;
  50. FVPHandle: TgxVertexProgramHandle;
  51. FShadeTexture: TgxTexture;
  52. FOnGetIntensity: TgxCelShaderGetIntensity;
  53. FOutlinePass,
  54. FUnApplyShadeTexture: Boolean;
  55. FOutlineColor: TgxColor;
  56. protected
  57. procedure SetCelShaderOptions(const val: TgxCelShaderOptions);
  58. procedure SetOutlineWidth(const val: Single);
  59. procedure SetOutlineColor(const val: TgxColor);
  60. procedure BuildShadeTexture;
  61. procedure Loaded; override;
  62. function GenerateVertexProgram: string;
  63. public
  64. constructor Create(AOwner: TComponent); override;
  65. destructor Destroy; override;
  66. procedure DoApply(var rci: TgxRenderContextInfo; Sender: TObject); override;
  67. function DoUnApply(var rci: TgxRenderContextInfo): Boolean; override;
  68. property ShadeTexture: TgxTexture read FShadeTexture;
  69. published
  70. property CelShaderOptions: TgxCelShaderOptions read FCelShaderOptions write
  71. SetCelShaderOptions;
  72. property OutlineColor: TgxColor read FOutlineColor write SetOutlineColor;
  73. property OutlineWidth: Single read FOutlineWidth write SetOutlineWidth;
  74. property OnGetIntensity: TgxCelShaderGetIntensity read FOnGetIntensity write
  75. FOnGetIntensity;
  76. end;
  77. //------------- Erosion Shader ---------------------
  78. type
  79. (* Custom class for a shader that Erode surface object *)
  80. TgxCustomGLSLSimpleErosionShader = class(TGXSLCustomShader)
  81. private
  82. FMaterialLibrary: TgxAbstractMaterialLibrary;
  83. FMainTex : TgxTexture;
  84. FNoiseTex : TgxTexture;
  85. FErosionTex : TgxTexture;
  86. FMainTexName : TgxLibMaterialName;
  87. FNoiseTexName : TgxLibMaterialName;
  88. FErosionTexName : TgxLibMaterialName;
  89. FErosionScale: Single;
  90. FErosionFactor: Single;
  91. FIntensityFactor1: Single;
  92. FIntensityFactor2: Single;
  93. FSpecularColor : TgxColor;
  94. FAmbientColor : TgxColor;
  95. FAmbientFactor : Single;
  96. FDiffuseFactor : Single;
  97. FSpecularFactor : Single;
  98. FSpecularRoughness : Single;
  99. FAnisotropicRoughness : Single;
  100. function GetMaterialLibrary: TgxAbstractMaterialLibrary;
  101. procedure SetMainTexTexture(const Value: TgxTexture);
  102. procedure SetNoiseTexTexture(const Value: TgxTexture);
  103. procedure SetErosionTexTexture(const Value: TgxTexture);
  104. function GetMainTexName: TgxLibMaterialName;
  105. procedure SetMainTexName(const Value: TgxLibMaterialName);
  106. function GetNoiseTexName: TgxLibMaterialName;
  107. procedure SetNoiseTexName(const Value: TgxLibMaterialName);
  108. function GetErosionTexName: TgxLibMaterialName;
  109. procedure SetErosionTexName(const Value: TgxLibMaterialName);
  110. procedure SetAmbientColor(AValue: TgxColor);
  111. procedure SetSpecularColor(AValue: TgxColor);
  112. protected
  113. procedure DoApply(var rci : TgxRenderContextInfo; Sender : TObject); override;
  114. function DoUnApply(var rci: TgxRenderContextInfo): Boolean; override;
  115. procedure SetMaterialLibrary(const Value: TgxAbstractMaterialLibrary); virtual;
  116. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  117. public
  118. constructor Create(AOwner : TComponent); override;
  119. destructor Destroy; override;
  120. // property Color1: TgxColor read FColor1;
  121. // property Color2: TgxColor read FColor2;
  122. property MaterialLibrary: TgxAbstractMaterialLibrary read getMaterialLibrary write SetMaterialLibrary;
  123. property MainTexture: TgxTexture read FMainTex write SetMainTexTexture;
  124. property MainTextureName: TgxLibMaterialName read GetMainTexName write SetMainTexName;
  125. property NoiseTexture: TgxTexture read FNoiseTex write SetNoiseTexTexture;
  126. property NoiseTextureName: TgxLibMaterialName read GetNoiseTexName write SetNoiseTexName;
  127. property ErosionTexture: TgxTexture read FErosionTex write SetErosionTexTexture;
  128. property ErosionTextureName: TgxLibMaterialName read GetErosionTexName write SetErosionTexName;
  129. property ErosionFactor: Single read FErosionFactor write FErosionFactor;
  130. property ErosionScale: Single read FErosionFactor write FErosionFactor;
  131. property IntensityFactor1: Single read FIntensityFactor1 write FIntensityFactor1;
  132. property IntensityFactor2: Single read FIntensityFactor2 write FIntensityFactor2;
  133. property SpecularColor : TgxColor Read FSpecularColor Write setSpecularColor;
  134. property AmbientColor : TgxColor Read FAmbientColor Write setAmbientColor;
  135. property AmbientFactor : Single Read FAmbientFactor Write FAmbientFactor;
  136. property DiffuseFactor : Single Read FDiffuseFactor Write FDiffuseFactor;
  137. property SpecularFactor : Single Read FSpecularFactor Write FSpecularFactor;
  138. property SpecularRoughness : Single Read FSpecularRoughness Write FSpecularRoughness;
  139. property AnisotropicRoughness : Single Read FAnisotropicRoughness Write FAnisotropicRoughness;
  140. end;
  141. TgxSLSimpleErosionShader = class(TgxCustomGLSLSimpleErosionShader)
  142. published
  143. // property Color1;
  144. // property Color2;
  145. property MaterialLibrary;
  146. property MainTexture;
  147. property MainTextureName;
  148. property NoiseTexture;
  149. property NoiseTextureName;
  150. property ErosionTexture;
  151. property ErosionTextureName;
  152. property ErosionScale;
  153. property ErosionFactor;
  154. property IntensityFactor1;
  155. property IntensityFactor2;
  156. property SpecularColor;
  157. property AmbientColor;
  158. property AmbientFactor;
  159. property DiffuseFactor;
  160. property SpecularFactor;
  161. property SpecularRoughness;
  162. property AnisotropicRoughness;
  163. end;
  164. //-------------- Glass Shader --------------------
  165. type
  166. (* Custom class for Glass shader:
  167. Environment mapping and refraction mapping using the fresnel terms *)
  168. TgxCustomGLSLGlassShader = class(TGXSLCustomShader)
  169. private
  170. FDiffuseColor: TgxColor;
  171. FDepth: Single;
  172. FMix: Single;
  173. FAlpha: Single;
  174. FMaterialLibrary: TgxAbstractMaterialLibrary;
  175. FMainTexture: TgxTexture; // EnvMap
  176. FMainTexName: TgxLibMaterialName;
  177. FRefractionTexture: TgxTexture;
  178. FRefractionTexName: TgxLibMaterialName;
  179. FOwnerObject: TgxBaseSceneObject;
  180. FBlendSrc: TgxBlendFunction;
  181. FBlendDst: TgxBlendFunction;
  182. function GetMaterialLibrary: TgxAbstractMaterialLibrary;
  183. procedure SetMainTexTexture(const Value: TgxTexture);
  184. function GetMainTexName: TgxLibMaterialName;
  185. procedure SetMainTexName(const Value: TgxLibMaterialName);
  186. procedure SetRefractionTexTexture(const Value: TgxTexture);
  187. function GetRefractionTexName: TgxLibMaterialName;
  188. procedure SetRefractionTexName(const Value: TgxLibMaterialName);
  189. procedure SetDiffuseColor(AValue: TgxColor);
  190. protected
  191. procedure DoApply(var rci: TgxRenderContextInfo; Sender: TObject); override;
  192. function DoUnApply(var rci: TgxRenderContextInfo): Boolean; override;
  193. procedure SetMaterialLibrary(const Value
  194. : TgxAbstractMaterialLibrary); virtual;
  195. procedure Notification(AComponent: TComponent;
  196. Operation: TOperation); override;
  197. public
  198. constructor Create(AOwner: TComponent); override;
  199. destructor Destroy; override;
  200. property DiffuseColor: TgxColor read FDiffuseColor Write SetDiffuseColor;
  201. property Depth: Single read FDepth write FDepth;
  202. property Mix: Single read FMix write FMix;
  203. property Alpha: Single read FAlpha write FAlpha;
  204. property MaterialLibrary: TgxAbstractMaterialLibrary read GetMaterialLibrary
  205. write SetMaterialLibrary;
  206. property MainTexture: TgxTexture read FMainTexture write SetMainTexTexture;
  207. property MainTextureName: TgxLibMaterialName read GetMainTexName
  208. write SetMainTexName;
  209. property RefractionTexture: TgxTexture read FRefractionTexture
  210. write SetRefractionTexTexture;
  211. property RefractionTextureName: TgxLibMaterialName read GetRefractionTexName
  212. write SetRefractionTexName;
  213. property OwnerObject: TgxBaseSceneObject read FOwnerObject
  214. write FOwnerObject;
  215. property BlendSrc: TgxBlendFunction read FBlendSrc write FBlendSrc
  216. default bfSrcAlpha;
  217. property BlendDst: TgxBlendFunction read FBlendDst write FBlendDst
  218. default bfDstAlpha;
  219. end;
  220. TgxSLGlassShader = class(TgxCustomGLSLGlassShader)
  221. published
  222. property DiffuseColor;
  223. property Depth;
  224. property Mix;
  225. property Alpha;
  226. property MaterialLibrary;
  227. property MainTexture;
  228. property MainTextureName;
  229. property RefractionTexture;
  230. property RefractionTextureName;
  231. property OwnerObject;
  232. property BlendSrc;
  233. property BlendDst;
  234. end;
  235. //-----------Gooch Shader -----------------------
  236. type
  237. (* Custom class for Gooch Shader *)
  238. TgxCustomGLSLSimpleGoochShader = class(TGXSLCustomShader)
  239. private
  240. FDiffuseColor : TgxColor;
  241. FWarmColor : TgxColor;
  242. FCoolColor : TgxColor;
  243. FSpecularColor : TgxColor;
  244. FAmbientColor : TgxColor;
  245. FDiffuseWarm : Single;
  246. FDiffuseCool : Single;
  247. FAmbientFactor : Single;
  248. FDiffuseFactor : Single;
  249. FSpecularFactor : Single;
  250. FBlendingMode: TgxBlendingModeEx;
  251. procedure SetDiffuseColor(AValue: TgxColor);
  252. procedure SetAmbientColor(AValue: TgxColor);
  253. procedure SetSpecularColor(AValue: TgxColor);
  254. procedure SetWarmColor(AValue: TgxColor);
  255. procedure SetCoolColor(AValue: TgxColor);
  256. protected
  257. procedure DoApply(var rci : TgxRenderContextInfo; Sender : TObject); override;
  258. function DoUnApply(var rci: TgxRenderContextInfo): Boolean; override;
  259. public
  260. constructor Create(AOwner : TComponent); override;
  261. destructor Destroy; override;
  262. property DiffuseColor : TgxColor read FDiffuseColor Write setDiffuseColor;
  263. property WarmColor : TgxColor read FWarmColor Write setWarmColor;
  264. property CoolColor : TgxColor Read FCoolColor Write setCoolColor;
  265. property SpecularColor : TgxColor Read FSpecularColor Write setSpecularColor;
  266. property AmbientColor : TgxColor Read FAmbientColor Write setAmbientColor;
  267. property WarmFactor : Single Read FDiffuseWarm Write FDiffuseWarm;
  268. property CoolFactor : Single Read FDiffuseCool Write FDiffuseCool;
  269. property AmbientFactor : Single Read FAmbientFactor Write FAmbientFactor;
  270. property DiffuseFactor : Single Read FDiffuseFactor Write FDiffuseFactor;
  271. property SpecularFactor : Single Read FSpecularFactor Write FSpecularFactor;
  272. property BlendingMode: TgxBlendingModeEx read FBlendingMode write FBlendingMode default bmxOpaque;
  273. end;
  274. type
  275. TgxSLSimpleGoochShader = class(TgxCustomGLSLSimpleGoochShader)
  276. published
  277. property DiffuseColor;
  278. property WarmColor;
  279. property CoolColor;
  280. property SpecularColor;
  281. property AmbientColor;
  282. property WarmFactor;
  283. property CoolFactor;
  284. property AmbientFactor;
  285. property DiffuseFactor;
  286. property SpecularFactor;
  287. end;
  288. //------------Fur Shader ------------------
  289. type
  290. (* Custom class for Fur Shader *)
  291. TgxCustomGLSLFurShader = class(TGXSLCustomShader)
  292. private
  293. FMaterialLibrary: TgxAbstractMaterialLibrary;
  294. FCurrentPass: Integer;
  295. FPassCount: Single;
  296. FFurLength: Single;
  297. FMaxFurLength: Single;
  298. FFurScale: Single;
  299. FRandomFurLength : Boolean;
  300. FColorScale: TgxColor;
  301. FAmbient: TgxColor;
  302. FGravity : TgxCoordinates;
  303. FLightIntensity : Single;
  304. FMainTex : TgxTexture;
  305. FNoiseTex : TgxTexture;
  306. FNoiseTexName : TgxLibMaterialName;
  307. FMainTexName : TgxLibMaterialName;
  308. FBlendSrc : TgxBlendFunction;
  309. FBlendDst : TgxBlendFunction;
  310. // FBlendEquation : TgxBlendEquation;
  311. function GetMaterialLibrary: TgxAbstractMaterialLibrary;
  312. procedure SetMainTexTexture(const Value: TgxTexture);
  313. procedure SetNoiseTexTexture(const Value: TgxTexture);
  314. function GetNoiseTexName: TgxLibMaterialName;
  315. procedure SetNoiseTexName(const Value: TgxLibMaterialName);
  316. function GetMainTexName: TgxLibMaterialName;
  317. procedure SetMainTexName(const Value: TgxLibMaterialName);
  318. procedure SetGravity(APosition:TgxCoordinates);
  319. procedure SetAmbient(AValue: TgxColor);
  320. procedure SetColorScale(AValue: TgxColor);
  321. protected
  322. procedure DoApply(var rci : TgxRenderContextInfo; Sender : TObject); override;
  323. function DoUnApply(var rci: TgxRenderContextInfo): Boolean; override;
  324. procedure SetMaterialLibrary(const Value: TgxAbstractMaterialLibrary); virtual;
  325. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  326. public
  327. //Common stuff
  328. constructor Create(AOwner : TComponent); override;
  329. destructor Destroy; override;
  330. property PassCount: Single read FPassCount write FPassCount;
  331. property FurLength: Single read FFurLength write FFurLength;
  332. property MaxFurLength: Single read FMaxFurLength write FMaxFurLength;
  333. property FurDensity: Single read FFurScale write FFurScale;
  334. property RandomFurLength : Boolean read FRandomFurLength Write FRandomFurLength;
  335. property ColorScale: TgxColor read FColorScale Write setColorScale;
  336. property Ambient: TgxColor read FAmbient write setAmbient;
  337. property MaterialLibrary: TgxAbstractMaterialLibrary read getMaterialLibrary write SetMaterialLibrary;
  338. property MainTexture: TgxTexture read FMainTex write SetMainTexTexture;
  339. property MainTextureName: TgxLibMaterialName read GetMainTexName write SetMainTexName;
  340. property NoiseTexture: TgxTexture read FNoiseTex write SetNoiseTexTexture;
  341. property NoiseTextureName: TgxLibMaterialName read GetNoiseTexName write SetNoiseTexName;
  342. //property BlendEquation : TBlendEquation read FBlendEquation write FBlendEquation default beMin;
  343. property BlendSrc : TgxBlendFunction read FBlendSrc write FBlendSrc default bfSrcColor;
  344. property BlendDst : TgxBlendFunction read FBlendDst write FBlendDst default bfOneMinusDstColor;
  345. property Gravity : TgxCoordinates Read FGravity write setGravity;
  346. property LightIntensity : Single read FLightIntensity Write FLightIntensity;
  347. end;
  348. TgxSLFurShader = class(TgxCustomGLSLFurShader)
  349. published
  350. property PassCount;
  351. property FurLength;
  352. property MaxFurLength;
  353. property FurDensity;
  354. property RandomFurLength;
  355. property ColorScale;
  356. property Ambient;
  357. property LightIntensity;
  358. property Gravity;
  359. property BlendSrc;
  360. property BlendDst;
  361. property MainTexture;
  362. property MainTextureName;
  363. property NoiseTexture;
  364. property NoiseTextureName;
  365. end;
  366. //------------ Ivory Shader ----------------
  367. type
  368. (* Custom class for a shader that simulate Ivory Material *)
  369. TgxCustomGLSLIvoryShader = class(TGXSLCustomShader)
  370. protected
  371. procedure DoApply(var rci : TgxRenderContextInfo; Sender : TObject); override;
  372. function DoUnApply(var rci: TgxRenderContextInfo): Boolean; override;
  373. public
  374. constructor Create(AOwner : TComponent); override;
  375. destructor Destroy; override;
  376. end;
  377. type
  378. TgxSLIvoryShader = class(TgxCustomGLSLIvoryShader)
  379. end;
  380. //------------- Lattice Shader ----------------------
  381. type
  382. (* Custom class for a shader that simulate Lattice *)
  383. TgxCustomGLSLSimpleLatticeShader = class(TGXSLCustomShader)
  384. private
  385. FLatticeScale: TgxCoordinates2;
  386. FLatticeThreshold: TgxCoordinates2;
  387. procedure SetLatticeScale(const Value: TgxCoordinates2);
  388. procedure SetLatticeThreshold(const Value: TgxCoordinates2);
  389. protected
  390. procedure DoApply(var rci : TgxRenderContextInfo; Sender : TObject); override;
  391. function DoUnApply(var rci: TgxRenderContextInfo): Boolean; override;
  392. public
  393. constructor Create(AOwner : TComponent); override;
  394. destructor Destroy; override;
  395. property LatticeScale: TgxCoordinates2 read FLatticeScale write SetLatticeScale;
  396. property LatticeThreshold: TgxCoordinates2 read FLatticeThreshold write SetLatticeThreshold;
  397. end;
  398. (* Custom class for GLSLLatticeShader.
  399. A shader that simulate Lattice with Diffuse/Specular and support Texture *)
  400. TgxCustomGLSLLatticeShader = class(TgxCustomGLSLSimpleLatticeShader)
  401. private
  402. FAmbientColor: TgxColor;
  403. FDiffuseColor: TgxColor;
  404. FSpecularColor: TgxColor;
  405. FMaterialLibrary: TgxAbstractMaterialLibrary;
  406. FMainTexture: TgxTexture;
  407. FMainTexName : TgxLibMaterialName;
  408. FSpecularPower: Single;
  409. FLightPower: Single;
  410. function GetMaterialLibrary: TgxAbstractMaterialLibrary;
  411. procedure SetMainTexTexture(const Value: TgxTexture);
  412. function GetMainTexName: TgxLibMaterialName;
  413. procedure SetMainTexName(const Value: TgxLibMaterialName);
  414. procedure SetDiffuseColor(AValue: TgxColor);
  415. procedure SetAmbientColor(AValue: TgxColor);
  416. procedure SetSpecularColor(AValue: TgxColor);
  417. protected
  418. procedure DoInitialize(var rci : TgxRenderContextInfo; Sender : TObject); override;
  419. procedure DoApply(var rci : TgxRenderContextInfo; Sender : TObject); override;
  420. procedure SetMaterialLibrary(const Value: TgxAbstractMaterialLibrary); virtual;
  421. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  422. public
  423. constructor Create(AOwner : TComponent); override;
  424. destructor Destroy; override;
  425. property DiffuseColor : TgxColor read FDiffuseColor Write setDiffuseColor;
  426. property SpecularColor : TgxColor Read FSpecularColor Write setSpecularColor;
  427. property AmbientColor : TgxColor Read FAmbientColor Write setAmbientColor;
  428. property MaterialLibrary: TgxAbstractMaterialLibrary read getMaterialLibrary write SetMaterialLibrary;
  429. property MainTexture: TgxTexture read FMainTexture write SetMainTexTexture;
  430. property MainTextureName: TgxLibMaterialName read GetMainTexName write SetMainTexName;
  431. property SpecularPower: Single read FSpecularPower write FSpecularPower;
  432. property LightPower: Single read FLightPower write FLightPower;
  433. end;
  434. TgxSLSimpleLatticeShader = class(TgxCustomGLSLSimpleLatticeShader)
  435. published
  436. property LatticeScale;
  437. property LatticeThreshold;
  438. end;
  439. TgxSLLatticeShader = class(TgxCustomGLSLLatticeShader)
  440. published
  441. property LatticeScale;
  442. property LatticeThreshold;
  443. property AmbientColor;
  444. property DiffuseColor;
  445. property SpecularColor;
  446. property MainTexture;
  447. property SpecularPower;
  448. property LightPower;
  449. end;
  450. //----------------- SEM Shader ---------------------
  451. type
  452. (* Custom class for SEM Shader : Spherical Environment Mapping *)
  453. TgxCustomGLSLSemShader = class(TGXSLCustomShader)
  454. private
  455. FAmbientColor: TgxColor;
  456. // FDiffuseColor: TgxColor;
  457. FSpecularColor: TgxColor;
  458. FAmbientFactor : Single;
  459. FDiffuseFactor : Single;
  460. FSpecularFactor : Single;
  461. FMaterialLibrary: TgxAbstractMaterialLibrary;
  462. FMainTexture: TgxTexture;
  463. FMainTexName : TgxLibMaterialName;
  464. // FSpecularPower: Single;
  465. // FLightPower: Single;
  466. function GetMaterialLibrary: TgxAbstractMaterialLibrary;
  467. procedure SetMainTexTexture(const Value: TgxTexture);
  468. function GetMainTexName: TgxLibMaterialName;
  469. procedure SetMainTexName(const Value: TgxLibMaterialName);
  470. //procedure SetDiffuseColor(AValue: TgxColor);
  471. procedure SetAmbientColor(AValue: TgxColor);
  472. procedure SetSpecularColor(AValue: TgxColor);
  473. protected
  474. procedure DoApply(var rci : TgxRenderContextInfo; Sender : TObject); override;
  475. function DoUnApply(var rci: TgxRenderContextInfo): Boolean; override;
  476. procedure SetMaterialLibrary(const Value: TgxAbstractMaterialLibrary); virtual;
  477. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  478. public
  479. constructor Create(AOwner : TComponent); override;
  480. destructor Destroy; override;
  481. // property DiffuseColor : TgxColor read FDiffuseColor Write setDiffuseColor;
  482. property SpecularColor : TgxColor Read FSpecularColor Write setSpecularColor;
  483. property AmbientColor : TgxColor Read FAmbientColor Write setAmbientColor;
  484. property AmbientFactor : Single Read FAmbientFactor Write FAmbientFactor;
  485. property DiffuseFactor : Single Read FDiffuseFactor Write FDiffuseFactor;
  486. property SpecularFactor : Single Read FSpecularFactor Write FSpecularFactor;
  487. property MaterialLibrary: TgxAbstractMaterialLibrary read getMaterialLibrary write SetMaterialLibrary;
  488. property MainTexture: TgxTexture read FMainTexture write SetMainTexTexture;
  489. property MainTextureName: TgxLibMaterialName read GetMainTexName write SetMainTexName;
  490. // property SpecularPower: Single read FSpecularPower write FSpecularPower;
  491. // property LightPower: Single read FLightPower write FLightPower;
  492. end;
  493. TgxSLSemShader = class(TgxCustomGLSLSemShader)
  494. published
  495. property AmbientColor;
  496. // property DiffuseColor;
  497. property SpecularColor;
  498. property AmbientFactor;
  499. property DiffuseFactor;
  500. property SpecularFactor;
  501. property MaterialLibrary;
  502. property MainTexture;
  503. property MainTextureName;
  504. end;
  505. //----------------- Toon Shader ---------------------
  506. type
  507. (* Custom class for Toon Shader *)
  508. TgxCustomGLSLToonShader = class(TGXSLCustomShader)
  509. private
  510. FHighlightColor : TgxColor;
  511. FMidColor : TgxColor;
  512. FLightenShadowColor : TgxColor;
  513. FDarkenShadowColor : TgxColor;
  514. FOutlineColor : TgxColor;
  515. FHighlightSize : Single;
  516. FMidSize : Single;
  517. FShadowSize : Single;
  518. FOutlineWidth : Single;
  519. procedure SetHighLightColor(AValue: TgxColor);
  520. procedure SetMidColor(AValue: TgxColor);
  521. procedure SetLightenShadowColor(AValue: TgxColor);
  522. procedure SetDarkenShadowColor(AValue: TgxColor);
  523. procedure SetOutlineColor(AValue: TgxColor);
  524. protected
  525. procedure DoApply(var rci : TgxRenderContextInfo; Sender : TObject); override;
  526. function DoUnApply(var rci: TgxRenderContextInfo): Boolean; override;
  527. public
  528. constructor Create(AOwner : TComponent); override;
  529. destructor Destroy; override;
  530. property HighlightColor : TgxColor read FHighlightColor Write setHighlightColor;
  531. property MidColor : TgxColor read FMidColor Write setMidColor;
  532. property LightenShadowColor : TgxColor Read FLightenShadowColor Write setLightenShadowColor;
  533. property DarkenShadowrColor : TgxColor Read FDarkenShadowColor Write setDarkenShadowColor;
  534. property OutlinetColor : TgxColor Read FOutlineColor Write setOutlineColor;
  535. property HighlightSize : Single read FHighlightSize write FHighlightSize;
  536. property MidSize : Single read FMidSize write FMidSize;
  537. property ShadowSize : Single read FShadowSize write FShadowSize;
  538. property OutlineWidth : Single read FOutlineWidth write FOutlineWidth;
  539. end;
  540. type
  541. TgxSLToonShader = class(TgxCustomGLSLToonShader)
  542. published
  543. property HighlightColor;
  544. property MidColor;
  545. property LightenShadowColor;
  546. property DarkenShadowrColor;
  547. property OutlinetColor;
  548. property HighlightSize;
  549. property MidSize;
  550. property ShadowSize;
  551. property OutlineWidth;
  552. end;
  553. //----------- Vertex Displacement Shader -----------
  554. (*
  555. VertexDisplacement shader: Basic Vertex Displacement with Perlin Noise
  556. You can Improved it:
  557. The vertex displacement can be done by reading a 2D or 3D texture.
  558. It can be done along the normal or the tangent.
  559. It can be scaled, twisted, modulated, inverted...
  560. Converted from : https://www.clicktorelease.com/blog/vertex-displacement-noise-3d-webgl-glsl-three-js
  561. At this time only one light source is supported
  562. *)
  563. TgxCustomGLSLVertexDisplacementShader = class(TGXSLCustomShader)
  564. private
  565. FAmbientColor: TgxColor;
  566. // FDiffuseColor: TgxColor;
  567. FSpecularColor: TgxColor;
  568. FAmbientFactor : Single;
  569. FDiffuseFactor : Single;
  570. FSpecularFactor : Single;
  571. FMaterialLibrary: TgxAbstractMaterialLibrary;
  572. FMainTexture: TgxTexture;
  573. FMainTexName : TgxLibMaterialName;
  574. FElapsedTime : Single;
  575. FNoise : Single;
  576. FDisplacementScale : Single;
  577. FNoiseScale : Single;
  578. FTurbulenceFactor : Single;
  579. FNoisePeriod : Single;
  580. FTimeFactor : Single;
  581. function GetMaterialLibrary: TgxAbstractMaterialLibrary;
  582. procedure SetMainTexTexture(const Value: TgxTexture);
  583. function GetMainTexName: TgxLibMaterialName;
  584. procedure SetMainTexName(const Value: TgxLibMaterialName);
  585. //procedure SetDiffuseColor(AValue: TgxColor);
  586. procedure SetAmbientColor(AValue: TgxColor);
  587. procedure SetSpecularColor(AValue: TgxColor);
  588. protected
  589. procedure DoApply(var rci : TgxRenderContextInfo; Sender : TObject); override;
  590. function DoUnApply(var rci: TgxRenderContextInfo): Boolean; override;
  591. procedure SetMaterialLibrary(const Value: TgxAbstractMaterialLibrary); virtual;
  592. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  593. public
  594. constructor Create(AOwner : TComponent); override;
  595. destructor Destroy; override;
  596. // property DiffuseColor : TgxColor read FDiffuseColor Write setDiffuseColor;
  597. property SpecularColor : TgxColor Read FSpecularColor Write setSpecularColor;
  598. property AmbientColor : TgxColor Read FAmbientColor Write setAmbientColor;
  599. property AmbientFactor : Single Read FAmbientFactor Write FAmbientFactor;
  600. property DiffuseFactor : Single Read FDiffuseFactor Write FDiffuseFactor;
  601. property SpecularFactor : Single Read FSpecularFactor Write FSpecularFactor;
  602. property MaterialLibrary: TgxAbstractMaterialLibrary read getMaterialLibrary write SetMaterialLibrary;
  603. property MainTexture: TgxTexture read FMainTexture write SetMainTexTexture;
  604. property MainTextureName: TgxLibMaterialName read GetMainTexName write SetMainTexName;
  605. property ElapsedTime: Single read FElapsedTime write FElapsedTime;
  606. property NoiseFactor : Single read FNoise write FNoise;
  607. property NoiseScale : Single read FNoiseScale write FNoiseScale;
  608. property TurbulenceFactor : Single read FTurbulenceFactor write FTurbulenceFactor;
  609. property NoisePeriod : Single read FNoisePeriod write FNoisePeriod;
  610. property DisplacementScale : Single read FDisplacementScale write FDisplacementScale;
  611. property TimeFactor : Single read FTimeFactor write FTimeFactor;
  612. end;
  613. TgxSLVertexDisplacementShader = class(TgxCustomGLSLVertexDisplacementShader)
  614. published
  615. property AmbientColor;
  616. // property DiffuseColor;
  617. property SpecularColor;
  618. property AmbientFactor;
  619. property DiffuseFactor;
  620. property SpecularFactor;
  621. property MaterialLibrary;
  622. property MainTexture;
  623. property MainTextureName;
  624. property ElapsedTime;
  625. property NoiseFactor;
  626. property NoiseScale;
  627. property TurbulenceFactor;
  628. property NoisePeriod;
  629. property DisplacementScale;
  630. property TimeFactor;
  631. end;
  632. //----------------------------------------------------------
  633. implementation
  634. //----------------------------------------------------------
  635. const
  636. fBuffSize: Integer = 512;
  637. (***************************************************
  638. TgxCelShader
  639. ***************************************************)
  640. constructor TgxCelShader.Create(AOwner: TComponent);
  641. begin
  642. inherited;
  643. FOutlineWidth := 3;
  644. FCelShaderOptions := [csoOutlines];
  645. FShadeTexture := TgxTexture.Create(Self);
  646. with FShadeTexture do
  647. begin
  648. Enabled := True;
  649. MinFilter := miNearest;
  650. MagFilter := maNearest;
  651. TextureWrap := twNone;
  652. TextureMode := tmModulate;
  653. end;
  654. FOutlineColor := TgxColor.Create(Self);
  655. FOutlineColor.OnNotifyChange := NotifyChange;
  656. FOutlineColor.Initialize(clrBlack);
  657. ShaderStyle := ssLowLevel;
  658. FVPHandle := TgxVertexProgramHandle.Create;
  659. end;
  660. destructor TgxCelShader.Destroy;
  661. begin
  662. FVPHandle.Free;
  663. FShadeTexture.Free;
  664. FOutlineColor.Free;
  665. inherited;
  666. end;
  667. procedure TgxCelShader.Loaded;
  668. begin
  669. inherited;
  670. BuildShadeTexture;
  671. end;
  672. procedure TgxCelShader.BuildShadeTexture;
  673. var
  674. bmp32: TgxBitmap32;
  675. i: Integer;
  676. intensity: Byte;
  677. begin
  678. if csoNoBuildShadeTexture in FCelShaderOptions then
  679. exit;
  680. with FShadeTexture do
  681. begin
  682. ImageClassName := 'TgxBlankImage';
  683. TgxBlankImage(Image).Width := 128;
  684. TgxBlankImage(Image).Height := 2;
  685. end;
  686. bmp32 := FShadeTexture.Image.GetBitmap32;
  687. bmp32.Blank := false;
  688. for i := 0 to bmp32.Width - 1 do
  689. begin
  690. intensity := i * (256 div bmp32.Width);
  691. if Assigned(FOnGetIntensity) then
  692. FOnGetIntensity(Self, intensity)
  693. else
  694. begin
  695. if intensity > 230 then
  696. intensity := 255
  697. else if intensity > 150 then
  698. intensity := 230
  699. else if intensity > 100 then
  700. intensity := intensity + 50
  701. else
  702. intensity := 150;
  703. end;
  704. bmp32.Data^[i].r := intensity;
  705. bmp32.Data^[i].g := intensity;
  706. bmp32.Data^[i].b := intensity;
  707. bmp32.Data^[i].a := 1;
  708. bmp32.Data^[i + bmp32.Width] := bmp32.Data^[i];
  709. end;
  710. end;
  711. function TgxCelShader.GenerateVertexProgram: string;
  712. var
  713. VP: TStringList;
  714. begin
  715. VP := TStringList.Create;
  716. VP.Add('!!ARBvp1.0');
  717. VP.Add('OPTION ARB_position_invariant;');
  718. VP.Add('PARAM mvinv[4] = { state.matrix.modelview.inverse };');
  719. VP.Add('PARAM lightPos = program.local[0];');
  720. VP.Add('TEMP temp, light, normal;');
  721. VP.Add(' DP4 light.x, mvinv[0], lightPos;');
  722. VP.Add(' DP4 light.y, mvinv[1], lightPos;');
  723. VP.Add(' DP4 light.z, mvinv[2], lightPos;');
  724. VP.Add(' ADD light, light, -vertex.position;');
  725. VP.Add(' DP3 temp.x, light, light;');
  726. VP.Add(' RSQ temp.x, temp.x;');
  727. VP.Add(' MUL light, temp.x, light;');
  728. VP.Add(' DP3 temp, vertex.normal, vertex.normal;');
  729. VP.Add(' RSQ temp.x, temp.x;');
  730. VP.Add(' MUL normal, temp.x, vertex.normal;');
  731. VP.Add(' MOV result.color, state.material.diffuse;');
  732. if csoTextured in FCelShaderOptions then
  733. begin
  734. VP.Add(' MOV result.texcoord[0], vertex.texcoord[0];');
  735. VP.Add(' DP3 result.texcoord[1].x, normal, light;');
  736. end
  737. else
  738. begin
  739. VP.Add(' DP3 result.texcoord[0].x, normal, light;');
  740. end;
  741. VP.Add('END');
  742. Result := VP.Text;
  743. VP.Free;
  744. end;
  745. procedure TgxCelShader.DoApply(var rci: TgxRenderContextInfo; Sender: TObject);
  746. var
  747. light: TGLVector;
  748. begin
  749. if (csDesigning in ComponentState) then
  750. exit;
  751. FVPHandle.AllocateHandle;
  752. if FVPHandle.IsDataNeedUpdate then
  753. begin
  754. FVPHandle.LoadARBProgram(GenerateVertexProgram);
  755. Enabled := FVPHandle.Ready;
  756. FVPHandle.NotifyDataUpdated;
  757. if not Enabled then
  758. Abort;
  759. end;
  760. rci.gxStates.Disable(stLighting);
  761. glGetLightfv(GL_LIGHT0, GL_POSITION, @light.X);
  762. FVPHandle.Enable;
  763. FVPHandle.Bind;
  764. glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_NV, 0, @light.X);
  765. if (csoTextured in FCelShaderOptions) then
  766. FShadeTexture.ApplyAsTexture2(rci, nil)
  767. else
  768. FShadeTexture.Apply(rci);
  769. FOutlinePass := csoOutlines in FCelShaderOptions;
  770. FUnApplyShadeTexture := True;
  771. end;
  772. function TgxCelShader.DoUnApply(var rci: TgxRenderContextInfo): Boolean;
  773. begin
  774. Result := False;
  775. if (csDesigning in ComponentState) then
  776. exit;
  777. FVPHandle.Disable;
  778. if FUnApplyShadeTexture then
  779. begin
  780. if (csoTextured in FCelShaderOptions) then
  781. FShadeTexture.UnApplyAsTexture2(rci, false)
  782. else
  783. FShadeTexture.UnApply(rci);
  784. FUnApplyShadeTexture := False;
  785. end;
  786. if FOutlinePass then
  787. with rci.gxStates do
  788. begin
  789. ActiveTexture := 0;
  790. ActiveTextureEnabled[ttTexture2D] := False;
  791. Enable(stBlend);
  792. Enable(stLineSmooth);
  793. Disable(stLineStipple);
  794. Enable(stCullFace);
  795. PolygonMode := pmLines;
  796. LineWidth := FOutlineWidth;
  797. CullFaceMode := cmFront;
  798. LineSmoothHint := hintNicest;
  799. SetBlendFunc(bfSrcAlpha, bfOneMinusSrcAlpha);
  800. DepthFunc := cfLEqual;
  801. glColor4fv(@FOutlineColor.AsAddress^);
  802. Result := True;
  803. FOutlinePass := False;
  804. Exit;
  805. end
  806. else
  807. with rci.gxStates do
  808. begin
  809. rci.gxStates.PolygonMode := pmFill;
  810. rci.gxStates.CullFaceMode := cmBack;
  811. rci.gxStates.DepthFunc := cfLEqual;
  812. end;
  813. end;
  814. procedure TgxCelShader.SetCelShaderOptions(const val: TgxCelShaderOptions);
  815. begin
  816. if val <> FCelShaderOptions then
  817. begin
  818. FCelShaderOptions := val;
  819. BuildShadeTexture;
  820. FVPHandle.NotifyChangesOfData;
  821. NotifyChange(Self);
  822. end;
  823. end;
  824. procedure TgxCelShader.SetOutlineWidth(const val: Single);
  825. begin
  826. if val <> FOutlineWidth then
  827. begin
  828. FOutlineWidth := val;
  829. NotifyChange(Self);
  830. end;
  831. end;
  832. procedure TgxCelShader.SetOutlineColor(const val: TgxColor);
  833. begin
  834. if val <> FOutlineColor then
  835. begin
  836. FOutlineColor.Assign(val);
  837. NotifyChange(Self);
  838. end;
  839. end;
  840. (****************************************
  841. TgxCustomGLSLSimpleErosionShader
  842. *****************************************)
  843. constructor TgxCustomGLSLSimpleErosionShader.Create(AOwner: TComponent);
  844. begin
  845. inherited;
  846. with VertexProgram.Code do
  847. begin
  848. Add('uniform float Scale; ');
  849. Add('varying vec3 normal; ');
  850. Add('varying vec2 vTexCoord; ');
  851. Add('varying vec3 lightVec; ');
  852. Add('varying vec3 viewVec; ');
  853. Add('varying vec3 Position; ');
  854. Add(' ');
  855. Add('void main(void) { ');
  856. // Add(' mat4 mWorld = gl_ModelViewMatrix; ');
  857. Add(' vec3 Normal = gl_Normal; ');
  858. Add(' vec4 lightPos = gl_LightSource[0].position;');
  859. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  860. Add(' normal = gl_NormalMatrix * gl_Normal; ');
  861. Add(' Position = vec3(gl_Vertex)*Scale; ');
  862. Add(' vTexCoord = gl_MultiTexCoord0; ');
  863. Add(' lightVec = vec3(lightPos - vert); ');
  864. Add(' viewVec = -vec3(vert); ');
  865. Add(' gl_Position = ftransform(); ');
  866. Add('} ');
  867. end;
  868. with FragmentProgram.Code do
  869. begin
  870. Add('uniform float ErosionFactor; ');
  871. Add('uniform float IntensityFactor1; ');
  872. Add('uniform float IntensityFactor2; ');
  873. Add('uniform sampler2D MainTexture; ');
  874. Add('uniform sampler2D Noise2d; ');
  875. Add('uniform sampler2D ErosionTexture; ');
  876. Add('uniform vec4 SpecularColor; ');
  877. Add('uniform vec4 AmbientColor; ');
  878. Add('uniform float DiffuseIntensity; ');
  879. Add('uniform float AmbientIntensity; ');
  880. Add('uniform float SpecularIntensity; ');
  881. Add('uniform float SpecularRoughness; ');
  882. Add('uniform float AnisoRoughness; ');
  883. Add('varying vec3 normal; ');
  884. Add('varying vec2 vTexCoord; ');
  885. Add('varying vec3 lightVec; ');
  886. Add('varying vec3 viewVec; ');
  887. Add('varying vec3 Position; ');
  888. Add(' ');
  889. Add('void main (void) ');
  890. Add('{ ');
  891. Add(' vec3 offset = vec3(- ErosionFactor, - ErosionFactor + 0.06, - ErosionFactor * 0.92); ');
  892. Add(' vec4 DiffuseColor; ');
  893. Add(' vec4 Color1 = texture2D(MainTexture,vTexCoord); ');
  894. Add(' vec4 Color2 = texture2D(ErosionTexture,vTexCoord); ');
  895. Add(' // Compute noise ');
  896. Add(' vec3 noiseCoord = Position.xyz + offset; ');
  897. Add(' vec4 noiseVec = texture2D(Noise2d, noiseCoord.xy); ');
  898. Add(' float intensity = (abs(noiseVec[0] - 0.25) + ');
  899. Add(' abs(noiseVec[1] - 0.125) + ');
  900. Add(' abs(noiseVec[2] - 0.0625) + ');
  901. Add(' abs(noiseVec[3] - 0.03125)); ');
  902. Add(' // continue noise evaluation');
  903. Add(' intensity = IntensityFactor1 * (noiseVec.x + noiseVec.y+ noiseVec.z + noiseVec.w); ');
  904. Add(' intensity = IntensityFactor2 * abs(2.0 * intensity -1.0); ');
  905. Add(' // discard pixels in a psuedo-random fashion (noise) ');
  906. Add(' if (intensity < fract(0.5 - offset.x - offset.y - offset.z)) discard; ');
  907. Add(' // color fragments different colors using noise ');
  908. Add(' clamp(intensity, 0.0, 1.0); ');
  909. Add(' Color2.a =1.0-intensity; ');
  910. Add(' Color1.a =1.0; ');
  911. Add(' DiffuseColor = mix(Color2, Color1, intensity); ');
  912. Add(' DiffuseColor.a = intensity; ');
  913. Add(' // Anisotropic Specular Lighting Reflection ');
  914. // Anisotropic Specular Reflection
  915. // This is useful for depicting surfaces
  916. // such as velvet or brushed metal,
  917. // it allows you to stretch the highlight along a
  918. // SpecDirection vector (in object space)
  919. // add new var and replace the follow line
  920. // vec3 T = cross(norm,V) by vec3 T = cross(norm,normalize(SpecDirection));
  921. Add(' vec3 norm = normalize(normal); ');
  922. Add(' vec3 L = normalize(lightVec); ');
  923. Add(' vec3 V = normalize(viewVec); ');
  924. Add(' vec3 halfAngle = normalize(L + V); ');
  925. Add(' vec3 T = cross(norm,V); ');
  926. Add(' float NdotL = dot(L, norm); ');
  927. Add(' float NdotH = clamp(dot(halfAngle, norm), 0.0, 1.0); ');
  928. Add(' // "Half-Lambert" technique for more pleasing diffuse term ');
  929. Add(' float diffuse = 0.5 * NdotL + 0.5; ');
  930. Add(' float specular = pow(NdotH,1.0/SpecularRoughness); '); //54
  931. Add(' // Heidrich-Seidel anisotropic distribution ');
  932. Add(' float ldott = dot(L,T); ');
  933. Add(' float vdott = dot(V,T); ');
  934. Add(' float aniso = pow(sin(ldott)*sin(vdott) + cos(ldott)*cos(vdott),1.0/AnisoRoughness); ');
  935. Add(' vec3 FinalColour = AmbientColor*AmbientIntensity + ');
  936. Add(' DiffuseColor*diffuse*DiffuseIntensity + ');
  937. Add(' SpecularColor*aniso*specular*SpecularIntensity; ');
  938. Add(' gl_FragColor = vec4(FinalColour,intensity); ');
  939. Add('} ');
  940. end;
  941. //setup initial parameters
  942. FAmbientColor := TgxColor.Create(self);
  943. FAmbientColor.SetColor(0.2,0.2,0.2,1.0);
  944. FSpecularColor := TgxColor.Create(self);
  945. FSpecularColor.SetColor(0.75,0.75,0.75,1.0);
  946. FAmbientFactor := 0.8;
  947. FDiffuseFactor :=0.9;
  948. FSpecularFactor :=0.8;
  949. FSpecularRoughness :=0.45;
  950. FAnisotropicRoughness :=0.35;
  951. FErosionScale := 0.03;
  952. FErosionFactor := 0.35;
  953. FIntensityFactor1 := 0.75;
  954. FIntensityFactor2 := 1.95;
  955. end;
  956. destructor TgxCustomGLSLSimpleErosionShader.Destroy;
  957. begin
  958. FAmbientColor.Free;
  959. FSpecularColor.Free;
  960. inherited;
  961. end;
  962. procedure TgxCustomGLSLSimpleErosionShader.DoApply(var rci : TgxRenderContextInfo; Sender : TObject);
  963. begin
  964. GetGXSLProg.UseProgramObject;
  965. param['AmbientColor'].AsVector4f := FAmbientColor.Color;
  966. param['SpecularColor'].AsVector4f := FSpecularColor.Color;
  967. param['AmbientIntensity'].AsVector1f := FAmbientFactor;
  968. param['DiffuseIntensity'].AsVector1f := FDiffuseFactor;
  969. param['SpecularIntensity'].AsVector1f := FSpecularFactor;
  970. param['SpecularRoughness'].AsVector1f := FSpecularRoughness;
  971. param['AnisoRoughness'].AsVector1f := FAnisotropicRoughness;
  972. param['ErosionFactor'].AsVector1f := FErosionFactor;
  973. param['IntensityFactor1'].AsVector1f := FIntensityFactor1;
  974. param['IntensityFactor2'].AsVector1f := FIntensityFactor2;
  975. param['Scale'].AsVector1f := FErosionScale;
  976. param['MainTexture'].AsTexture2D[0] := FMainTex;
  977. param['Noise2d'].AsTexture2D[1] := FNoiseTex;
  978. param['ErosionTexture'].AsTexture2D[2] := FErosionTex;
  979. // GetGXSLProg.UniformTextureHandle['Noise2d', 1, GL_TEXTURE_2D] := FNoiseTexture.Handle;
  980. end;
  981. function TgxCustomGLSLSimpleErosionShader.DoUnApply(var rci: TgxRenderContextInfo): Boolean;
  982. begin
  983. GetGXSLProg.EndUseProgramObject;
  984. //gl.PopAttrib;
  985. Result := False;
  986. end;
  987. function TgxCustomGLSLSimpleErosionShader.GetMaterialLibrary: TgxAbstractMaterialLibrary;
  988. begin
  989. Result := FMaterialLibrary;
  990. end;
  991. procedure TgxCustomGLSLSimpleErosionShader.SetMaterialLibrary(const Value: TgxAbstractMaterialLibrary);
  992. begin
  993. if FMaterialLibrary <> nil then FMaterialLibrary.RemoveFreeNotification(Self);
  994. FMaterialLibrary := Value;
  995. if (FMaterialLibrary <> nil)
  996. and (FMaterialLibrary is TgxAbstractMaterialLibrary) then
  997. FMaterialLibrary.FreeNotification(Self);
  998. end;
  999. procedure TgxCustomGLSLSimpleErosionShader.SetMainTexTexture(const Value: TgxTexture);
  1000. begin
  1001. if FMainTex = Value then Exit;
  1002. FMainTex := Value;
  1003. NotifyChange(Self)
  1004. end;
  1005. procedure TgxCustomGLSLSimpleErosionShader.SetNoiseTexTexture(const Value: TgxTexture);
  1006. begin
  1007. if FNoiseTex = Value then Exit;
  1008. FNoiseTex := Value;
  1009. NotifyChange(Self);
  1010. end;
  1011. procedure TgxCustomGLSLSimpleErosionShader.SetErosionTexTexture(const Value: TgxTexture);
  1012. begin
  1013. if FErosionTex = Value then Exit;
  1014. FErosionTex := Value;
  1015. NotifyChange(Self);
  1016. end;
  1017. function TgxCustomGLSLSimpleErosionShader.GetNoiseTexName: TgxLibMaterialName;
  1018. begin
  1019. Result := TgxMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FNoiseTex);
  1020. if Result = '' then Result := FNoiseTexName;
  1021. end;
  1022. procedure TgxCustomGLSLSimpleErosionShader.SetNoiseTexName(const Value: TgxLibMaterialName);
  1023. begin
  1024. //Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1025. if FNoiseTexName = Value then Exit;
  1026. FNoiseTexName := Value;
  1027. FNoiseTex := TgxMaterialLibrary(FMaterialLibrary).TextureByName(FNoiseTexName);
  1028. NotifyChange(Self);
  1029. end;
  1030. function TgxCustomGLSLSimpleErosionShader.GetMainTexName: TgxLibMaterialName;
  1031. begin
  1032. Result := TgxMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FMainTex);
  1033. if Result = '' then Result := FMainTexName;
  1034. end;
  1035. procedure TgxCustomGLSLSimpleErosionShader.SetMainTexName(const Value: TgxLibMaterialName);
  1036. begin
  1037. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1038. if FMainTexName = Value then Exit;
  1039. FMainTexName := Value;
  1040. FMainTex := TgxMaterialLibrary(FMaterialLibrary).TextureByName(FMainTexName);
  1041. NotifyChange(Self);
  1042. end;
  1043. function TgxCustomGLSLSimpleErosionShader.GetErosionTexName: TgxLibMaterialName;
  1044. begin
  1045. Result := TgxMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FErosionTex);
  1046. if Result = '' then Result := FErosionTexName;
  1047. end;
  1048. procedure TgxCustomGLSLSimpleErosionShader.SetErosionTexName(const Value: TgxLibMaterialName);
  1049. begin
  1050. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1051. if FErosionTexName = Value then Exit;
  1052. FErosionTexName := Value;
  1053. FErosionTex := TgxMaterialLibrary(FMaterialLibrary).TextureByName(FErosionTexName);
  1054. NotifyChange(Self);
  1055. end;
  1056. procedure TgxCustomGLSLSimpleErosionShader.SetAmbientColor(AValue: TgxColor);
  1057. begin
  1058. FAmbientColor.DirectColor := AValue.Color;
  1059. end;
  1060. procedure TgxCustomGLSLSimpleErosionShader.SetSpecularColor(AValue: TgxColor);
  1061. begin
  1062. FSpecularColor.DirectColor := AValue.Color;
  1063. end;
  1064. procedure TgxCustomGLSLSimpleErosionShader.Notification(AComponent: TComponent; Operation: TOperation);
  1065. var
  1066. Index: Integer;
  1067. begin
  1068. inherited;
  1069. if Operation = opRemove then
  1070. if AComponent = FMaterialLibrary then
  1071. if FMaterialLibrary <> nil then
  1072. begin
  1073. // Need to nil the textures that were owned by it
  1074. if FNoiseTex <> nil then
  1075. begin
  1076. Index := TgxMaterialLibrary(FMaterialLibrary).Materials.GetTextureIndex(FNoiseTex);
  1077. if Index <> -1 then
  1078. SetNoiseTexTexture(nil);
  1079. end;
  1080. if FMainTex <> nil then
  1081. begin
  1082. Index := TgxMaterialLibrary(FMaterialLibrary).Materials.GetTextureIndex(FMainTex);
  1083. if Index <> -1 then
  1084. SetMainTexTexture(nil);
  1085. end;
  1086. if FErosionTex <> nil then
  1087. begin
  1088. Index := TgxMaterialLibrary(FMaterialLibrary).Materials.GetTextureIndex(FErosionTex);
  1089. if Index <> -1 then
  1090. SetErosionTexTexture(nil);
  1091. end;
  1092. FMaterialLibrary := nil;
  1093. end;
  1094. end;
  1095. (* ---------------------------------
  1096. TgxCustomGLSLGlassShader
  1097. ---------------------------------- *)
  1098. constructor TgxCustomGLSLGlassShader.Create(AOwner: TComponent);
  1099. begin
  1100. inherited;
  1101. with VertexProgram.Code do
  1102. begin
  1103. clear;
  1104. Add('varying vec3 Normal; ');
  1105. Add('varying vec3 EyeDir; ');
  1106. Add('varying vec4 EyePos; ');
  1107. Add('varying float LightIntensity; ');
  1108. Add('void main(void) ');
  1109. Add('{ ');
  1110. Add(' gl_Position = ftransform(); ');
  1111. Add(' vec3 LightPos = gl_LightSource[0].position.xyz;');
  1112. Add(' Normal = normalize(gl_NormalMatrix * gl_Normal); ');
  1113. Add(' vec4 pos = gl_ModelViewMatrix * gl_Vertex; ');
  1114. Add(' EyeDir = -pos.xyz; ');
  1115. Add(' EyePos = gl_ModelViewProjectionMatrix * gl_Vertex; ');
  1116. Add(' LightIntensity = max(dot(normalize(LightPos - EyeDir), Normal), 0.0); ');
  1117. Add('} ');
  1118. end;
  1119. with FragmentProgram.Code do
  1120. begin
  1121. clear;
  1122. Add('const vec3 Xunitvec = vec3 (1.0, 0.0, 0.0); ');
  1123. Add('const vec3 Yunitvec = vec3 (0.0, 1.0, 0.0); ');
  1124. Add('uniform vec4 BaseColor; ');
  1125. Add('uniform float Depth; ');
  1126. Add('uniform float MixRatio; ');
  1127. Add('uniform float AlphaIntensity; ');
  1128. // need to scale our framebuffer - it has a fixed width/height of 2048
  1129. Add('uniform float FrameWidth; ');
  1130. Add('uniform float FrameHeight; ');
  1131. Add('uniform sampler2D EnvMap; ');
  1132. Add('uniform sampler2D RefractionMap; ');
  1133. Add('varying vec3 Normal; ');
  1134. Add('varying vec3 EyeDir; ');
  1135. Add('varying vec4 EyePos; ');
  1136. Add('varying float LightIntensity; ');
  1137. Add('void main (void) ');
  1138. Add('{ ');
  1139. // Compute reflection vector
  1140. Add(' vec3 reflectDir = reflect(EyeDir, Normal); ');
  1141. // Compute altitude and azimuth angles
  1142. Add(' vec2 index; ');
  1143. Add(' index.y = dot(normalize(reflectDir), Yunitvec); ');
  1144. Add(' reflectDir.y = 0.0; ');
  1145. Add(' index.x = dot(normalize(reflectDir), Xunitvec) * 0.5; ');
  1146. // Translate index values into proper range
  1147. Add(' if (reflectDir.z >= 0.0) ');
  1148. Add(' index = (index + 1.0) * 0.5; ');
  1149. Add(' else ');
  1150. Add(' { ');
  1151. Add(' index.t = (index.t + 1.0) * 0.5; ');
  1152. Add(' index.s = (-index.s) * 0.5 + 1.0; ');
  1153. Add(' } ');
  1154. // if reflectDir.z >= 0.0, s will go from 0.25 to 0.75
  1155. // if reflectDir.z < 0.0, s will go from 0.75 to 1.25, and
  1156. // that's OK, because we've set the texture to wrap.
  1157. // Do a lookup into the environment map.
  1158. Add(' vec4 envColor = texture2D(EnvMap, index); ');
  1159. // calc fresnels term. This allows a view dependant blend of reflection/refraction
  1160. Add(' float fresnel = abs(dot(normalize(EyeDir), Normal)); ');
  1161. Add(' fresnel *= MixRatio; ');
  1162. Add(' fresnel = clamp(fresnel, 0.1, 0.9); ');
  1163. // calc refraction
  1164. Add(' vec3 refractionDir = normalize(EyeDir) - normalize(Normal); ');
  1165. // Scale the refraction so the z element is equal to depth
  1166. Add(' float depthVal = Depth / -refractionDir.z; ');
  1167. // perform the div by w
  1168. Add(' float recipW = 1.0 / EyePos.w; ');
  1169. Add(' vec2 eye = EyePos.xy * vec2(recipW); ');
  1170. // calc the refraction lookup
  1171. Add(' index.s = (eye.x + refractionDir.x * depthVal); ');
  1172. Add(' index.t = (eye.y + refractionDir.y * depthVal); ');
  1173. // scale and shift so we're in the range 0-1
  1174. Add(' index.s = index.s / 2.0 + 0.5; ');
  1175. Add(' index.t = index.t / 2.0 + 0.5; ');
  1176. // as we're looking at the framebuffer, we want it clamping at the edge of the rendered scene, not the edge of the texture,
  1177. // so we clamp before scaling to fit
  1178. Add(' float recip1k = 1.0 / 2048.0; ');
  1179. Add(' index.s = clamp(index.s, 0.0, 1.0 - recip1k); ');
  1180. Add(' index.t = clamp(index.t, 0.0, 1.0 - recip1k); ');
  1181. // scale the texture so we just see the rendered framebuffer
  1182. Add(' index.s = index.s * FrameWidth * recip1k; ');
  1183. Add(' index.t = index.t * FrameHeight * recip1k; ');
  1184. Add(' vec4 RefractionColor = texture2D(RefractionMap, index.st); ');
  1185. // Add(' RefractionColor.a = 0.9; ');
  1186. // Add(' RefractionColor = RefractionColor+vec3(0.75,0.75,0.75); ');//
  1187. // Add lighting to base color and mix
  1188. // Add(' vec4 base = LightIntensity * BaseColor; ');
  1189. Add(' envColor = mix(envColor, BaseColor,LightIntensity); ');
  1190. Add(' envColor = mix(envColor, RefractionColor, fresnel); ');
  1191. Add(' envColor.a = AlphaIntensity; ');
  1192. Add(' gl_FragColor = envColor; //vec4 (envColor.rgb, 0.3); ');
  1193. Add('} ');
  1194. end;
  1195. // FMainTexture := TgxTexture.Create(nil);
  1196. // FMainTexture.Disabled := False;
  1197. // FMainTexture.Enabled := True;
  1198. // setup initial parameters
  1199. FDiffuseColor := TgxColor.Create(Self);
  1200. FDepth := 0.1;
  1201. FMix := 1.0;
  1202. FAlpha := 1.0;
  1203. FDiffuseColor.SetColor(0.15, 0.15, 0.15, 1.0);
  1204. FBlendSrc := bfSrcAlpha;
  1205. FBlendDst := bfDstAlpha;
  1206. end;
  1207. destructor TgxCustomGLSLGlassShader.Destroy;
  1208. begin
  1209. FDiffuseColor.Destroy;
  1210. inherited;
  1211. end;
  1212. procedure TgxCustomGLSLGlassShader.DoApply(var rci: TgxRenderContextInfo;
  1213. Sender: TObject);
  1214. begin
  1215. // Auto Render EnvMap
  1216. // capture and create material from framebuffer
  1217. // I don't say why but We need to reset and reaffect our texture otherwise one of the texture is broken
  1218. with FMainTexture do
  1219. begin
  1220. PrepareBuildList;
  1221. glActiveTexture(GL_TEXTURE0_ARB);
  1222. glBindTexture(GL_TEXTURE_2D, Handle);
  1223. glActiveTexture(GL_TEXTURE0_ARB);
  1224. end;
  1225. with FRefractionTexture do
  1226. begin
  1227. PrepareBuildList;
  1228. glActiveTexture(GL_TEXTURE1_ARB);
  1229. glBindTexture(GL_TEXTURE_2D, Handle);
  1230. glActiveTexture(GL_TEXTURE0_ARB);
  1231. end;
  1232. FOwnerObject.Visible := False;
  1233. TgxSceneBuffer(rci.buffer).CopyToTexture(FMainTexture);
  1234. FOwnerObject.Visible := True;
  1235. GetGXSLProg.UseProgramObject;
  1236. // GetGXSLProg.Uniform4f['BaseColor'] := FDiffuseColor.Color;
  1237. // GetGXSLProg.Uniform1f['Depth'] := FDepth;
  1238. // GetGXSLProg.Uniform1f['MixRatio'] := FMix; // 0 - 2
  1239. // GetGXSLProg.Uniform1f['FrameWidth'] := fBuffSize * 3.125;
  1240. // GetGXSLProg.Uniform1f['FrameHeight'] := fBuffSize * 3.125;
  1241. // SetTex('EnvMap',FMainTexture); --> BUG
  1242. // SetTex('RefractionMap',FRefractionTexture);
  1243. param['BaseColor'].AsVector4f := FDiffuseColor.Color;
  1244. param['Depth'].AsVector1f := FDepth; // 0 - 0.3
  1245. param['MixRatio'].AsVector1f := FMix; // 0 - 2
  1246. param['AlphaIntensity'].AsVector1f := FAlpha; // 0 - 2
  1247. param['FrameWidth'].AsVector1f := fBuffSize * 3.75;
  1248. param['FrameHeight'].AsVector1f := fBuffSize * 3.75;
  1249. param['EnvMap'].AsTexture2D[0] := FMainTexture;
  1250. param['RefractionMap'].AsTexture2D[1] := FRefractionTexture;
  1251. glEnable(GL_BLEND);
  1252. glBlendFunc(cGLBlendFunctionToGLEnum[FBlendSrc],
  1253. cGLBlendFunctionToGLEnum[FBlendDst]);
  1254. end;
  1255. function TgxCustomGLSLGlassShader.DoUnApply
  1256. (var rci: TgxRenderContextInfo): Boolean;
  1257. begin
  1258. glDisable(GL_BLEND);
  1259. GetGXSLProg.EndUseProgramObject;
  1260. Result := False;
  1261. end;
  1262. function TgxCustomGLSLGlassShader.GetMaterialLibrary
  1263. : TgxAbstractMaterialLibrary;
  1264. begin
  1265. Result := FMaterialLibrary;
  1266. end;
  1267. procedure TgxCustomGLSLGlassShader.SetMaterialLibrary
  1268. (const Value: TgxAbstractMaterialLibrary);
  1269. begin
  1270. if FMaterialLibrary <> nil then
  1271. FMaterialLibrary.RemoveFreeNotification(Self);
  1272. FMaterialLibrary := Value;
  1273. if (FMaterialLibrary <> nil) and
  1274. (FMaterialLibrary is TgxAbstractMaterialLibrary) then
  1275. FMaterialLibrary.FreeNotification(Self);
  1276. end;
  1277. procedure TgxCustomGLSLGlassShader.SetMainTexTexture(const Value: TgxTexture);
  1278. begin
  1279. if FMainTexture = Value then
  1280. Exit;
  1281. FMainTexture := Value;
  1282. NotifyChange(Self)
  1283. end;
  1284. function TgxCustomGLSLGlassShader.GetMainTexName: TgxLibMaterialName;
  1285. begin
  1286. Result := TgxMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FMainTexture);
  1287. if Result = '' then
  1288. Result := FMainTexName;
  1289. end;
  1290. procedure TgxCustomGLSLGlassShader.SetMainTexName
  1291. (const Value: TgxLibMaterialName);
  1292. begin
  1293. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1294. if FMainTexName = Value then
  1295. Exit;
  1296. FMainTexName := Value;
  1297. FMainTexture := TgxMaterialLibrary(FMaterialLibrary)
  1298. .TextureByName(FMainTexName);
  1299. NotifyChange(Self);
  1300. end;
  1301. procedure TgxCustomGLSLGlassShader.SetRefractionTexTexture
  1302. (const Value: TgxTexture);
  1303. begin
  1304. if FRefractionTexture = Value then
  1305. Exit;
  1306. FRefractionTexture := Value;
  1307. NotifyChange(Self)
  1308. end;
  1309. function TgxCustomGLSLGlassShader.GetRefractionTexName: TgxLibMaterialName;
  1310. begin
  1311. Result := TgxMaterialLibrary(FMaterialLibrary)
  1312. .GetNameOfTexture(FRefractionTexture);
  1313. if Result = '' then
  1314. Result := FRefractionTexName;
  1315. end;
  1316. procedure TgxCustomGLSLGlassShader.SetRefractionTexName
  1317. (const Value: TgxLibMaterialName);
  1318. begin
  1319. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1320. if FRefractionTexName = Value then
  1321. Exit;
  1322. FRefractionTexName := Value;
  1323. FRefractionTexture := TgxMaterialLibrary(FMaterialLibrary)
  1324. .TextureByName(FRefractionTexName);
  1325. NotifyChange(Self);
  1326. end;
  1327. procedure TgxCustomGLSLGlassShader.SetDiffuseColor(AValue: TgxColor);
  1328. begin
  1329. FDiffuseColor.DirectColor := AValue.Color;
  1330. end;
  1331. procedure TgxCustomGLSLGlassShader.Notification(AComponent: TComponent;
  1332. Operation: TOperation);
  1333. var
  1334. Index: Integer;
  1335. begin
  1336. inherited;
  1337. if Operation = opRemove then
  1338. if AComponent = FMaterialLibrary then
  1339. if FMaterialLibrary <> nil then
  1340. begin
  1341. if FMainTexture <> nil then
  1342. begin
  1343. Index := TgxMaterialLibrary(FMaterialLibrary)
  1344. .Materials.GetTextureIndex(FMainTexture);
  1345. if Index <> -1 then
  1346. SetMainTexTexture(nil);
  1347. end;
  1348. if FRefractionTexture <> nil then
  1349. begin
  1350. Index := TgxMaterialLibrary(FMaterialLibrary)
  1351. .Materials.GetTextureIndex(FRefractionTexture);
  1352. if Index <> -1 then
  1353. SetRefractionTexTexture(nil);
  1354. end;
  1355. FMaterialLibrary := nil;
  1356. end;
  1357. end;
  1358. //-------------------------------------------------------------
  1359. // TgxCustomGLSLSimpleGoochShader
  1360. //-------------------------------------------------------------
  1361. constructor TgxCustomGLSLSimpleGoochShader.Create(AOwner: TComponent);
  1362. begin
  1363. inherited;
  1364. with VertexProgram.Code do
  1365. begin
  1366. Clear;
  1367. Add('varying vec3 vNormal; ');
  1368. Add('varying vec3 lightVec; ');
  1369. Add('varying vec3 viewVec; ');
  1370. Add('varying vec3 ReflectVec; ');
  1371. Add(' ');
  1372. Add('void main() ');
  1373. Add('{ ');
  1374. Add(' gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; ');
  1375. Add(' vec4 lightPos = gl_LightSource[0].position;');
  1376. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  1377. Add(' vec3 normal = gl_NormalMatrix * gl_Normal; ');
  1378. Add(' vNormal = normalize(normal); ');
  1379. Add(' lightVec = vec3(lightPos - vert); ');
  1380. Add(' ReflectVec = normalize(reflect(-lightVec, vNormal)); ');
  1381. Add(' viewVec = -vec3(vert); ');
  1382. Add('} ');
  1383. end;
  1384. with FragmentProgram.Code do
  1385. begin
  1386. Clear;
  1387. Add('uniform vec4 SurfaceColor; ');
  1388. Add('uniform vec4 WarmColor; ');
  1389. Add('uniform vec4 CoolColor; ');
  1390. Add('uniform vec4 SpecularColor; ');
  1391. Add('uniform vec4 AmbientColor; ');
  1392. Add('uniform float DiffuseWarm; ');
  1393. Add('uniform float DiffuseCool; ');
  1394. Add('uniform float AmbientFactor; ');
  1395. Add('uniform float DiffuseFactor; ');
  1396. Add('uniform float SpecularFactor; ');
  1397. Add('varying vec3 vNormal; ');
  1398. Add('varying vec3 lightVec; ');
  1399. Add('varying vec3 viewVec; ');
  1400. Add('varying vec3 ReflectVec; ');
  1401. Add(' ');
  1402. Add('void main() ');
  1403. Add('{ ');
  1404. Add('vec3 L = normalize(lightVec); ');
  1405. Add('vec3 V = normalize(viewVec); ');
  1406. Add('vec3 halfAngle = normalize(L + V); ');
  1407. Add('float NdotL = (dot(L, vNormal) + 1.0) * 0.5; ');
  1408. Add('float NdotH = clamp(dot(halfAngle, vNormal), 0.0, 1.0); ');
  1409. Add('// "Half-Lambert" technique for more pleasing diffuse term ');
  1410. Add('float diffuse = 0.5 * NdotL + 0.5; ');
  1411. Add('vec3 nreflect = normalize(ReflectVec); ');
  1412. Add('float specular = max(dot(nreflect, V), 0.0); ');
  1413. Add('specular = pow(specular, 64.0); ');
  1414. Add('vec4 kCool = min(CoolColor + DiffuseCool * SurfaceColor, 1.0); ');
  1415. Add('vec4 kWarm = min(WarmColor + DiffuseWarm * SurfaceColor, 1.0); ');
  1416. Add('vec4 Cgooch = mix(kWarm, kCool, diffuse); ');
  1417. Add('vec3 result = AmbientFactor * AmbientColor.rgb + DiffuseFactor * Cgooch.rgb + SpecularColor.rgb * SpecularFactor *specular; ');
  1418. Add('gl_FragColor = vec4(result,SurfaceColor.a); ');
  1419. Add('} ');
  1420. end;
  1421. // Initial stuff.
  1422. FDiffuseColor := TgxColor.Create(self);
  1423. FDiffuseColor.SetColor(0.75,0.75,0.75,1.0);
  1424. FWarmColor := TgxColor.Create(self);
  1425. FWarmColor.SetColor(0.88,0.81,0.49,1.0);
  1426. FCoolColor := TgxColor.Create(self);
  1427. FCoolColor.SetColor(0.58,0.10,0.76,1.0);
  1428. FAmbientColor := TgxColor.Create(self);
  1429. FAmbientColor.SetColor(0.3,0.3,0.3,1.0);
  1430. FSpecularColor := TgxColor.Create(self);
  1431. FSpecularColor.SetColor(1.0,1.0,1.0,1.0);
  1432. FDiffuseWarm := 0.55;
  1433. FDiffuseCool := 0.30;
  1434. FAmbientFactor := 1.0;
  1435. FDiffuseFactor :=0.8;
  1436. FSpecularFactor :=0.9;
  1437. FBlendingMode:=bmxOpaque;
  1438. end;
  1439. destructor TgxCustomGLSLSimpleGoochShader.Destroy;
  1440. begin
  1441. FDiffuseColor.Free;
  1442. FWarmColor.Free;
  1443. FCoolColor.Free;
  1444. FSpecularColor.Free;
  1445. FAmbientColor.Free;
  1446. inherited;
  1447. end;
  1448. procedure TgxCustomGLSLSimpleGoochShader.DoApply(var rci: TgxRenderContextInfo;
  1449. Sender: TObject);
  1450. begin
  1451. GetGXSLProg.UseProgramObject;
  1452. param['SurfaceColor'].AsVector4f := FDiffuseColor.Color;
  1453. param['WarmColor'].AsVector4f := FWarmColor.Color;
  1454. param['CoolColor'].AsVector4f := FCoolColor.Color;
  1455. param['AmbientColor'].AsVector4f := FAmbientColor.Color;
  1456. param['SpecularColor'].AsVector4f := FSpecularColor.Color;
  1457. param['DiffuseWarm'].AsVector1f := FDiffuseWarm;
  1458. param['DiffuseCool'].AsVector1f := FDiffuseCool;
  1459. param['AmbientFactor'].AsVector1f := FAmbientFactor;
  1460. param['DiffuseFactor'].AsVector1f := FDiffuseFactor;
  1461. param['SpecularFactor'].AsVector1f := FSpecularFactor;
  1462. // gl.PushAttrib(GL_COLOR_BUFFER_BIT);
  1463. ApplyBlendingModeEx(FBlendingMode);
  1464. // gl.Enable(GL_BLEND);
  1465. // gl.BlendFunc(cGLBlendFunctionToGLEnum[FBlendSrc],cGLBlendFunctionToGLEnum[FBlendDst]);
  1466. end;
  1467. function TgxCustomGLSLSimpleGoochShader.DoUnApply(var rci: TgxRenderContextInfo): Boolean;
  1468. begin
  1469. glActiveTexture(GL_TEXTURE0_ARB);
  1470. GetGXSLProg.EndUseProgramObject;
  1471. UnApplyBlendingModeEx;
  1472. // gl.PopAttrib;
  1473. Result := False;
  1474. end;
  1475. procedure TgxCustomGLSLSimpleGoochShader.SetDiffuseColor(AValue: TgxColor);
  1476. begin
  1477. FDiffuseColor.DirectColor := AValue.Color;
  1478. end;
  1479. procedure TgxCustomGLSLSimpleGoochShader.SetAmbientColor(AValue: TgxColor);
  1480. begin
  1481. FAmbientColor.DirectColor := AValue.Color;
  1482. end;
  1483. procedure TgxCustomGLSLSimpleGoochShader.SetSpecularColor(AValue: TgxColor);
  1484. begin
  1485. FSpecularColor.DirectColor := AValue.Color;
  1486. end;
  1487. procedure TgxCustomGLSLSimpleGoochShader.SetWarmColor(AValue: TgxColor);
  1488. begin
  1489. FWarmColor.DirectColor := AValue.Color;
  1490. end;
  1491. procedure TgxCustomGLSLSimpleGoochShader.SetCoolColor(AValue: TgxColor);
  1492. begin
  1493. FCoolColor.DirectColor := AValue.Color;
  1494. end;
  1495. (*------------------------------------------
  1496. // TgxCustomGLSLFurShader
  1497. ------------------------------------------*)
  1498. constructor TgxCustomGLSLFurShader.Create(AOwner: TComponent);
  1499. begin
  1500. inherited;
  1501. with VertexProgram.Code do
  1502. begin
  1503. clear;
  1504. Add('uniform float fFurLength; ');
  1505. Add('uniform float fFurMaxLength; ');
  1506. Add('uniform float pass_index; ');
  1507. Add('uniform int UseRandomLength; ');
  1508. Add('uniform float fLayer; // 0 to 1 for the level ');
  1509. Add('uniform vec3 vGravity; ');
  1510. Add('varying vec3 normal; ');
  1511. Add('varying vec2 vTexCoord; ');
  1512. Add('varying vec3 lightVec; ');
  1513. // Add('varying vec3 viewVec; ');
  1514. Add('float rand(vec2 co){ ');
  1515. Add(' return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); ');
  1516. Add('} ');
  1517. Add('void main() ');
  1518. Add('{ ');
  1519. Add(' mat4 mWorld = gl_ModelViewMatrix; ');
  1520. Add(' vec3 Normal = gl_Normal; ');
  1521. Add(' vec4 Position = gl_Vertex; ');
  1522. Add(' vec4 lightPos = gl_LightSource[0].position;');
  1523. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  1524. Add(' normal = gl_NormalMatrix * gl_Normal; ');
  1525. // Additional Gravit/Force Code
  1526. Add(' vec3 vGravity2 = vGravity *mat3(mWorld ); ');
  1527. // We use the pow function, so that only the tips of the hairs bend
  1528. Add(' float k = pow(fLayer, 3.0); ');
  1529. // Random the Hair length perhaps will can use a texture map for controling.
  1530. Add(' vec3 vNormal = normalize( Normal * mat3(mWorld )); ');
  1531. Add(' float RandomFurLength; ');
  1532. Add(' if (UseRandomLength == 1) { RandomFurLength = fFurLength+fFurLength*rand(vNormal.xy); } ');
  1533. Add(' else { RandomFurLength = fFurLength ; } ');
  1534. Add(' RandomFurLength = pass_index*(RandomFurLength * vNormal); ');
  1535. Add(' if (RandomFurLength > fFurMaxLength ) { RandomFurLength = fFurMaxLength; } ');
  1536. Add(' Position.xyz += RandomFurLength +(vGravity2 * k); ');
  1537. Add(' Position.xyz += pass_index*(fFurLength * Normal)+(vGravity2 * k); ');
  1538. Add(' vTexCoord = gl_MultiTexCoord0; ');
  1539. Add(' ');
  1540. Add(' gl_Position = gl_ModelViewProjectionMatrix * Position; ');
  1541. Add(' lightVec = vec3(lightPos - vert); ');
  1542. // Add(' viewVec = -vec3(vert); ');
  1543. Add('normal = vNormal; ');
  1544. Add('} ');
  1545. end;
  1546. with FragmentProgram.Code do
  1547. begin
  1548. clear;
  1549. Add('uniform vec4 fcolorScale; ');
  1550. Add('uniform float pass_index; ');
  1551. Add('uniform float fFurScale; ');
  1552. Add('uniform vec4 vAmbient; ');
  1553. Add('uniform float fLayer; // 0 to 1 for the level ');
  1554. Add('uniform float vLightIntensity; ');
  1555. Add('uniform sampler2D FurTexture; ');
  1556. Add('uniform sampler2D ColourTexture; ');
  1557. //textures
  1558. Add('varying vec2 vTexCoord; ');
  1559. Add('varying vec3 normal; ');
  1560. Add('varying vec3 lightVec; ');
  1561. // Add('varying vec3 viewVec; ');
  1562. Add('void main() ');
  1563. Add('{ ');
  1564. // A Faking shadow
  1565. Add(' vec4 fAlpha = texture2D( FurTexture, vTexCoord*fFurScale ); ');
  1566. Add(' float fakeShadow = mix(0.3, 1.0, fAlpha.a-fLayer); ');
  1567. Add(' ');
  1568. Add(' vec4 FinalColour = vec4(0.0,0.0,0.0,1.0); ');
  1569. Add('FinalColour = (fcolorScale*texture2D( ColourTexture, vTexCoord))*fakeShadow; ');
  1570. // This comment part it's for controling if we must draw the hair according the red channel and the alpha in NoiseMap
  1571. // Don' t work well a this time the NoiseMap must be perfect
  1572. // Add('float visibility = 0.0; ');
  1573. // Add('if (pass_index == 1.0) ');
  1574. // Add('{ ');
  1575. // Add(' visibility = 1.0; ');
  1576. // Add('} ');
  1577. // Add('else ');
  1578. // Add('{ ');
  1579. // Add(' if (fAlpha.a<fAlpha.r) { visibility = 0.0; } ');
  1580. // Add(' else { visibility =mix(0.1,1.0,(1.02-fLayer)); } //-1.0; ');
  1581. // Add('} ');
  1582. Add('float visibility =mix(0.1,1.0,(1.02-fLayer)); '); // The Last past must be transparent
  1583. // Simply Lighting - For this time only ONE light source is supported
  1584. Add('vec4 ambient = vAmbient*FinalColour; ');
  1585. Add('vec4 diffuse = FinalColour; ');
  1586. Add('vec3 L = normalize(lightVec); ');
  1587. Add('float NdotL = dot(L, normal); ');
  1588. Add('// "Half-Lambert" technique for more pleasing diffuse term ');
  1589. Add('diffuse = diffuse*(0.5*NdotL+0.5); ');
  1590. Add('FinalColour = vLightIntensity*(ambient+ diffuse); // + no specular; ');
  1591. Add('FinalColour.a = visibility ; ');
  1592. Add(' // Return the calculated color ');
  1593. Add(' gl_FragColor= FinalColour; ');
  1594. Add('} ');
  1595. end;
  1596. //Fur stuff
  1597. FPassCount := 16; // More is greater more the fur is dense
  1598. FFurLength := 0.3000; // The minimal Hair length
  1599. FMaxFurLength := 3.0;
  1600. FRandomFurLength := false;
  1601. FFurScale:=1.0;
  1602. FColorScale := TgxColor.Create(Self);
  1603. FColorScale.SetColor(0.2196,0.2201,0.2201,1.0);
  1604. FAmbient := TgxColor.Create(Self);
  1605. FAmbient.SetColor(1.0,1.0,1.0,1.0);
  1606. // The Blend Funcs are very important for realistic fur rendering it can vary follow your textures
  1607. FBlendSrc := bfOneMinusSrcColor;
  1608. FBlendDst := bfOneMinusSrcAlpha;
  1609. FGravity := TgxCoordinates.Create(self);
  1610. FGravity.AsAffineVector := AffinevectorMake(0.0,0.0,0.0);
  1611. FLightIntensity := 2.5;
  1612. end;
  1613. destructor TgxCustomGLSLFurShader.Destroy;
  1614. begin
  1615. Enabled:=false;
  1616. FGravity.Free;
  1617. FColorScale.Destroy;
  1618. FAmbient.Destroy;
  1619. inherited;
  1620. end;
  1621. procedure TgxCustomGLSLFurShader.DoApply(var rci: TgxRenderContextInfo;Sender: TObject);
  1622. begin
  1623. GetGXSLProg.UseProgramObject;
  1624. //Fur stuff
  1625. FCurrentPass := 1;
  1626. param['pass_index'].AsVector1f := 1.0;
  1627. param['fFurLength'].AsVector1f := FFurLength;
  1628. param['fFurMaxLength'].AsVector1f := FMaxFurLength;
  1629. param['fFurScale'].AsVector1f := FFurScale;
  1630. if FRandomFurLength then param['UseRandomLength'].AsVector1i := 1
  1631. else param['UseRandomLength'].AsVector1i := 0;
  1632. param['fcolorScale'].AsVector4f := FColorScale.Color;
  1633. param['FurTexture'].AsTexture2D[0] := FNoiseTex;
  1634. param['ColourTexture'].AsTexture2D[1] := FMainTex;
  1635. param['vGravity'].AsVector3f := FGravity.AsAffineVector;
  1636. param['vAmbient'].AsVector4f := FAmbient.Color; //vectorMake(0.5,0.5,0.5,1.0);
  1637. param['fLayer'].AsVector1f := 1/PassCount;
  1638. param['vLightIntensity'].AsVector1f := FLightIntensity;
  1639. glPushAttrib(GL_COLOR_BUFFER_BIT);
  1640. glEnable(GL_BLEND);
  1641. glBlendFunc(cGLBlendFunctionToGLEnum[FBlendSrc],cGLBlendFunctionToGLEnum[FBlendDst]);
  1642. // gl.BlendFunc(GL_SRC_ALPHA, cGLBlendFunctionToGLEnum[FBlendSrc]);
  1643. // gl.BlendFunc(GL_DST_ALPHA,cGLBlendFunctionToGLEnum[FBlendDst]);
  1644. // gl.BlendEquation(cGLBlendEquationToGLEnum[BlendEquation]);
  1645. end;
  1646. function TgxCustomGLSLFurShader.DoUnApply(var rci: TgxRenderContextInfo): Boolean;
  1647. begin
  1648. if FCurrentPass < PassCount then
  1649. begin
  1650. Inc(FCurrentPass);
  1651. //GetGXSLProg.Uniform1f['pass_index'] := FCurrentPass;
  1652. param['pass_index'].AsVector1f := FCurrentPass;
  1653. param['fLayer'].AsVector1f := FCurrentPass/PassCount;
  1654. Result := True;
  1655. end
  1656. else
  1657. begin
  1658. // glActiveTextureARB(GL_TEXTURE0_ARB);
  1659. glActiveTexture(GL_TEXTURE0_ARB);
  1660. GetGXSLProg.EndUseProgramObject;
  1661. glPopAttrib;
  1662. Result := False;
  1663. end;
  1664. end;
  1665. function TgxCustomGLSLFurShader.GetMaterialLibrary: TgxAbstractMaterialLibrary;
  1666. begin
  1667. Result := FMaterialLibrary;
  1668. end;
  1669. procedure TgxCustomGLSLFurShader.SetMaterialLibrary(const Value: TgxAbstractMaterialLibrary);
  1670. begin
  1671. if FMaterialLibrary <> nil then FMaterialLibrary.RemoveFreeNotification(Self);
  1672. FMaterialLibrary := Value;
  1673. if (FMaterialLibrary <> nil)
  1674. and (FMaterialLibrary is TgxAbstractMaterialLibrary) then
  1675. FMaterialLibrary.FreeNotification(Self);
  1676. end;
  1677. procedure TgxCustomGLSLFurShader.SetMainTexTexture(const Value: TgxTexture);
  1678. begin
  1679. if FMainTex = Value then
  1680. Exit;
  1681. FMainTex := Value;
  1682. NotifyChange(self)
  1683. end;
  1684. procedure TgxCustomGLSLFurShader.SetNoiseTexTexture(const Value: TgxTexture);
  1685. begin
  1686. if FNoiseTex = Value then
  1687. Exit;
  1688. FNoiseTex := Value;
  1689. NotifyChange(self);
  1690. end;
  1691. function TgxCustomGLSLFurShader.GetNoiseTexName: TgxLibMaterialName;
  1692. begin
  1693. Result := TgxMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FNoiseTex);
  1694. if Result = '' then
  1695. Result := FNoiseTexName;
  1696. end;
  1697. procedure TgxCustomGLSLFurShader.SetNoiseTexName
  1698. (const Value: TgxLibMaterialName);
  1699. begin
  1700. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1701. if FNoiseTexName = Value then
  1702. Exit;
  1703. FNoiseTexName := Value;
  1704. FNoiseTex := TgxMaterialLibrary(FMaterialLibrary)
  1705. .TextureByName(FNoiseTexName);
  1706. NotifyChange(self);
  1707. end;
  1708. function TgxCustomGLSLFurShader.GetMainTexName: TgxLibMaterialName;
  1709. begin
  1710. Result := TgxMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FMainTex);
  1711. if Result = '' then
  1712. Result := FMainTexName;
  1713. end;
  1714. procedure TgxCustomGLSLFurShader.SetMainTexName
  1715. (const Value: TgxLibMaterialName);
  1716. begin
  1717. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1718. if FMainTexName = Value then
  1719. Exit;
  1720. FMainTexName := Value;
  1721. FMainTex := TgxMaterialLibrary(FMaterialLibrary)
  1722. .TextureByName(FMainTexName);
  1723. NotifyChange(self);
  1724. end;
  1725. procedure TgxCustomGLSLFurShader.Notification(AComponent: TComponent;
  1726. Operation: TOperation);
  1727. var
  1728. Index: Integer;
  1729. begin
  1730. inherited;
  1731. if Operation = opRemove then
  1732. if AComponent = FMaterialLibrary then
  1733. if FMaterialLibrary <> nil then
  1734. begin
  1735. // Need to nil the textures that were owned by it
  1736. if FNoiseTex <> nil then
  1737. begin
  1738. Index := TgxMaterialLibrary(FMaterialLibrary)
  1739. .Materials.GetTextureIndex(FNoiseTex);
  1740. if Index <> -1 then
  1741. SetNoiseTexTexture(nil);
  1742. end;
  1743. if FMainTex <> nil then
  1744. begin
  1745. Index := TgxMaterialLibrary(FMaterialLibrary)
  1746. .Materials.GetTextureIndex(FMainTex);
  1747. if Index <> -1 then
  1748. SetMainTexTexture(nil);
  1749. end;
  1750. FMaterialLibrary := nil;
  1751. end;
  1752. end;
  1753. procedure TgxCustomGLSLFurShader.SetGravity(APosition: TgxCoordinates);
  1754. begin
  1755. FGravity.SetPoint(APosition.DirectX, APosition.DirectY, APosition.DirectZ);
  1756. end;
  1757. procedure TgxCustomGLSLFurShader.SetAmbient(AValue: TgxColor);
  1758. begin
  1759. FAmbient.DirectColor := AValue.Color;
  1760. end;
  1761. procedure TgxCustomGLSLFurShader.SetColorScale(AValue: TgxColor);
  1762. begin
  1763. FColorScale.DirectColor := AValue.Color;
  1764. end;
  1765. (*****************************************************
  1766. TgxCustomGLSLIvoryShader
  1767. ****************************************************)
  1768. constructor TgxCustomGLSLIvoryShader.Create(AOwner: TComponent);
  1769. begin
  1770. inherited;
  1771. with VertexProgram.Code do
  1772. begin
  1773. clear;
  1774. Add('varying vec3 normal; ');
  1775. Add('varying vec3 lightVec; ');
  1776. Add('varying vec3 viewVec; ');
  1777. Add(' ');
  1778. Add('void main() ');
  1779. Add('{ ');
  1780. Add(' gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; ');
  1781. Add(' vec4 lightPos = gl_LightSource[0].position;');
  1782. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  1783. Add(' normal = gl_NormalMatrix * gl_Normal; ');
  1784. Add(' lightVec = vec3(lightPos - vert); ');
  1785. Add(' viewVec = -vec3(vert); ');
  1786. Add('} ');
  1787. end;
  1788. with FragmentProgram.Code do
  1789. begin
  1790. clear;
  1791. Add('varying vec3 normal; ');
  1792. Add('varying vec3 lightVec; ');
  1793. Add('varying vec3 viewVec; ');
  1794. Add(' ');
  1795. Add('void main() ');
  1796. Add('{ ');
  1797. Add('vec3 norm = normalize(normal); ');
  1798. Add('vec3 L = normalize(lightVec); ');
  1799. Add('vec3 V = normalize(viewVec); ');
  1800. Add('vec3 halfAngle = normalize(L + V); ');
  1801. Add('float NdotL = dot(L, norm); ');
  1802. Add('float NdotH = clamp(dot(halfAngle, norm), 0.0, 1.0); ');
  1803. Add('// "Half-Lambert" technique for more pleasing diffuse term ');
  1804. Add('float diffuse = 0.5 * NdotL + 0.5; ');
  1805. Add('float specular = pow(NdotH, 64.0); ');
  1806. Add('float result = diffuse + specular; ');
  1807. Add('gl_FragColor = vec4(result); ');
  1808. Add('} ');
  1809. end;
  1810. // Initial stuff.
  1811. end;
  1812. destructor TgxCustomGLSLIvoryShader.Destroy;
  1813. begin
  1814. inherited;
  1815. end;
  1816. procedure TgxCustomGLSLIvoryShader.DoApply(var rci: TgxRenderContextInfo;
  1817. Sender: TObject);
  1818. begin
  1819. GetGXSLProg.UseProgramObject;
  1820. end;
  1821. function TgxCustomGLSLIvoryShader.DoUnApply
  1822. (var rci: TgxRenderContextInfo): Boolean;
  1823. begin
  1824. Result := False;
  1825. GetGXSLProg.EndUseProgramObject;
  1826. end;
  1827. (*****************************************************
  1828. TgxCustomGLSLSimpleLatticeShader
  1829. *****************************************************)
  1830. constructor TgxCustomGLSLSimpleLatticeShader.Create(AOwner: TComponent);
  1831. begin
  1832. inherited;
  1833. with FragmentProgram.Code do
  1834. begin
  1835. clear;
  1836. Add(' uniform vec2 Scale; ');
  1837. Add(' uniform vec2 Threshold; ');
  1838. Add(' ');
  1839. Add(' void main (void) ');
  1840. Add('{ ');
  1841. Add(' float ss = fract(gl_TexCoord[0].s * Scale.s); ');
  1842. Add(' float tt = fract(gl_TexCoord[0].t * Scale.t); ');
  1843. Add(' ');
  1844. Add(' if ((ss > Threshold.s) && (tt > Threshold.t)) discard; ');
  1845. Add(' gl_FragColor = gl_Color;');
  1846. Add('} ');
  1847. end;
  1848. // Initial stuff.
  1849. FLatticeScale := TgxCoordinates2.Create(self);
  1850. FLatticeThreshold := TgxCoordinates2.Create(self);
  1851. FLatticeScale.SetPoint2D(10, 40);
  1852. FLatticeThreshold.SetPoint2D(0.15, 0.3);
  1853. end;
  1854. destructor TgxCustomGLSLSimpleLatticeShader.Destroy;
  1855. begin
  1856. FLatticeScale.Destroy;
  1857. FLatticeThreshold.Destroy;
  1858. inherited;
  1859. end;
  1860. procedure TgxCustomGLSLSimpleLatticeShader.DoApply
  1861. (var rci: TgxRenderContextInfo; Sender: TObject);
  1862. begin
  1863. GetGXSLProg.UseProgramObject;
  1864. param['Scale'].AsVector2f := FLatticeScale.AsPoint2D;
  1865. param['Threshold'].AsVector2f := FLatticeThreshold.AsPoint2D;
  1866. end;
  1867. function TgxCustomGLSLSimpleLatticeShader.DoUnApply
  1868. (var rci: TgxRenderContextInfo): Boolean;
  1869. begin
  1870. Result := False;
  1871. // gl.ActiveTexture(GL_TEXTURE0_ARB);
  1872. GetGXSLProg.EndUseProgramObject;
  1873. end;
  1874. procedure TgxCustomGLSLSimpleLatticeShader.SetLatticeScale
  1875. (const Value: TgxCoordinates2);
  1876. begin
  1877. FLatticeScale.Assign(Value);
  1878. end;
  1879. procedure TgxCustomGLSLSimpleLatticeShader.SetLatticeThreshold
  1880. (const Value: TgxCoordinates2);
  1881. begin
  1882. FLatticeThreshold.Assign(Value);
  1883. end;
  1884. // TgxCustomGLSLLatticeShader
  1885. constructor TgxCustomGLSLLatticeShader.Create(AOwner: TComponent);
  1886. begin
  1887. inherited;
  1888. FAmbientColor := TgxColor.Create(self);
  1889. FDiffuseColor := TgxColor.Create(self);
  1890. FSpecularColor := TgxColor.Create(self);
  1891. // setup initial parameters
  1892. FAmbientColor.SetColor(0.15, 0.15, 0.15, 1);
  1893. FDiffuseColor.SetColor(1, 1, 1, 1);
  1894. FSpecularColor.SetColor(1, 1, 1, 1);
  1895. FSpecularPower := 8; // 6
  1896. FLightPower := 1;
  1897. end;
  1898. destructor TgxCustomGLSLLatticeShader.Destroy;
  1899. begin
  1900. FAmbientColor.Destroy;
  1901. FDiffuseColor.Destroy;
  1902. FSpecularColor.Destroy;
  1903. inherited;
  1904. end;
  1905. procedure TgxCustomGLSLLatticeShader.DoApply(var rci: TgxRenderContextInfo;
  1906. Sender: TObject);
  1907. begin
  1908. inherited;
  1909. param['AmbientColor'].AsVector4f := FAmbientColor.Color;
  1910. param['DiffuseColor'].AsVector4f := FDiffuseColor.Color;
  1911. param['SpecularColor'].AsVector4f := FSpecularColor.Color;
  1912. param['SpecPower'].AsVector1f := FSpecularPower;
  1913. param['LightIntensity'].AsVector1f := FLightPower;
  1914. param['MainTexture'].AsTexture2D[0] := FMainTexture;
  1915. end;
  1916. procedure TgxCustomGLSLLatticeShader.DoInitialize
  1917. (var rci: TgxRenderContextInfo; Sender: TObject);
  1918. begin
  1919. with VertexProgram.Code do
  1920. begin
  1921. clear;
  1922. Add('varying vec3 Normal; ');
  1923. Add('varying vec3 LightVector; ');
  1924. Add('varying vec3 CameraVector; ');
  1925. Add('varying vec2 Texcoord; ');
  1926. Add(' ');
  1927. Add(' ');
  1928. Add('void main(void) ');
  1929. Add('{ ');
  1930. Add(' gl_Position = ftransform(); ');
  1931. Add(' Texcoord = gl_MultiTexCoord0.xy; ');
  1932. Add(' Normal = normalize(gl_NormalMatrix * gl_Normal); ');
  1933. Add(' vec3 p = (gl_ModelViewMatrix * gl_Vertex).xyz; ');
  1934. Add(' LightVector = normalize(gl_LightSource[0].position.xyz - p); ');
  1935. Add(' CameraVector = normalize(p); ');
  1936. Add('} ');
  1937. end;
  1938. with FragmentProgram.Code do
  1939. begin
  1940. clear;
  1941. Add(' uniform vec2 Scale; ');
  1942. Add(' uniform vec2 Threshold; ');
  1943. Add(' ');
  1944. Add('uniform vec4 AmbientColor; ');
  1945. Add('uniform vec4 DiffuseColor; ');
  1946. Add('uniform vec4 SpecularColor; ');
  1947. Add(' ');
  1948. Add('uniform float LightIntensity; ');
  1949. Add('uniform float SpecPower; ');
  1950. Add('uniform sampler2D MainTexture; ');
  1951. Add(' ');
  1952. Add('varying vec3 Normal; ');
  1953. Add('varying vec3 LightVector; ');
  1954. Add('varying vec3 CameraVector; ');
  1955. Add('varying vec2 Texcoord; ');
  1956. Add(' ');
  1957. Add('void main(void) ');
  1958. Add('{ ');
  1959. Add(' float ss = fract(Texcoord[0] * Scale.s); ');
  1960. Add(' float tt = fract(Texcoord[1] * Scale.t); ');
  1961. Add(' ');
  1962. Add(' if ((ss > Threshold.s) && (tt > Threshold.t)) discard; ');
  1963. Add(' ');
  1964. Add(' vec4 TextureContrib = texture2D(MainTexture, Texcoord); ');
  1965. Add(' vec4 DiffuseContrib = clamp(DiffuseColor * dot(LightVector, Normal), 0.0, 1.0); ');
  1966. Add(' ');
  1967. Add(' vec3 reflect_vec = reflect(CameraVector, -Normal); ');
  1968. Add(' float Temp = dot(reflect_vec, LightVector); ');
  1969. Add(' vec4 SpecContrib = SpecularColor * clamp(pow(Temp, SpecPower), 0.0, 0.95); ');
  1970. Add(' ');
  1971. Add(' gl_FragColor = TextureContrib * LightIntensity * (AmbientColor + DiffuseContrib) + LightIntensity * SpecContrib; ');
  1972. Add('} ');
  1973. end;
  1974. inherited;
  1975. end;
  1976. function TgxCustomGLSLLatticeShader.GetMaterialLibrary
  1977. : TgxAbstractMaterialLibrary;
  1978. begin
  1979. Result := FMaterialLibrary;
  1980. end;
  1981. procedure TgxCustomGLSLLatticeShader.SetMaterialLibrary
  1982. (const Value: TgxAbstractMaterialLibrary);
  1983. begin
  1984. if FMaterialLibrary <> nil then
  1985. FMaterialLibrary.RemoveFreeNotification(self);
  1986. FMaterialLibrary := Value;
  1987. if (FMaterialLibrary <> nil) and
  1988. (FMaterialLibrary is TgxAbstractMaterialLibrary) then
  1989. FMaterialLibrary.FreeNotification(self);
  1990. end;
  1991. procedure TgxCustomGLSLLatticeShader.SetMainTexTexture
  1992. (const Value: TgxTexture);
  1993. begin
  1994. if FMainTexture = Value then
  1995. Exit;
  1996. FMainTexture := Value;
  1997. NotifyChange(self)
  1998. end;
  1999. function TgxCustomGLSLLatticeShader.GetMainTexName: TgxLibMaterialName;
  2000. begin
  2001. Result := TgxMaterialLibrary(FMaterialLibrary)
  2002. .GetNameOfTexture(FMainTexture);
  2003. if Result = '' then
  2004. Result := FMainTexName;
  2005. end;
  2006. procedure TgxCustomGLSLLatticeShader.SetMainTexName
  2007. (const Value: TgxLibMaterialName);
  2008. begin
  2009. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  2010. if FMainTexName = Value then
  2011. Exit;
  2012. FMainTexName := Value;
  2013. FMainTexture := TgxMaterialLibrary(FMaterialLibrary)
  2014. .TextureByName(FMainTexName);
  2015. NotifyChange(self);
  2016. end;
  2017. procedure TgxCustomGLSLLatticeShader.SetDiffuseColor(AValue: TgxColor);
  2018. begin
  2019. FDiffuseColor.DirectColor := AValue.Color;
  2020. end;
  2021. procedure TgxCustomGLSLLatticeShader.SetAmbientColor(AValue: TgxColor);
  2022. begin
  2023. FAmbientColor.DirectColor := AValue.Color;
  2024. end;
  2025. procedure TgxCustomGLSLLatticeShader.SetSpecularColor(AValue: TgxColor);
  2026. begin
  2027. FSpecularColor.DirectColor := AValue.Color;
  2028. end;
  2029. procedure TgxCustomGLSLLatticeShader.Notification(AComponent: TComponent;
  2030. Operation: TOperation);
  2031. var
  2032. Index: Integer;
  2033. begin
  2034. inherited;
  2035. if Operation = opRemove then
  2036. if AComponent = FMaterialLibrary then
  2037. if FMaterialLibrary <> nil then
  2038. begin
  2039. if FMainTexture <> nil then
  2040. begin
  2041. Index := TgxMaterialLibrary(FMaterialLibrary)
  2042. .Materials.GetTextureIndex(FMainTexture);
  2043. if Index <> -1 then
  2044. SetMainTexTexture(nil);
  2045. end;
  2046. FMaterialLibrary := nil;
  2047. end;
  2048. end;
  2049. (*************************************************
  2050. TgxCustomGLSLSemShader
  2051. ************************************************)
  2052. constructor TgxCustomGLSLSemShader.Create(AOwner: TComponent);
  2053. begin
  2054. inherited;
  2055. with VertexProgram.Code do
  2056. begin
  2057. clear;
  2058. Add('varying vec3 viewVec; ');
  2059. Add('varying vec3 normal; ');
  2060. Add('varying vec3 lightVec; ');
  2061. Add('void main() { ');
  2062. Add(' vec4 p = gl_ModelViewMatrix * gl_Vertex; ');
  2063. Add(' vec4 lightPos = gl_LightSource[0].position;');
  2064. Add(' lightVec = vec3(lightPos - p); ');
  2065. Add(' viewVec = -vec3(p); ');
  2066. Add(' normal = normalize(gl_NormalMatrix * gl_Normal ); ');
  2067. Add(' gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; ');
  2068. Add('} ');
  2069. end;
  2070. with FragmentProgram.Code do
  2071. begin
  2072. clear;
  2073. Add('uniform vec4 AmbientColor; ');
  2074. Add('uniform vec4 SpecularColor; ');
  2075. Add('uniform float DiffuseIntensity; ');
  2076. Add('uniform float AmbientIntensity; ');
  2077. Add('uniform float SpecularIntensity; ');
  2078. Add('uniform sampler2D MainTexture; ');
  2079. Add('varying vec3 viewVec; ');
  2080. Add('varying vec3 normal; ');
  2081. Add('varying vec3 lightVec; ');
  2082. Add('void main() { ');
  2083. Add(' vec3 V = normalize(viewVec); ');
  2084. Add(' vec3 r = reflect( V, normal ); ');
  2085. Add(' float m = 2.0 * sqrt( pow( r.x, 2.0 ) + pow( r.y, 2.0 ) + pow( r.z + 1.0, 2.0 ) ); ');
  2086. Add(' vec2 vN = r.xy / m + 0.5; ');
  2087. Add(' vec4 DiffuseColor; ');
  2088. Add(' DiffuseColor = texture2D( MainTexture, vN ); //.rgb; ');
  2089. // Simple Lighting
  2090. Add(' vec3 L = normalize(lightVec); ');
  2091. Add(' vec3 halfAngle = normalize(L + V); ');
  2092. Add(' float NdotL = dot(L, normal); ');
  2093. Add(' float NdotH = clamp(dot(halfAngle, normal), 0.0, 1.0); ');
  2094. Add(' // "Half-Lambert" technique for more pleasing diffuse term ');
  2095. Add(' float diffuse = DiffuseColor*(0.5 * NdotL + 0.5); ');
  2096. Add(' float specular = pow(NdotH, 64.0); ');
  2097. Add(' vec4 FinalColour = AmbientColor*AmbientIntensity + ');
  2098. Add(' DiffuseColor*diffuse*DiffuseIntensity + ');
  2099. Add(' SpecularColor*specular*SpecularIntensity; ');
  2100. Add(' gl_FragColor = FinalColour; //vec4( FinalColour, 1.0 ); ');
  2101. Add('} ');
  2102. end;
  2103. FAmbientColor := TgxColor.Create(self);
  2104. // FDiffuseColor := TgxColor.Create(Self);
  2105. FSpecularColor := TgxColor.Create(self);
  2106. // setup initial parameters
  2107. FAmbientColor.SetColor(0.15, 0.15, 0.15, 1.0);
  2108. // FDiffuseColor.SetColor(1, 1, 1, 1);
  2109. FSpecularColor.SetColor(1.0, 1.0, 1.0, 1.0);
  2110. FAmbientFactor := 0.8;
  2111. FDiffuseFactor := 0.9;
  2112. FSpecularFactor := 0.8;
  2113. end;
  2114. destructor TgxCustomGLSLSemShader.Destroy;
  2115. begin
  2116. FAmbientColor.Destroy;
  2117. // FDiffuseColor.Destroy;
  2118. FSpecularColor.Destroy;
  2119. inherited;
  2120. end;
  2121. procedure TgxCustomGLSLSemShader.DoApply(var rci: TgxRenderContextInfo;
  2122. Sender: TObject);
  2123. begin
  2124. GetGXSLProg.UseProgramObject;
  2125. // Param['DiffuseColor'].AsVector4f := FDiffuseColor.Color;
  2126. param['AmbientColor'].AsVector4f := FAmbientColor.Color;
  2127. param['SpecularColor'].AsVector4f := FSpecularColor.Color;
  2128. param['AmbientIntensity'].AsVector1f := FAmbientFactor;
  2129. param['DiffuseIntensity'].AsVector1f := FDiffuseFactor;
  2130. param['SpecularIntensity'].AsVector1f := FSpecularFactor;
  2131. // Param['SpecPower'].AsVector1f := FSpecularPower;
  2132. // Param['LightIntensity'].AsVector1f := FLightPower;
  2133. param['MainTexture'].AsTexture2D[0] := FMainTexture;
  2134. end;
  2135. function TgxCustomGLSLSemShader.DoUnApply
  2136. (var rci: TgxRenderContextInfo): Boolean;
  2137. begin
  2138. glActiveTexture(GL_TEXTURE0_ARB);
  2139. GetGXSLProg.EndUseProgramObject;
  2140. Result := False;
  2141. end;
  2142. function TgxCustomGLSLSemShader.GetMaterialLibrary
  2143. : TgxAbstractMaterialLibrary;
  2144. begin
  2145. Result := FMaterialLibrary;
  2146. end;
  2147. procedure TgxCustomGLSLSemShader.SetMaterialLibrary
  2148. (const Value: TgxAbstractMaterialLibrary);
  2149. begin
  2150. if FMaterialLibrary <> nil then
  2151. FMaterialLibrary.RemoveFreeNotification(self);
  2152. FMaterialLibrary := Value;
  2153. if (FMaterialLibrary <> nil) and
  2154. (FMaterialLibrary is TgxAbstractMaterialLibrary) then
  2155. FMaterialLibrary.FreeNotification(self);
  2156. end;
  2157. procedure TgxCustomGLSLSemShader.SetMainTexTexture(const Value: TgxTexture);
  2158. begin
  2159. if FMainTexture = Value then
  2160. Exit;
  2161. FMainTexture := Value;
  2162. NotifyChange(self)
  2163. end;
  2164. function TgxCustomGLSLSemShader.GetMainTexName: TgxLibMaterialName;
  2165. begin
  2166. Result := TgxMaterialLibrary(FMaterialLibrary)
  2167. .GetNameOfTexture(FMainTexture);
  2168. if Result = '' then
  2169. Result := FMainTexName;
  2170. end;
  2171. procedure TgxCustomGLSLSemShader.SetMainTexName
  2172. (const Value: TgxLibMaterialName);
  2173. begin
  2174. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  2175. if FMainTexName = Value then
  2176. Exit;
  2177. FMainTexName := Value;
  2178. FMainTexture := TgxMaterialLibrary(FMaterialLibrary)
  2179. .TextureByName(FMainTexName);
  2180. NotifyChange(self);
  2181. end;
  2182. // procedure TgxCustomGLSLSemShader.SetDiffuseColor(AValue: TgxColor);
  2183. // begin
  2184. // FDiffuseColor.DirectColor := AValue.Color;
  2185. // end;
  2186. procedure TgxCustomGLSLSemShader.SetAmbientColor(AValue: TgxColor);
  2187. begin
  2188. FAmbientColor.DirectColor := AValue.Color;
  2189. end;
  2190. procedure TgxCustomGLSLSemShader.SetSpecularColor(AValue: TgxColor);
  2191. begin
  2192. FSpecularColor.DirectColor := AValue.Color;
  2193. end;
  2194. procedure TgxCustomGLSLSemShader.Notification(AComponent: TComponent;
  2195. Operation: TOperation);
  2196. var
  2197. Index: Integer;
  2198. begin
  2199. inherited;
  2200. if Operation = opRemove then
  2201. if AComponent = FMaterialLibrary then
  2202. if FMaterialLibrary <> nil then
  2203. begin
  2204. if FMainTexture <> nil then
  2205. begin
  2206. Index := TgxMaterialLibrary(FMaterialLibrary)
  2207. .Materials.GetTextureIndex(FMainTexture);
  2208. if Index <> -1 then
  2209. SetMainTexTexture(nil);
  2210. end;
  2211. FMaterialLibrary := nil;
  2212. end;
  2213. end;
  2214. (****************************************
  2215. TgxCustomGLSLToonShader
  2216. ****************************************)
  2217. constructor TgxCustomGLSLToonShader.Create(AOwner: TComponent);
  2218. begin
  2219. inherited;
  2220. with VertexProgram.Code do
  2221. begin
  2222. Clear;
  2223. Add('varying vec3 vNormal; ');
  2224. Add('varying vec3 LightVec; ');
  2225. Add('varying vec3 ViewVec; ');
  2226. Add(' ');
  2227. Add('void main() ');
  2228. Add('{ ');
  2229. Add(' vec4 lightPos = gl_LightSource[0].position;');
  2230. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  2231. Add(' vec3 normal = gl_NormalMatrix * gl_Normal; ');
  2232. Add(' vNormal = normalize(normal); ');
  2233. Add(' LightVec = vec3(lightPos - vert); ');
  2234. Add(' ViewVec = -vec3(vert); ');
  2235. //Add(' gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; ');
  2236. Add(' gl_Position = ftransform(); ');
  2237. Add('} ');
  2238. end;
  2239. with FragmentProgram.Code do
  2240. begin
  2241. Clear;
  2242. Add('uniform vec4 HighlightColor; ');
  2243. Add('uniform vec4 MidColor; ');
  2244. Add('uniform vec4 LightenShadowColor; ');
  2245. Add('uniform vec4 DarkenShadowColor; ');
  2246. Add('uniform vec4 OutlineColor; ');
  2247. Add('uniform float HighlightSize; '); // 0.95
  2248. Add('uniform float MidSize; '); // 0.5
  2249. Add('uniform float ShadowSize; '); // 0.25
  2250. Add('uniform float OutlineWidth; ');
  2251. Add('varying vec3 vNormal; ');
  2252. Add('varying vec3 LightVec; ');
  2253. Add('varying vec3 ViewVec; ');
  2254. Add('void main() ');
  2255. Add('{ ');
  2256. Add(' vec3 n = normalize(vNormal); ');
  2257. Add(' vec3 l = normalize(LightVec); ');
  2258. Add(' vec3 v = normalize(ViewVec); ');
  2259. Add(' float lambert = dot(l,n); ');
  2260. Add(' vec4 colour = MidColor; ');
  2261. Add(' if (lambert>HighlightSize) colour = HighlightColor; ');
  2262. Add(' else if (lambert>MidSize) colour = MidColor; ');
  2263. Add(' else if (lambert>ShadowSize) colour = LightenShadowColor; ');
  2264. Add(' else if (lambert<ShadowSize) colour = DarkenShadowColor; ');
  2265. Add(' if (dot(n,v)<OutlineWidth) colour = OutlineColor; ');
  2266. Add(' gl_FragColor = colour; ');
  2267. Add('} ');
  2268. end;
  2269. // Initial stuff.
  2270. FHighLightColor := TgxColor.Create(self);
  2271. FHighLightColor.SetColor(0.9,0.9,0.9,1.0);
  2272. FMidColor := TgxColor.Create(self);
  2273. FMidColor.SetColor(0.75,0.75,0.75,1.0);
  2274. FLightenShadowColor := TgxColor.Create(self);
  2275. FLightenShadowColor.SetColor(0.5,0.5,0.5,1.0);
  2276. FDarkenShadowColor := TgxColor.Create(self);
  2277. FDarkenShadowColor.SetColor(0.3,0.3,0.3,1.0);
  2278. FOutlineColor := TgxColor.Create(self);
  2279. FOutlineColor.SetColor(0,0,0,1.0);
  2280. FHighlightSize := 0.95;
  2281. FMidSize := 0.50;
  2282. FShadowSize := 0.25;
  2283. FOutlineWidth := 0.25;
  2284. end;
  2285. destructor TgxCustomGLSLToonShader.Destroy;
  2286. begin
  2287. FHighLightColor.Free;
  2288. FMidColor.Free;
  2289. FLightenShadowColor.Free;
  2290. FDarkenShadowColor.Free;
  2291. FOutlineColor.Free;
  2292. inherited;
  2293. end;
  2294. procedure TgxCustomGLSLToonShader.DoApply(var rci: TgxRenderContextInfo;Sender: TObject);
  2295. begin
  2296. GetGXSLProg.UseProgramObject;
  2297. param['HighlightColor'].AsVector4f := FHighlightColor.Color;
  2298. param['MidColor'].AsVector4f := FMidColor.Color;
  2299. param['LightenShadowColor'].AsVector4f := FLightenShadowColor.Color;
  2300. param['DarkenShadowColor'].AsVector4f := FDarkenShadowColor.Color;
  2301. param['OutlineColor'].AsVector4f := FOutlineColor.Color;
  2302. param['HighlightSize'].AsVector1f := FHighlightSize;
  2303. param['MidSize'].AsVector1f := FMidSize;
  2304. param['ShadowSize'].AsVector1f := FShadowSize;
  2305. param['OutlineWidth'].AsVector1f := FOutlineWidth;
  2306. end;
  2307. function TgxCustomGLSLToonShader.DoUnApply(var rci: TgxRenderContextInfo): Boolean;
  2308. begin
  2309. GetGXSLProg.EndUseProgramObject;
  2310. Result := False;
  2311. end;
  2312. procedure TgxCustomGLSLToonShader.SetHighlightColor(AValue: TgxColor);
  2313. begin
  2314. FHighlightColor.DirectColor := AValue.Color;
  2315. end;
  2316. procedure TgxCustomGLSLToonShader.SetMidColor(AValue: TgxColor);
  2317. begin
  2318. FMidColor.DirectColor := AValue.Color;
  2319. end;
  2320. procedure TgxCustomGLSLToonShader.SetLightenShadowColor(AValue: TgxColor);
  2321. begin
  2322. FLightenShadowColor.DirectColor := AValue.Color;
  2323. end;
  2324. procedure TgxCustomGLSLToonShader.SetDarkenShadowColor(AValue: TgxColor);
  2325. begin
  2326. FDarkenShadowColor.DirectColor := AValue.Color;
  2327. end;
  2328. procedure TgxCustomGLSLToonShader.SetOutlineColor(AValue: TgxColor);
  2329. begin
  2330. FOutlineColor.DirectColor := AValue.Color;
  2331. end;
  2332. (*************************************************
  2333. TgxCustomGLSLVertexDisplacementShader
  2334. *************************************************)
  2335. constructor TgxCustomGLSLVertexDisplacementShader.Create(AOwner: TComponent);
  2336. begin
  2337. inherited;
  2338. with VertexProgram.Code do
  2339. begin
  2340. clear;
  2341. Add('uniform float time; ');
  2342. Add('uniform float NoiseFactor; ');
  2343. Add('uniform float TurbulenceFactor; ');
  2344. Add('uniform float NoiseScale; ');
  2345. Add('uniform float NoisePeriod; ');
  2346. Add('uniform float DisplacementScale; ');
  2347. Add('uniform float TimeFactor; ');
  2348. Add('varying vec3 viewVec; ');
  2349. Add('varying vec3 normal; ');
  2350. Add('varying vec3 lightVec; ');
  2351. Add('varying vec2 vTexCoord; ');
  2352. Add('varying float noise; ');
  2353. //
  2354. // GLSL textureless classic 3D noise "cnoise",
  2355. // with an RSL-style periodic variant "pnoise".
  2356. // Author: Stefan Gustavson ([email protected])
  2357. // Version: 2011-10-11
  2358. //
  2359. // Many thanks to Ian McEwan of Ashima Arts for the
  2360. // ideas for permutation and gradient selection.
  2361. //
  2362. // Copyright (c) 2011 Stefan Gustavson. All rights reserved.
  2363. // Distributed under the MIT license. See LICENSE file.
  2364. // https://github.com/ashima/webgl-noise
  2365. //
  2366. Add('vec3 mod289(vec3 x) ');
  2367. Add('{ ');
  2368. Add(' return x - floor(x * (1.0 / 289.0)) * 289.0; ');
  2369. Add('} ');
  2370. Add('vec4 mod289(vec4 x) ');
  2371. Add('{ ');
  2372. Add(' return x - floor(x * (1.0 / 289.0)) * 289.0; ');
  2373. Add('} ');
  2374. Add('vec4 permute(vec4 x) ');
  2375. Add('{ ');
  2376. Add(' return mod289(((x*34.0)+1.0)*x); ');
  2377. Add('} ');
  2378. Add('vec4 taylorInvSqrt(vec4 r) ');
  2379. Add('{ ');
  2380. Add(' return 1.79284291400159 - 0.85373472095314 * r; ');
  2381. Add('} ');
  2382. Add('vec3 fade(vec3 t) { ');
  2383. Add(' return t*t*t*(t*(t*6.0-15.0)+10.0); ');
  2384. Add('} ');
  2385. // Classic Perlin noise, periodic variant
  2386. Add('float pnoise(vec3 P, vec3 rep) ');
  2387. Add('{ ');
  2388. Add(' vec3 Pi0 = mod(floor(P), rep); // Integer part, modulo period ');
  2389. Add(' vec3 Pi1 = mod(Pi0 + vec3(1.0), rep); // Integer part + 1, mod period ');
  2390. Add(' Pi0 = mod289(Pi0); ');
  2391. Add(' Pi1 = mod289(Pi1); ');
  2392. Add(' vec3 Pf0 = fract(P); // Fractional part for interpolation ');
  2393. Add(' vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0 ');
  2394. Add(' vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); ');
  2395. Add(' vec4 iy = vec4(Pi0.yy, Pi1.yy); ');
  2396. Add(' vec4 iz0 = Pi0.zzzz; ');
  2397. Add(' vec4 iz1 = Pi1.zzzz; ');
  2398. Add(' vec4 ixy = permute(permute(ix) + iy); ');
  2399. Add(' vec4 ixy0 = permute(ixy + iz0); ');
  2400. Add(' vec4 ixy1 = permute(ixy + iz1); ');
  2401. Add(' vec4 gx0 = ixy0 * (1.0 / 7.0); ');
  2402. Add(' vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5; ');
  2403. Add(' gx0 = fract(gx0); ');
  2404. Add(' vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0); ');
  2405. Add(' vec4 sz0 = step(gz0, vec4(0.0)); ');
  2406. Add(' gx0 -= sz0 * (step(0.0, gx0) - 0.5); ');
  2407. Add(' gy0 -= sz0 * (step(0.0, gy0) - 0.5); ');
  2408. Add(' vec4 gx1 = ixy1 * (1.0 / 7.0); ');
  2409. Add(' vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5; ');
  2410. Add(' gx1 = fract(gx1); ');
  2411. Add(' vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1); ');
  2412. Add(' vec4 sz1 = step(gz1, vec4(0.0)); ');
  2413. Add(' gx1 -= sz1 * (step(0.0, gx1) - 0.5); ');
  2414. Add(' gy1 -= sz1 * (step(0.0, gy1) - 0.5); ');
  2415. Add(' vec3 g000 = vec3(gx0.x,gy0.x,gz0.x); ');
  2416. Add(' vec3 g100 = vec3(gx0.y,gy0.y,gz0.y); ');
  2417. Add(' vec3 g010 = vec3(gx0.z,gy0.z,gz0.z); ');
  2418. Add(' vec3 g110 = vec3(gx0.w,gy0.w,gz0.w); ');
  2419. Add(' vec3 g001 = vec3(gx1.x,gy1.x,gz1.x); ');
  2420. Add(' vec3 g101 = vec3(gx1.y,gy1.y,gz1.y); ');
  2421. Add(' vec3 g011 = vec3(gx1.z,gy1.z,gz1.z); ');
  2422. Add(' vec3 g111 = vec3(gx1.w,gy1.w,gz1.w); ');
  2423. Add(' vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); ');
  2424. Add(' g000 *= norm0.x; ');
  2425. Add(' g010 *= norm0.y; ');
  2426. Add(' g100 *= norm0.z; ');
  2427. Add(' g110 *= norm0.w; ');
  2428. Add(' vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); ');
  2429. Add(' g001 *= norm1.x; ');
  2430. Add(' g011 *= norm1.y; ');
  2431. Add(' g101 *= norm1.z; ');
  2432. Add(' g111 *= norm1.w; ');
  2433. Add(' float n000 = dot(g000, Pf0); ');
  2434. Add(' float n100 = dot(g100, vec3(Pf1.x, Pf0.yz)); ');
  2435. Add(' float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z)); ');
  2436. Add(' float n110 = dot(g110, vec3(Pf1.xy, Pf0.z)); ');
  2437. Add(' float n001 = dot(g001, vec3(Pf0.xy, Pf1.z)); ');
  2438. Add(' float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z)); ');
  2439. Add(' float n011 = dot(g011, vec3(Pf0.x, Pf1.yz)); ');
  2440. Add(' float n111 = dot(g111, Pf1); ');
  2441. Add(' vec3 fade_xyz = fade(Pf0); ');
  2442. Add(' vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z); ');
  2443. Add(' vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y); ');
  2444. Add(' float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x); ');
  2445. Add(' return 2.2 * n_xyz; ');
  2446. Add('} ');
  2447. Add('float turbulence( vec3 p ) { ');
  2448. Add(' float w = 100.0; ');
  2449. Add(' float t = -.5; ');
  2450. Add(' for (float f = 1.0 ; f <= 10.0 ; f++ ){ ');
  2451. Add(' float power = pow( 2.0, f ); ');
  2452. Add(' t += abs( pnoise( vec3( power * p ), vec3( 10.0, 10.0, 10.0 ) ) / power ); ');
  2453. Add(' } ');
  2454. Add(' return t; ');
  2455. Add('} ');
  2456. Add('void main() { '); //96
  2457. Add(' mat4 mWorld = gl_ModelViewMatrix; ');
  2458. Add(' vec3 Normal = gl_NormalMatrix * gl_Normal; //gl_Normal; ');
  2459. Add(' vec4 Position = gl_Vertex; ');
  2460. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  2461. Add(' vec4 lightPos = gl_LightSource[0].position;');
  2462. Add(' vTexCoord = gl_MultiTexCoord0; ');
  2463. Add(' vec3 vNormal = normalize( Normal * mat3(mWorld )); ');
  2464. Add(' time = TimeFactor*time; ');
  2465. // add time to the noise parameters so it's animated
  2466. Add(' noise = NoiseFactor* -0.10* turbulence( TurbulenceFactor * vNormal+time ); ');
  2467. // get a 3d noise using the position, low frequency
  2468. Add(' float b = (NoisePeriod*time)*pnoise( vec3((NoiseScale *time)* (Position.xyz + vec3(time ))), vec3(100) ); ');
  2469. // compose both noises
  2470. Add(' float displacement =( noise + b); ');
  2471. Add(' vec4 newPosition =vec4((Position.xyz + DisplacementScale*(vec3(vNormal * displacement))),1.0); ');
  2472. Add(' normal = vNormal; ');
  2473. Add(' lightVec = vec3(lightPos - vert); ');
  2474. Add(' viewVec = -vec3(vert); ');
  2475. Add(' gl_Position = gl_ModelViewProjectionMatrix * newPosition; ');
  2476. Add('} ');
  2477. end;
  2478. with FragmentProgram.Code do
  2479. begin
  2480. clear;
  2481. Add('uniform vec4 AmbientColor; ');
  2482. Add('uniform vec4 SpecularColor; ');
  2483. Add('uniform float DiffuseIntensity; ');
  2484. Add('uniform float AmbientIntensity; ');
  2485. Add('uniform float SpecularIntensity; ');
  2486. Add('uniform sampler2D MainTexture; ');
  2487. Add('varying vec3 viewVec; ');
  2488. Add('varying vec3 normal; ');
  2489. Add('varying vec3 lightVec; ');
  2490. Add('varying float noise; ');
  2491. Add('float random( vec3 scale, float seed ){ ');
  2492. Add(' return fract( sin( dot( gl_FragCoord.xyz + seed, scale ) ) * 43758.5453 + seed ) ; ');
  2493. Add('} ');
  2494. Add('void main() { ');
  2495. // get a random offset
  2496. Add(' float r = 0.01 * random( vec3( 12.9898, 78.233, 151.7182 ), 0.0 ); ');
  2497. // lookup vertically in the texture, using noise and offset
  2498. // to get the right RGB colour
  2499. Add(' vec2 tPos = vec2( 0, 1.0 - 1.3 * noise + r ); ');
  2500. Add(' vec4 DiffuseColor; ');
  2501. Add(' DiffuseColor = texture2D( MainTexture, tPos ); ');
  2502. // Simple Lighting
  2503. Add(' vec3 L = normalize(lightVec); ');
  2504. Add(' vec3 V = normalize(viewVec); ');
  2505. Add(' vec3 halfAngle = normalize(L + V); ');
  2506. Add(' float NdotL = dot(L, normal); ');
  2507. Add(' float NdotH = clamp(dot(halfAngle, normal), 0.0, 1.0); ');
  2508. Add(' // "Half-Lambert" technique for more pleasing diffuse term ');
  2509. Add(' float diffuse = DiffuseColor*(0.5 * NdotL + 0.5); ');
  2510. Add(' float specular = pow(NdotH, 64.0); ');
  2511. Add(' vec4 FinalColour = AmbientColor*AmbientIntensity + ');
  2512. Add(' DiffuseColor*diffuse*DiffuseIntensity + ');
  2513. Add(' SpecularColor*specular*SpecularIntensity; ');
  2514. Add(' gl_FragColor = FinalColour; ; ');
  2515. // Add(' gl_FragColor = vec4(DiffuseColor,1.0); ');
  2516. Add('} ');
  2517. end;
  2518. FAmbientColor := TgxColor.Create(Self);
  2519. //FDiffuseColor := TgxColor.Create(Self);
  2520. FSpecularColor := TgxColor.Create(Self);
  2521. //setup initial parameters
  2522. FAmbientColor.SetColor(0.15, 0.15, 0.15, 1.0);
  2523. //FDiffuseColor.SetColor(1, 1, 1, 1);
  2524. FSpecularColor.SetColor(1.0, 1.0, 1.0, 1.0);
  2525. FAmbientFactor := 0.8;
  2526. FDiffuseFactor :=0.9;
  2527. FSpecularFactor :=0.8;
  2528. FElapsedTime := 1.0;
  2529. FNoise := 10.0;
  2530. FDisplacementScale := 1.0;
  2531. FNoiseScale := 0.05;
  2532. FTurbulenceFactor := 0.5;
  2533. FNoisePeriod := 5.0;
  2534. FTimeFactor := 0.05;
  2535. end;
  2536. destructor TgxCustomGLSLVertexDisplacementShader.Destroy;
  2537. begin
  2538. FAmbientColor.Destroy;
  2539. // FDiffuseColor.Destroy;
  2540. FSpecularColor.Destroy;
  2541. inherited;
  2542. end;
  2543. procedure TgxCustomGLSLVertexDisplacementShader.DoApply(var rci: TgxRenderContextInfo; Sender: TObject);
  2544. begin
  2545. GetGXSLProg.UseProgramObject;
  2546. // Param['DiffuseColor'].AsVector4f := FDiffuseColor.Color;
  2547. param['AmbientColor'].AsVector4f := FAmbientColor.Color;
  2548. param['SpecularColor'].AsVector4f := FSpecularColor.Color;
  2549. param['AmbientIntensity'].AsVector1f := FAmbientFactor;
  2550. param['DiffuseIntensity'].AsVector1f := FDiffuseFactor;
  2551. param['SpecularIntensity'].AsVector1f := FSpecularFactor;
  2552. Param['time'].AsVector1f := FElapsedTime;
  2553. Param['NoiseFactor'].AsVector1f := FNoise;
  2554. Param['NoiseScale'].AsVector1f := FNoiseScale;
  2555. Param['TurbulenceFactor'].AsVector1f := FTurbulenceFactor;
  2556. Param['NoisePeriod'].AsVector1f := FNoisePeriod;
  2557. Param['DisplacementScale'].AsVector1f := FDisplacementScale;
  2558. Param['TimeFactor'].AsVector1f := FTimeFactor;
  2559. Param['MainTexture'].AsTexture2D[0] := FMainTexture;
  2560. end;
  2561. function TgxCustomGLSLVertexDisplacementShader.DoUnApply(var rci: TgxRenderContextInfo): Boolean;
  2562. begin
  2563. glActiveTexture(GL_TEXTURE0_ARB);
  2564. GetGXSLProg.EndUseProgramObject;
  2565. Result := False;
  2566. end;
  2567. function TgxCustomGLSLVertexDisplacementShader.GetMaterialLibrary: TgxAbstractMaterialLibrary;
  2568. begin
  2569. Result := FMaterialLibrary;
  2570. end;
  2571. procedure TgxCustomGLSLVertexDisplacementShader.SetMaterialLibrary(const Value: TgxAbstractMaterialLibrary);
  2572. begin
  2573. if FMaterialLibrary <> nil then FMaterialLibrary.RemoveFreeNotification(Self);
  2574. FMaterialLibrary := Value;
  2575. if (FMaterialLibrary <> nil)
  2576. and (FMaterialLibrary is TgxAbstractMaterialLibrary) then
  2577. FMaterialLibrary.FreeNotification(Self);
  2578. end;
  2579. procedure TgxCustomGLSLVertexDisplacementShader.SetMainTexTexture(const Value: TgxTexture);
  2580. begin
  2581. if FMainTexture = Value then Exit;
  2582. FMainTexture := Value;
  2583. NotifyChange(Self)
  2584. end;
  2585. function TgxCustomGLSLVertexDisplacementShader.GetMainTexName: TgxLibMaterialName;
  2586. begin
  2587. Result := TgxMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FMainTexture);
  2588. if Result = '' then Result := FMainTexName;
  2589. end;
  2590. procedure TgxCustomGLSLVertexDisplacementShader.SetMainTexName(const Value: TgxLibMaterialName);
  2591. begin
  2592. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  2593. if FMainTexName = Value then Exit;
  2594. FMainTexName := Value;
  2595. FMainTexture := TgxMaterialLibrary(FMaterialLibrary).TextureByName(FMainTexName);
  2596. NotifyChange(Self);
  2597. end;
  2598. //procedure TgxCustomGLSLVertexDisplacementShader.SetDiffuseColor(AValue: TgxColor);
  2599. //begin
  2600. // FDiffuseColor.DirectColor := AValue.Color;
  2601. //end;
  2602. procedure TgxCustomGLSLVertexDisplacementShader.SetAmbientColor(AValue: TgxColor);
  2603. begin
  2604. FAmbientColor.DirectColor := AValue.Color;
  2605. end;
  2606. procedure TgxCustomGLSLVertexDisplacementShader.SetSpecularColor(AValue: TgxColor);
  2607. begin
  2608. FSpecularColor.DirectColor := AValue.Color;
  2609. end;
  2610. procedure TgxCustomGLSLVertexDisplacementShader.Notification
  2611. (AComponent: TComponent; Operation: TOperation);
  2612. var
  2613. Index: Integer;
  2614. begin
  2615. inherited;
  2616. if Operation = opRemove then
  2617. if AComponent = FMaterialLibrary then
  2618. if FMaterialLibrary <> nil then
  2619. begin
  2620. if FMainTexture <> nil then
  2621. begin
  2622. Index := TgxMaterialLibrary(FMaterialLibrary)
  2623. .Materials.GetTextureIndex(FMainTexture);
  2624. if Index <> -1 then
  2625. SetMainTexTexture(nil);
  2626. end;
  2627. FMaterialLibrary := nil;
  2628. end;
  2629. end;
  2630. end.