From d17e04967a7dbbfbd12ce33a08e279f8626b1907 Mon Sep 17 00:00:00 2001
From: SamGondelman <samuel_gondelman@alumni.brown.edu>
Date: Fri, 18 Jan 2019 13:49:16 -0800
Subject: [PATCH] fix zone bugs

---
 .../src/RenderableZoneEntityItem.cpp          | 61 +++++++++++--------
 libraries/entities/src/ZoneEntityItem.cpp     |  3 +-
 libraries/entities/src/ZoneEntityItem.h       |  3 -
 libraries/graphics/src/graphics/Haze.cpp      |  9 ---
 libraries/graphics/src/graphics/Haze.h        | 30 ++++-----
 libraries/render-utils/src/Haze.slh           |  2 +-
 6 files changed, 51 insertions(+), 57 deletions(-)

diff --git a/libraries/entities-renderer/src/RenderableZoneEntityItem.cpp b/libraries/entities-renderer/src/RenderableZoneEntityItem.cpp
index 57ff8ed8c2..631148c27a 100644
--- a/libraries/entities-renderer/src/RenderableZoneEntityItem.cpp
+++ b/libraries/entities-renderer/src/RenderableZoneEntityItem.cpp
@@ -198,24 +198,33 @@ void ZoneEntityRenderer::removeFromScene(const ScenePointer& scene, Transaction&
 void ZoneEntityRenderer::doRenderUpdateSynchronousTyped(const ScenePointer& scene, Transaction& transaction, const TypedEntityPointer& entity) {
     DependencyManager::get<EntityTreeRenderer>()->updateZone(entity->getID());
 
+    auto position = entity->getWorldPosition();
+    auto rotation = entity->getWorldOrientation();
+    auto dimensions = entity->getScaledDimensions();
+    bool rotationChanged = rotation != _lastRotation;
+    bool transformChanged = rotationChanged || position != _lastPosition || dimensions != _lastDimensions;
+
+    auto proceduralUserData = entity->getUserData();
+    bool proceduralUserDataChanged = _proceduralUserData != proceduralUserData;
+
     // FIXME one of the bools here could become true between being fetched and being reset, 
     // resulting in a lost update
-    bool keyLightChanged = entity->keyLightPropertiesChanged();
-    bool ambientLightChanged = entity->ambientLightPropertiesChanged();
-    bool skyboxChanged = entity->skyboxPropertiesChanged();
+    bool keyLightChanged = entity->keyLightPropertiesChanged() || rotationChanged;
+    bool ambientLightChanged = entity->ambientLightPropertiesChanged() || transformChanged;
+    bool skyboxChanged = entity->skyboxPropertiesChanged() || proceduralUserDataChanged;
     bool hazeChanged = entity->hazePropertiesChanged();
     bool bloomChanged = entity->bloomPropertiesChanged();
-
     entity->resetRenderingPropertiesChanged();
-    _lastPosition = entity->getWorldPosition();
-    _lastRotation = entity->getWorldOrientation();
-    _lastDimensions = entity->getScaledDimensions();
 
-    _keyLightProperties = entity->getKeyLightProperties();
-    _ambientLightProperties = entity->getAmbientLightProperties();
-    _skyboxProperties = entity->getSkyboxProperties();
-    _hazeProperties = entity->getHazeProperties();
-    _bloomProperties = entity->getBloomProperties();
+    if (transformChanged) {
+        _lastPosition = entity->getWorldPosition();
+        _lastRotation = entity->getWorldOrientation();
+        _lastDimensions = entity->getScaledDimensions();
+    }
+
+    if (proceduralUserDataChanged) {
+        _proceduralUserData = entity->getUserData();
+    }
 
 #if 0
     if (_lastShapeURL != _typedEntity->getCompoundShapeURL()) {
@@ -239,21 +248,29 @@ void ZoneEntityRenderer::doRenderUpdateSynchronousTyped(const ScenePointer& scen
     updateKeyZoneItemFromEntity(entity);
 
     if (keyLightChanged) {
+        _keyLightProperties = entity->getKeyLightProperties();
         updateKeySunFromEntity(entity);
     }
 
     if (ambientLightChanged) {
+        _ambientLightProperties = entity->getAmbientLightProperties();
         updateAmbientLightFromEntity(entity);
     }
 
-    if (skyboxChanged || _proceduralUserData != entity->getUserData()) {
+    if (skyboxChanged) {
+        _skyboxProperties = entity->getSkyboxProperties();
         updateKeyBackgroundFromEntity(entity);
     }
 
     if (hazeChanged) {
+        _hazeProperties = entity->getHazeProperties();
         updateHazeFromEntity(entity);
     }
 
+    if (bloomChanged) {
+        _bloomProperties = entity->getBloomProperties();
+        updateBloomFromEntity(entity);
+    }
 
     bool visuallyReady = true;
     uint32_t skyboxMode = entity->getSkyboxMode();
@@ -264,10 +281,6 @@ void ZoneEntityRenderer::doRenderUpdateSynchronousTyped(const ScenePointer& scen
     }
 
     entity->setVisuallyReady(visuallyReady);
-
-    if (bloomChanged) {
-        updateBloomFromEntity(entity);
-    }
 }
 
 void ZoneEntityRenderer::doRenderUpdateAsynchronousTyped(const TypedEntityPointer& entity) {
@@ -344,7 +357,7 @@ void ZoneEntityRenderer::updateKeySunFromEntity(const TypedEntityPointer& entity
     // Set the keylight
     sunLight->setColor(ColorUtils::toVec3(_keyLightProperties.getColor()));
     sunLight->setIntensity(_keyLightProperties.getIntensity());
-    sunLight->setDirection(entity->getTransform().getRotation() * _keyLightProperties.getDirection());
+    sunLight->setDirection(_lastRotation * _keyLightProperties.getDirection());
     sunLight->setCastShadows(_keyLightProperties.getCastShadows());
 }
 
@@ -356,7 +369,6 @@ void ZoneEntityRenderer::updateAmbientLightFromEntity(const TypedEntityPointer&
     ambientLight->setPosition(_lastPosition);
     ambientLight->setOrientation(_lastRotation);
 
-
     // Set the ambient light
     ambientLight->setAmbientIntensity(_ambientLightProperties.getAmbientIntensity());
 
@@ -395,8 +407,6 @@ void ZoneEntityRenderer::updateHazeFromEntity(const TypedEntityPointer& entity)
     haze->setHazeAttenuateKeyLight(_hazeProperties.getHazeAttenuateKeyLight());
     haze->setHazeKeyLightRangeFactor(graphics::Haze::convertHazeRangeToHazeRangeFactor(_hazeProperties.getHazeKeyLightRange()));
     haze->setHazeKeyLightAltitudeFactor(graphics::Haze::convertHazeAltitudeToHazeAltitudeFactor(_hazeProperties.getHazeKeyLightAltitude()));
-
-    haze->setTransform(entity->getTransform().getMatrix());
 }
 
 void ZoneEntityRenderer::updateBloomFromEntity(const TypedEntityPointer& entity) {
@@ -414,13 +424,13 @@ void ZoneEntityRenderer::updateKeyBackgroundFromEntity(const TypedEntityPointer&
 
     editBackground();
     setSkyboxColor(toGlm(_skyboxProperties.getColor()));
-    setProceduralUserData(entity->getUserData());
+    setProceduralUserData(_proceduralUserData);
     setSkyboxURL(_skyboxProperties.getURL());
 }
 
 void ZoneEntityRenderer::updateKeyZoneItemFromEntity(const TypedEntityPointer& entity) {
     // Update rotation values
-    editSkybox()->setOrientation(entity->getTransform().getRotation());
+    editSkybox()->setOrientation(_lastRotation);
 
     /* TODO: Implement the sun model behavior / Keep this code here for reference, this is how we
     {
@@ -540,9 +550,6 @@ void ZoneEntityRenderer::setSkyboxColor(const glm::vec3& color) {
 }
 
 void ZoneEntityRenderer::setProceduralUserData(const QString& userData) {
-    if (_proceduralUserData != userData) {
-        _proceduralUserData = userData;
-        std::dynamic_pointer_cast<ProceduralSkybox>(editSkybox())->parse(_proceduralUserData);
-    }
+    std::dynamic_pointer_cast<ProceduralSkybox>(editSkybox())->parse(userData);
 }
 
diff --git a/libraries/entities/src/ZoneEntityItem.cpp b/libraries/entities/src/ZoneEntityItem.cpp
index 7f7f6170d4..7b0491dbc0 100644
--- a/libraries/entities/src/ZoneEntityItem.cpp
+++ b/libraries/entities/src/ZoneEntityItem.cpp
@@ -119,7 +119,7 @@ bool ZoneEntityItem::setSubClassProperties(const EntityItemProperties& propertie
     SET_ENTITY_PROPERTY_FROM_PROPERTIES(bloomMode, setBloomMode);
 
     somethingChanged = somethingChanged || _keyLightPropertiesChanged || _ambientLightPropertiesChanged ||
-        _stagePropertiesChanged || _skyboxPropertiesChanged || _hazePropertiesChanged || _bloomPropertiesChanged;
+        _skyboxPropertiesChanged || _hazePropertiesChanged || _bloomPropertiesChanged;
 
     return somethingChanged;
 }
@@ -394,7 +394,6 @@ void ZoneEntityItem::resetRenderingPropertiesChanged() {
         _skyboxPropertiesChanged = false;
         _hazePropertiesChanged = false;
         _bloomPropertiesChanged = false;
-        _stagePropertiesChanged = false;
     });
 }
 
diff --git a/libraries/entities/src/ZoneEntityItem.h b/libraries/entities/src/ZoneEntityItem.h
index 813115add9..11c85dab89 100644
--- a/libraries/entities/src/ZoneEntityItem.h
+++ b/libraries/entities/src/ZoneEntityItem.h
@@ -102,8 +102,6 @@ public:
     bool hazePropertiesChanged() const { return _hazePropertiesChanged; }
     bool bloomPropertiesChanged() const { return _bloomPropertiesChanged; }
 
-    bool stagePropertiesChanged() const { return _stagePropertiesChanged; }
-
     void resetRenderingPropertiesChanged();
 
     virtual bool supportsDetailedIntersection() const override { return true; }
@@ -155,7 +153,6 @@ protected:
     bool _skyboxPropertiesChanged { false };
     bool _hazePropertiesChanged{ false };
     bool _bloomPropertiesChanged { false };
-    bool _stagePropertiesChanged { false };
 
     static bool _drawZoneBoundaries;
     static bool _zonesArePickable;
diff --git a/libraries/graphics/src/graphics/Haze.cpp b/libraries/graphics/src/graphics/Haze.cpp
index ded48429ba..d9bee7507f 100644
--- a/libraries/graphics/src/graphics/Haze.cpp
+++ b/libraries/graphics/src/graphics/Haze.cpp
@@ -182,12 +182,3 @@ void Haze::setHazeBackgroundBlend(const float hazeBackgroundBlend) {
         _hazeParametersBuffer.edit<Parameters>().hazeBackgroundBlend = newBlend;
     }
 }
-
-void Haze::setTransform(const glm::mat4& transform) {
-    auto& params = _hazeParametersBuffer.get<Parameters>();
-
-    if (params.transform != transform) {
-        _hazeParametersBuffer.edit<Parameters>().transform = transform;
-    }
-}
-
diff --git a/libraries/graphics/src/graphics/Haze.h b/libraries/graphics/src/graphics/Haze.h
index 59138319f4..25004f098f 100644
--- a/libraries/graphics/src/graphics/Haze.h
+++ b/libraries/graphics/src/graphics/Haze.h
@@ -92,8 +92,6 @@ namespace graphics {
 
         void setHazeBackgroundBlend(const float hazeBackgroundBlend);
 
-        void setTransform(const glm::mat4& transform);
-
         using UniformBufferView = gpu::BufferView;
         UniformBufferView getHazeParametersBuffer() const { return _hazeParametersBuffer; }
 
@@ -101,30 +99,32 @@ namespace graphics {
         class Parameters {
         public:
             // DO NOT CHANGE ORDER HERE WITHOUT UNDERSTANDING THE std140 LAYOUT
-            glm::vec3 hazeColor{ INITIAL_HAZE_COLOR };
-            float hazeGlareBlend{ convertGlareAngleToPower(INITIAL_HAZE_GLARE_ANGLE) };
+            glm::vec3 hazeColor { INITIAL_HAZE_COLOR };
+            float hazeGlareBlend { convertGlareAngleToPower(INITIAL_HAZE_GLARE_ANGLE) };
 
-            glm::vec3 hazeGlareColor{ INITIAL_HAZE_GLARE_COLOR };
-            float hazeBaseReference{ INITIAL_HAZE_BASE_REFERENCE };
+            glm::vec3 hazeGlareColor { INITIAL_HAZE_GLARE_COLOR };
+            float hazeBaseReference { INITIAL_HAZE_BASE_REFERENCE };
 
             glm::vec3 colorModulationFactor;
-            int hazeMode{ 0 };    // bit 0 - set to activate haze attenuation of fragment color
+            int hazeMode { 0 };   // bit 0 - set to activate haze attenuation of fragment color
                                   // bit 1 - set to add the effect of altitude to the haze attenuation
                                   // bit 2 - set to activate directional light attenuation mode
                                   // bit 3 - set to blend between blend-in and blend-out colours
 
-            glm::mat4 transform;
+            // Padding required to align the struct
+#if defined(__clang__)
+            __attribute__((unused))
+#endif
+            vec3 __padding;
 
             // Amount of background (skybox) to display, overriding the haze effect for the background
-            float hazeBackgroundBlend{ INITIAL_HAZE_BACKGROUND_BLEND };
+            float hazeBackgroundBlend { INITIAL_HAZE_BACKGROUND_BLEND };
             // The haze attenuation exponents used by both fragment and directional light attenuation
-            float hazeRangeFactor{ convertHazeRangeToHazeRangeFactor(INITIAL_HAZE_RANGE) };
-            float hazeHeightFactor{ convertHazeAltitudeToHazeAltitudeFactor(INITIAL_HAZE_HEIGHT) };
-            float hazeKeyLightRangeFactor{ convertHazeRangeToHazeRangeFactor(INITIAL_KEY_LIGHT_RANGE) };
+            float hazeRangeFactor { convertHazeRangeToHazeRangeFactor(INITIAL_HAZE_RANGE) };
+            float hazeHeightFactor { convertHazeAltitudeToHazeAltitudeFactor(INITIAL_HAZE_HEIGHT) };
+            float hazeKeyLightRangeFactor { convertHazeRangeToHazeRangeFactor(INITIAL_KEY_LIGHT_RANGE) };
 
-            float hazeKeyLightAltitudeFactor{ convertHazeAltitudeToHazeAltitudeFactor(INITIAL_KEY_LIGHT_ALTITUDE) };
-            // Padding required to align the structure to sizeof(vec4)
-            vec3 __padding;
+            float hazeKeyLightAltitudeFactor { convertHazeAltitudeToHazeAltitudeFactor(INITIAL_KEY_LIGHT_ALTITUDE) };
 
             Parameters() {}
         };
diff --git a/libraries/render-utils/src/Haze.slh b/libraries/render-utils/src/Haze.slh
index 0bf1d5d689..e2285febe4 100644
--- a/libraries/render-utils/src/Haze.slh
+++ b/libraries/render-utils/src/Haze.slh
@@ -28,7 +28,7 @@ struct HazeParams {
     vec3 colorModulationFactor;
     int hazeMode;
 
-    mat4 transform;
+    vec3 spare;
     float backgroundBlend;
 
     float hazeRangeFactor;