In the first blog post, we quickly explored shader programming in Delphi FireMonkey. And we learned how to create a material source using TCustomMaterial. Then we applied that material to a 3D control. This post is based on a blog post by Embarcadero MVP Erik van Bilsen.
Now, we will add a color property to our 3D control to customize color. This can be achieved by adding a uniform input to the pixel shader for the color. So, on the Delphi side, the Color property should be added to the new material.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
type TCustomColorMaterial = class(TCustomMaterial) ... private FColor: TAlphaColor; procedure SetColor(const AValue: TAlphaColor); protected procedure DoApply(const Context: TContext3D); override; procedure DoInitialize; override; public constructor Create; override; property Color: TAlphaColor read FColor write SetColor; end; { TCustomColorMaterial } constructor TCustomColorMaterial.Create; begin inherited; FColor := TAlphaColors.Blue; end; procedure TCustomColorMaterial.DoApply(const Context: TContext3D); begin inherited; Context.SetShaderVariable('Color', FColor); end; procedure TCustomColorMaterial.DoInitialize; begin inherited; ... FPixelShader := TShaderManager.RegisterShaderFromData('CustomColor.fps', TContextShaderKind.PixelShader, '', [ TContextShaderSource.Create(FShaderArch, FPixelShaderData, [TContextShaderVariable.Create('Color', TContextShaderVariableKind.Vector, 0, FColorSize)]) ]); end; procedure TCustomColorMaterial.SetColor(const AValue: TAlphaColor); begin if (AValue <> FColor) then begin FColor := AValue; DoChange; end; end; |
- From this code we can observe that to pass the color to the GPU shader, we must override the DoApply function. In the DoApply function, you call SetShaderVariable method to pass the color to the shader.
Finally, you must update the material source by adding a new Color property. Here is the implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
type TCustomColorMaterialSource = class(TMaterialSource) private function GetColor: TAlphaColor; procedure SetColor(const AValue: TAlphaColor); protected function CreateMaterial: TMaterial; override; published property Color: TAlphaColor read GetColor write SetColor; end; { TCustomColorMaterialSource } function TCustomColorMaterialSource.CreateMaterial: TMaterial; begin Result := TCustomColorMaterial.Create; end; function TCustomColorMaterialSource.GetColor: TAlphaColor; begin Result := TCustomColorMaterial(Material).Color; end; procedure TCustomColorMaterialSource.SetColor(const AValue: TAlphaColor); begin TCustomColorMaterial(Material).Color := AValue; end; |
And here is our result:
Head over and be sure to check out the full blog post and demo applications here. And stay tuned for the next part which we will add a texture to our 3D control.
You can program easily window shader and apply color to the GPU shader using Delphi’s IDE Software. Try your Free Trial here and begin developing in C++ or Delphi environments right away.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition