GLMaterialScript.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. {
  5. Material Script Batch loader for TGLMaterialLibrary for runtime.
  6. }
  7. unit GLMaterialScript;
  8. interface
  9. {$I GLScene.inc}
  10. uses
  11. System.SysUtils,
  12. System.Classes,
  13. VCL.StdCtrls,
  14. GLVectorTypes,
  15. GLTexture,
  16. GLTextureFormat,
  17. GLGraphics,
  18. GLUtils,
  19. GLColor,
  20. GLCoordinates,
  21. GLMaterial,
  22. GLState;
  23. type
  24. TGLShaderItem = class(TCollectionItem)
  25. private
  26. FShader: TGLShader;
  27. FName: string;
  28. procedure SetShader(const Value: TGLShader);
  29. procedure SetName(const Value: string);
  30. protected
  31. function GetDisplayName: string; override;
  32. public
  33. constructor Create(Collection: TCollection); override;
  34. destructor Destroy; override;
  35. procedure Assign(Source: TPersistent); override;
  36. published
  37. property Shader: TGLShader read FShader write SetShader;
  38. property Name: string read FName write SetName;
  39. end;
  40. TGLShaderItems = class(TOwnedCollection)
  41. private
  42. procedure SetItems(Index: Integer; const Val: TGLShaderItem);
  43. function GetItems(Index: Integer): TGLShaderItem;
  44. public
  45. constructor Create(AOwner: TPersistent);
  46. property Items[Index: Integer]: TGLShaderItem read GetItems write SetItems; default;
  47. end;
  48. TGLMaterialLibraryItem = class(TCollectionItem)
  49. private
  50. FMaterialLibrary: TGLMaterialLibrary;
  51. FName: string;
  52. procedure SetMaterialLibrary(const Value: TGLMaterialLibrary);
  53. procedure SetName(const Value: string);
  54. protected
  55. function GetDisplayName: string; override;
  56. public
  57. constructor Create(Collection: TCollection); override;
  58. destructor Destroy; override;
  59. procedure Assign(Source: TPersistent); override;
  60. published
  61. property MaterialLibrary: TGLMaterialLibrary read FMaterialLibrary write SetMaterialLibrary;
  62. property Name: string read FName write SetName;
  63. end;
  64. TGLMaterialLibraryItems = class(TOwnedCollection)
  65. private
  66. procedure SetItems(Index: Integer; const Val: TGLMaterialLibraryItem);
  67. function GetItems(Index: Integer): TGLMaterialLibraryItem;
  68. public
  69. constructor Create(AOwner: TPersistent);
  70. property Items[Index: Integer]: TGLMaterialLibraryItem read GetItems write SetItems; default;
  71. end;
  72. TGLMaterialScripter = class(TComponent)
  73. private
  74. FShaderItems: TGLShaderItems;
  75. FMaterialLibraryItems: TGLMaterialLibraryItems;
  76. FAppend: Boolean;
  77. FOverwrite: Boolean;
  78. FScript: TStrings;
  79. FMemo: TMemo;
  80. FMaterialLibrary: TGLMaterialLibrary;
  81. Count: Longint;
  82. infini: Longint;
  83. done: Boolean;
  84. NewMat: TGLLibMaterial;
  85. tmpcoords: TGLCoordinates;
  86. tmpcolor: TGLColor;
  87. tmpcoords4: TGLCoordinates4;
  88. tmpstr: string;
  89. procedure SeTGLShaderItems(const Value: TGLShaderItems);
  90. procedure SeTGLMaterialLibraryItems(const Value: TGLMaterialLibraryItems);
  91. procedure SetAppend(const Value: Boolean);
  92. procedure SetOverwrite(const Value: Boolean);
  93. procedure SetScript(const Value: TStrings);
  94. procedure SetMaterialLibrary(const Value: TGLMaterialLibrary);
  95. procedure SetMemo(const Value: TMemo);
  96. // error checking
  97. procedure CheckError;
  98. function ClassExists(arguement: string): Boolean;
  99. function CheckRepeatDone: Boolean;
  100. // extraction functions
  101. function ExtractValue: string;
  102. procedure ExtractCoords3;
  103. procedure ExtractCoords4;
  104. procedure ExtractColors;
  105. function DeleteSpaces(Value: string): string;
  106. function SubstrExists(substr: string): Boolean;
  107. function ValueExists(Value: string): Boolean;
  108. // these are our viable scripts
  109. procedure ZMaterial;
  110. // internally called scripts for value extraction
  111. procedure XMaterial;
  112. procedure XName;
  113. procedure XShader;
  114. procedure XTexture2Name;
  115. procedure XTextureOffset;
  116. procedure XTextureScale;
  117. procedure XTexture;
  118. procedure XCompression;
  119. procedure XEnvColor;
  120. procedure XFilteringQuality;
  121. procedure XImageAlpha;
  122. procedure XImageBrightness;
  123. procedure XImageClass;
  124. procedure XImageGamma;
  125. procedure XMagFilter;
  126. procedure XMappingMode;
  127. procedure XMappingSCoordinates;
  128. procedure XMappingTCoordinates;
  129. procedure XMinFilter;
  130. procedure XNormalMapScale;
  131. procedure XTextureFormat;
  132. procedure XTextureMode;
  133. procedure XTextureWrap;
  134. procedure XBlendingMode;
  135. procedure XPolygonMode;
  136. procedure XFacingCulling;
  137. procedure XLibMaterialName;
  138. procedure XMaterialOptions;
  139. procedure XMaterialLibrary;
  140. procedure XBackProperties;
  141. procedure XBackAmbient;
  142. procedure XBackDiffuse;
  143. procedure XBackEmission;
  144. procedure XBackShininess;
  145. procedure XBackSpecular;
  146. procedure XFrontProperties;
  147. procedure XFrontAmbient;
  148. procedure XFrontDiffuse;
  149. procedure XFrontEmission;
  150. procedure XFrontShininess;
  151. procedure XFrontSpecular;
  152. procedure XPersistantImage;
  153. procedure XBlankImage;
  154. procedure XPictureFileName;
  155. procedure XPicturePX;
  156. procedure XPictureNX;
  157. procedure XPicturePY;
  158. procedure XPictureNY;
  159. procedure XPicturePZ;
  160. procedure XPictureNZ;
  161. protected
  162. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  163. public
  164. property DebugMemo: TMemo read FMemo write SetMemo;
  165. constructor Create(AOwner: TComponent); override;
  166. destructor Destroy; override;
  167. procedure CompileScript;
  168. published
  169. property Script: TStrings read FScript write SetScript;
  170. property MaterialLibrary: TGLMaterialLibrary read FMaterialLibrary write SetMaterialLibrary;
  171. property Shaders: TGLShaderItems read FShaderItems write SeTGLShaderItems;
  172. property MaterialLibraries: TGLMaterialLibraryItems read FMaterialLibraryItems write SeTGLMaterialLibraryItems;
  173. property AppendToMaterialLibrary: Boolean read FAppend write SetAppend;
  174. property OverwriteToMaterialLibrary: Boolean read FOverwrite write SetOverwrite;
  175. end;
  176. //----------------------------------------------------------------------
  177. implementation
  178. //----------------------------------------------------------------------
  179. procedure TGLShaderItem.SetShader(const Value: TGLShader);
  180. begin
  181. if assigned(Value) then
  182. begin
  183. FShader := Value;
  184. FName := FShader.Name;
  185. end;
  186. end;
  187. procedure TGLShaderItem.Assign(Source: TPersistent);
  188. begin
  189. if Source is TGLShaderItem then
  190. begin
  191. FShader := TGLShaderItem(Source).FShader;
  192. end;
  193. inherited Destroy;
  194. end;
  195. constructor TGLShaderItem.Create(Collection: TCollection);
  196. begin
  197. inherited Create(Collection);
  198. FName := 'Shader';
  199. end;
  200. destructor TGLShaderItem.Destroy;
  201. begin
  202. inherited Destroy;
  203. end;
  204. function TGLShaderItem.GetDisplayName : String;
  205. begin
  206. if FName = '' then
  207. Result:='Shader'
  208. else
  209. Result := FName;
  210. end;
  211. //------------------------
  212. { TGLShaderItems }
  213. //------------------------
  214. constructor TGLShaderItems.Create(AOwner: TPersistent);
  215. begin
  216. inherited Create(AOwner, TGLShaderItem);
  217. end;
  218. function TGLShaderItems.GetItems(index: Integer): TGLShaderItem;
  219. begin
  220. Result:=TGLShaderItem(inherited Items[index]);
  221. end;
  222. procedure TGLShaderItems.SetItems(index: Integer; const val: TGLShaderItem);
  223. begin
  224. inherited Items[index]:=val;
  225. end;
  226. procedure TGLMaterialScripter.SeTGLShaderItems(const Value: TGLShaderItems);
  227. begin
  228. FShaderItems.Assign(Value);
  229. end;
  230. procedure TGLShaderItem.SetName(const Value: String);
  231. begin
  232. FName := Value;
  233. end;
  234. procedure TGLMaterialScripter.CompileScript;
  235. begin
  236. done := false;
  237. NewMat := nil;
  238. count := 0;
  239. infini := 0;
  240. tmpcoords := nil;
  241. tmpcoords4 := nil;
  242. tmpcolor := nil;
  243. tmpstr := '';
  244. repeat
  245. inc(count);
  246. if pos('{',FScript.Strings[count]) > 0 then
  247. begin
  248. if substrexists('material') then ZMaterial;
  249. end;
  250. checkerror;
  251. until checkrepeatdone;
  252. end;
  253. procedure TGLMaterialScripter.SetMaterialLibrary(
  254. const Value: TGLMaterialLibrary);
  255. begin
  256. if FMaterialLibrary <> nil then FMaterialLibrary.RemoveFreeNotification(Self);
  257. FMaterialLibrary := Value;
  258. if FMaterialLibrary <> nil then FMaterialLibrary.FreeNotification(Self);
  259. end;
  260. procedure TGLMaterialScripter.SetMemo(const Value: TMemo);
  261. begin
  262. if FMemo <> nil then FMemo.RemoveFreeNotification(Self);
  263. FMemo := Value;
  264. if FMemo <> nil then FMemo.FreeNotification(Self);
  265. end;
  266. procedure TGLMaterialScripter.SetScript(const Value: TStrings);
  267. begin
  268. if assigned(value) then
  269. FScript.Assign(Value);
  270. end;
  271. procedure TGLMaterialScripter.CheckError;
  272. begin
  273. if count >= FScript.Count then done := true;
  274. if done then raise exception.Create('User Error : No closing "}"');
  275. inc(infini);
  276. if infini > 1280000 then
  277. begin
  278. raise exception.Create('Internal Error : Infinate Loop');
  279. done := true;
  280. exit;
  281. end;
  282. end;
  283. function TGLMaterialScripter.CheckRepeatDone: boolean;
  284. begin
  285. checkrepeatdone := false;
  286. if pos('}',FScript.Strings[count]) > 0 then
  287. begin
  288. checkrepeatdone := true;
  289. inc(count);
  290. end;
  291. if done then checkrepeatdone := true;
  292. end;
  293. function TGLMaterialScripter.ClassExists(arguement: string): boolean;
  294. var temp : string;
  295. i : word;
  296. begin
  297. classexists := false;
  298. if (pos(uppercase(arguement), uppercase(FScript.Strings[count])) > 0) and // check if there is an arguement
  299. (pos('=', FScript.Strings[count]) > pos(uppercase(arguement), uppercase(FScript.Strings[count])))and // check if it is before '='
  300. (pos('=', FScript.Strings[count]) > 0) then // check if there even is a '='
  301. begin
  302. temp := FScript.Strings[count];
  303. for i := 0 to length(temp) do
  304. if pos(' ', temp) = 1 then
  305. delete(temp,1,1);
  306. if pos(uppercase(arguement),uppercase(temp)) = 1 then
  307. if (temp[length(arguement) + 1] = ' ') or (temp[length(arguement) + 1] = '=') then
  308. begin
  309. classexists := true;
  310. if assigned(FMemo) then Fmemo.Lines.Add('Stage is at : ' + arguement);
  311. end;
  312. end;
  313. end;
  314. function TGLMaterialScripter.SubstrExists(substr: string): boolean;
  315. begin
  316. if pos(uppercase(substr),uppercase(FScript.Strings[count])) > 0 then result := true
  317. else
  318. result := false;
  319. end;
  320. constructor TGLMaterialScripter.Create(AOwner: TComponent);
  321. begin
  322. inherited Create(AOwner);
  323. FScript := TStringList.Create;
  324. FShaderItems:=TGLShaderItems.Create(Self);
  325. FMaterialLibraryItems:=TGLMaterialLibraryItems.Create(Self);
  326. FAppend := true;
  327. FOverwrite := false;
  328. end;
  329. function TGLMaterialScripter.DeleteSpaces(value: string): string;
  330. var i : byte;
  331. begin
  332. result := value;
  333. for i := 0 to length(result) do
  334. if pos(' ',result) > 0 then
  335. delete(result,pos(' ',result), 1);
  336. end;
  337. destructor TGLMaterialScripter.Destroy;
  338. begin
  339. FShaderItems.Free;
  340. FMaterialLibraryItems.Free;
  341. FScript.Free;
  342. inherited Destroy;
  343. end;
  344. procedure TGLMaterialScripter.ExtractColors;
  345. var val : string;
  346. begin
  347. val := Extractvalue;
  348. if pos('(',val) > 0 then
  349. begin
  350. tmpcolor.Alpha := GLUtils.StrToFloatDef(copy(val, pos('(',val) + 1, pos(';',val) - 2));
  351. delete(val,1,pos(';',val));
  352. tmpcolor.Red := GLUtils.StrToFloatDef(copy(val, 1, pos(';',val) - 1));
  353. delete(val,1,pos(';',val));
  354. tmpcolor.Green := GLUtils.StrToFloatDef(copy(val, 1, pos(';',val) - 1));
  355. delete(val,1,pos(';',val));
  356. tmpcolor.Blue := GLUtils.StrToFloatDef(copy(val, 1, pos(')',val) - 1));
  357. end;
  358. end;
  359. procedure TGLMaterialScripter.ExtractCoords3;
  360. var val : string;
  361. begin
  362. val := Extractvalue;
  363. if pos('(',val) > 0 then
  364. begin
  365. tmpcoords.X := GLUtils.StrToFloatDef(copy(val, pos('(',val) + 1, pos(';',val) - 2));
  366. delete(val,1,pos(';',val));
  367. tmpcoords.Y := GLUtils.StrToFloatDef(copy(val, 1, pos(';',val) - 1));
  368. delete(val,1,pos(';',val));
  369. tmpcoords.Z := GLUtils.StrToFloatDef(copy(val, 1, pos(')',val) - 1));
  370. end;
  371. end;
  372. procedure TGLMaterialScripter.ExtractCoords4;
  373. var val : string;
  374. begin
  375. val := Extractvalue;
  376. if pos('(',val) > 0 then
  377. begin
  378. tmpcoords4.W := GLUtils.StrToFloatDef(copy(val, pos('(',val) + 1, pos(';',val) - 2));
  379. delete(val,1,pos(';',val));
  380. tmpcoords4.X := GLUtils.StrToFloatDef(copy(val, 1, pos(';',val) - 1));
  381. delete(val,1,pos(';',val));
  382. tmpcoords4.Y := GLUtils.StrToFloatDef(copy(val, 1, pos(';',val) - 1));
  383. delete(val,1,pos(';',val));
  384. tmpcoords4.Z := GLUtils.StrToFloatDef(copy(val, 1, pos(')',val) - 1));
  385. end;
  386. end;
  387. function TGLMaterialScripter.ExtractValue: string;
  388. begin
  389. extractvalue := copy(FScript.Strings[count], pos('=',FScript.Strings[count]) + 1, length(FScript.Strings[count]) - pos('=',FScript.Strings[count]));
  390. end;
  391. procedure TGLMaterialScripter.XPersistantImage;
  392. begin
  393. if classexists('file') then
  394. begin
  395. if (extractvalue <> '') and (fileexists(extractvalue)) then
  396. begin
  397. with NewMat.Material.Texture.Image as TGLPersistentImage do
  398. LoadFromFile(extractvalue);
  399. NewMat.Material.Texture.Disabled := false;
  400. if assigned(FMemo) then FMemo.Lines.Add('File loaded : ' + extractvalue);
  401. end;
  402. end;
  403. end;
  404. procedure TGLMaterialScripter.XBlankImage;
  405. begin
  406. if classexists('file') then
  407. begin
  408. if (extractvalue <> '') and (fileexists(extractvalue)) then
  409. begin
  410. with NewMat.Material.Texture.Image as TGLBlankImage do // heres the difference
  411. LoadFromFile(extractvalue);
  412. NewMat.Material.Texture.Disabled := false;
  413. if assigned(FMemo) then FMemo.Lines.Add('File loaded : ' + extractvalue);
  414. end;
  415. end;
  416. end;
  417. procedure TGLMaterialScripter.XPictureFileName;
  418. begin
  419. if classexists('picturefilename') then
  420. with NewMat.Material.Texture.Image as TGLPicFileImage do
  421. if fileexists(extractvalue) then
  422. begin
  423. picturefilename := extractvalue;
  424. NewMat.Material.Texture.Disabled := false;
  425. end;
  426. end;
  427. procedure TGLMaterialScripter.XPictureNX;
  428. begin
  429. if classexists('picturenx') then
  430. if fileexists(extractvalue) then
  431. with NewMat.Material.Texture.Image as TGLCubeMapImage do
  432. Picture[cmtNX].LoadFromFile(extractvalue);
  433. end;
  434. procedure TGLMaterialScripter.XPictureNY;
  435. begin
  436. if classexists('pictureny') then
  437. if fileexists(extractvalue) then
  438. with NewMat.Material.Texture.Image as TGLCubeMapImage do
  439. Picture[cmtNY].LoadFromFile(extractvalue);
  440. end;
  441. procedure TGLMaterialScripter.XPictureNZ;
  442. begin
  443. if classexists('picturenz') then
  444. if fileexists(extractvalue) then
  445. with NewMat.Material.Texture.Image as TGLCubeMapImage do
  446. Picture[cmtNZ].LoadFromFile(extractvalue);
  447. end;
  448. procedure TGLMaterialScripter.XPicturePX;
  449. begin
  450. if classexists('picturepx') then
  451. if fileexists(extractvalue) then
  452. with NewMat.Material.Texture.Image as TGLCubeMapImage do
  453. Picture[cmtPX].LoadFromFile(extractvalue);
  454. end;
  455. procedure TGLMaterialScripter.XPicturePY;
  456. begin
  457. if classexists('picturepy') then
  458. if fileexists(extractvalue) then
  459. with NewMat.Material.Texture.Image as TGLCubeMapImage do
  460. Picture[cmtPY].LoadFromFile(extractvalue);
  461. end;
  462. procedure TGLMaterialScripter.XPicturePZ;
  463. begin
  464. if classexists('picturepz') then
  465. if fileexists(extractvalue) then
  466. with NewMat.Material.Texture.Image as TGLCubeMapImage do
  467. Picture[cmtPZ].LoadFromFile(extractvalue);
  468. end;
  469. function TGLMaterialScripter.ValueExists(value: string): boolean;
  470. begin
  471. if uppercase(tmpstr) = uppercase(value) then result := true
  472. else
  473. result := false;
  474. end;
  475. procedure TGLMaterialScripter.XMaterialLibrary;
  476. var i : word;
  477. begin
  478. if classexists('materiallibrary') then
  479. if MaterialLibraries.count > 0 then
  480. for i := 0 to MaterialLibraries.Count - 1 do
  481. if assigned(MaterialLibraries.Items[i].MaterialLibrary) then
  482. if uppercase(MaterialLibraries.Items[i].MaterialLibrary.Name) = uppercase(extractvalue) then
  483. NewMat.Material.MaterialLibrary := MaterialLibraries.Items[i].MaterialLibrary;
  484. end;
  485. procedure TGLMaterialScripter.XShader;
  486. var i : word;
  487. begin
  488. if classexists('shader') then
  489. if Shaders.count > 0 then
  490. for i := 0 to Shaders.Count - 1 do
  491. if assigned(Shaders.Items[i].Shader) then
  492. if uppercase(Shaders.Items[i].Shader.Name) = uppercase(extractvalue) then
  493. NewMat.Shader := Shaders.Items[i].Shader;
  494. end;
  495. procedure TGLMaterialScripter.ZMaterial;
  496. var i : byte;
  497. exists : boolean;
  498. begin
  499. if assigned(FMaterialLibrary) then
  500. begin
  501. NewMat := FMaterialLibrary.Materials.Add;
  502. repeat
  503. inc(count);
  504. XMaterial;
  505. if pos('{',FScript.Strings[count]) > 0 then
  506. for i := 0 to 2 do // need repair : something went wrong, and now we have to check 3 times over :/
  507. begin
  508. XTexture;
  509. XBackProperties;
  510. XFrontProperties;
  511. end;
  512. checkerror;
  513. until checkrepeatdone;
  514. // now we use append and overwrite settings to find out what is what
  515. tmpstr := NewMat.Name;
  516. delete(tmpstr,1,3); // removes the "TAG" not to confuse the system
  517. exists := false;
  518. if FMaterialLibrary.Materials.Count > 0 then
  519. for i := 0 to FMaterialLibrary.Materials.Count - 1 do
  520. if tmpstr = FMaterialLibrary.Materials.Items[i].Name then
  521. exists := true;
  522. if Exists then // does exist
  523. begin
  524. if FOverwrite then
  525. begin
  526. FMaterialLibrary.Materials.Delete(FMaterialLibrary.LibMaterialByName(tmpstr).Index);
  527. NewMat.Name := tmpstr;
  528. end
  529. else
  530. if FAppend then
  531. begin
  532. NewMat.Free;
  533. end;
  534. end
  535. else // doesn't exist
  536. begin
  537. NewMat.Name := tmpstr;
  538. if not FAppend then
  539. NewMat.Free;
  540. end;
  541. end;
  542. end;
  543. ///////////////////////////
  544. // extraction procedures //
  545. ///////////////////////////
  546. procedure TGLMaterialScripter.XBackAmbient;
  547. begin
  548. if classexists('ambient') then
  549. begin
  550. tmpcolor := NewMat.Material.BackProperties.Ambient;
  551. extractcolors;
  552. NewMat.Material.BackProperties.Ambient := tmpcolor;
  553. end;
  554. end;
  555. procedure TGLMaterialScripter.XBackDiffuse;
  556. begin
  557. if classexists('diffuse') then
  558. begin
  559. tmpcolor := NewMat.Material.BackProperties.Diffuse;
  560. extractcolors;
  561. NewMat.Material.BackProperties.Diffuse := tmpcolor;
  562. end;
  563. end;
  564. procedure TGLMaterialScripter.XBackEmission;
  565. begin
  566. if classexists('emission') then
  567. begin
  568. tmpcolor := NewMat.Material.BackProperties.Emission;
  569. extractcolors;
  570. NewMat.Material.BackProperties.Emission := tmpcolor;
  571. end;
  572. end;
  573. procedure TGLMaterialScripter.XBackShininess;
  574. begin
  575. if classexists('shininess') then
  576. if extractvalue <> '' then
  577. NewMat.Material.BackProperties.Shininess := strtoint(extractvalue);
  578. end;
  579. procedure TGLMaterialScripter.XBackSpecular;
  580. begin
  581. if classexists('specular') then
  582. begin
  583. tmpcolor := NewMat.Material.BackProperties.Specular;
  584. extractcolors;
  585. NewMat.Material.BackProperties.Specular := tmpcolor;
  586. end;
  587. end;
  588. procedure TGLMaterialScripter.XBlendingMode;
  589. begin
  590. if classexists('blendingmode') then
  591. begin
  592. tmpstr := extractvalue;
  593. if valueexists('bmOpaque') then Newmat.Material.BlendingMode := bmOpaque;
  594. if valueexists('bmTransparency') then Newmat.Material.BlendingMode := bmTransparency;
  595. if valueexists('bmAdditive') then Newmat.Material.BlendingMode := bmAdditive;
  596. if valueexists('bmAlphaTest100') then Newmat.Material.BlendingMode := bmAlphaTest100;
  597. if valueexists('bmAlphaTest50') then Newmat.Material.BlendingMode := bmAlphaTest50;
  598. end;
  599. end;
  600. procedure TGLMaterialScripter.XPolygonMode;
  601. begin
  602. if classexists('polygonmode') then
  603. begin
  604. tmpstr := extractvalue;
  605. if valueexists('pmFill') then Newmat.Material.PolygonMode := pmFill;
  606. if valueexists('pmLines') then Newmat.Material.PolygonMode := pmLines;
  607. if valueexists('pmPoints') then Newmat.Material.PolygonMode := pmPoints;
  608. end;
  609. end;
  610. procedure TGLMaterialScripter.XCompression;
  611. begin
  612. if classexists('compression') then
  613. begin
  614. tmpstr := extractvalue;
  615. if valueexists('tcDefault') then Newmat.Material.Texture.Compression := tcDefault;
  616. if valueexists('tcHighQuality') then Newmat.Material.Texture.Compression := tcHighQuality;
  617. if valueexists('tcHighSpeed') then Newmat.Material.Texture.Compression := tcHighSpeed;
  618. if valueexists('tcNone') then Newmat.Material.Texture.Compression := tcNone;
  619. if valueexists('tcStandard') then Newmat.Material.Texture.Compression := tcStandard;
  620. end;
  621. end;
  622. procedure TGLMaterialScripter.XEnvColor;
  623. begin
  624. if classexists('envcolor') then
  625. begin
  626. tmpcolor := NewMat.Material.Texture.EnvColor;
  627. extractcolors;
  628. NewMat.Material.Texture.EnvColor := tmpcolor;
  629. end;
  630. end;
  631. procedure TGLMaterialScripter.XFacingCulling;
  632. begin
  633. if classexists('faceculling') then
  634. begin
  635. tmpstr := extractvalue;
  636. if valueexists('fcBufferDefault') then Newmat.Material.FaceCulling := fcBufferDefault;
  637. if valueexists('fcCull') then Newmat.Material.FaceCulling := fcCull;
  638. if valueexists('fcNoCull') then Newmat.Material.FaceCulling := fcNoCull;
  639. end;
  640. end;
  641. procedure TGLMaterialScripter.XFilteringQuality;
  642. begin
  643. if classexists('filteringquality') then
  644. begin
  645. tmpstr := extractvalue;
  646. if valueexists('tfIsotropic') then Newmat.Material.Texture.FilteringQuality := tfIsotropic;
  647. if valueexists('tfAnisotropic') then Newmat.Material.Texture.FilteringQuality := tfAnisotropic;
  648. end;
  649. end;
  650. procedure TGLMaterialScripter.XfrontAmbient;
  651. begin
  652. if classexists('ambient') then
  653. begin
  654. tmpcolor := NewMat.Material.frontProperties.Ambient;
  655. extractcolors;
  656. NewMat.Material.frontProperties.Ambient := tmpcolor;
  657. end;
  658. end;
  659. procedure TGLMaterialScripter.XfrontDiffuse;
  660. begin
  661. if classexists('diffuse') then
  662. begin
  663. tmpcolor := NewMat.Material.frontProperties.Diffuse;
  664. extractcolors;
  665. NewMat.Material.frontProperties.Diffuse := tmpcolor;
  666. end;
  667. end;
  668. procedure TGLMaterialScripter.XfrontEmission;
  669. begin
  670. if classexists('emission') then
  671. begin
  672. tmpcolor := NewMat.Material.frontProperties.Emission;
  673. extractcolors;
  674. NewMat.Material.frontProperties.Emission := tmpcolor;
  675. end;
  676. end;
  677. procedure TGLMaterialScripter.XfrontShininess;
  678. begin
  679. if classexists('shininess') then
  680. if extractvalue <> '' then
  681. NewMat.Material.frontProperties.Shininess := strtoint(extractvalue);
  682. end;
  683. procedure TGLMaterialScripter.XfrontSpecular;
  684. begin
  685. if classexists('specular') then
  686. begin
  687. tmpcolor := NewMat.Material.frontProperties.Specular;
  688. extractcolors;
  689. NewMat.Material.frontProperties.Specular := tmpcolor;
  690. end;
  691. end;
  692. procedure TGLMaterialScripter.XImageAlpha;
  693. begin
  694. if classexists('imagealpha') then
  695. begin
  696. tmpstr := extractvalue;
  697. if valueexists('tiaDefault') then Newmat.Material.Texture.ImageAlpha := tiaDefault;
  698. if valueexists('tiaInverseLuminance') then Newmat.Material.Texture.ImageAlpha := tiaInverseLuminance;
  699. if valueexists('tiaInverseLuminanceSqrt') then Newmat.Material.Texture.ImageAlpha := tiaInverseLuminanceSqrt;
  700. if valueexists('tiaLuminance') then Newmat.Material.Texture.ImageAlpha := tiaLuminance;
  701. if valueexists('tiaLuminanceSqrt') then Newmat.Material.Texture.ImageAlpha := tiaLuminanceSqrt;
  702. if valueexists('tiaOpaque') then Newmat.Material.Texture.ImageAlpha := tiaOpaque;
  703. if valueexists('tiaSuperBlackTransparent') then Newmat.Material.Texture.ImageAlpha := tiaSuperBlackTransparent;
  704. if valueexists('tiaTopLeftPointColorTransparent') then Newmat.Material.Texture.ImageAlpha := tiaTopLeftPointColorTransparent;
  705. if valueexists('tiaAlphaFromIntensity') then Newmat.Material.Texture.ImageAlpha := tiaAlphaFromIntensity;
  706. end;
  707. end;
  708. procedure TGLMaterialScripter.XImageBrightness;
  709. begin
  710. if classexists('imagebrightness') then
  711. if extractvalue <> '' then
  712. NewMat.Material.Texture.ImageBrightness := GLUtils.StrToFloatDef(extractvalue);
  713. end;
  714. procedure TGLMaterialScripter.XImageGamma;
  715. begin
  716. if classexists('imagegamma') then
  717. if extractvalue <> '' then
  718. NewMat.Material.Texture.ImageGamma := GLUtils.StrToFloatDef(extractvalue);
  719. end;
  720. procedure TGLMaterialScripter.XLibMaterialName;
  721. begin
  722. if classexists('libmaterialname') then NewMat.Material.LibMaterialName := extractvalue;
  723. end;
  724. procedure TGLMaterialScripter.XMagFilter;
  725. begin
  726. if classexists('magfilter') then
  727. begin
  728. tmpstr := extractvalue;
  729. if valueexists('maLinear') then Newmat.Material.Texture.MagFilter := maLinear;
  730. if valueexists('maNearest') then Newmat.Material.Texture.MagFilter := maNearest;
  731. end;
  732. end;
  733. procedure TGLMaterialScripter.XMappingMode;
  734. begin
  735. if classexists('mappingmode') then
  736. begin
  737. tmpstr := extractvalue;
  738. if valueexists('tmmUser') then Newmat.Material.Texture.MappingMode := tmmUser;
  739. if valueexists('tmmCubeMapCamera') then Newmat.Material.Texture.MappingMode := tmmCubeMapCamera;
  740. if valueexists('tmmCubeMapLight0') then Newmat.Material.Texture.MappingMode := tmmCubeMapLight0;
  741. if valueexists('tmmCubeMapNormal') then Newmat.Material.Texture.MappingMode := tmmCubeMapNormal;
  742. if valueexists('tmmCubeMapReflection') then Newmat.Material.Texture.MappingMode := tmmCubeMapReflection;
  743. if valueexists('tmmEyeLinear') then Newmat.Material.Texture.MappingMode := tmmEyeLinear;
  744. if valueexists('tmmObjectLinear') then Newmat.Material.Texture.MappingMode := tmmObjectLinear;
  745. if valueexists('tmmSphere') then Newmat.Material.Texture.MappingMode := tmmSphere;
  746. end;
  747. end;
  748. procedure TGLMaterialScripter.XMappingSCoordinates;
  749. begin
  750. if classexists('mappingscoordinates') then
  751. begin
  752. tmpcoords4 := NewMat.Material.Texture.MappingSCoordinates;
  753. extractcoords4;
  754. NewMat.Material.Texture.MappingSCoordinates := tmpcoords4;
  755. end;
  756. end;
  757. procedure TGLMaterialScripter.XMappingTCoordinates;
  758. begin
  759. if classexists('mappingtcoordinates') then
  760. begin
  761. tmpcoords4 := NewMat.Material.Texture.MappingTCoordinates;
  762. extractcoords4;
  763. NewMat.Material.Texture.MappingTCoordinates := tmpcoords4;
  764. end;
  765. end;
  766. procedure TGLMaterialScripter.XMaterialOptions;
  767. var a,b : boolean;
  768. begin
  769. if classexists('materialoptions') then
  770. begin
  771. a := false;
  772. b := false;
  773. tmpstr := extractvalue;
  774. if uppercase(copy(tmpstr, pos('[',tmpstr) + 1, pos(',',tmpstr) - 2)) = uppercase('true') then a := true
  775. else
  776. if uppercase(copy(tmpstr, pos('[',tmpstr) + 1, pos(',',tmpstr) - 2)) = uppercase('false') then a := false;
  777. delete(tmpstr,1,pos(',',tmpstr));
  778. if uppercase(copy(tmpstr, 1, pos(']',tmpstr) - 1)) = uppercase('true') then b := true
  779. else
  780. if uppercase(copy(tmpstr, 1, pos(']',tmpstr) - 1)) = uppercase('false') then b := false;
  781. if a then NewMat.Material.MaterialOptions := NewMat.Material.MaterialOptions + [moIgnoreFog];
  782. if b then NewMat.Material.MaterialOptions := NewMat.Material.MaterialOptions + [moNoLighting];
  783. end;
  784. end;
  785. procedure TGLMaterialScripter.XMinFilter;
  786. begin
  787. if classexists('minfilter') then
  788. begin
  789. tmpstr := extractvalue;
  790. if valueexists('miLinearMipmapLinear') then Newmat.Material.Texture.MinFilter := miLinearMipmapLinear;
  791. if valueexists('miLinearMipmapNearest') then Newmat.Material.Texture.MinFilter := miLinearMipmapNearest;
  792. if valueexists('miNearest') then Newmat.Material.Texture.MinFilter := miNearest;
  793. if valueexists('miNearestMipmapLinear') then Newmat.Material.Texture.MinFilter := miNearestMipmapLinear;
  794. if valueexists('miNearestMipmapNearest') then Newmat.Material.Texture.MinFilter := miNearestMipmapNearest;
  795. if valueexists('miLinear') then Newmat.Material.Texture.MinFilter := miLinear;
  796. end;
  797. end;
  798. procedure TGLMaterialScripter.XName;
  799. begin
  800. if classexists('name') then NewMat.Name := 'TAG' + Extractvalue; // we gonna use for appending and such, quick fix style
  801. end;
  802. procedure TGLMaterialScripter.XNormalMapScale;
  803. begin
  804. if classexists('normalmapscale') then
  805. if extractvalue <> '' then
  806. NewMat.Material.Texture.NormalMapScale := GLUtils.StrToFloatDef(extractvalue);
  807. end;
  808. procedure TGLMaterialScripter.XTexture2Name;
  809. begin
  810. if classexists('texture2name') then NewMat.Texture2Name := ExtractValue;
  811. end;
  812. procedure TGLMaterialScripter.XTextureFormat;
  813. begin
  814. if classexists('textureformat') then
  815. begin
  816. tmpstr := extractvalue;
  817. if valueexists('tfDefault') then Newmat.Material.Texture.TextureFormat := tfDefault;
  818. if valueexists('tfIntensity') then Newmat.Material.Texture.TextureFormat := tfIntensity;
  819. if valueexists('tfLuminance') then Newmat.Material.Texture.TextureFormat := tfLuminance;
  820. if valueexists('tfLuminanceAlpha') then Newmat.Material.Texture.TextureFormat := tfLuminanceAlpha;
  821. if valueexists('tfNormalMap') then Newmat.Material.Texture.TextureFormat := tfNormalMap;
  822. if valueexists('tfRGB') then Newmat.Material.Texture.TextureFormat := tfRGB;
  823. if valueexists('tfRGB16') then Newmat.Material.Texture.TextureFormat := tfRGB16;
  824. if valueexists('tfRGBA') then Newmat.Material.Texture.TextureFormat := tfRGBA;
  825. if valueexists('tfRGBA16') then Newmat.Material.Texture.TextureFormat := tfRGBA16;
  826. if valueexists('tfAlpha') then Newmat.Material.Texture.TextureFormat := tfAlpha;
  827. end;
  828. end;
  829. procedure TGLMaterialScripter.XTextureMode;
  830. begin
  831. if classexists('texturemode') then
  832. begin
  833. tmpstr := extractvalue;
  834. if valueexists('tmDecal') then Newmat.Material.Texture.TextureMode := tmDecal;
  835. if valueexists('tmModulate') then Newmat.Material.Texture.TextureMode := tmModulate;
  836. if valueexists('tmReplace') then Newmat.Material.Texture.TextureMode := tmReplace;
  837. if valueexists('tmBlend') then Newmat.Material.Texture.TextureMode := tmBlend;
  838. end;
  839. end;
  840. procedure TGLMaterialScripter.XTextureOffset;
  841. begin
  842. if classexists('textureoffset') then // i hate this, delphi doesn't allow var object reference for procs
  843. begin
  844. tmpcoords := Newmat.TextureOffset;
  845. extractcoords3;
  846. Newmat.TextureOffset := tmpcoords;
  847. end;
  848. end;
  849. procedure TGLMaterialScripter.XTextureScale;
  850. begin
  851. if classexists('texturescale') then
  852. begin
  853. tmpcoords := Newmat.TextureScale;
  854. extractcoords3;
  855. NewMat.TextureScale := tmpcoords;
  856. end;
  857. end;
  858. procedure TGLMaterialScripter.XTextureWrap;
  859. begin
  860. if classexists('texturewrap') then
  861. begin
  862. tmpstr := extractvalue;
  863. if valueexists('twBoth') then Newmat.Material.Texture.TextureWrap := twBoth;
  864. if valueexists('twHorizontal') then Newmat.Material.Texture.TextureWrap := twHorizontal;
  865. if valueexists('twNone') then Newmat.Material.Texture.TextureWrap := twNone;
  866. if valueexists('twVertical') then Newmat.Material.Texture.TextureWrap := twVertical;
  867. end;
  868. end;
  869. ///////////////////////////////////////
  870. // sub routines : substr{arguements} //
  871. ///////////////////////////////////////
  872. procedure TGLMaterialScripter.XTexture;
  873. begin
  874. if substrexists('texture') then
  875. begin
  876. if assigned(FMemo) then FMemo.Lines.Add('texture');
  877. repeat
  878. inc(count);
  879. XCompression;
  880. XEnvColor;
  881. XFilteringQuality;
  882. XImageAlpha;
  883. XImageBrightness;
  884. XImageClass;
  885. XImageGamma;
  886. XMagFilter;
  887. XMappingMode;
  888. XMappingSCoordinates;
  889. XMappingTCoordinates;
  890. XMinFilter;
  891. XNormalMapScale;
  892. XTextureFormat;
  893. XTextureMode;
  894. XTextureWrap;
  895. checkerror;
  896. until checkrepeatdone;
  897. end;
  898. end;
  899. procedure TGLMaterialScripter.XMaterial;
  900. begin
  901. XName;
  902. XShader;
  903. XTexture2Name;
  904. XTextureOffset;
  905. XTextureScale;
  906. XMaterialOptions;
  907. XLibMaterialName;
  908. XBlendingMode;
  909. XPolygonMode;
  910. XFacingCulling;
  911. XMaterialLibrary;
  912. end;
  913. procedure TGLMaterialScripter.XfrontProperties;
  914. begin
  915. if substrexists('frontProperties') then
  916. begin
  917. if assigned(FMemo) then FMemo.Lines.Add('frontproperties');
  918. repeat
  919. inc(count);
  920. XfrontAmbient;
  921. XfrontDiffuse;
  922. XfrontEmission;
  923. XfrontShininess;
  924. XfrontSpecular;
  925. checkerror;
  926. until checkrepeatdone;
  927. end;
  928. end;
  929. procedure TGLMaterialScripter.XImageClass; // reckon this will be most difficult to get right
  930. begin
  931. if classexists('imageclassname') then
  932. begin
  933. tmpstr := extractvalue;
  934. tmpstr := deletespaces(tmpstr);
  935. if valueexists('persistentimage{') then
  936. repeat
  937. inc(count);
  938. Newmat.Material.Texture.ImageClassName := TGLPersistentImage.ClassName;
  939. XPersistantImage;
  940. checkerror;
  941. until checkrepeatdone;
  942. if valueexists('blankimage{') then
  943. repeat
  944. inc(count);
  945. Newmat.Material.Texture.ImageClassName := TGLBlankImage.ClassName;
  946. XBlankImage;
  947. checkerror;
  948. until checkrepeatdone;
  949. if valueexists('picfileimage{') then //picturefilename
  950. repeat
  951. inc(count);
  952. Newmat.Material.Texture.ImageClassName := TGLPicFileImage.ClassName;
  953. XPictureFilename;
  954. checkerror;
  955. until checkrepeatdone;
  956. if valueexists('cubemapimage{') then // px, nx, py, ny, pz, nz
  957. repeat
  958. inc(count);
  959. Newmat.Material.Texture.ImageClassName := TGLCubeMapImage.ClassName;
  960. XPicturePX;
  961. XPictureNX;
  962. XPicturePY;
  963. XPictureNY;
  964. XPicturePZ;
  965. XPictureNZ;
  966. NewMat.Material.Texture.Disabled := false;
  967. checkerror;
  968. until checkrepeatdone;
  969. // procedural noise not supported by GLTexture yet
  970. end;
  971. end;
  972. procedure TGLMaterialScripter.XBackProperties;
  973. begin
  974. if substrexists('BackProperties') then
  975. begin
  976. if assigned(FMemo) then FMemo.Lines.Add('backproperties');
  977. repeat
  978. inc(count);
  979. XBackAmbient;
  980. XBackDiffuse;
  981. XBackEmission;
  982. XBackShininess;
  983. XBackSpecular;
  984. checkerror;
  985. until checkrepeatdone;
  986. end;
  987. end;
  988. { TGLMaterialLibraryItems }
  989. constructor TGLMaterialLibraryItems.Create(AOwner: TPersistent);
  990. begin
  991. inherited Create(AOwner, TGLMaterialLibraryItem);
  992. end;
  993. function TGLMaterialLibraryItems.GetItems(index: Integer): TGLMaterialLibraryItem;
  994. begin
  995. Result:=TGLMaterialLibraryItem(inherited Items[index]);
  996. end;
  997. procedure TGLMaterialLibraryItems.SetItems(index: Integer;
  998. const val: TGLMaterialLibraryItem);
  999. begin
  1000. inherited Items[index]:=val;
  1001. end;
  1002. { TGLMaterialLibraryItem }
  1003. procedure TGLMaterialLibraryItem.Assign(Source: TPersistent);
  1004. begin
  1005. if Source is TGLMaterialLibraryItem then
  1006. begin
  1007. FMaterialLibrary := TGLMaterialLibraryItem(Source).FMaterialLibrary;
  1008. end;
  1009. inherited Destroy;
  1010. end;
  1011. constructor TGLMaterialLibraryItem.Create(Collection: TCollection);
  1012. begin
  1013. inherited Create(Collection);
  1014. FName := 'MaterialLibrary';
  1015. end;
  1016. destructor TGLMaterialLibraryItem.Destroy;
  1017. begin
  1018. inherited Destroy;
  1019. end;
  1020. function TGLMaterialLibraryItem.GetDisplayName: String;
  1021. begin
  1022. if FName = '' then
  1023. Result:='MaterialLibrary'
  1024. else
  1025. Result := FName;
  1026. end;
  1027. procedure TGLMaterialLibraryItem.SetMaterialLibrary(
  1028. const Value: TGLMaterialLibrary);
  1029. begin
  1030. if assigned(Value) then
  1031. begin
  1032. FMaterialLibrary := Value;
  1033. FName := FMaterialLibrary.Name;
  1034. end;
  1035. end;
  1036. procedure TGLMaterialLibraryItem.SetName(const Value: String);
  1037. begin
  1038. FName := Value;
  1039. end;
  1040. procedure TGLMaterialScripter.SeTGLMaterialLibraryItems(
  1041. const Value: TGLMaterialLibraryItems);
  1042. begin
  1043. FMaterialLibraryItems.Assign(Value);
  1044. end;
  1045. procedure TGLMaterialScripter.SetAppend(const Value: boolean);
  1046. begin
  1047. FAppend := Value;
  1048. end;
  1049. procedure TGLMaterialScripter.SetOverwrite(const Value: boolean);
  1050. begin
  1051. FOverwrite := Value;
  1052. end;
  1053. procedure TGLMaterialScripter.Notification(AComponent: TComponent;
  1054. Operation: TOperation);
  1055. begin
  1056. inherited;
  1057. if Operation = opRemove then
  1058. begin
  1059. if AComponent = FMaterialLibrary then
  1060. FMaterialLibrary := nil
  1061. else if AComponent = FMemo then
  1062. FMemo := nil;
  1063. end;
  1064. end;
  1065. end.