66
77#include " util/Angle.h"
88#include " util/Color.h"
9+ #include " subsystem/RenderSystem.h"
910#include < math.h>
1011
1112using namespace Supernova ;
@@ -23,7 +24,10 @@ Light::~Light(){
2324void Light::setType (LightType type){
2425 LightComponent& lightcomp = getComponent<LightComponent>();
2526
26- lightcomp.type = type;
27+ if (lightcomp.type != type){
28+ lightcomp.type = type;
29+ scene->getSystem <RenderSystem>()->needReloadMeshes ();
30+ }
2731}
2832
2933LightType Light::getType () const {
@@ -36,8 +40,10 @@ void Light::setDirection(Vector3 direction){
3640 LightComponent& lightcomp = getComponent<LightComponent>();
3741 Transform& transform = getComponent<Transform>();
3842
39- lightcomp.direction = direction;
40- transform.needUpdate = true ; // Does not affect children
43+ if (lightcomp.direction != direction){
44+ lightcomp.direction = direction;
45+ transform.needUpdate = true ;
46+ }
4147}
4248
4349void Light::setDirection (const float x, const float y, const float z){
@@ -69,7 +75,10 @@ Vector3 Light::getColor() const{
6975void Light::setRange (float range){
7076 LightComponent& lightcomp = getComponent<LightComponent>();
7177
72- lightcomp.range = range;
78+ if (lightcomp.range != range){
79+ lightcomp.range = range;
80+ lightcomp.needUpdateShadowMap = true ;
81+ }
7382}
7483
7584float Light::getRange () const {
@@ -82,8 +91,9 @@ void Light::setIntensity(float intensity){
8291 LightComponent& lightcomp = getComponent<LightComponent>();
8392 Transform& transform = getComponent<Transform>();
8493
85- if (intensity > 0 && lightcomp.intensity == 0 )
86- transform.needUpdate = true ; // Does not affect children
94+ if (intensity > 0 && lightcomp.intensity == 0 ){
95+ lightcomp.needUpdateShadowMap = true ;
96+ }
8797
8898 lightcomp.intensity = intensity;
8999}
@@ -97,14 +107,25 @@ float Light::getIntensity() const{
97107void Light::setConeAngle (float inner, float outer){
98108 LightComponent& lightcomp = getComponent<LightComponent>();
99109
100- lightcomp.innerConeCos = cos (Angle::defaultToRad (inner / 2 ));
101- lightcomp.outerConeCos = cos (Angle::defaultToRad (outer / 2 ));
110+ float innerConeCos = cos (Angle::defaultToRad (inner / 2 ));
111+ float outerConeCos = cos (Angle::defaultToRad (outer / 2 ));
112+
113+ if (lightcomp.innerConeCos != innerConeCos || lightcomp.outerConeCos != outerConeCos){
114+ lightcomp.innerConeCos = innerConeCos;
115+ lightcomp.outerConeCos = outerConeCos;
116+ lightcomp.needUpdateShadowMap = true ;
117+ }
102118}
103119
104120void Light::setInnerConeAngle (float inner){
105121 LightComponent& lightcomp = getComponent<LightComponent>();
106122
107- lightcomp.innerConeCos = cos (Angle::defaultToRad (inner / 2 ));
123+ float innerConeCos = cos (Angle::defaultToRad (inner / 2 ));
124+
125+ if (lightcomp.innerConeCos != innerConeCos){
126+ lightcomp.innerConeCos = innerConeCos;
127+ lightcomp.needUpdateShadowMap = true ;
128+ }
108129}
109130
110131float Light::getInnerConeAngle () const {
@@ -116,7 +137,12 @@ float Light::getInnerConeAngle() const{
116137void Light::setOuterConeAngle (float outer){
117138 LightComponent& lightcomp = getComponent<LightComponent>();
118139
119- lightcomp.outerConeCos = cos (Angle::defaultToRad (outer / 2 ));
140+ float outerConeCos = cos (Angle::defaultToRad (outer / 2 ));
141+
142+ if (lightcomp.outerConeCos != outerConeCos){
143+ lightcomp.outerConeCos = outerConeCos;
144+ lightcomp.needUpdateShadowMap = true ;
145+ }
120146}
121147
122148float Light::getOuterConeAngle () const {
@@ -128,7 +154,12 @@ float Light::getOuterConeAngle() const{
128154void Light::setShadows (bool shadows){
129155 LightComponent& lightcomp = getComponent<LightComponent>();
130156
131- lightcomp.shadows = shadows;
157+ if (lightcomp.shadows != shadows){
158+ lightcomp.shadows = shadows;
159+
160+ lightcomp.needUpdateShadowMap = true ;
161+ scene->getSystem <RenderSystem>()->needReloadMeshes ();
162+ }
132163}
133164
134165bool Light::isShadows () const {
@@ -164,13 +195,21 @@ unsigned int Light::getShadowMapSize() const{
164195void Light::setShadowCameraNearFar (float near, float far){
165196 LightComponent& lightcomp = getComponent<LightComponent>();
166197
167- lightcomp.shadowCameraNearFar = Vector2 (near, far);
198+ if (lightcomp.shadowCameraNearFar != Vector2 (near, far)){
199+ lightcomp.shadowCameraNearFar = Vector2 (near, far);
200+ lightcomp.automaticShadowCamera = false ;
201+ lightcomp.needUpdateShadowMap = true ;
202+ }
168203}
169204
170205void Light::setCameraNear (float near){
171206 LightComponent& lightcomp = getComponent<LightComponent>();
172207
173- lightcomp.shadowCameraNearFar .x = near;
208+ if (lightcomp.shadowCameraNearFar .x != near){
209+ lightcomp.shadowCameraNearFar .x = near;
210+ lightcomp.automaticShadowCamera = false ;
211+ lightcomp.needUpdateShadowMap = true ;
212+ }
174213}
175214
176215float Light::getCameraNear () const {
@@ -182,7 +221,11 @@ float Light::getCameraNear() const{
182221void Light::setCameraFar (float far){
183222 LightComponent& lightcomp = getComponent<LightComponent>();
184223
185- lightcomp.shadowCameraNearFar .y = far;
224+ if (lightcomp.shadowCameraNearFar .y != far){
225+ lightcomp.shadowCameraNearFar .y = far;
226+ lightcomp.automaticShadowCamera = false ;
227+ lightcomp.needUpdateShadowMap = true ;
228+ }
186229}
187230
188231float Light::getCameraFar () const {
@@ -191,10 +234,28 @@ float Light::getCameraFar() const{
191234 return lightcomp.shadowCameraNearFar .y ;
192235}
193236
237+ void Light::setAutomaticShadowCamera (bool automatic){
238+ LightComponent& lightcomp = getComponent<LightComponent>();
239+
240+ if (lightcomp.automaticShadowCamera != automatic){
241+ lightcomp.automaticShadowCamera = automatic;
242+ lightcomp.needUpdateShadowMap = true ;
243+ }
244+ }
245+
246+ bool Light::isAutomaticShadowCamera () const {
247+ LightComponent& lightcomp = getComponent<LightComponent>();
248+
249+ return lightcomp.automaticShadowCamera ;
250+ }
251+
194252void Light::setNumCascades (unsigned int numCascades){
195253 LightComponent& lightcomp = getComponent<LightComponent>();
196254
197- lightcomp.numShadowCascades = numCascades;
255+ if (lightcomp.numShadowCascades != numCascades){
256+ lightcomp.numShadowCascades = numCascades;
257+ lightcomp.needUpdateShadowMap = true ;
258+ }
198259}
199260
200261float Light::getNumCascades () const {
0 commit comments