| 1 |
{ |
| 2 |
Copyright 2023-2024 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 |
{ Main view, where most of the application logic takes place. } |
| 17 |
unit GameViewMain; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleVectors, CastleComponentSerialize, CastleScene, |
| 23 |
CastleUIControls, CastleControls, CastleKeysMouse, X3DNodes, |
| 24 |
CastleRenderOptions; |
| 25 |
|
| 26 |
type |
| 27 |
{ Main view, where most of the application logic takes place. } |
| 28 |
TViewMain = class(TCastleView) |
| 29 |
published |
| 30 |
{ Components designed using CGE editor. |
| 31 |
These fields will be automatically initialized at Start. } |
| 32 |
LabelFps: TCastleLabel; |
| 33 |
SceneLevel1: TCastleScene; |
| 34 |
strict private |
| 35 |
Projector: TSpotLightNode; |
| 36 |
public |
| 37 |
constructor Create(AOwner: TComponent); override; |
| 38 |
procedure Start; override; |
| 39 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 40 |
end; |
| 41 |
|
| 42 |
var |
| 43 |
ViewMain: TViewMain; |
| 44 |
|
| 45 |
implementation |
| 46 |
|
| 47 |
uses SysUtils; |
| 48 |
|
| 49 |
{ TViewMain ----------------------------------------------------------------- } |
| 50 |
|
| 51 |
constructor TViewMain.Create(AOwner: TComponent); |
| 52 |
begin |
| 53 |
inherited; |
| 54 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 55 |
end; |
| 56 |
|
| 57 |
procedure TViewMain.Start; |
| 58 |
|
| 59 |
{ Limit the texture display to the place where its coordinates are in 0..1 range. |
| 60 |
|
| 61 |
Explanation why this is needed: |
| 62 |
The texture rendered by projecting in AddProjection is by default |
| 63 |
not limited to its 0..1 texture coordinates. |
| 64 |
And ProjTexture.RepeatS/T cannot change this (ProjTexture.RepeatS/T |
| 65 |
only changes *what* is visible outside 0..1 range, not *if* it is visible). |
| 66 |
|
| 67 |
So we setup a small shader that will reject applying texture when |
| 68 |
the texture coordinate is outside 0..1 range. } |
| 69 |
function CreateEffectToLimitTexture: TEffectNode; |
| 70 |
var |
| 71 |
EffectPart: TEffectPartNode; |
| 72 |
begin |
| 73 |
Result := TEffectNode.Create; |
| 74 |
Result.Language := slGLSL; |
| 75 |
|
| 76 |
{ Add TEffectPartNode which actually contains shader code. |
| 77 |
You can load shader code from file, |
| 78 |
you could also set it explicitly from string using EffectPart.Contents := '...'; } |
| 79 |
EffectPart := TEffectPartNode.Create; |
| 80 |
EffectPart.ShaderType := stFragment; |
| 81 |
EffectPart.SetUrl(['castle-data:/shaders/limit_texture.fs']); |
| 82 |
Result.SetParts([EffectPart]); |
| 83 |
end; |
| 84 |
|
| 85 |
{ Add texture projected by Projector to given mesh in given scene. } |
| 86 |
procedure AddProjection(const Projector: TSpotLightNode; |
| 87 |
const ProjectedTextureUrl: String; |
| 88 |
const Scene: TCastleScene; const BlenderMeshName: String); |
| 89 |
var |
| 90 |
Shape: TShapeNode; |
| 91 |
MeshGroup: TGroupNode; |
| 92 |
MeshChild: TX3DNode; |
| 93 |
IndexedTriangleSet: TIndexedTriangleSetNode; |
| 94 |
ProjTexCooord: TProjectedTextureCoordinateNode; |
| 95 |
ProjTexture: TImageTextureNode; |
| 96 |
Material: TPhysicalMaterialNode; |
| 97 |
begin |
| 98 |
MeshGroup := Scene.Node(TGroupNode, BlenderMeshName) as TGroupNode; |
| 99 |
for MeshChild in MeshGroup.FdChildren do |
| 100 |
if MeshChild is TShapeNode then |
| 101 |
begin |
| 102 |
Shape := TShapeNode(MeshChild); |
| 103 |
|
| 104 |
ProjTexCooord := TProjectedTextureCoordinateNode.Create; |
| 105 |
ProjTexCooord.FdProjector.Value := Projector; |
| 106 |
|
| 107 |
IndexedTriangleSet := Shape.Geometry as TIndexedTriangleSetNode; |
| 108 |
IndexedTriangleSet.TexCoord := ProjTexCooord; |
| 109 |
|
| 110 |
ProjTexture := TImageTextureNode.Create; |
| 111 |
ProjTexture.SetUrl([ProjectedTextureUrl]); |
| 112 |
ProjTexture.RepeatS := false; |
| 113 |
ProjTexture.RepeatT := false; |
| 114 |
ProjTexture.SetEffects([CreateEffectToLimitTexture]); |
| 115 |
|
| 116 |
{ In this demo we just assume that Appearance and Material nodes |
| 117 |
are already present in the model (so they have been created in Blender) |
| 118 |
and that material is physical. } |
| 119 |
Material := Shape.Appearance.Material as TPhysicalMaterialNode; |
| 120 |
Material.BaseTexture := ProjTexture; |
| 121 |
end; |
| 122 |
end; |
| 123 |
|
| 124 |
begin |
| 125 |
inherited; |
| 126 |
|
| 127 |
Projector := TSpotLightNode.Create; |
| 128 |
|
| 129 |
{ Set initial projector location/direction. |
| 130 |
|
| 131 |
Note: If you transform SceneLevel1, remember that Projector location/direction |
| 132 |
is relative to SceneLevel1. Conceptually, projector defined like this |
| 133 |
"moves along" with SceneLevel1. |
| 134 |
If you want to counter this, you have to set Projector location/direction |
| 135 |
based on some world-space value, converted to SceneLevel1 coordinates, |
| 136 |
like |
| 137 |
|
| 138 |
Projector.Location := SceneLevel1.WorldToLocal(Vector3(10, 10, 10)); |
| 139 |
Projector.Direction := SceneLevel1.WorldToLocalDirection(Vector3(-10, -10, -10)); |
| 140 |
|
| 141 |
In this demo, we assume SceneLevel1 is just not transformed, |
| 142 |
for simplicity. |
| 143 |
} |
| 144 |
Projector.Location := Vector3(10, 10, 10); |
| 145 |
Projector.Direction := Vector3(-10, -10, -10); |
| 146 |
|
| 147 |
// Allows to see the need for AddEffectToLimitTexture |
| 148 |
// Projector.Location := Vector3(0.5, 5, 0.5); |
| 149 |
// Projector.Direction := Vector3(0, -1, 0); |
| 150 |
|
| 151 |
{ See ../data/projective_texturing_simple.x3dv for explanation of |
| 152 |
various TSpotLightNode fields. } |
| 153 |
Projector.CutOffAngle := 0.15; |
| 154 |
Projector.IsOn := false; |
| 155 |
|
| 156 |
AddProjection(Projector, 'castle-data:/test_texture.png', |
| 157 |
SceneLevel1, 'mesh_receiving_projection'); |
| 158 |
end; |
| 159 |
|
| 160 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 161 |
begin |
| 162 |
inherited; |
| 163 |
{ This virtual method is executed every frame (many times per second). } |
| 164 |
Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code'); |
| 165 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 166 |
end; |
| 167 |
|
| 168 |
end. |