-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstaging.pas
More file actions
196 lines (176 loc) · 6.84 KB
/
staging.pas
File metadata and controls
196 lines (176 loc) · 6.84 KB
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
unit Staging;
{$mode objfpc}{$H+}
{$define pointlight}
// {$define spotlight}
interface
// "uri" : "Tileable Brick Ground Textures - Set 1/Ground_01_Nrm.png"
// "uri" : "Tileable Brick Ground Textures - Set 1/Ground_01.png"
uses
Classes, Math, SysUtils, CastleRectangles, CastleLog,
CastleSceneCore, CastleScene, CastleTransform, CastleURIUtils,
CastleImages, CastleTriangles, CastleShapes, CastleVectors,
X3DNodes, X3DFields, X3DTIme, X3DLoad, CastleRenderOptions,
CastleViewport, CastleCameras, CastleProjection, MiscFunctions;
type
TCastleStage = Class(TCastleScene)
private
fGroundTransformNode: TTransformNode;
{$if defined(spotlight)}
fLightNode: TSpotLightNode;
{$elseif defined(pointlight)}
fLightNode: TPointLightNode;
{$else}
fLightNode: TDirectionalLightNode;
{$endif}
fShadowNode: TSpotLightNode;
fGroundModelRoot: TX3DRootNode;
fGroundSwitch: TSwitchNode;
public
GroundTexture: String;
procedure LoadStage(const GroundModel: String; const GroundLevel: Single = 0; const GroundScale: Single = 5; const GroundSize: Single = 20);
procedure LoadStage(const GroundLevel: Single = 0);
procedure LoadStage(const GroundLevel: Single; const GroundColor: TVector3);
procedure LoadStage(const GroundLevel: Single; const GroundColor: TVector3; const GroundModel: String; const GroundScale: Single = 5; const GroundSize: Single = 20);
property GroundTransformNode: TTransformNode read fGroundTransformNode write fGroundTransformNode;
procedure ChangeTextureCoordinates(const Model3D: TX3DRootNode; const AScale: Single = 1.0);
function ChangeTexture(const ANode: TX3DRootNode; const TextureUrl: String): TVector3Cardinal;
function CreateGroundPlane(AFileName: String; const AScale: Single = 1.0): TX3DRootNode;
procedure ShowGround(const AChoice: Boolean);
{$if defined(spotlight)}
property LightNode: TSpotLightNode read fLightNode write fLightNode;
{$elseif defined(pointlight)}
property LightNode: TPointLightNode read fLightNode write fLightNode;
{$else}
property LightNode: TDirectionalLightNode read fLightNode write fLightNode;
{$endif}
property ShadowNode: TSpotLightNode read fShadowNode write fShadowNode;
property GroundModelRoot: TX3DRootNode read fGroundModelRoot;
end;
implementation
procedure TCastleStage.LoadStage(const GroundModel: String; const GroundLevel: Single = 0; const GroundScale: Single = 5; const GroundSize: Single = 20);
begin
LoadStage(GroundLevel, Vector3(1,1,1), GroundModel, GroundScale, GroundSize);
end;
procedure TCastleStage.LoadStage(const GroundLevel: Single = 0);
begin
LoadStage(GroundLevel, Vector3(1,1,1));
end;
procedure TCastleStage.LoadStage(const GroundLevel: Single; const GroundColor: TVector3);
begin
LoadStage(GroundLevel, Vector3(1,1,1), EmptyStr);
end;
procedure TCastleStage.ShowGround(const AChoice: Boolean);
begin
if AChoice then
fGroundSwitch.WhichChoice := 0
else
fGroundSwitch.WhichChoice := -1;
end;
procedure TCastleStage.LoadStage(const GroundLevel: Single; const GroundColor: TVector3; const GroundModel: String; const GroundScale: Single = 5; const GroundSize: Single = 20);
var
StageRootNode: TX3DRootNode;
begin
try
StageRootNode := TX3DRootNode.Create;
if (GroundModel = EmptyStr) or not(URIFileExists(GroundModel)) then
fGroundTransformNode := CreateColorPlane(GroundSize, GroundSize, GroundLevel, GroundColor)
else
begin
fGroundModelRoot := CreateGroundPlane(GroundModel, GroundScale);
fGroundTransformNode := TTransformNode.Create;
fGroundTransformNode.Translation := Vector3(0, GroundLevel, 0);
fGroundTransformNode.Scale := Vector3(GroundSize, 1, GroundSize);
fGroundTransformNode.AddChildren(fGroundModelRoot);
end;
fGroundTransformNode.X3DName := 'GroundTransformNode';
fGroundSwitch := TSwitchNode.Create;
fGroundSwitch.WhichChoice := 0;
fGroundSwitch.AddChildren(fGroundTransformNode);
{$if defined(spotlight)}
fLightNode := CreateSpotLight(Vector3(0, 30, 30));
fLightNode.Shadows := True;
{
fLightNode.DefaultShadowMap := TGeneratedShadowMapNode.Create;
fLightNode.DefaultShadowMap.Update := upAlways;
fLightNode.DefaultShadowMap.Size := 4096;
}
{$elseif defined(pointlight)}
fLightNode := CreatePointLight;
// fLightNode.Shadows := True;
{$else}
fLightNode := CreateDirectionalLight;
fLightNode.Shadows := True;
{$endif}
StageRootNode.AddChildren(fLightNode);
{
fShadowNode := CreateSpotLight;
fShadowNode.Shadows := True;
StageRootNode.AddChildren(fShadowNode);
}
StageRootNode.AddChildren(fGroundSwitch);
Load(StageRootNode, True);
// ReceiveShadowVolumes:=True;
Spatial := [ssDynamicCollisions, ssRendering];
ProcessEvents := True;
RenderOptions.PhongShading := true;
RenderOptions.ShadowSampling := ssSimple;
// RenderOptions.BlendingSort := sort2D;
except
on E : Exception do
begin
WriteLnLog('Oops #1' + LineEnding + E.ClassName + LineEnding + E.Message);
end;
end;
end;
procedure TCastleStage.ChangeTextureCoordinates(const Model3D: TX3DRootNode; const AScale: Single = 1.0);
var
TextureCoordinateNode: TTextureCoordinateNode;
NewCoords: array[0..3] of TVector2;
begin
if (AScale = 0) then
begin
WriteLnLog('Trying to change ground texture scale to zero - ignoring');
Exit;
end;
// Find the texture
TextureCoordinateNode := Model3D.TryFindNodeByName(TTextureCoordinateNode, 'ObjFrontTextureCoordinates', false) as TTextureCoordinateNode;
if not (TextureCoordinateNode = nil) then
begin
// Set new texture coordinates
NewCoords[0] := Vector2(AScale, 0);
NewCoords[1] := Vector2(AScale, AScale);
NewCoords[2] := Vector2(0, AScale);
NewCoords[3] := Vector2(0, 0);
TextureCoordinateNode.SetPoint(NewCoords);
end
else
WriteLnLog('Failed to change ground texture scale to ' + FloatToStr(AScale));
end;
function TCastleStage.ChangeTexture(const ANode: TX3DRootNode; const TextureUrl: String): TVector3Cardinal;
var
TextureNode: TImageTextureNode;
begin
Result := TVector3Cardinal.Zero;
TextureNode := ANode.TryFindNodeByName(TImageTextureNode, 'ObjFrontTexture', false) as TImageTextureNode;
if not (TextureNode = nil) then
begin
TextureNode.SetUrl(TextureUrl);
if TextureNode.IsTextureImage then
begin
Result := TextureNode.TextureImage.Dimensions;
GroundTexture := TextureUrl;
end;
end
else
WriteLnLog('Failed to change ground texture to ' + TextureUrl);
end;
function TCastleStage.CreateGroundPlane(AFileName: String; const AScale: Single = 1.0): TX3DRootNode;
var
PlaneNode: TX3DRootNode;
begin
PlaneNode := LoadNode('castle-data:/ground/plane.x3dv');
ChangeTexture(PlaneNode, AFileName);
ChangeTextureCoordinates(PlaneNode, AScale);
Result := PlaneNode;
end;
end.