1 // Copyright 2001-2019 Crytek GmbH / Crytek Group. All rights reserved.
4 #include "BaseEnv/Components/BaseEnv_EntityLightComponent.h"
6 #include <CryRenderer/IRenderAuxGeom.h>
7 #include <CryRenderer/IRenderer.h>
9 #include "BaseEnv/BaseEnv_AutoRegistrar.h"
11 namespace SchematycBaseEnv
13 namespace EntityLightComponentUtils
15 struct SPreviewProperties
: public Schematyc2::IComponentPreviewProperties
22 // Schematyc2::IComponentPreviewProperties
24 virtual void Serialize(Serialization::IArchive
& archive
) override
26 archive(bShowGizmos
, "bShowGizmos", "Show Gizmos");
27 archive(gizmoLength
, "gizmoLength", "Gizmo Length");
30 // ~Schematyc2::IComponentPreviewProperties
37 const Schematyc2::SGUID
CEntityLightComponent::s_componentGUID
= "ed123a98-462f-49a0-8d1b-362d6449d81a";
39 CEntityLightComponent::SProperties::SProperties()
42 , diffuseMultiplier(1.0f
)
43 , specularMultiplier(1.0f
)
44 , hdrDynamicMultiplier(1.0f
)
47 , attenuationBulbSize(0.05f
)
50 void CEntityLightComponent::SProperties::Serialize(Serialization::IArchive
& archive
)
52 archive(bOn
, "on", "On");
53 archive
.doc("Initial on/off state");
55 archive(color
, "color", "Color");
58 archive(Serialization::Range(diffuseMultiplier
, 0.01f
, 10.0f
), "diffuseMultiplier", "Diffuse Multiplier");
59 archive
.doc("Diffuse Multiplier");
61 archive(Serialization::Range(specularMultiplier
, 0.01f
, 10.0f
), "specularMultiplier", "Specular Multiplier");
62 archive
.doc("Specular Multiplier");
64 archive(Serialization::Range(hdrDynamicMultiplier
, 0.01f
, 10.0f
), "hdrDynamicMultiplier", "HDR Dynamic Multiplier");
65 archive
.doc("HDR Dynamic Multiplier");
67 archive(Serialization::Range(radius
, 0.01f
, 50.0f
), "radius", "Radius");
68 archive
.doc("Radius");
70 archive(Serialization::Range(frustumAngle
, 0.01f
, 90.0f
), "frustumAngle", "Frustum Angle");
71 archive
.doc("Frustum Angle");
73 archive(Serialization::Range(attenuationBulbSize
, 0.01f
, 5.0f
), "attenuationBulbSize", "Attenuation Bulb Size");
74 archive
.doc("Attenuation Bulb Size");
76 archive(TransformDecorator
<ETransformComponent::PositionAndRotation
>(transform
), "transform", "Transform");
77 archive
.doc("Transform relative to parent/entity");
80 Schematyc2::SEnvFunctionResult
CEntityLightComponent::SProperties::SetTransform(const Schematyc2::SEnvFunctionContext
& context
, const STransform
& _transform
)
82 transform
= _transform
;
83 return Schematyc2::SEnvFunctionResult();
86 CEntityLightComponent::CEntityLightComponent()
87 : m_pProperties(nullptr)
91 bool CEntityLightComponent::Init(const Schematyc2::SComponentParams
& params
)
93 if(!CEntityTransformComponentBase::Init(params
))
97 m_pProperties
= params
.properties
.ToPtr
<SProperties
>();
101 void CEntityLightComponent::Run(Schematyc2::ESimulationMode simulationMode
)
103 if(m_pProperties
->bOn
)
109 Schematyc2::IComponentPreviewPropertiesPtr
CEntityLightComponent::GetPreviewProperties() const
111 return std::make_shared
<EntityLightComponentUtils::SPreviewProperties
>();
114 void CEntityLightComponent::Preview(const SRendParams
& params
, const SRenderingPassInfo
& passInfo
, const Schematyc2::IComponentPreviewProperties
& previewProperties
) const
116 const EntityLightComponentUtils::SPreviewProperties
& previewPropertiesImpl
= static_cast<const EntityLightComponentUtils::SPreviewProperties
&>(previewProperties
);
117 if(previewPropertiesImpl
.bShowGizmos
)
119 if(CEntityTransformComponentBase::SlotIsValid())
121 IRenderAuxGeom
& renderAuxGeom
= *gEnv
->pRenderer
->GetIRenderAuxGeom();
122 const Matrix34 worldTM
= CEntityTransformComponentBase::GetWorldTM();
123 const float lineThickness
= 4.0f
;
125 renderAuxGeom
.DrawLine(worldTM
.GetTranslation(), ColorB(255, 0, 0, 255), worldTM
.GetTranslation() + (worldTM
.GetColumn0().GetNormalized() * previewPropertiesImpl
.gizmoLength
), ColorB(255, 0, 0, 255), lineThickness
);
126 renderAuxGeom
.DrawLine(worldTM
.GetTranslation(), ColorB(0, 255, 0, 255), worldTM
.GetTranslation() + (worldTM
.GetColumn1().GetNormalized() * previewPropertiesImpl
.gizmoLength
), ColorB(0, 255, 0, 255), lineThickness
);
127 renderAuxGeom
.DrawLine(worldTM
.GetTranslation(), ColorB(0, 0, 255, 255), worldTM
.GetTranslation() + (worldTM
.GetColumn2().GetNormalized() * previewPropertiesImpl
.gizmoLength
), ColorB(0, 0, 255, 255), lineThickness
);
132 void CEntityLightComponent::Shutdown()
134 CEntityTransformComponentBase::Shutdown();
137 Schematyc2::INetworkSpawnParamsPtr
CEntityLightComponent::GetNetworkSpawnParams() const
139 return Schematyc2::INetworkSpawnParamsPtr();
142 void CEntityLightComponent::Register()
144 Schematyc2::IEnvRegistry
& envRegistry
= gEnv
->pSchematyc2
->GetEnvRegistry();
147 Schematyc2::IComponentFactoryPtr pComponentFactory
= SCHEMATYC2_MAKE_COMPONENT_FACTORY_SHARED(CEntityLightComponent
, SProperties
, s_componentGUID
);
148 pComponentFactory
->SetName("Light");
149 pComponentFactory
->SetNamespace("Base");
150 pComponentFactory
->SetAuthor("Crytek");
151 pComponentFactory
->SetDescription("Entity light component");
152 pComponentFactory
->SetFlags(Schematyc2::EComponentFlags::CreatePropertyGraph
);
153 pComponentFactory
->SetAttachmentType(Schematyc2::EComponentSocket::Parent
, g_tranformAttachmentGUID
);
154 pComponentFactory
->SetAttachmentType(Schematyc2::EComponentSocket::Child
, g_tranformAttachmentGUID
);
155 envRegistry
.RegisterComponentFactory(pComponentFactory
);
161 Schematyc2::CEnvFunctionDescriptorPtr pFunctionDescriptor
= Schematyc2::MakeEnvFunctionDescriptorShared(&CEntityLightComponent::SProperties::SetTransform
);
162 pFunctionDescriptor
->SetGUID("729721c4-a09e-4903-a8a6-fa69388acfc6");
163 pFunctionDescriptor
->SetName("SetTransform");
164 pFunctionDescriptor
->BindInput(0, 0, "Transform", "Transform");
165 envRegistry
.RegisterFunction(pFunctionDescriptor
);
169 bool CEntityLightComponent::Switch(bool bOn
)
171 const bool bPrevOn
= m_bOn
;
186 void CEntityLightComponent::SwitchOn()
188 ColorF color
= m_pProperties
->color
;
189 color
.r
*= m_pProperties
->diffuseMultiplier
;
190 color
.g
*= m_pProperties
->diffuseMultiplier
;
191 color
.b
*= m_pProperties
->diffuseMultiplier
;
194 light
.m_Flags
= 0; //TODO [andriy 25.05.16]: double check that. was ' = DLF_ALLOW_LPV' (see CL 1380778)
195 light
.SetLightColor(color
);
196 light
.SetSpecularMult(m_pProperties
->specularMultiplier
);
197 light
.SetRadius(m_pProperties
->radius
, m_pProperties
->attenuationBulbSize
);
198 light
.m_fHDRDynamic
= m_pProperties
->hdrDynamicMultiplier
;
199 light
.m_fLightFrustumAngle
= m_pProperties
->frustumAngle
;
201 const int slot
= CEntityComponentBase::GetEntity().LoadLight(g_emptySlot
, &light
);
202 CEntityTransformComponentBase::SetSlot(slot
, m_pProperties
->transform
);
207 void CEntityLightComponent::SwitchOff()
209 CEntityTransformComponentBase::FreeSlot();
215 SCHEMATYC2_GAME_ENV_AUTO_REGISTER(&SchematycBaseEnv::CEntityLightComponent::Register
)