| 1 |
{%MainUnit castlescene.pas} |
| 2 |
{ |
| 3 |
Copyright 2022-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
type |
| 20 |
{ Attaches a gizmo to visualize parent transform. |
| 21 |
Create this only if you need it (usually check "if CastleDesignMode ..."). |
| 22 |
@exclude } |
| 23 |
TInternalCastleEditorGizmo = class(TCastleTransform) |
| 24 |
strict private |
| 25 |
GizmoSelect: TCastleBox; |
| 26 |
GizmoImage: TCastleImageTransform; |
| 27 |
GizmoBillboard: TCastleBillboard; |
| 28 |
GizmoScene: TCastleScene; //< Note that it may remain nil, if LoadVisualization not called |
| 29 |
LastIsProjectionOrthographic: Boolean; |
| 30 |
{ Set various properties for internal TCastleTransform, shown only for debug. } |
| 31 |
procedure InternalTransform(const T: TCastleTransform); |
| 32 |
function GetSelectBoxTranslation: TVector3; |
| 33 |
procedure SetSelectBoxTranslation(const Value: TVector3); |
| 34 |
protected |
| 35 |
function LocalRayCollision(const RayOrigin, RayDirection: TVector3; |
| 36 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): TRayCollision; override; |
| 37 |
public |
| 38 |
class var |
| 39 |
{ When non-zero, LocalBoundingBox and BoundingBox of it is empty. } |
| 40 |
EmptyBoundingBox: Cardinal; |
| 41 |
|
| 42 |
constructor Create(AOwner: TComponent); override; |
| 43 |
procedure LocalRender(const Params: TRenderParams); override; |
| 44 |
function LocalBoundingBox: TBox3D; override; |
| 45 |
|
| 46 |
{ Show given node graph as the gizmo visualization. |
| 47 |
The given RootNode will become owned by this instance. } |
| 48 |
procedure LoadVisualization(const RootNode: TX3DRootNode); |
| 49 |
|
| 50 |
{ Create a line shape, suitable for node graph in LoadVisualization. |
| 51 |
Creates TLineSetNode with @link(TLineSetNode.Mode) = lmPair, |
| 52 |
as this the most universal line mode to define various shapes. |
| 53 |
The version that returns TCoordinateNode lets you initialize |
| 54 |
(and later update) lines coords explicitly. } |
| 55 |
function LinesShape(const Points: array of TVector3): TShapeNode; overload; |
| 56 |
function LinesShape(out Coord: TCoordinateNode): TShapeNode; overload; |
| 57 |
{ Set the image to display as icon. } |
| 58 |
procedure SetIconUrl(const Url: String); |
| 59 |
property SelectBoxTranslation: TVector3 read GetSelectBoxTranslation write SetSelectBoxTranslation; |
| 60 |
|
| 61 |
class function ShouldGizmoBeRendered( |
| 62 |
const IsProjectionOrthographic: Boolean; |
| 63 |
const SelfPosition, RenderingCameraPosition: TVector3): Boolean; |
| 64 |
end; |
| 65 |
|
| 66 |
{$endif read_interface} |
| 67 |
|
| 68 |
{$ifdef read_implementation} |
| 69 |
|
| 70 |
{ TInternalCastleEditorGizmo ------------------------------------------------- } |
| 71 |
|
| 72 |
constructor TInternalCastleEditorGizmo.Create(AOwner: TComponent); |
| 73 |
const |
| 74 |
GizmoSize = 1.0; |
| 75 |
begin |
| 76 |
inherited; |
| 77 |
SetTransient; |
| 78 |
|
| 79 |
// This allows user to select parent (camera, light etc.) by clicking in CGE editor |
| 80 |
GizmoSelect := TCastleBox.Create(Self); |
| 81 |
GizmoSelect.Size := CastleVectors.Vector3(GizmoSize, GizmoSize, GizmoSize); |
| 82 |
InternalTransform(GizmoSelect); |
| 83 |
GizmoSelect.Visible := false; |
| 84 |
// to make picking natural, restore to default these values, after InternalTransform modified them |
| 85 |
GizmoSelect.Pickable := true; |
| 86 |
GizmoSelect.InternalExcludeFromParentBoundingVolume := false; |
| 87 |
Add(GizmoSelect); |
| 88 |
|
| 89 |
GizmoImage := TCastleImageTransform.Create(Self); |
| 90 |
GizmoImage.Size := Vector2(GizmoSize, GizmoSize); |
| 91 |
GizmoImage.Mipmaps := true; |
| 92 |
GizmoImage.Exists := false; // will change to true in SetIconUrl, just a tiny optimization to not use GizmoImage when no icon |
| 93 |
InternalTransform(GizmoImage); |
| 94 |
Add(GizmoImage); |
| 95 |
|
| 96 |
GizmoBillboard := TCastleBillboard.Create(Self); |
| 97 |
GizmoBillboard.AxisOfRotation := TVector3.Zero; |
| 98 |
GizmoImage.AddBehavior(GizmoBillboard); |
| 99 |
|
| 100 |
LastIsProjectionOrthographic := false; |
| 101 |
end; |
| 102 |
|
| 103 |
procedure TInternalCastleEditorGizmo.InternalTransform(const T: TCastleTransform); |
| 104 |
begin |
| 105 |
T.InternalExcludeFromParentBoundingVolume := true; |
| 106 |
T.Pickable := false; |
| 107 |
T.Collides := false; |
| 108 |
T.CastShadows := false; |
| 109 |
end; |
| 110 |
|
| 111 |
procedure TInternalCastleEditorGizmo.LoadVisualization(const RootNode: TX3DRootNode); |
| 112 |
begin |
| 113 |
{ GizmoScene is created on demand, to avoid creating it for things that |
| 114 |
never call LoadVisualization. } |
| 115 |
if GizmoScene = nil then |
| 116 |
begin |
| 117 |
GizmoScene := TCastleScene.Create(Self); |
| 118 |
InternalTransform(GizmoScene); |
| 119 |
Add(GizmoScene); |
| 120 |
end; |
| 121 |
|
| 122 |
GizmoScene.Load(RootNode, true); |
| 123 |
end; |
| 124 |
|
| 125 |
function TInternalCastleEditorGizmo.LinesShape(const Points: array of TVector3): TShapeNode; |
| 126 |
var |
| 127 |
Coord: TCoordinateNode; |
| 128 |
begin |
| 129 |
Result := LinesShape(Coord); |
| 130 |
Coord.SetPoint(Points); |
| 131 |
end; |
| 132 |
|
| 133 |
function TInternalCastleEditorGizmo.LinesShape(out Coord: TCoordinateNode): TShapeNode; |
| 134 |
var |
| 135 |
LineSet: TLineSetNode; |
| 136 |
Material: TUnlitMaterialNode; |
| 137 |
Appearance: TAppearanceNode; |
| 138 |
begin |
| 139 |
Coord := TCoordinateNode.Create; |
| 140 |
|
| 141 |
LineSet := TLineSetNode.Create; |
| 142 |
LineSet.Mode := lmPair; |
| 143 |
LineSet.Coord := Coord; |
| 144 |
|
| 145 |
Material := TUnlitMaterialNode.Create; |
| 146 |
Material.EmissiveColor := HexToColorRGB('FC9C2D'); |
| 147 |
|
| 148 |
Appearance := TAppearanceNode.Create; |
| 149 |
Appearance.Material := Material; |
| 150 |
|
| 151 |
Result := TShapeNode.Create; |
| 152 |
Result.Geometry := LineSet; |
| 153 |
Result.Appearance := Appearance; |
| 154 |
end; |
| 155 |
|
| 156 |
function TInternalCastleEditorGizmo.LocalBoundingBox: TBox3D; |
| 157 |
begin |
| 158 |
if EmptyBoundingBox <> 0 then |
| 159 |
Exit(TBox3D.Empty) |
| 160 |
else |
| 161 |
Result := inherited LocalBoundingBox; |
| 162 |
end; |
| 163 |
|
| 164 |
class function TInternalCastleEditorGizmo.ShouldGizmoBeRendered( |
| 165 |
const IsProjectionOrthographic: Boolean; |
| 166 |
const SelfPosition, RenderingCameraPosition: TVector3): Boolean; |
| 167 |
const |
| 168 |
DistanceToExistGizmo = 1; |
| 169 |
var |
| 170 |
DistanceToCameraSqr: Single; |
| 171 |
begin |
| 172 |
{ ShouldGizmoBeRendered always answers true when using orthographic camera, |
| 173 |
this way you can see and select orthographic camera at design-time |
| 174 |
even when current camera is at exactly the same position. |
| 175 |
This works OK in 2D mode, without causing any issues, so don't let |
| 176 |
ShouldGizmoBeRendered complicate it. } |
| 177 |
if IsProjectionOrthographic then |
| 178 |
Exit(true); |
| 179 |
|
| 180 |
DistanceToCameraSqr := PointsDistanceSqr(SelfPosition, RenderingCameraPosition); |
| 181 |
Result := DistanceToCameraSqr > Sqr(DistanceToExistGizmo); |
| 182 |
end; |
| 183 |
|
| 184 |
procedure TInternalCastleEditorGizmo.LocalRender(const Params: TRenderParams); |
| 185 |
var |
| 186 |
GizmoRendered: Boolean; |
| 187 |
begin |
| 188 |
{ We save LastIsProjectionOrthographic value, to be used in next |
| 189 |
LocalRayCollision call. |
| 190 |
Note that we don't save whole ShouldGizmoBeRendered result, |
| 191 |
this would cause bugs: camera may be present in 2 viewports, |
| 192 |
e.g. at design-time: main viewport, and the "camera preview". |
| 193 |
If we would save ShouldGizmoBeRendered result, then camera gizmo |
| 194 |
(e.g. to translate) would not be pickable when camera is also selected |
| 195 |
(and camera is always selected when it shows its gizmo). } |
| 196 |
LastIsProjectionOrthographic := |
| 197 |
(Params.RenderingCamera.Camera <> nil) and |
| 198 |
(Params.RenderingCamera.Camera.ProjectionType = ptOrthographic); |
| 199 |
|
| 200 |
GizmoRendered := ShouldGizmoBeRendered(LastIsProjectionOrthographic, |
| 201 |
Params.Transformation^.Transform.MultPoint(TVector3.Zero), |
| 202 |
Params.RenderingCamera.View.Translation |
| 203 |
); |
| 204 |
if not GizmoRendered then |
| 205 |
Exit; // do not show gizmo |
| 206 |
inherited; |
| 207 |
end; |
| 208 |
|
| 209 |
function TInternalCastleEditorGizmo.LocalRayCollision(const RayOrigin, RayDirection: TVector3; |
| 210 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): TRayCollision; |
| 211 |
var |
| 212 |
GizmoRendered: Boolean; |
| 213 |
begin |
| 214 |
{ Do not collide with FGizmoSelect if not ShouldGizmoBeRendered. |
| 215 |
This is esp. important for TCastleCamera gizmo, |
| 216 |
without this code we would prevent selecting any transform when we look from |
| 217 |
some non-design camera, |
| 218 |
as camera's GizmoSelect would collide with all rays cast from camera position. } |
| 219 |
GizmoRendered := ShouldGizmoBeRendered(LastIsProjectionOrthographic, |
| 220 |
TVector3.Zero, RayOrigin); |
| 221 |
if not GizmoRendered then |
| 222 |
Exit(nil); // do not pick with gizmo with raycast |
| 223 |
Result := inherited; |
| 224 |
end; |
| 225 |
|
| 226 |
procedure TInternalCastleEditorGizmo.SetIconUrl(const Url: String); |
| 227 |
begin |
| 228 |
GizmoImage.Url := Url; |
| 229 |
GizmoImage.Exists := Url <> ''; |
| 230 |
end; |
| 231 |
|
| 232 |
function TInternalCastleEditorGizmo.GetSelectBoxTranslation: TVector3; |
| 233 |
begin |
| 234 |
Result := GizmoSelect.Translation; |
| 235 |
end; |
| 236 |
|
| 237 |
procedure TInternalCastleEditorGizmo.SetSelectBoxTranslation(const Value: TVector3); |
| 238 |
begin |
| 239 |
GizmoSelect.Translation := Value; |
| 240 |
end; |
| 241 |
|
| 242 |
{$endif read_implementation} |