Debian Codegraph @

src/scene/load/collada/x3dloadinternalcollada_materials.inc

raw
1 {
2 Copyright 2008-2018 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 {$ifdef read_interface_nested}
17
18 type
19 { Collada materials. Data contains TColladaEffect, which is a reference
20 to Effects list (it is not owned by this TColladaMaterialsMap). }
21 TColladaMaterialsMap = {$ifdef FPC}specialize{$endif} TDictionary<string, TColladaEffect>;
22
23 var
24 { List of Collada materials. Collada material is just a reference
25 to Collada effect with a name.
26 This way we handle instance_effect in <material> node.
27 Many materials may refer to a single effect. }
28 Materials: TColladaMaterialsMap;
29
30 procedure ReadLibraryMaterials(LibraryElement: TDOMElement); forward;
31
32 {$endif read_interface_nested}
33
34 {$ifdef read_implementation_nested}
35
36 { Read <material>. It is added to the Materials list. }
37 procedure ReadMaterial(MatElement: TDOMElement);
38
39 function ReadParamAsVector3(Element: TDOMElement): TVector3;
40 var
41 AType: string;
42 begin
43 if not Element.AttributeString('type', AType) then
44 begin
45 WritelnWarning('Collada', '<param> has no type attribute');
46 Result := TVector3.Zero;
47 end else
48 if AType <> 'float3' then
49 begin
50 WritelnWarning('Collada', 'Expected <param> with type "float3"');
51 Result := TVector3.Zero;
52 end else
53 Result := Vector3FromStr(Element.TextData);
54 end;
55
56 function ReadParamAsFloat(Element: TDOMElement): Float;
57 var
58 AType: string;
59 begin
60 if not Element.AttributeString('type', AType) then
61 begin
62 WritelnWarning('Collada', '<param> has no type attribute');
63 Result := 0;
64 end else
65 if AType <> 'float' then
66 begin
67 WritelnWarning('Collada', 'Expected <param> with type "float"');
68 Result := 0;
69 end else
70 Result := StrToFloatDot(Element.TextData);
71 end;
72
73 var
74 MatId: string;
75
76 { For Collada < 1.4.x }
77 procedure TryCollada13;
78 var
79 ShaderElement, TechniqueElement, PassElement, ProgramElement: TDOMElement;
80 ParamName: string;
81 I: TXMLElementIterator;
82 Effect: TColladaEffect;
83 Appearance: TAppearanceNode;
84 Mat: TMaterialNode;
85 begin
86 Effect := TColladaEffect.Create;
87 Effects.Add(Effect);
88
89 Appearance := TAppearanceNode.Create(MatId, BaseUrl);
90 Effect.Appearance := Appearance;
91
92 Mat := TMaterialNode.Create('', BaseUrl);
93 Appearance.FdMaterial.Value := Mat;
94
95 { Collada 1.3 doesn't really have a concept of effects used by materials.
96 But to be consistent, we add one effect (to Effects list)
97 and one reference to it (to Materials list). }
98 Materials.AddOrSetValue(MatId, Effect);
99
100 // TODO: generate CommonSurfaceShader as an alternative?
101
102 ShaderElement := MatElement.ChildElement('shader', false);
103 if ShaderElement <> nil then
104 begin
105 TechniqueElement := ShaderElement.ChildElement('technique', false);
106 if TechniqueElement <> nil then
107 begin
108 PassElement := TechniqueElement.ChildElement('pass', false);
109 if PassElement <> nil then
110 begin
111 ProgramElement := PassElement.ChildElement('program', false);
112 if ProgramElement <> nil then
113 begin
114 I := ProgramElement.ChildrenIterator('param');
115 try
116 while I.GetNext do
117 if I.Current.AttributeString('name', ParamName) then
118 begin
119 if ParamName = 'EMISSION' then
120 Mat.FdEmissiveColor.Value := ReadParamAsVector3(I.Current) else
121 if ParamName = 'AMBIENT' then
122 Mat.FdAmbientIntensity.Value := ReadParamAsVector3(I.Current).Average else
123 if ParamName = 'DIFFUSE' then
124 Mat.FdDiffuseColor.Value := ReadParamAsVector3(I.Current) else
125 if ParamName = 'SPECULAR' then
126 Mat.FdSpecularColor.Value := ReadParamAsVector3(I.Current) else
127 if ParamName = 'SHININESS' then
128 Mat.FdShininess.Value := ReadParamAsFloat(I.Current) / 128.0 else
129 if ParamName = 'REFLECTIVE' then
130 {Mat.FdMirrorColor.Value := } ReadParamAsVector3(I.Current) else
131 if ParamName = 'REFLECTIVITY' then
132 begin
133 if AllowCastleExtensions then
134 Mat.FdMirror.Value := ReadParamAsFloat(I.Current)
135 else
136 ReadParamAsFloat(I.Current);
137 end else
138 (*
139 Blender Collada 1.3.1 exporter bug: it sets
140 type of TRANSPARENT param as "float".
141 Although content inicates "float3",
142 like Collada 1.3.1 spec requires (page 129),
143 and consistently with what is in Collada 1.4.1 spec.
144
145 I don't handle this anyway, so I just ignore it for now.
146 Should be reported to Blender.
147
148 if ParamName = 'TRANSPARENT' then
149 {Mat.FdTransparencyColor.Value := } ReadParamAsVector3(I.Current) else
150 *)
151 if ParamName = 'TRANSPARENCY' then
152 Mat.FdTransparency.Value := ReadParamAsFloat(I.Current);
153 { other ParamName not handled }
154 end;
155 finally FreeAndNil(I) end;
156 end;
157 end;
158 end;
159 end;
160 end;
161
162 { For Collada >= 1.4.x }
163 procedure TryCollada14;
164 var
165 InstanceEffect: TDOMElement;
166 EffectId: string;
167 Effect: TColladaEffect;
168 begin
169 if MatId = '' then Exit;
170
171 InstanceEffect := MatElement.ChildElement('instance_effect', false);
172 if InstanceEffect <> nil then
173 begin
174 if InstanceEffect.AttributeString('url', EffectId) and
175 SCharIs(EffectId, 1, '#') then
176 begin
177 Delete(EffectId, 1, 1); { delete initial '#' char }
178 Effect := Effects.Find(EffectId);
179 if Effect <> nil then
180 Materials.AddOrSetValue(MatId, Effect)
181 else
182 WritelnWarning('Collada', Format('Material "%s" references ' +
183 'non-existing effect "%s"', [MatId, EffectId]));
184 end;
185 end;
186 end;
187
188 begin
189 if not MatElement.AttributeString('id', MatId) then
190 MatId := '';
191
192 if Version14 then
193 TryCollada14 else
194 TryCollada13;
195 end;
196
197 { Read <library_materials> (Collada >= 1.4.x) or
198 <library type="MATERIAL"> (Collada < 1.4.x). }
199 procedure ReadLibraryMaterials(LibraryElement: TDOMElement);
200 var
201 I: TXMLElementIterator;
202 begin
203 I := LibraryElement.ChildrenIterator('material');
204 try
205 while I.GetNext do
206 ReadMaterial(I.Current);
207 { other I.Current.TagName not supported for now }
208 finally FreeAndNil(I) end;
209 end;
210
211 {$endif read_implementation_nested}