| 1 |
{ |
| 2 |
Copyright 2016-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Screen effects done by blending screen with given color (TCastleFlashEffect). } |
| 17 |
unit CastleFlashEffect; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses Classes, |
| 24 |
CastleUIControls, CastleColors, CastleRectangles, CastleGLImages, |
| 25 |
CastleImages, CastleComponentSerialize; |
| 26 |
|
| 27 |
type |
| 28 |
{ Fade out, flash, and similar screen effects |
| 29 |
done by blending screen with given color. } |
| 30 |
TCastleFlashEffect = class(TCastleUserInterface) |
| 31 |
strict private |
| 32 |
FIntensity: Single; |
| 33 |
FColor: TCastleColor; |
| 34 |
FDark: boolean; |
| 35 |
FDuration: Single; |
| 36 |
FImage: TCastleImagePersistent; |
| 37 |
FDrawableImageAsGrayscale: TDrawableImage; |
| 38 |
procedure ImageChanged(Sender: TObject); |
| 39 |
public |
| 40 |
const |
| 41 |
DefaultDuration = 0.5; |
| 42 |
constructor Create(AOwner: TComponent); override; |
| 43 |
destructor Destroy; override; |
| 44 |
procedure Update(const SecondsPassed: Single; var HandleInput: boolean); override; |
| 45 |
procedure Render; override; |
| 46 |
|
| 47 |
procedure Flash(const AColor: TCastleColor; const ADark: boolean); |
| 48 |
procedure Reset; |
| 49 |
|
| 50 |
{ Is the effect in-progress. |
| 51 |
This is temporarily @true after calling @link(Flash). |
| 52 |
It automatically ends (switches to @false) after the @link(Duration) |
| 53 |
has passed, and when you call @link(Reset) explicitly. } |
| 54 |
function Active: boolean; |
| 55 |
published |
| 56 |
property Duration: Single read FDuration write FDuration |
| 57 |
{$ifdef FPC}default DefaultDuration{$endif}; |
| 58 |
|
| 59 |
{ Set this image to modulate the color with an image. |
| 60 |
The image is always stretched to cover our whole size. } |
| 61 |
property Image: TCastleImagePersistent read FImage; |
| 62 |
|
| 63 |
property FullSize default true; |
| 64 |
end; |
| 65 |
|
| 66 |
implementation |
| 67 |
|
| 68 |
uses SysUtils, |
| 69 |
CastleUtils, CastleGLUtils, CastleLog, CastleRenderOptions; |
| 70 |
|
| 71 |
constructor TCastleFlashEffect.Create(AOwner: TComponent); |
| 72 |
begin |
| 73 |
inherited; |
| 74 |
FDuration := DefaultDuration; |
| 75 |
FullSize := true; |
| 76 |
FImage := TCastleImagePersistent.Create; |
| 77 |
FImage.OnChange := {$ifdef FPC}@{$endif}ImageChanged; |
| 78 |
end; |
| 79 |
|
| 80 |
destructor TCastleFlashEffect.Destroy; |
| 81 |
begin |
| 82 |
FreeAndNil(FImage); |
| 83 |
FreeAndNil(FDrawableImageAsGrayscale); |
| 84 |
inherited; |
| 85 |
end; |
| 86 |
|
| 87 |
procedure TCastleFlashEffect.ImageChanged(Sender: TObject); |
| 88 |
|
| 89 |
{ For FadeColor, the image should be |
| 90 |
- white opaque where the effect IS applied |
| 91 |
- white transparent where the effect IS NOT applied. |
| 92 |
For FadeDarkColor, the image should be |
| 93 |
- black opaque where the effect IS applied |
| 94 |
(This is only an approximation. |
| 95 |
Only really correct if Color is exactly black. |
| 96 |
There's no correct value here otherwise, that would work equally |
| 97 |
to what DrawRectangle(FinalColor, ...) does when Image = nil. |
| 98 |
We would need to render image with different shader, e.g. doing |
| 99 |
lerp inside shader. |
| 100 |
This is the drawback of "Dark" flash mode.) |
| 101 |
- white opaque where the effect IS NOT applied. } |
| 102 |
function CreateGrayscaleFromAlpha(const Img: TCastleImage): TGrayscaleImage; |
| 103 |
var |
| 104 |
X, Y, Z: Integer; |
| 105 |
begin |
| 106 |
Result := TGrayscaleImage.Create(Img.Width, Img.Height, Img.Depth); |
| 107 |
for X := 0 to Img.Width - 1 do |
| 108 |
for Y := 0 to Img.Height - 1 do |
| 109 |
for Z := 0 to Img.Depth - 1 do |
| 110 |
Result.PixelPtr(X, Y, Z)^ := High(Byte) - |
| 111 |
Clamped(Round(Img.Colors[X, Y, Z][3] * 255), Low(Byte), High(Byte)); |
| 112 |
end; |
| 113 |
|
| 114 |
var |
| 115 |
ImageAsGrayscale: TCastleImage; |
| 116 |
begin |
| 117 |
VisibleChange([chRender]); |
| 118 |
|
| 119 |
FreeAndNil(FDrawableImageAsGrayscale); |
| 120 |
|
| 121 |
{ TODO: It would be better to get rid of generating FDrawableImageAsGrayscale, |
| 122 |
and also of limitation that FImage.Image must be TCastleImage |
| 123 |
(cannot TEncodedImage). } |
| 124 |
|
| 125 |
if FImage.Image <> nil then |
| 126 |
begin |
| 127 |
if FImage.Image is TCastleImage then |
| 128 |
begin |
| 129 |
ImageAsGrayscale := CreateGrayscaleFromAlpha(FImage.Image as TCastleImage); |
| 130 |
FDrawableImageAsGrayscale := TDrawableImage.Create(ImageAsGrayscale, true, true); |
| 131 |
end else |
| 132 |
WritelnWarning('TODO: TCastleFlashEffect.Image must not be GPU compressed for now'); |
| 133 |
end; |
| 134 |
end; |
| 135 |
|
| 136 |
procedure TCastleFlashEffect.Flash(const AColor: TCastleColor; const ADark: boolean); |
| 137 |
begin |
| 138 |
FColor := AColor; |
| 139 |
FIntensity := 1; |
| 140 |
FDark := ADark; |
| 141 |
end; |
| 142 |
|
| 143 |
procedure TCastleFlashEffect.Update(const SecondsPassed: Single; |
| 144 |
var HandleInput: boolean); |
| 145 |
begin |
| 146 |
inherited; |
| 147 |
if FIntensity > 0 then |
| 148 |
FIntensity := FIntensity - ((1 / Duration) * SecondsPassed); |
| 149 |
end; |
| 150 |
|
| 151 |
procedure TCastleFlashEffect.Render; |
| 152 |
var |
| 153 |
FinalImage: TDrawableImage; |
| 154 |
FinalColor: TCastleColor; |
| 155 |
SourceFactor: TBlendingSourceFactor; |
| 156 |
DestinationFactor: TBlendingDestinationFactor; |
| 157 |
begin |
| 158 |
inherited; |
| 159 |
if Active then |
| 160 |
begin |
| 161 |
if FDark then |
| 162 |
begin |
| 163 |
if FImage.Empty then |
| 164 |
FinalImage := nil |
| 165 |
else |
| 166 |
FinalImage := FDrawableImageAsGrayscale; |
| 167 |
|
| 168 |
FinalColor := FadeDarkColor(FColor, FIntensity); |
| 169 |
{ Constants below make resulting screen color = FinalColor * previous screen color. |
| 170 |
Note that as long as all components of Color are <= 1, |
| 171 |
then all components of our FinalColor are also always <= 1, |
| 172 |
and this means that we will always make the screen darker (or equal, |
| 173 |
but never brighter). } |
| 174 |
SourceFactor := bsZero; |
| 175 |
DestinationFactor := bdSrcColor; |
| 176 |
end else |
| 177 |
begin |
| 178 |
if FImage.Empty then |
| 179 |
FinalImage := nil |
| 180 |
else |
| 181 |
FinalImage := FImage.DrawableImage; |
| 182 |
|
| 183 |
FinalColor := FadeColor(FColor, FIntensity); |
| 184 |
SourceFactor := bsSrcAlpha; |
| 185 |
DestinationFactor := bdOneMinusSrcAlpha; |
| 186 |
end; |
| 187 |
|
| 188 |
if FinalImage <> nil then |
| 189 |
begin |
| 190 |
FinalImage.ScaleCorners := UIScale; |
| 191 |
FinalImage.Color := FinalColor; |
| 192 |
FinalImage.Alpha := acBlending; |
| 193 |
FinalImage.BlendingSourceFactor := SourceFactor; |
| 194 |
FinalImage.BlendingDestinationFactor := DestinationFactor; |
| 195 |
FinalImage.Draw(RenderRect); |
| 196 |
end else |
| 197 |
DrawRectangle(RenderRect, FinalColor, SourceFactor, DestinationFactor, true); |
| 198 |
end; |
| 199 |
end; |
| 200 |
|
| 201 |
procedure TCastleFlashEffect.Reset; |
| 202 |
begin |
| 203 |
FIntensity := 0; |
| 204 |
end; |
| 205 |
|
| 206 |
function TCastleFlashEffect.Active: boolean; |
| 207 |
begin |
| 208 |
Result := FIntensity > 0; |
| 209 |
end; |
| 210 |
|
| 211 |
initialization |
| 212 |
RegisterSerializableComponent(TCastleFlashEffect, 'Flash Effect'); |
| 213 |
end. |