| 1 |
{%MainUnit castlescene.pas} |
| 2 |
{ |
| 3 |
Copyright 2021-2024 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 |
{ Sphere with configurable size, position and material. } |
| 21 |
TCastleSphere = class(TCastleAbstractPrimitive) |
| 22 |
strict private |
| 23 |
SphereNode: TSphereNode; |
| 24 |
FRadius: Single; |
| 25 |
FDoubleSided: Boolean; |
| 26 |
FSlices, FStacks: Cardinal; |
| 27 |
procedure SetRadius(const Value: Single); |
| 28 |
procedure SetDoubleSided(const Value: Boolean); |
| 29 |
procedure SetSlices(const Value: Cardinal); |
| 30 |
procedure SetStacks(const Value: Cardinal); |
| 31 |
public |
| 32 |
constructor Create(AOwner: TComponent); override; |
| 33 |
destructor Destroy; override; |
| 34 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 35 |
published |
| 36 |
{ Sphere radius. } |
| 37 |
property Radius: Single read FRadius write SetRadius {$ifdef FPC}default 1{$endif}; |
| 38 |
|
| 39 |
{ When this is @true, the object is visible (and lit) from both sides. } |
| 40 |
property DoubleSided: Boolean read FDoubleSided write SetDoubleSided default false; |
| 41 |
|
| 42 |
{ CollisionSphereRadius is always derived from Radius for this component. } |
| 43 |
property CollisionSphereRadius stored false; |
| 44 |
|
| 45 |
{ How many "slices" to use to approximate the sphere. |
| 46 |
The sphere is rendered using polygons (triangles and quads) |
| 47 |
that approximate the sphere shape. |
| 48 |
The more slices, the more the sphere will look like a sphere. |
| 49 |
|
| 50 |
Slices divide the objects like slices of a pizza. |
| 51 |
|
| 52 |
The default value of 0 means to follow global variable |
| 53 |
@link(DefaultTriangulationSlices).} |
| 54 |
property Slices: Cardinal read FSlices write SetSlices default 0; |
| 55 |
|
| 56 |
{ How many "stacks" to use to approximate the sphere. |
| 57 |
The sphere is rendered using polygons (triangles and quads) |
| 58 |
that approximate the sphere shape. |
| 59 |
The more stacks, the more the sphere will look like a sphere. |
| 60 |
|
| 61 |
Stacks divide the objects like stacks of a tower. |
| 62 |
|
| 63 |
The default value of 0 means to follow global variable |
| 64 |
@link(DefaultTriangulationStacks).} |
| 65 |
property Stacks: Cardinal read FStacks write SetStacks default 0; |
| 66 |
end; |
| 67 |
|
| 68 |
{$endif read_interface} |
| 69 |
|
| 70 |
{$ifdef read_implementation} |
| 71 |
|
| 72 |
{ TCastleSphere ---------------------------------------------------------------- } |
| 73 |
|
| 74 |
constructor TCastleSphere.Create(AOwner: TComponent); |
| 75 |
begin |
| 76 |
inherited; |
| 77 |
|
| 78 |
{ default properties values } |
| 79 |
FRadius := 1; |
| 80 |
|
| 81 |
SphereNode := TSphereNode.Create; |
| 82 |
SphereNode.Radius := FRadius; |
| 83 |
SphereNode.Solid := not FDoubleSided; |
| 84 |
SphereNode.Slices := FSlices; |
| 85 |
SphereNode.Stacks := FStacks; |
| 86 |
ShapeNode.Geometry := SphereNode; |
| 87 |
|
| 88 |
{ set CollisionSphereRadius to make collisions work better for sphere } |
| 89 |
CollisionSphereRadius := FRadius; |
| 90 |
end; |
| 91 |
|
| 92 |
destructor TCastleSphere.Destroy; |
| 93 |
begin |
| 94 |
inherited; |
| 95 |
end; |
| 96 |
|
| 97 |
function TCastleSphere.PropertySections(const PropertyName: String): TPropertySections; |
| 98 |
begin |
| 99 |
if ArrayContainsString(PropertyName, ['Radius', 'DoubleSided', |
| 100 |
'Slices', 'Stacks']) then |
| 101 |
Result := [psBasic] |
| 102 |
else |
| 103 |
Result := inherited PropertySections(PropertyName); |
| 104 |
end; |
| 105 |
|
| 106 |
procedure TCastleSphere.SetRadius(const Value: Single); |
| 107 |
begin |
| 108 |
if FRadius <> Value then |
| 109 |
begin |
| 110 |
FRadius := Value; |
| 111 |
SphereNode.Radius := FRadius; |
| 112 |
CollisionSphereRadius := FRadius; |
| 113 |
UpdateCollider; |
| 114 |
end; |
| 115 |
end; |
| 116 |
|
| 117 |
procedure TCastleSphere.SetDoubleSided(const Value: Boolean); |
| 118 |
begin |
| 119 |
if FDoubleSided <> Value then |
| 120 |
begin |
| 121 |
FDoubleSided := Value; |
| 122 |
SphereNode.Solid := not Value; |
| 123 |
end; |
| 124 |
end; |
| 125 |
|
| 126 |
procedure TCastleSphere.SetSlices(const Value: Cardinal); |
| 127 |
begin |
| 128 |
if FSlices <> Value then |
| 129 |
begin |
| 130 |
FSlices := Value; |
| 131 |
SphereNode.Slices := FSlices; |
| 132 |
end; |
| 133 |
end; |
| 134 |
|
| 135 |
procedure TCastleSphere.SetStacks(const Value: Cardinal); |
| 136 |
begin |
| 137 |
if FStacks <> Value then |
| 138 |
begin |
| 139 |
FStacks := Value; |
| 140 |
SphereNode.Stacks := FStacks; |
| 141 |
end; |
| 142 |
end; |
| 143 |
|
| 144 |
{$endif read_implementation} |