| 1 |
{ |
| 2 |
Copyright 2020-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 |
{ Main view, where most of the application logic takes place. } |
| 16 |
unit GameViewMain; |
| 17 |
|
| 18 |
interface |
| 19 |
|
| 20 |
uses Classes, |
| 21 |
CastleVectors, CastleComponentSerialize, |
| 22 |
CastleUIControls, CastleControls, CastleKeysMouse, CastleScene, CastleRenderOptions, |
| 23 |
X3DNodes; |
| 24 |
|
| 25 |
type |
| 26 |
{ Main view, where most of the application logic takes place. } |
| 27 |
TViewMain = class(TCastleView) |
| 28 |
published |
| 29 |
{ Components designed using CGE editor. |
| 30 |
These fields will be automatically initialized at Start. } |
| 31 |
LabelFps: TCastleLabel; |
| 32 |
Monkey2: TCastleScene; |
| 33 |
private |
| 34 |
CustomShaderActive: Boolean; |
| 35 |
Appearance: TAppearanceNode; |
| 36 |
ComposedShader: TComposedShaderNode; |
| 37 |
public |
| 38 |
constructor Create(AOwner: TComponent); override; |
| 39 |
procedure Start; override; |
| 40 |
procedure Stop; override; |
| 41 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 42 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 43 |
end; |
| 44 |
|
| 45 |
var |
| 46 |
ViewMain: TViewMain; |
| 47 |
|
| 48 |
implementation |
| 49 |
|
| 50 |
uses SysUtils, |
| 51 |
CastleUtils; |
| 52 |
|
| 53 |
{ TViewMain ----------------------------------------------------------------- } |
| 54 |
|
| 55 |
constructor TViewMain.Create(AOwner: TComponent); |
| 56 |
begin |
| 57 |
inherited; |
| 58 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 59 |
end; |
| 60 |
|
| 61 |
procedure TViewMain.Start; |
| 62 |
var |
| 63 |
VertexShader, FragmentShader: TShaderPartNode; |
| 64 |
begin |
| 65 |
inherited; |
| 66 |
|
| 67 |
{ Define nodes with GLSL shader code. } |
| 68 |
|
| 69 |
VertexShader := TShaderPartNode.Create; |
| 70 |
VertexShader.ShaderType := stVertex; |
| 71 |
VertexShader.Contents := |
| 72 |
// Needed to calculate vertex position |
| 73 |
'uniform mat4 castle_ModelViewMatrix;' + NL + |
| 74 |
'uniform mat4 castle_ProjectionMatrix;' + NL + |
| 75 |
'attribute vec4 castle_Vertex;' + NL + |
| 76 |
|
| 77 |
// Needed to calculate intensity, to have some interesting shading |
| 78 |
'uniform mat3 castle_NormalMatrix;' + NL + |
| 79 |
'attribute vec3 castle_Normal;' + NL + |
| 80 |
|
| 81 |
// Pass intensity to fragment shader, to make simple toon shading |
| 82 |
'varying float intensity;' + NL + |
| 83 |
|
| 84 |
'void main(void)' + NL + |
| 85 |
'{' + NL + |
| 86 |
// Calculate intensity as simplest diffuse from a headlight |
| 87 |
' intensity = dot(' + NL + |
| 88 |
' normalize(vec3(0.0, 0.0, 1.0)),' + NL + |
| 89 |
' normalize(castle_NormalMatrix * castle_Normal));' + NL + |
| 90 |
// Calculate final vertex position, determines where shape is rendered |
| 91 |
' gl_Position = castle_ProjectionMatrix * (castle_ModelViewMatrix * castle_Vertex);' + NL + |
| 92 |
'}'; |
| 93 |
|
| 94 |
FragmentShader := TShaderPartNode.Create; |
| 95 |
FragmentShader.ShaderType := stFragment; |
| 96 |
FragmentShader.Contents := |
| 97 |
'varying float intensity;' + NL + |
| 98 |
'void main(void)' + NL + |
| 99 |
'{' + NL + |
| 100 |
' float intensity_toon = floor(intensity * 5.0) / 5.0;' + NL + |
| 101 |
' gl_FragColor = vec4(1.0 * intensity_toon, 0.0, 0.0, 1.0);' + NL + |
| 102 |
'}'; |
| 103 |
|
| 104 |
ComposedShader := TComposedShaderNode.Create; |
| 105 |
ComposedShader.SetParts([VertexShader, FragmentShader]); |
| 106 |
|
| 107 |
{ By calling KeepExistingBegin we say we want to manually manage |
| 108 |
the lifetime of ComposedShader. Otherwise it would be freed |
| 109 |
as soon as it is no longer used, which means that pressing S twice |
| 110 |
(see TViewMain.Press) would crash, as ComposedShader would be freed. |
| 111 |
In TViewMain.Stop we will have to take care to free the node. } |
| 112 |
ComposedShader.KeepExistingBegin; |
| 113 |
|
| 114 |
{ We know that Blender exported material wich such name to glTF, |
| 115 |
and we know that our glTF importer turned it into TAppearanceNode. } |
| 116 |
Appearance := Monkey2.Node('MonkeyMaterial') as TAppearanceNode; |
| 117 |
|
| 118 |
CustomShaderActive := true; |
| 119 |
Appearance.SetShaders([ComposedShader]); |
| 120 |
end; |
| 121 |
|
| 122 |
procedure TViewMain.Stop; |
| 123 |
begin |
| 124 |
ComposedShader.KeepExistingEnd; |
| 125 |
FreeIfUnusedAndNil(ComposedShader); |
| 126 |
inherited; |
| 127 |
end; |
| 128 |
|
| 129 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 130 |
begin |
| 131 |
inherited; |
| 132 |
{ This virtual method is executed every frame (many times per second). } |
| 133 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 134 |
end; |
| 135 |
|
| 136 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 137 |
begin |
| 138 |
Result := inherited; |
| 139 |
if Result then Exit; // allow the ancestor to handle keys |
| 140 |
|
| 141 |
if Event.IsKey(keyS) then |
| 142 |
begin |
| 143 |
CustomShaderActive := not CustomShaderActive; |
| 144 |
if CustomShaderActive then |
| 145 |
Appearance.SetShaders([ComposedShader]) |
| 146 |
else |
| 147 |
// clearing Shaders list will restore default rendering |
| 148 |
Appearance.SetShaders([]); |
| 149 |
|
| 150 |
Exit(true); // key was handled |
| 151 |
end; |
| 152 |
end; |
| 153 |
|
| 154 |
end. |