GLSL.MultiMaterialShader.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // The graphics engine GLScene
  3. //
  4. unit GLSL.MultiMaterialShader;
  5. (*
  6. A shader that applies a render pass for each material in
  7. its assigned MaterialLibrary.
  8. *)
  9. interface
  10. uses
  11. System.Classes,
  12. GLS.Material,
  13. GLS.Context,
  14. GLS.RenderContextInfo,
  15. GLS.State;
  16. type
  17. TGLMultiMaterialShader = class(TGLShader)
  18. private
  19. FPass: Integer;
  20. FMaterialLibrary: TGLMaterialLibrary;
  21. FVisibleAtDesignTime: boolean;
  22. FShaderActiveAtDesignTime: boolean;
  23. FShaderStyle: TGLShaderStyle;
  24. procedure SetVisibleAtDesignTime(const Value: boolean);
  25. procedure SetShaderStyle(const Value: TGLShaderStyle);
  26. protected
  27. procedure SetMaterialLibrary(const val: TGLMaterialLibrary);
  28. procedure DoApply(var rci: TGLRenderContextInfo; Sender: TObject); override;
  29. function DoUnApply(var rci: TGLRenderContextInfo): boolean; override;
  30. public
  31. constructor Create(aOwner: TComponent); override;
  32. published
  33. property MaterialLibrary: TGLMaterialLibrary read FMaterialLibrary
  34. write SetMaterialLibrary;
  35. property VisibleAtDesignTime: boolean read FVisibleAtDesignTime
  36. write SetVisibleAtDesignTime;
  37. property ShaderStyle: TGLShaderStyle read FShaderStyle write SetShaderStyle;
  38. end;
  39. // ------------------------------------------------------------------
  40. implementation
  41. // ------------------------------------------------------------------
  42. // ------------------
  43. // ------------------ TGLMultiMaterialShader ------------------
  44. // ------------------
  45. constructor TGLMultiMaterialShader.Create(aOwner: TComponent);
  46. begin
  47. inherited;
  48. FShaderStyle := ssReplace;
  49. FVisibleAtDesignTime := False;
  50. end;
  51. procedure TGLMultiMaterialShader.DoApply(var rci: TGLRenderContextInfo;
  52. Sender: TObject);
  53. begin
  54. if not Assigned(FMaterialLibrary) then
  55. exit;
  56. FShaderActiveAtDesignTime := FVisibleAtDesignTime;
  57. FPass := 1;
  58. if (not(csDesigning in ComponentState)) or FShaderActiveAtDesignTime then
  59. begin
  60. rci.ignoreDepthRequests := True;
  61. rci.GLStates.Enable(stDepthTest);
  62. rci.GLStates.DepthFunc := cfLEqual;
  63. if FMaterialLibrary.Materials.Count > 0 then
  64. FMaterialLibrary.Materials[0].Apply(rci);
  65. rci.ignoreDepthRequests := False;
  66. end;
  67. end;
  68. function TGLMultiMaterialShader.DoUnApply
  69. (var rci: TGLRenderContextInfo): boolean;
  70. begin
  71. Result := False;
  72. if not Assigned(FMaterialLibrary) then
  73. exit;
  74. if (not(csDesigning in ComponentState)) or FShaderActiveAtDesignTime then
  75. begin
  76. if FMaterialLibrary.Materials.Count > 0 then
  77. // handle multi-pass materials
  78. if FMaterialLibrary.Materials[FPass - 1].UnApply(rci) then
  79. begin
  80. Result := True;
  81. exit;
  82. end;
  83. if (FPass >= FMaterialLibrary.Materials.Count) then
  84. begin
  85. rci.GLStates.DepthFunc := cfLess;
  86. exit;
  87. end;
  88. FMaterialLibrary.Materials[FPass].Apply(rci);
  89. Result := True;
  90. Inc(FPass);
  91. end;
  92. end;
  93. procedure TGLMultiMaterialShader.SetMaterialLibrary
  94. (const val: TGLMaterialLibrary);
  95. begin
  96. if val <> FMaterialLibrary then
  97. begin
  98. FMaterialLibrary := val;
  99. NotifyChange(Self);
  100. end;
  101. end;
  102. procedure TGLMultiMaterialShader.SetShaderStyle(const Value: TGLShaderStyle);
  103. begin
  104. FShaderStyle := Value;
  105. inherited ShaderStyle := FShaderStyle;
  106. end;
  107. procedure TGLMultiMaterialShader.SetVisibleAtDesignTime(const Value: boolean);
  108. begin
  109. FVisibleAtDesignTime := Value;
  110. if csDesigning in ComponentState then
  111. NotifyChange(Self);
  112. end;
  113. end.