| 1 |
{ |
| 2 |
Copyright 2008-2022 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_implementation_nested} |
| 17 |
|
| 18 |
var |
| 19 |
Lights: TX3DChildrenList; |
| 20 |
|
| 21 |
{ Read <library_lights> (Collada 1.4.x). Fills Lights list. } |
| 22 |
procedure ReadLibraryLights(LibraryE: TDOMElement); |
| 23 |
|
| 24 |
function ReadAttenuation(E: TDOMElement): TVector3; |
| 25 |
begin |
| 26 |
Result := Vector3(1, 0, 0); |
| 27 |
ReadChildFloat(E, 'constant_attenuation', Result.X); |
| 28 |
ReadChildFloat(E, 'linear_attenuation', Result.Y); |
| 29 |
ReadChildFloat(E, 'quadratic_attenuation', Result.Z); |
| 30 |
end; |
| 31 |
|
| 32 |
var |
| 33 |
I: TXMLElementIterator; |
| 34 |
Light: TAbstractLightNode; |
| 35 |
Point: TPointLightNode; |
| 36 |
Directional: TDirectionalLightNode; |
| 37 |
Spot: TSpotLightNode; |
| 38 |
Id, Profile: string; |
| 39 |
TechniqueE, LightE, ExtraE: TDOMElement; |
| 40 |
FalloffAngle, Radius: Float; |
| 41 |
LightColor: TVector3; |
| 42 |
begin |
| 43 |
I := LibraryE.ChildrenIterator('light'); |
| 44 |
try |
| 45 |
while I.GetNext do |
| 46 |
if I.Current.AttributeString('id', Id) then |
| 47 |
begin |
| 48 |
Light := nil; |
| 49 |
LightE := nil; |
| 50 |
|
| 51 |
TechniqueE := I.Current.ChildElement('technique_common', false); |
| 52 |
if TechniqueE <> nil then |
| 53 |
begin |
| 54 |
LightE := TechniqueE.ChildElement('point', false); |
| 55 |
if LightE <> nil then |
| 56 |
begin |
| 57 |
Point := TPointLightNode.Create(Id, BaseUrl); |
| 58 |
Point.FdAttenuation.Value := ReadAttenuation(LightE); |
| 59 |
Light := Point; |
| 60 |
end else |
| 61 |
begin |
| 62 |
LightE := TechniqueE.ChildElement('directional', false); |
| 63 |
if LightE <> nil then |
| 64 |
begin |
| 65 |
Directional := TDirectionalLightNode.Create(Id, BaseUrl); |
| 66 |
{ default X3D light direction is -Z, matches Collada } |
| 67 |
Light := Directional; |
| 68 |
end else |
| 69 |
begin |
| 70 |
LightE := TechniqueE.ChildElement('spot', false); |
| 71 |
if LightE <> nil then |
| 72 |
begin |
| 73 |
Spot := TSpotLightNode.Create(Id, BaseUrl); |
| 74 |
Spot.FdAttenuation.Value := ReadAttenuation(LightE); |
| 75 |
{ default X3D spot direction is -Z, matches Collada } |
| 76 |
if not ReadChildFloat(LightE, 'falloff_angle', FalloffAngle) then |
| 77 |
FalloffAngle := 180; { Collada default } |
| 78 |
Spot.FdCutOffAngle.Value := DegToRad(FalloffAngle); |
| 79 |
{ falloff_exponent cannot be nicely translated to X3D beamWidth, |
| 80 |
see notes about SpotLight.beamWidth at VRML/X3D renderer. } |
| 81 |
Light := Spot; |
| 82 |
end else |
| 83 |
begin |
| 84 |
LightE := TechniqueE.ChildElement('ambient', false); |
| 85 |
if LightE <> nil then |
| 86 |
begin |
| 87 |
Point := TPointLightNode.Create(Id, BaseUrl); |
| 88 |
{ ambient light can be translated to normal PointLight with |
| 89 |
intensity = 0 (this scales diffuse and specular to zero). } |
| 90 |
Point.FdIntensity.Value := 0; |
| 91 |
Point.FdAmbientIntensity.Value := 1; |
| 92 |
Light := Point; |
| 93 |
end else |
| 94 |
WritelnWarning('Collada', 'No supported light inside <technique_common>'); |
| 95 |
end; |
| 96 |
end; |
| 97 |
end; |
| 98 |
end else |
| 99 |
WritelnWarning('Collada', 'No supported technique inside <light>'); |
| 100 |
|
| 101 |
if Light <> nil then |
| 102 |
begin |
| 103 |
Light.FdGlobal.Value := true; |
| 104 |
if ReadChildVector(LightE, 'color', LightColor) then |
| 105 |
Light.FdColor.Value := LightColor; |
| 106 |
if Light is TAbstractPositionalLightNode then |
| 107 |
begin |
| 108 |
{ calculate light radius } |
| 109 |
Radius := MaxSingle; |
| 110 |
ExtraE := I.Current.ChildElement('extra', false); |
| 111 |
if ExtraE <> nil then |
| 112 |
begin |
| 113 |
TechniqueE := ExtraE.ChildElement('technique', false); |
| 114 |
if (TechniqueE <> nil) and |
| 115 |
TechniqueE.AttributeString('profile', Profile) and |
| 116 |
(Profile = 'blender') then |
| 117 |
ReadChildFloat(TechniqueE, 'dist', Radius); |
| 118 |
end; |
| 119 |
TAbstractPositionalLightNode(Light).FdRadius.Value := Radius; |
| 120 |
end; |
| 121 |
Lights.Add(Light); |
| 122 |
end; |
| 123 |
end; |
| 124 |
finally FreeAndNil(I) end; |
| 125 |
end; |
| 126 |
|
| 127 |
{$endif read_implementation_nested} |