Debian Codegraph @

src/scene/castlerendererinternalshader_bumpmapping.inc

raw
1 {%MainUnit castlerendererinternalshader.pas}
2 {
3 Copyright 2010-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 { Generating shader code for bump mapping. }
18
19 {$ifdef read_interface}
20
21 //type
22 { Generating shader code for bump mapping. }
23 TBumpMappingShader = record
24 strict private
25 { Set by GenerateCode, so can be used by SetUniformsOnce,
26 but *not* by SetDynamicUniforms. }
27 HasBumpMappingUniform_NormalMapTextureUnit: Boolean;
28 BumpMappingUniform_NormalMapTextureUnit: Integer;
29 HasBumpMappingUniform_NormalMapScale: Boolean;
30 BumpMappingUniform_NormalMapScale: Single;
31 HasBumpMappingUniform_HeightMapScale: Boolean;
32 BumpMappingUniform_HeightMapScale: Single;
33 public
34 BumpMapping: TBumpMapping;
35
36 NormalMapTextureCoordinatesId: Cardinal;
37 NormalMapTextureUnit: Cardinal;
38 NormalMapScale: Single;
39
40 HeightMapInAlpha: Boolean;
41 HeightMapScale: Single;
42
43 procedure Clear;
44 procedure PrepareHash(var Hash: TShaderCodeHash);
45 procedure GenerateCode(const Shader: TShader);
46 procedure SetUniformsOnce(const AProgram: TX3DShaderProgram);
47 end;
48
49 {$endif read_interface}
50
51 {$ifdef read_implementation}
52
53 procedure TBumpMappingShader.Clear;
54 begin
55 BumpMapping := Low(TBumpMapping);
56 NormalMapTextureUnit := 0;
57 NormalMapTextureCoordinatesId := 0;
58 NormalMapScale := 1;
59 HeightMapInAlpha := false;
60 HeightMapScale := 0;
61 end;
62
63 procedure TBumpMappingShader.PrepareHash(var Hash: TShaderCodeHash);
64 begin
65 if BumpMapping <> bmNone then
66 begin
67 Hash.AddInteger(
68 47 * Ord(BumpMapping) +
69 373 * NormalMapTextureUnit +
70 379 * NormalMapTextureCoordinatesId +
71 383 * Ord(HeightMapInAlpha)
72 );
73 Hash.AddFloat(HeightMapScale, 2203);
74 Hash.AddFloat(NormalMapScale, 2281);
75 end;
76 end;
77
78 procedure TBumpMappingShader.GenerateCode(const Shader: TShader);
79 var
80 VertexShader, FragmentShader: String;
81 begin
82 HasBumpMappingUniform_NormalMapTextureUnit := false;
83 HasBumpMappingUniform_NormalMapScale := false;
84 HasBumpMappingUniform_HeightMapScale := false;
85
86 if BumpMapping = bmNone then Exit;
87
88 // add basic bump mapping code
89 VertexShader := {$I bump_mapping.vs.inc};
90 FragmentShader := {$I bump_mapping.fs.inc};
91 StringReplaceAllVar(FragmentShader, '<NormalMapTextureCoordinatesId>',
92 IntToStr(NormalMapTextureCoordinatesId));
93 Shader.Plug(stVertex , VertexShader);
94 Shader.Plug(stFragment, FragmentShader);
95
96 HasBumpMappingUniform_NormalMapTextureUnit := true;
97 BumpMappingUniform_NormalMapTextureUnit := NormalMapTextureUnit;
98
99 HasBumpMappingUniform_NormalMapScale := true;
100 BumpMappingUniform_NormalMapScale := NormalMapScale;
101
102 if HeightMapInAlpha and (BumpMapping >= bmParallax) then
103 begin
104 // add parallax bump mapping code, with a variant for "steep parallax"
105 VertexShader := {$I bump_mapping_parallax.vs.inc};
106 FragmentShader := {$I bump_mapping_parallax.fs.inc};
107 if BumpMapping >= bmSteepParallax then
108 FragmentShader := '#define CASTLE_BUMP_MAPPING_PARALLAX_STEEP' + NL
109 + FragmentShader;
110 Shader.Plug(stVertex , VertexShader);
111 Shader.Plug(stFragment, FragmentShader);
112
113 HasBumpMappingUniform_HeightMapScale := true;
114 BumpMappingUniform_HeightMapScale := HeightMapScale;
115
116 if (BumpMapping >= bmSteepParallaxShadowing) and (Shader.LightShaders.Count > 0) then
117 begin
118 // add steep parallax with self-shadowing bump mapping code
119 VertexShader := {$I bump_mapping_steep_parallax_shadowing.vs.inc};
120 FragmentShader := {$I bump_mapping_steep_parallax_shadowing.fs.inc};
121 if Shader.LightShaders[0].Node is TAbstractPositionalLightNode then
122 VertexShader := '#define CASTLE_LIGHT0_POSITIONAL' + NL
123 + VertexShader;
124 Shader.Plug(stVertex , VertexShader);
125 Shader.Plug(stFragment, FragmentShader);
126 end;
127 end;
128 end;
129
130 procedure TBumpMappingShader.SetUniformsOnce(const AProgram: TX3DShaderProgram);
131 begin
132 if HasBumpMappingUniform_NormalMapTextureUnit then
133 AProgram.SetUniform('castle_normal_map',
134 BumpMappingUniform_NormalMapTextureUnit);
135
136 if HasBumpMappingUniform_NormalMapScale then
137 AProgram.SetUniform('castle_normalScale',
138 BumpMappingUniform_NormalMapScale);
139
140 if HasBumpMappingUniform_HeightMapScale then
141 AProgram.SetUniform('castle_parallax_bm_scale',
142 BumpMappingUniform_HeightMapScale);
143 end;
144
145 {$endif read_implementation}