Debian Codegraph @

src/scene/castleinternalrenderer_surfacetextures.inc

raw
1 {%MainUnit castleinternalrenderer.pas}
2 {
3 Copyright 2002-2023 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 { Textures that affect surface parameters beyond standard MainTexture
18 (diffuse for Phong, emissive for Unlit, base for PhysicalMaterial).
19 Like normal maps, and various other
20 textures defined by the CommonSurfaceShader and materials in X3D v4. }
21
22 {$ifdef read_interface}
23
24 private
25 { Return OpenGL 2D texture initialized for given texture node.
26 If Node is nil, or it's corresponding OpenGL resource is not initialized
27 for whatever reason --- returns 0. }
28 function GLTexture2D(Node: TAbstractTextureNode;
29 out TextureHasAlpha: boolean): TGLTextureId; overload;
30 function GLTexture2D(Node: TAbstractTextureNode): TGLTextureId; overload;
31
32 { If this Shape uses a normal map, use it for bump mapping. }
33 procedure BumpMappingEnable(const Shape: TX3DRendererShape;
34 const RenderOptions: TCastleRenderOptions;
35 var ABoundTextureUnits, ATexCoordsNeeded: Cardinal; Shader: TShader);
36
37 { If this Shape uses other surface texture, use it. }
38 procedure SurfaceTexturesEnable(const Shape: TX3DRendererShape;
39 var ABoundTextureUnits, ATexCoordsNeeded: Cardinal; Shader: TShader);
40
41 { Does this shape possibly use some texture that requires Phong shading.
42 Decide before actually making any OpenGL operations. }
43 class function ShapeTextureForcesPhongShading(const Shape: TX3DRendererShape): boolean;
44
45 {$endif read_interface}
46
47 {$ifdef read_implementation}
48
49 function TRenderer.GLTexture2D(Node: TAbstractTextureNode;
50 out TextureHasAlpha: boolean): TGLTextureId;
51 var
52 TextureRes: TTextureResource;
53 begin
54 Result := 0;
55 if Node <> nil then
56 begin
57 TextureRes := TTextureResources.Get(Node);
58 if TextureRes is T2DTextureResource then // also checks is TextureRes <> nil
59 begin
60 Result := T2DTextureResource(TextureRes).GLName;
61 TextureHasAlpha := Node.AlphaChannelFinal <> acNone;
62 end;
63 end;
64 end;
65
66 function TRenderer.GLTexture2D(Node: TAbstractTextureNode): TGLTextureId;
67 var
68 IgnoreTextureHasAlpha: boolean;
69 begin
70 Result := GLTexture2D(Node, IgnoreTextureHasAlpha);
71 end;
72
73 class function TRenderer.ShapeTextureForcesPhongShading(const Shape: TX3DRendererShape): boolean;
74 var
75 SurfaceShader: TCommonSurfaceShaderNode;
76 MatOne: TAbstractOneSidedMaterialNode;
77 MatPhong: TMaterialNode;
78 MatPhysical: TPhysicalMaterialNode;
79 begin
80 Result := false;
81 if Shape.Node <> nil then
82 begin
83 {$warnings off} // using deprecated to keep backward compatibility
84 SurfaceShader := Shape.Node.CommonSurfaceShader;
85 {$warnings on}
86 if SurfaceShader <> nil then
87 begin
88 Result :=
89 (SurfaceShader.AmbientTexture <> nil) or
90 (SurfaceShader.EmissiveTexture <> nil) or
91 (SurfaceShader.NormalTexture <> nil) or
92 (SurfaceShader.ShininessTexture <> nil) or
93 (SurfaceShader.SpecularTexture <> nil);
94 end else
95 if Shape.Node.Appearance <> nil then
96 begin
97 if Shape.Node.Appearance.NormalMap <> nil then
98 Exit(true);
99
100 if Shape.Node.Appearance.Material is TAbstractOneSidedMaterialNode then
101 begin
102 MatOne := TAbstractOneSidedMaterialNode(Shape.Node.Appearance.Material);
103 if (MatOne.EmissiveTexture <> nil) or
104 (MatOne.NormalTexture <> nil) then
105 Exit(true);
106 end;
107
108 if Shape.Node.Appearance.Material is TMaterialNode then
109 begin
110 MatPhong := TMaterialNode(Shape.Node.Appearance.Material);
111 if (MatPhong.AmbientTexture <> nil) or
112 // (MatPhong.DiffuseTexture <> nil) or // this is OK for Gouraud shading
113 (MatPhong.ShininessTexture <> nil) or
114 (MatPhong.SpecularTexture <> nil) or
115 (MatPhong.OcclusionTexture <> nil) then
116 Exit(true);
117 end;
118
119 if Shape.Node.Appearance.Material is TPhysicalMaterialNode then
120 begin
121 MatPhysical := TPhysicalMaterialNode(Shape.Node.Appearance.Material);
122 if // (MatPhysical.BaseTexture <> nil) or // this is OK for Gouraud shading? Actually PhysicalMaterial always requires Phong shading anyway.
123 (MatPhysical.MetallicRoughnessTexture <> nil) or
124 (MatPhysical.OcclusionTexture <> nil) then
125 Exit(true);
126 end;
127 end;
128 end;
129 end;
130
131 procedure TRenderer.BumpMappingEnable(const Shape: TX3DRendererShape;
132 const RenderOptions: TCastleRenderOptions;
133 var ABoundTextureUnits, ATexCoordsNeeded: Cardinal; Shader: TShader);
134
135 procedure Enable(const NormalTexture: TAbstractTextureNode;
136 NormalTextureCoordinatesId: Integer;
137 const NormalMapScale, ParallaxHeight: Single);
138 var
139 GLNormalTexture: TGLTextureId;
140 { Does NormalTexture have alpha channel. }
141 HasAlpha: boolean;
142 begin
143 if Shape.BumpMappingAllowed and
144 (BumpMapping(RenderOptions) <> bmNone) and
145 (ABoundTextureUnits < GLFeatures.MaxTextureUnits) then
146 begin
147 GLNormalTexture := GLTexture2D(NormalTexture, HasAlpha);
148 if GLNormalTexture <> 0 then
149 begin
150 // fix NormalTextureCoordinatesId if needed
151 NormalTextureCoordinatesId := Max(0, NormalTextureCoordinatesId);
152
153 Shape.BumpMappingUsed := true;
154 Shape.BumpMappingTextureCoordinatesId := NormalTextureCoordinatesId;
155
156 ActiveTexture(ABoundTextureUnits);
157 glBindTexture(GL_TEXTURE_2D, GLNormalTexture);
158 Shader.EnableBumpMapping(BumpMapping(RenderOptions), ABoundTextureUnits,
159 NormalTextureCoordinatesId, NormalMapScale, HasAlpha, ParallaxHeight);
160 Inc(ABoundTextureUnits);
161
162 { There is no need to increase the ATexCoordsNeeded unconditionally.
163 If the NormalTextureCoordinatesId = 0 and
164 ATexCoordsNeeded = 1 then we're already satisfied, as then the normalmap
165 will use the existing texture coordinates from e.g. diffuse texture. }
166 MaxVar(ATexCoordsNeeded, NormalTextureCoordinatesId + 1);
167 end;
168 end;
169 end;
170
171 var
172 SurfaceShader: TCommonSurfaceShaderNode;
173 Mat: TAbstractMaterialNode;
174 MatOneSided: TAbstractOneSidedMaterialNode;
175 Appearance: TAppearanceNode;
176 TexCoordIndex: Integer;
177 begin
178 if Shape.State.ShapeNode <> nil then
179 begin
180 {$warnings off} // using deprecated to keep backward compatibility
181 SurfaceShader := Shape.State.ShapeNode.CommonSurfaceShader;
182 {$warnings on}
183 if SurfaceShader <> nil then
184 begin
185 Enable(
186 SurfaceShader.NormalTexture,
187 SurfaceShader.NormalTextureCoordinatesId,
188 1,
189 SurfaceShader.NormalTextureParallaxHeight);
190 end else
191 if Shape.State.ShapeNode.Appearance <> nil then
192 begin
193 Appearance := Shape.State.ShapeNode.Appearance;
194 Mat := Appearance.Material;
195 if (Mat is TAbstractOneSidedMaterialNode) and
196 (TAbstractOneSidedMaterialNode(Mat).NormalTexture <> nil) then
197 begin
198 MatOneSided := TAbstractOneSidedMaterialNode(Mat);
199 Shape.Geometry.FindTextureMapping(MatOneSided.NormalTextureMapping, TexCoordIndex, true);
200 Enable(MatOneSided.NormalTexture, TexCoordIndex, MatOneSided.NormalScale, RenderOptions.BumpMappingParallaxHeight);
201 end else
202 begin
203 Enable(Appearance.NormalMap, 0, 1, Appearance.HeightMapScale);
204 end;
205 end;
206 end;
207 end;
208
209 procedure TRenderer.SurfaceTexturesEnable(const Shape: TX3DRendererShape;
210 var ABoundTextureUnits, ATexCoordsNeeded: Cardinal; Shader: TShader);
211
212 procedure RequireTexCoord(const TextureCoordinatesId: Integer);
213 begin
214 { Note that there is no need to increase the ATexCoordsNeeded unconditionally.
215 If the NormalTextureCoordinatesId = 0 and
216 ATexCoordsNeeded = 1 then we're already satisfied, as then the normalmap
217 will use the existing texture coordinates from e.g. diffuse texture. }
218
219 MaxVar(ATexCoordsNeeded, TextureCoordinatesId + 1);
220 end;
221
222 function MappingToIndex(const Mapping: String): Integer;
223 begin
224 Shape.Geometry.FindTextureMapping(Mapping, Result, true);
225 end;
226
227 procedure SurfaceTextureEnable(const SurfaceTexture: TSurfaceTexture;
228 const Texture: TAbstractTextureNode;
229 const TextureCoordinatesId: Integer;
230 const UniformTextureName, PlugCode: String);
231 var
232 GLTextureId: TGLTextureId;
233 begin
234 if (Texture <> nil) and
235 (ABoundTextureUnits < GLFeatures.MaxTextureUnits) then
236 begin
237 GLTextureId := GLTexture2D(Texture);
238 if GLTextureId <> 0 then
239 begin
240 ActiveTexture(ABoundTextureUnits);
241 glBindTexture(GL_TEXTURE_2D, GLTextureId);
242 Shader.EnableSurfaceTexture(SurfaceTexture, ABoundTextureUnits,
243 Max(0, TextureCoordinatesId),
244 UniformTextureName,
245 PlugCode);
246 Inc(ABoundTextureUnits);
247
248 RequireTexCoord(TextureCoordinatesId);
249 end;
250 end;
251 end;
252
253 procedure EmissiveTextureEnable(
254 const Texture: TAbstractTextureNode;
255 const TextureCoordinatesId: Integer;
256 const ChannelMask: String = 'rgb');
257 begin
258 SurfaceTextureEnable(stEmissive,
259 Texture,
260 TextureCoordinatesId,
261 'castle_SurfaceTexture_emissive',
262 'void PLUG_material_emissive(inout vec3 parameter)' + NL +
263 '{' + NL +
264 ' parameter *= castle_texture_color_to_linear(texture2D({uniform_texture_name}, {coord_name}.st).' + ChannelMask + ');' + NL +
265 '}');
266 end;
267
268 procedure AmbientTextureEnable(
269 const Texture: TAbstractTextureNode;
270 const TextureCoordinatesId: Integer;
271 const ChannelMask: String = 'rgb');
272 begin
273 SurfaceTextureEnable(stAmbient,
274 Texture,
275 TextureCoordinatesId,
276 'castle_SurfaceTexture_ambient',
277 'void PLUG_material_ambient(inout vec3 parameter)' + NL +
278 '{' + NL +
279 ' parameter *= castle_texture_color_to_linear(texture2D({uniform_texture_name}, {coord_name}.st).' + ChannelMask + ');' + NL +
280 '}');
281 end;
282
283 procedure SpecularTextureEnable(
284 const Texture: TAbstractTextureNode;
285 const TextureCoordinatesId: Integer;
286 const ChannelMask: String = 'rgb');
287 begin
288 SurfaceTextureEnable(stSpecular,
289 Texture,
290 TextureCoordinatesId,
291 'castle_SurfaceTexture_specular',
292 'void PLUG_material_specular(inout vec3 parameter)' + NL +
293 '{' + NL +
294 ' parameter *= castle_texture_color_to_linear(texture2D({uniform_texture_name}, {coord_name}.st).' + ChannelMask + ');' + NL +
295 '}');
296 end;
297
298 procedure ShininessTextureEnable(
299 const Texture: TAbstractTextureNode;
300 const TextureCoordinatesId: Integer;
301 const ChannelMask: String = 'a');
302 begin
303 SurfaceTextureEnable(stShininess,
304 Texture,
305 TextureCoordinatesId,
306 'castle_SurfaceTexture_shininess',
307 'void PLUG_material_shininess(inout float parameter)' + NL +
308 '{' + NL +
309 ' parameter *= texture2D({uniform_texture_name}, {coord_name}.st).' + ChannelMask + ';' + NL +
310 '}');
311 end;
312
313 procedure MetallicRoughnessTextureEnable(
314 const Texture: TAbstractTextureNode;
315 const TextureCoordinatesId: Integer);
316 begin
317 SurfaceTextureEnable(stMetallicRoughness,
318 Texture,
319 TextureCoordinatesId,
320 'castle_SurfaceTexture_metallic_roughness',
321 'void PLUG_material_metallic_roughness(inout float metallic, inout float roughness)' + NL +
322 '{' + NL +
323 { Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.
324 Read it to place metallic is x, roughness in y. }
325 ' vec2 mr = texture2D({uniform_texture_name}, {coord_name}.st).bg;' + NL +
326 ' metallic *= mr.x;' + NL +
327 ' roughness *= mr.y;' + NL +
328 '}');
329 end;
330
331 procedure OcclusionTextureEnable(
332 const Texture: TAbstractTextureNode;
333 const TextureCoordinatesId: Integer;
334 const Strength: Single);
335 var
336 USingle: TDynamicUniformSingle;
337 begin
338 SurfaceTextureEnable(stOcclusion,
339 Texture,
340 TextureCoordinatesId,
341 'castle_SurfaceTexture_occlusion',
342 'uniform float castle_MaterialOcclusionStrength;' + NL +
343 'void PLUG_material_occlusion(inout vec4 color)' + NL +
344 '{' + NL +
345 ' float occlusion = texture2D({uniform_texture_name}, {coord_name}.st).r;' + NL +
346 ' color.rgb = mix(color.rgb, color.rgb * occlusion, castle_MaterialOcclusionStrength);' + NL +
347 '}');
348
349 { This procedure is called even with Texture = nil.
350 In this case SurfaceTextureEnable, so we also don't need to add this uniform. }
351 if Texture <> nil then
352 begin
353 USingle := TDynamicUniformSingle.Create;
354 USingle.Name := 'castle_MaterialOcclusionStrength';
355 USingle.Value := Strength;
356 Shader.DynamicUniforms.Add(USingle);
357 end;
358 end;
359
360 var
361 SurfaceShader: TCommonSurfaceShaderNode;
362 Material: TAbstractMaterialNode;
363 MatPhong: TMaterialNode;
364 MatPhysical: TPhysicalMaterialNode;
365 MatUnlit: TUnlitMaterialNode;
366 begin
367 if Shape.State.ShapeNode <> nil then
368 begin
369 {$warnings off} // using deprecated to keep backward compatibility
370 SurfaceShader := Shape.State.ShapeNode.CommonSurfaceShader;
371 {$warnings on}
372 if SurfaceShader <> nil then
373 begin
374 EmissiveTextureEnable(
375 SurfaceShader.EmissiveTexture,
376 SurfaceShader.EmissiveTextureCoordinatesId,
377 SurfaceShader.EmissiveTextureChannelMask);
378 AmbientTextureEnable(
379 SurfaceShader.AmbientTexture,
380 SurfaceShader.AmbientTextureCoordinatesId,
381 SurfaceShader.AmbientTextureChannelMask);
382 SpecularTextureEnable(
383 SurfaceShader.SpecularTexture,
384 SurfaceShader.SpecularTextureCoordinatesId,
385 SurfaceShader.SpecularTextureChannelMask);
386 ShininessTextureEnable(
387 SurfaceShader.ShininessTexture,
388 SurfaceShader.ShininessTextureCoordinatesId,
389 SurfaceShader.ShininessTextureChannelMask);
390 // These textures will be rendered using the MainTexture mechanism, but they still need tex coords
391 if SurfaceShader.DiffuseTexture <> nil then
392 RequireTexCoord(SurfaceShader.DiffuseTextureCoordinatesId);
393 if SurfaceShader.MultiDiffuseAlphaTexture <> nil then
394 RequireTexCoord(SurfaceShader.DiffuseTextureCoordinatesId); // TODO test X3DOM/InstantReality are they using DiffuseTextureCoordinatesId in this case
395 end else
396 begin
397 if Shape.State.ShapeNode.Appearance <> nil then
398 Material := Shape.State.ShapeNode.Appearance.Material
399 else
400 Material := nil;
401 if Material is TMaterialNode then // also checks Material <> nil
402 begin
403 MatPhong := TMaterialNode(Material);
404 EmissiveTextureEnable(
405 MatPhong.EmissiveTexture,
406 MappingToIndex(MatPhong.EmissiveTextureMapping));
407 AmbientTextureEnable(
408 MatPhong.AmbientTexture,
409 MappingToIndex(MatPhong.AmbientTextureMapping));
410 SpecularTextureEnable(
411 MatPhong.SpecularTexture,
412 MappingToIndex(MatPhong.SpecularTextureMapping));
413 ShininessTextureEnable(
414 MatPhong.ShininessTexture,
415 MappingToIndex(MatPhong.ShininessTextureMapping));
416 OcclusionTextureEnable(
417 MatPhong.OcclusionTexture,
418 MappingToIndex(MatPhong.OcclusionTextureMapping),
419 MatPhong.OcclusionStrength);
420 // These textures will be rendered using the MainTexture mechanism, but they still need tex coords
421 if MatPhong.DiffuseTexture <> nil then
422 RequireTexCoord(MappingToIndex(MatPhong.DiffuseTextureMapping));
423 end else
424 if Material is TPhysicalMaterialNode then // also checks Material <> nil
425 begin
426 MatPhysical := TPhysicalMaterialNode(Material);
427 EmissiveTextureEnable(
428 MatPhysical.EmissiveTexture,
429 MappingToIndex(MatPhysical.EmissiveTextureMapping));
430 MetallicRoughnessTextureEnable(
431 MatPhysical.MetallicRoughnessTexture,
432 MappingToIndex(MatPhysical.MetallicRoughnessTextureMapping));
433 OcclusionTextureEnable(
434 MatPhysical.OcclusionTexture,
435 MappingToIndex(MatPhysical.OcclusionTextureMapping),
436 MatPhysical.OcclusionStrength);
437 // These textures will be rendered using the MainTexture mechanism, but they still need tex coords
438 if MatPhysical.BaseTexture <> nil then
439 RequireTexCoord(MappingToIndex(MatPhysical.BaseTextureMapping));
440 end else
441 if Material is TUnlitMaterialNode then // also checks Material <> nil
442 begin
443 MatUnlit := TUnlitMaterialNode(Material);
444 // These textures will be rendered using the MainTexture mechanism, but they still need tex coords
445 if MatUnlit.EmissiveTexture <> nil then
446 RequireTexCoord(MappingToIndex(MatUnlit.EmissiveTextureMapping));
447 end;
448 end;
449 end;
450 end;
451
452 {$endif read_implementation}