Skip to content

Instantly share code, notes, and snippets.

@michaliskambi
Last active June 13, 2023 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaliskambi/04e803545b386460fa490fbafea46473 to your computer and use it in GitHub Desktop.
Save michaliskambi/04e803545b386460fa490fbafea46473 to your computer and use it in GitHub Desktop.
Behavior that affects all children scenes, setting their DistanceCulling (TDistanceCullingGroup)
{ Behavior that affects all children scenes, setting their DistanceCulling (TDistanceCullingGroup).
See https://forum.castle-engine.io/t/shadow-ignors-distanceculling/670/9 .
You can make it available in CGE editor at design-time using
"4. Running editor with custom components" on
https://castle-engine.io/custom_components#_running_editor_with_custom_components .
}
unit DistanceCullingGroup;
interface
uses CastleTransform, CastleScene;
type
{ Behavior that affects all children scenes, setting their DistanceCulling. }
TDistanceCullingGroup = class(TCastleBehavior)
strict private
FDistance: Single;
procedure SetDistance(const Value: Single);
protected
procedure ParentAfterAttach; override;
public
{ Call each time when you added some child, or changed the child scale.
It is automatically called when @link(Distance) changes. }
procedure UpdateChildren;
property Distance: Single read FDistance write SetDistance;
end;
implementation
uses CastleComponentSerialize, CastleVectors;
procedure TDistanceCullingGroup.SetDistance(const Value: Single);
begin
if FDistance <> Value then
begin
FDistance := Value;
UpdateChildren;
end;
end;
procedure TDistanceCullingGroup.ParentAfterAttach;
begin
inherited;
UpdateChildren;
end;
procedure TDistanceCullingGroup.UpdateChildren;
{ Update recursively all scenes inside Transform. }
procedure UpdateTransform(const Transform: TCastleTransform;
const TransformDistanceCulling: Single);
var
C: TCastleTransform;
begin
if Transform is TCastleScene then
TCastleScene(Transform).DistanceCulling := TransformDistanceCulling;
for C in Transform do
UpdateTransform(C, TransformDistanceCulling / Approximate3DScale(C.Scale));
end;
begin
if Parent <> nil then
UpdateTransform(Parent, Distance);
end;
initialization
RegisterSerializableComponent(TDistanceCullingGroup, 'Distance Culling Group');
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment