Fix for skybox not always loading

This commit is contained in:
Atlante45 2016-01-11 18:58:46 -08:00
parent 0008b1a055
commit 5e41421f9d
2 changed files with 89 additions and 78 deletions

View file

@ -24,7 +24,6 @@
#include <SceneScriptingInterface.h> #include <SceneScriptingInterface.h>
#include <ScriptEngine.h> #include <ScriptEngine.h>
#include <procedural/ProceduralSkybox.h> #include <procedural/ProceduralSkybox.h>
#include <TextureCache.h>
#include "EntityTreeRenderer.h" #include "EntityTreeRenderer.h"
@ -142,7 +141,7 @@ void EntityTreeRenderer::update() {
// even if we haven't changed positions, if we previously attempted to set the skybox, but // even if we haven't changed positions, if we previously attempted to set the skybox, but
// have a pending download of the skybox texture, then we should attempt to reapply to // have a pending download of the skybox texture, then we should attempt to reapply to
// get the correct texture. // get the correct texture.
if (_pendingSkyboxTextureDownload) { if (_pendingSkyboxTexture && _skyboxTexture && _skyboxTexture->isLoaded()) {
applyZonePropertiesToScene(_bestZone); applyZonePropertiesToScene(_bestZone);
} }
@ -255,26 +254,31 @@ void EntityTreeRenderer::forceRecheckEntities() {
void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptr<ZoneEntityItem> zone) { void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptr<ZoneEntityItem> zone) {
QSharedPointer<SceneScriptingInterface> scene = DependencyManager::get<SceneScriptingInterface>(); auto scene = DependencyManager::get<SceneScriptingInterface>();
auto sceneStage = scene->getStage(); auto sceneStage = scene->getStage();
auto skyStage = scene->getSkyStage();
auto sceneKeyLight = sceneStage->getKeyLight(); auto sceneKeyLight = sceneStage->getKeyLight();
auto sceneLocation = sceneStage->getLocation(); auto sceneLocation = sceneStage->getLocation();
auto sceneTime = sceneStage->getTime(); auto sceneTime = sceneStage->getTime();
auto backgroundMode = BACKGROUND_MODE_INHERIT; // Default
if (!_hasPreviousZone) {
_previousKeyLightColor = sceneKeyLight->getColor();
_previousKeyLightIntensity = sceneKeyLight->getIntensity();
_previousKeyLightAmbientIntensity = sceneKeyLight->getAmbientIntensity();
_previousKeyLightDirection = sceneKeyLight->getDirection();
_previousStageSunModelEnabled = sceneStage->isSunModelEnabled();
_previousStageLongitude = sceneLocation->getLongitude();
_previousStageLatitude = sceneLocation->getLatitude();
_previousStageAltitude = sceneLocation->getAltitude();
_previousStageHour = sceneTime->getHour();
_previousStageDay = sceneTime->getDay();
_hasPreviousZone = true;
}
if (zone) { if (zone) {
if (!_hasPreviousZone) { backgroundMode = zone->getBackgroundMode();
_previousKeyLightColor = sceneKeyLight->getColor();
_previousKeyLightIntensity = sceneKeyLight->getIntensity();
_previousKeyLightAmbientIntensity = sceneKeyLight->getAmbientIntensity();
_previousKeyLightDirection = sceneKeyLight->getDirection();
_previousStageSunModelEnabled = sceneStage->isSunModelEnabled();
_previousStageLongitude = sceneLocation->getLongitude();
_previousStageLatitude = sceneLocation->getLatitude();
_previousStageAltitude = sceneLocation->getAltitude();
_previousStageHour = sceneTime->getHour();
_previousStageDay = sceneTime->getDay();
_hasPreviousZone = true;
}
sceneKeyLight->setColor(ColorUtils::toVec3(zone->getKeyLightProperties().getColor())); sceneKeyLight->setColor(ColorUtils::toVec3(zone->getKeyLightProperties().getColor()));
sceneKeyLight->setIntensity(zone->getKeyLightProperties().getIntensity()); sceneKeyLight->setIntensity(zone->getKeyLightProperties().getIntensity());
sceneKeyLight->setAmbientIntensity(zone->getKeyLightProperties().getAmbientIntensity()); sceneKeyLight->setAmbientIntensity(zone->getKeyLightProperties().getAmbientIntensity());
@ -284,85 +288,89 @@ void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptr<ZoneEntityIt
zone->getStageProperties().getAltitude()); zone->getStageProperties().getAltitude());
sceneTime->setHour(zone->getStageProperties().calculateHour()); sceneTime->setHour(zone->getStageProperties().calculateHour());
sceneTime->setDay(zone->getStageProperties().calculateDay()); sceneTime->setDay(zone->getStageProperties().calculateDay());
}
if (zone->getBackgroundMode() == BACKGROUND_MODE_ATMOSPHERE) {
switch (backgroundMode) {
case BACKGROUND_MODE_ATMOSPHERE: {
EnvironmentData data = zone->getEnvironmentData(); EnvironmentData data = zone->getEnvironmentData();
glm::vec3 keyLightDirection = sceneKeyLight->getDirection(); glm::vec3 keyLightDirection = sceneKeyLight->getDirection();
glm::vec3 inverseKeyLightDirection = keyLightDirection * -1.0f; glm::vec3 inverseKeyLightDirection = keyLightDirection * -1.0f;
// NOTE: is this right? It seems like the "sun" should be based on the center of the // NOTE: is this right? It seems like the "sun" should be based on the center of the
// atmosphere, not where the camera is. // atmosphere, not where the camera is.
glm::vec3 keyLightLocation = _viewState->getAvatarPosition() glm::vec3 keyLightLocation = _viewState->getAvatarPosition()
+ (inverseKeyLightDirection * data.getAtmosphereOuterRadius()); + (inverseKeyLightDirection * data.getAtmosphereOuterRadius());
data.setSunLocation(keyLightLocation); data.setSunLocation(keyLightLocation);
const float KEY_LIGHT_INTENSITY_TO_SUN_BRIGHTNESS_RATIO = 20.0f; const float KEY_LIGHT_INTENSITY_TO_SUN_BRIGHTNESS_RATIO = 20.0f;
float sunBrightness = sceneKeyLight->getIntensity() * KEY_LIGHT_INTENSITY_TO_SUN_BRIGHTNESS_RATIO; float sunBrightness = sceneKeyLight->getIntensity() * KEY_LIGHT_INTENSITY_TO_SUN_BRIGHTNESS_RATIO;
data.setSunBrightness(sunBrightness); data.setSunBrightness(sunBrightness);
_viewState->overrideEnvironmentData(data); _viewState->overrideEnvironmentData(data);
scene->getSkyStage()->setBackgroundMode(model::SunSkyStage::SKY_DOME); skyStage->setBackgroundMode(model::SunSkyStage::SKY_DOME);
_pendingSkyboxTextureDownload = false; _pendingSkyboxTexture = false;
_skyboxTexture.clear();
} else { break;
_viewState->endOverrideEnvironmentData(); }
auto stage = scene->getSkyStage(); case BACKGROUND_MODE_SKYBOX: {
if (zone->getBackgroundMode() == BACKGROUND_MODE_SKYBOX) { auto skybox = std::dynamic_pointer_cast<ProceduralSkybox>(skyStage->getSkybox());
auto skybox = std::dynamic_pointer_cast<ProceduralSkybox>(stage->getSkybox()); skybox->setColor(zone->getSkyboxProperties().getColorVec3());
skybox->setColor(zone->getSkyboxProperties().getColorVec3()); static QString userData;
static QString userData; if (userData != zone->getUserData()) {
if (userData != zone->getUserData()) { userData = zone->getUserData();
userData = zone->getUserData(); auto procedural = std::make_shared<Procedural>(userData);
ProceduralPointer procedural(new Procedural(userData)); if (procedural->_enabled) {
if (procedural->_enabled) { skybox->setProcedural(procedural);
skybox->setProcedural(procedural);
} else {
skybox->setProcedural(ProceduralPointer());
}
}
if (zone->getSkyboxProperties().getURL().isEmpty()) {
skybox->setCubemap(gpu::TexturePointer());
_pendingSkyboxTextureDownload = false;
} else { } else {
// Update the Texture of the Skybox with the one pointed by this zone skybox->setProcedural(ProceduralPointer());
auto cubeMap = DependencyManager::get<TextureCache>()->getTexture(zone->getSkyboxProperties().getURL(), CUBE_TEXTURE);
if (cubeMap->getGPUTexture()) {
skybox->setCubemap(cubeMap->getGPUTexture());
_pendingSkyboxTextureDownload = false;
} else {
_pendingSkyboxTextureDownload = true;
}
} }
stage->setBackgroundMode(model::SunSkyStage::SKY_BOX);
} else {
stage->setBackgroundMode(model::SunSkyStage::SKY_DOME); // let the application atmosphere through
_pendingSkyboxTextureDownload = false;
} }
if (zone->getSkyboxProperties().getURL().isEmpty()) {
skybox->setCubemap(gpu::TexturePointer());
_pendingSkyboxTexture = false;
_skyboxTexture.clear();
} else {
// Update the Texture of the Skybox with the one pointed by this zone
auto textureCache = DependencyManager::get<TextureCache>();
_skyboxTexture = textureCache->getTexture(zone->getSkyboxProperties().getURL(), CUBE_TEXTURE);
if (_skyboxTexture->getGPUTexture()) {
skybox->setCubemap(_skyboxTexture->getGPUTexture());
_pendingSkyboxTexture = false;
} else {
_pendingSkyboxTexture = true;
}
}
_viewState->endOverrideEnvironmentData();
skyStage->setBackgroundMode(model::SunSkyStage::SKY_BOX);
break;
} }
} else { case BACKGROUND_MODE_INHERIT:
_pendingSkyboxTextureDownload = false; if (_hasPreviousZone) {
if (_hasPreviousZone) { sceneKeyLight->setColor(_previousKeyLightColor);
sceneKeyLight->setColor(_previousKeyLightColor); sceneKeyLight->setIntensity(_previousKeyLightIntensity);
sceneKeyLight->setIntensity(_previousKeyLightIntensity); sceneKeyLight->setAmbientIntensity(_previousKeyLightAmbientIntensity);
sceneKeyLight->setAmbientIntensity(_previousKeyLightAmbientIntensity); sceneKeyLight->setDirection(_previousKeyLightDirection);
sceneKeyLight->setDirection(_previousKeyLightDirection); sceneStage->setSunModelEnable(_previousStageSunModelEnabled);
sceneStage->setSunModelEnable(_previousStageSunModelEnabled); sceneStage->setLocation(_previousStageLongitude, _previousStageLatitude,
sceneStage->setLocation(_previousStageLongitude, _previousStageLatitude, _previousStageAltitude);
_previousStageAltitude); sceneTime->setHour(_previousStageHour);
sceneTime->setHour(_previousStageHour); sceneTime->setDay(_previousStageDay);
sceneTime->setDay(_previousStageDay); _hasPreviousZone = false;
_hasPreviousZone = false; }
} _viewState->endOverrideEnvironmentData();
_viewState->endOverrideEnvironmentData(); skyStage->setBackgroundMode(model::SunSkyStage::SKY_DOME); // let the application atmosphere through
scene->getSkyStage()->setBackgroundMode(model::SunSkyStage::SKY_DOME); // let the application atmosphere through _pendingSkyboxTexture = false;
_skyboxTexture.clear();
break;
} }
} }
const FBXGeometry* EntityTreeRenderer::getGeometryForEntity(EntityItemPointer entityItem) { const FBXGeometry* EntityTreeRenderer::getGeometryForEntity(EntityItemPointer entityItem) {
const FBXGeometry* result = NULL; const FBXGeometry* result = NULL;
if (entityItem->getType() == EntityTypes::Model) { if (entityItem->getType() == EntityTypes::Model) {
std::shared_ptr<RenderableModelEntityItem> modelEntityItem = std::shared_ptr<RenderableModelEntityItem> modelEntityItem =
std::dynamic_pointer_cast<RenderableModelEntityItem>(entityItem); std::dynamic_pointer_cast<RenderableModelEntityItem>(entityItem);

View file

@ -15,12 +15,13 @@
#include <QSet> #include <QSet>
#include <QStack> #include <QStack>
#include <EntityTree.h> #include <AbstractAudioInterface.h>
#include <EntityScriptingInterface.h> // for RayToEntityIntersectionResult #include <EntityScriptingInterface.h> // for RayToEntityIntersectionResult
#include <EntityTree.h>
#include <MouseEvent.h> #include <MouseEvent.h>
#include <OctreeRenderer.h> #include <OctreeRenderer.h>
#include <ScriptCache.h> #include <ScriptCache.h>
#include <AbstractAudioInterface.h> #include <TextureCache.h>
class AbstractScriptingServicesInterface; class AbstractScriptingServicesInterface;
class AbstractViewStateInterface; class AbstractViewStateInterface;
@ -142,9 +143,11 @@ private:
void forceRecheckEntities(); void forceRecheckEntities();
glm::vec3 _lastAvatarPosition; glm::vec3 _lastAvatarPosition;
bool _pendingSkyboxTextureDownload = false;
QVector<EntityItemID> _currentEntitiesInside; QVector<EntityItemID> _currentEntitiesInside;
bool _pendingSkyboxTexture = false;
NetworkTexturePointer _skyboxTexture;
bool _wantScripts; bool _wantScripts;
ScriptEngine* _entitiesScriptEngine; ScriptEngine* _entitiesScriptEngine;