GLS.ShaderMultiMaterial.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit GLS.ShaderMultiMaterial;
  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. GLMaterial,
  13. GLContext,
  14. GLRenderContextInfo,
  15. GLState;
  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 write SetMaterialLibrary;
  34. property VisibleAtDesignTime : boolean read FVisibleAtDesignTime write SetVisibleAtDesignTime;
  35. property ShaderStyle:TGLShaderStyle read FShaderStyle write SetShaderStyle;
  36. end;
  37. // ------------------------------------------------------------------
  38. implementation
  39. // ------------------------------------------------------------------
  40. // ------------------
  41. // ------------------ TGLMultiMaterialShader ------------------
  42. // ------------------
  43. constructor TGLMultiMaterialShader.Create(aOwner : TComponent);
  44. begin
  45. inherited;
  46. FShaderStyle:=ssReplace;
  47. FVisibleAtDesignTime := False;
  48. end;
  49. procedure TGLMultiMaterialShader.DoApply(var rci: TGLRenderContextInfo; Sender : TObject);
  50. begin
  51. if not Assigned(FMaterialLibrary) then exit;
  52. FShaderActiveAtDesignTime := FVisibleAtDesignTime;
  53. FPass:=1;
  54. if (not (csDesigning in ComponentState)) or FShaderActiveAtDesignTime then begin
  55. rci.ignoreDepthRequests := True;
  56. rci.GLStates.Enable(stDepthTest);
  57. rci.GLStates.DepthFunc := cfLEqual;
  58. if FMaterialLibrary.Materials.Count>0 then
  59. FMaterialLibrary.Materials[0].Apply(rci);
  60. rci.ignoreDepthRequests := False;
  61. end;
  62. end;
  63. function TGLMultiMaterialShader.DoUnApply(
  64. var rci: TGLRenderContextInfo): Boolean;
  65. begin
  66. Result:=False;
  67. if not Assigned(FMaterialLibrary) then exit;
  68. if (not (csDesigning in ComponentState)) or FShaderActiveAtDesignTime then begin
  69. if FMaterialLibrary.Materials.Count>0 then
  70. // handle multi-pass materials
  71. if FMaterialLibrary.Materials[FPass-1].UnApply(rci) then
  72. begin
  73. Result:=true;
  74. Exit;
  75. end;
  76. if (FPass >= FMaterialLibrary.Materials.Count) then begin
  77. rci.GLStates.DepthFunc := cfLess;
  78. exit;
  79. end;
  80. FMaterialLibrary.Materials[FPass].Apply(rci);
  81. Result:=True;
  82. Inc(FPass);
  83. end;
  84. end;
  85. procedure TGLMultiMaterialShader.SetMaterialLibrary(
  86. const val: TGLMaterialLibrary);
  87. begin
  88. if val<>FMaterialLibrary then begin
  89. FMaterialLibrary:=val;
  90. NotifyChange(Self);
  91. end;
  92. end;
  93. procedure TGLMultiMaterialShader.SetShaderStyle(const Value: TGLShaderStyle);
  94. begin
  95. FShaderStyle := Value;
  96. inherited ShaderStyle :=FShaderStyle;
  97. end;
  98. procedure TGLMultiMaterialShader.SetVisibleAtDesignTime(
  99. const Value: boolean);
  100. begin
  101. FVisibleAtDesignTime := Value;
  102. if csDesigning in ComponentState then
  103. NotifyChange(Self);
  104. end;
  105. end.