-
Notifications
You must be signed in to change notification settings - Fork 64
/
Materials.pas
253 lines (219 loc) · 6.82 KB
/
Materials.pas
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
unit Materials;
{ Fireworks shader based on Martijn Steinrucken's "[SH17A] Fireworks" shader.
https://www.shadertoy.com/view/ldBfzw }
interface
uses
System.Classes,
System.SysUtils,
FMX.Types3D,
FMX.Graphics,
FMX.Materials,
FMX.MaterialSources;
type
TFireworksMaterialSource = class(TMaterialSource)
private
function GetTimeInSeconds: Single;
procedure SetTimeInSeconds(const AValue: Single);
function GetFireworksCount: Integer;
procedure SetFireworksCount(const AValue: Integer);
function GetSparkCount: Integer;
procedure SetSparkCount(const AValue: Integer);
protected
function CreateMaterial: TMaterial; override;
published
property TimeInSeconds: Single read GetTimeInSeconds write SetTimeInSeconds;
property SparkCount: Integer read GetSparkCount write SetSparkCount;
property FireworksCount: Integer read GetFireworksCount write SetFireworksCount;
end;
type
TFireworksMaterial = class(TCustomMaterial)
private class var
FShaderArch: TContextShaderArch;
FVertexShaderData: TBytes;
FPixelShaderData: TBytes;
FMatrixIndex: Integer;
FMatrixSize: Integer;
FFloatSize: Integer;
private
FTimeInSeconds: Single;
FFireworksCount: Integer;
FSparkCount: Integer;
procedure SetTimeInSeconds(const AValue: Single);
procedure SetFireworksCount(const AValue: Integer);
procedure SetSparkCount(const AValue: Integer);
private
class procedure LoadShaders; static;
class function LoadShader(const AResourceName: String): TBytes; static;
protected
procedure DoInitialize; override;
procedure DoApply(const Context: TContext3D); override;
public
constructor Create; override;
property TimeInSeconds: Single read FTimeInSeconds write SetTimeInSeconds;
property FireworksCount: Integer read FFireworksCount write SetFireworksCount;
property SparkCount: Integer read FSparkCount write SetSparkCount;
end;
implementation
uses
System.Types,
System.Math.Vectors,
{$IF Defined(MSWINDOWS)}
FMX.Context.DX9,
FMX.Context.DX11;
{$ELSEIF Defined(ANDROID)}
FMX.Context.GLES;
{$ELSEIF Defined(IOS)}
FMX.Context.GLES,
FMX.Context.Metal;
{$ELSEIF Defined(MACOS)}
FMX.Context.Mac,
FMX.Context.Metal;
{$ELSE}
{$MESSAGE Error 'Unsupported platform'}
{$ENDIF}
{$R 'Shaders\Shaders.res'}
{ TFireworksMaterialSource }
function TFireworksMaterialSource.CreateMaterial: TMaterial;
begin
Result := TFireworksMaterial.Create;
end;
function TFireworksMaterialSource.GetFireworksCount: Integer;
begin
Result := TFireworksMaterial(Material).FireworksCount;
end;
function TFireworksMaterialSource.GetSparkCount: Integer;
begin
Result := TFireworksMaterial(Material).SparkCount;
end;
function TFireworksMaterialSource.GetTimeInSeconds: Single;
begin
Result := TFireworksMaterial(Material).TimeInSeconds;
end;
procedure TFireworksMaterialSource.SetFireworksCount(const AValue: Integer);
begin
TFireworksMaterial(Material).FireworksCount := AValue;
end;
procedure TFireworksMaterialSource.SetSparkCount(const AValue: Integer);
begin
TFireworksMaterial(Material).SparkCount := AValue;
end;
procedure TFireworksMaterialSource.SetTimeInSeconds(const AValue: Single);
begin
TFireworksMaterial(Material).TimeInSeconds := AValue;
end;
{ TFireworksMaterial }
constructor TFireworksMaterial.Create;
begin
inherited;
FFireworksCount := 9;
FSparkCount := 50;
end;
procedure TFireworksMaterial.DoApply(const Context: TContext3D);
begin
inherited;
Context.SetShaderVariable('Time', [Vector3D(FTimeInSeconds, 0, 0, 0)]);
{ NOTE: In the shader, to loop variable starts at -2, so we need to subtract
2 from FFireworksCount to set the correct count. }
Context.SetShaderVariable('FireworksCount', [Vector3D(FFireworksCount - 2, 0, 0, 0)]);
Context.SetShaderVariable('SparkCount', [Vector3D(FSparkCount, 0, 0, 0)]);
end;
procedure TFireworksMaterial.DoInitialize;
begin
inherited;
if (FShaderArch = TContextShaderArch.Undefined) then
LoadShaders;
FVertexShader := TShaderManager.RegisterShaderFromData('fireworks.fvs',
TContextShaderKind.VertexShader, '', [
TContextShaderSource.Create(FShaderArch, FVertexShaderData,
[TContextShaderVariable.Create('MVPMatrix', TContextShaderVariableKind.Matrix,
FMatrixIndex, FMatrixSize)])
]);
FPixelShader := TShaderManager.RegisterShaderFromData('fireworks.fps',
TContextShaderKind.PixelShader, '', [
TContextShaderSource.Create(FShaderArch, FPixelShaderData,
[TContextShaderVariable.Create('Time', TContextShaderVariableKind.Float, 0, FFloatSize),
TContextShaderVariable.Create('FireworksCount', TContextShaderVariableKind.Float, FFloatSize, FFloatSize),
TContextShaderVariable.Create('SparkCount', TContextShaderVariableKind.Float, FFloatSize * 2, FFloatSize)])
]);
end;
class function TFireworksMaterial.LoadShader(const AResourceName: String): TBytes;
begin
var Stream := TResourceStream.Create(HInstance, AResourceName, RT_RCDATA);
try
SetLength(Result, Stream.Size);
Stream.ReadBuffer(Result, Length(Result));
finally
Stream.Free;
end;
end;
class procedure TFireworksMaterial.LoadShaders;
begin
var Suffix := '';
var ContextClass := TContextManager.DefaultContextClass;
{$IF Defined(MSWINDOWS)}
if (ContextClass.InheritsFrom(TCustomDX9Context)) then
begin
FShaderArch := TContextShaderArch.DX9;
FMatrixIndex := 0;
FMatrixSize := 4;
FFloatSize := 1;
Suffix := 'DX9';
end
else if (ContextClass.InheritsFrom(TCustomDX11Context)) then
begin
FShaderArch := TContextShaderArch.DX11;
FMatrixIndex := 0;
FMatrixSize := 64;
FFloatSize := 4;
Suffix := 'DX11';
end;
{$ELSE}
if (ContextClass.InheritsFrom(TCustomContextOpenGL)) then
begin
FShaderArch := TContextShaderArch.GLSL;
FMatrixIndex := 0;
FMatrixSize := 4;
FFloatSize := 1;
Suffix := 'GL';
end;
{$ENDIF}
{$IF Defined(MACOS)}
if (ContextClass.InheritsFrom(TCustomContextMetal)) then
begin
FShaderArch := TContextShaderArch.Metal;
FMatrixIndex := 1;
FMatrixSize := 4;
FFloatSize := 1;
Suffix := 'MTL';
end;
{$ENDIF}
if (FShaderArch = TContextShaderArch.Undefined) then
raise EContext3DException.Create('Unknown or unsupported 3D context class');
FVertexShaderData := LoadShader('VERTEX_SHADER_' + Suffix);
FPixelShaderData := LoadShader('PIXEL_SHADER_' + Suffix);
end;
procedure TFireworksMaterial.SetFireworksCount(const AValue: Integer);
begin
if (AValue <> FFireworksCount) then
begin
FFireworksCount := AValue;
DoChange;
end;
end;
procedure TFireworksMaterial.SetSparkCount(const AValue: Integer);
begin
if (AValue <> FSparkCount) then
begin
FSparkCount := AValue;
DoChange;
end;
end;
procedure TFireworksMaterial.SetTimeInSeconds(const AValue: Single);
begin
if (AValue <> FTimeInSeconds) then
begin
FTimeInSeconds := AValue;
DoChange;
end;
end;
end.