Luxophia
284 строки · 6.3 Кб
1unit LIB.Material;
2
3interface
4
5uses
6System.Classes,
7System.UITypes,
8System.Math.Vectors,
9System.SysUtils,
10System.RTLConsts,
11
12FMX.Types3D,
13FMX.Controls3D,
14FMX.MaterialSources,
15LUX,
16LUX.FMX.Material,
17LUX.FMX.Types3D;
18
19type
20// TMyMaterial
21
22TMyMaterial = class(TLuxMaterial)
23private
24protected
25_MatrixLS: TShaderVarMatrix3D;
26_MatrixLG: TShaderVarMatrix3D;
27_MatrixGL: TShaderVarMatrix3D;
28_Light: TShaderVarLight;
29_EyePos: TShaderVarVector3D;
30_Opacity: TShaderVarSingle;
31_Size: TShaderVarPoint3D;
32_Texture3D: TShaderVarTexture3D<TTexture3DRGBA32F>;
33procedure DoApply(const Context_: TContext3D); override;
34public
35constructor Create; override;
36destructor Destroy; override;
37property Size: TShaderVarPoint3D read _Size;
38property Texture3D: TShaderVarTexture3D<TTexture3DRGBA32F> read _Texture3D;
39end;
40
41// TMyMaterialSource
42
43TMyMaterialSource = class(TLuxMaterialSource<TMyMaterial>)
44private
45protected
46function GetSize: TPoint3D;
47procedure SetSize(const Size_: TPoint3D);
48function GetTexture3D: TTexture3DRGBA32F;
49public
50property Size: TPoint3D read GetSize write SetSize;
51property Texture3D: TTexture3DRGBA32F read GetTexture3D;
52end;
53
54// TVolumeCube
55
56TVolumeCube = class(TControl3D)
57private
58procedure MakeModel;
59protected
60_Geometry: TMeshData;
61_Material: TMyMaterialSource;
62procedure SetWidth(const Value_: Single); override;
63procedure SetHeight(const Value_: Single); override;
64procedure SetDepth(const Value_: Single); override;
65function GetTexture3D: TTexture3DRGBA32F;
66procedure Render; override;
67public
68constructor Create(Owner_: TComponent); override;
69destructor Destroy; override;
70property Material: TMyMaterialSource read _Material;
71end;
72
73
74implementation
75
76// TMyMaterial
77
78procedure TMyMaterial.DoApply(const Context_: TContext3D);
79begin
80inherited;
81
82with Context_ do
83begin
84SetShaders(_ShaderV.Shader, _ShaderP.Shader);
85
86_MatrixLS.Value := CurrentModelViewProjectionMatrix;
87_MatrixLG.Value := CurrentMatrix;
88_MatrixGL.Value := CurrentMatrix.Inverse;
89_Light.Value := Lights[0];
90_EyePos.Value := CurrentCameraInvMatrix.M[3];
91_Opacity.Value := CurrentOpacity;
92end;
93
94_ShaderV.SendVars(Context_);
95_ShaderP.SendVars(Context_);
96end;
97
98constructor TMyMaterial.Create;
99begin
100inherited;
101
102_MatrixLS := TShaderVarMatrix3D.Create('_MatrixLS');
103_MatrixLG := TShaderVarMatrix3D.Create('_MatrixLG');
104_MatrixGL := TShaderVarMatrix3D.Create('_MatrixGL');
105_Light := TShaderVarLight.Create('_Light');
106_EyePos := TShaderVarVector3D.Create('_EyePos');
107_Opacity := TShaderVarSingle.Create('_Opacity');
108_Size := TShaderVarPoint3D.Create('_Size');
109_Texture3D := TShaderVarTexture3D<TTexture3DRGBA32F>.Create('_Texture3D');
110
111_Size.Value := TPoint3D.Create(1, 1, 1);
112
113_ShaderV.Vars := [_MatrixLS];
114
115_ShaderP.Vars := [_MatrixLG, _MatrixGL, _Light, _EyePos, _Opacity, _Size, _Texture3D];
116end;
117
118destructor TMyMaterial.Destroy;
119begin
120_MatrixLS.Free;
121_MatrixLG.Free;
122_MatrixGL.Free;
123_Light.Free;
124_EyePos.Free;
125_Opacity.Free;
126_Size.Free;
127_Texture3D.Free;
128
129inherited;
130end;
131
132// TMyMaterialSource
133
134function TMyMaterialSource.GetSize: TPoint3D;
135begin
136Result := _Material.Size.Value;
137end;
138
139procedure TMyMaterialSource.SetSize(const Size_: TPoint3D);
140begin
141_Material.Size.Value := Size_;
142end;
143
144function TMyMaterialSource.GetTexture3D: TTexture3DRGBA32F;
145begin
146Result := _Material.Texture3D.Value;
147end;
148
149// TVolumeCube
150
151procedure TVolumeCube.MakeModel;
152begin
153_Material.Size := TPoint3D.Create(Width, Height, Depth);
154with _Geometry do
155begin
156with VertexBuffer do
157begin
158Length := 8 { Poin };
159
160Vertices[0] := TPoint3D.Create(0, 0, 0);
161Vertices[1] := TPoint3D.Create(Width, 0, 0);
162Vertices[2] := TPoint3D.Create(0, Height, 0);
163Vertices[3] := TPoint3D.Create(Width, Height, 0);
164Vertices[4] := TPoint3D.Create(0, 0, Depth);
165Vertices[5] := TPoint3D.Create(Width, 0, Depth);
166Vertices[6] := TPoint3D.Create(0, Height, Depth);
167Vertices[7] := TPoint3D.Create(Width, Height, Depth);
168end;
169
170with IndexBuffer do
171begin
172Length := 3 { Poin } * 2 { Face } * 6 { Quad };
173
174{ 4 5
175100---------101
176/ | / |
1770 / | 1 / |
178000---------001 |
179| | | |
180| | | |
181| 110---|-----111
182| / 6 | / 7
183| / | /
184010---------011
1852 3 }
186
187Indices[00] := 0;
188Indices[01] := 2;
189Indices[02] := 4;
190Indices[03] := 6;
191Indices[04] := 4;
192Indices[05] := 2;
193
194Indices[06] := 1;
195Indices[07] := 5;
196Indices[08] := 3;
197Indices[09] := 7;
198Indices[10] := 3;
199Indices[11] := 5;
200
201Indices[12] := 0;
202Indices[13] := 4;
203Indices[14] := 1;
204Indices[15] := 5;
205Indices[16] := 1;
206Indices[17] := 4;
207
208Indices[18] := 2;
209Indices[19] := 3;
210Indices[20] := 6;
211Indices[21] := 7;
212Indices[22] := 6;
213Indices[23] := 3;
214
215Indices[24] := 0;
216Indices[25] := 1;
217Indices[26] := 2;
218Indices[27] := 3;
219Indices[28] := 2;
220Indices[29] := 1;
221
222Indices[30] := 4;
223Indices[31] := 6;
224Indices[32] := 5;
225Indices[33] := 7;
226Indices[34] := 5;
227Indices[35] := 6;
228end;
229end;
230end;
231
232procedure TVolumeCube.SetWidth(const Value_: Single);
233begin
234inherited;
235
236MakeModel;
237end;
238
239procedure TVolumeCube.SetHeight(const Value_: Single);
240begin
241inherited;
242MakeModel;
243end;
244
245procedure TVolumeCube.SetDepth(const Value_: Single);
246begin
247inherited;
248MakeModel;
249end;
250
251function TVolumeCube.GetTexture3D: TTexture3DRGBA32F;
252begin
253Result := _Material.Texture3D;
254end;
255
256procedure TVolumeCube.Render;
257begin
258Context.SetMatrix(TMatrix3D.CreateTranslation(TPoint3D.Create(-Width / 2, -Height / 2, -Depth / 2)
259) * AbsoluteMatrix);
260
261_Geometry.Render(Context, TMaterialSource.ValidMaterial(_Material), AbsoluteOpacity);
262end;
263
264constructor TVolumeCube.Create(Owner_: TComponent);
265begin
266inherited;
267_Geometry := TMeshData.Create;
268_Material := TMyMaterialSource.Create(Self);
269HitTest := False;
270MakeModel;
271end;
272
273destructor TVolumeCube.Destroy;
274begin
275_Geometry.Free;
276_Material.Free;
277inherited;
278end;
279
280initialization
281
282finalization
283
284end.
285