bool ValueAnimation::LoadXML(const XMLElement& source) { valueType_ = VAR_NONE; eventFrames_.Clear(); String interpMethodString = source.GetAttribute("interpolationmethod"); InterpMethod method = IM_LINEAR; for (int i = 0; i <= IM_SPLINE; ++i) { if (interpMethodString == interpMethodNames[i]) { method = (InterpMethod)i; break; } } SetInterpolationMethod(method); if (interpolationMethod_ == IM_SPLINE) splineTension_ = source.GetFloat("splinetension"); XMLElement keyFrameElem = source.GetChild("keyframe"); while (keyFrameElem) { float time = keyFrameElem.GetFloat("time"); Variant value = keyFrameElem.GetVariant(); SetKeyFrame(time, value); keyFrameElem = keyFrameElem.GetNext("keyframe"); } XMLElement eventFrameElem = source.GetChild("eventframe"); while (eventFrameElem) { float time = eventFrameElem.GetFloat("time"); unsigned eventType = eventFrameElem.GetUInt("eventtype"); VariantMap eventData = eventFrameElem.GetChild("eventdata").GetVariantMap(); SetEventFrame(time, StringHash(eventType), eventData); eventFrameElem = eventFrameElem.GetNext("eventframe"); } return true; }
bool ListView::FilterImplicitAttributes(XMLElement& dest) const { if (!ScrollView::FilterImplicitAttributes(dest)) return false; XMLElement childElem = dest.GetChild("element"); // Horizontal scroll bar if (!childElem) return false; childElem = childElem.GetNext("element"); // Vertical scroll bar if (!childElem) return false; childElem = childElem.GetNext("element"); // Scroll panel if (!childElem) return false; XMLElement containerElem = childElem.GetChild("element"); // Item container if (!containerElem) return false; if (!RemoveChildXML(containerElem, "Name", "LV_ItemContainer")) return false; if (!RemoveChildXML(containerElem, "Is Enabled", "true")) return false; if (!RemoveChildXML(containerElem, "Layout Mode", "Vertical")) return false; if (!RemoveChildXML(containerElem, "Size")) return false; if (hierarchyMode_) { containerElem = childElem.GetNext("element"); // Overlay container if (!containerElem) return false; if (!RemoveChildXML(containerElem, "Name", "LV_OverlayContainer")) return false; if (!RemoveChildXML(containerElem, "Clip Children", "true")) return false; if (!RemoveChildXML(containerElem, "Size")) return false; } return true; }
bool TextureCube::Load(Deserializer& source) { PROFILE(LoadTextureCube); ResourceCache* cache = GetSubsystem<ResourceCache>(); // In headless mode, do not actually load the texture, just return success Graphics* graphics = GetSubsystem<Graphics>(); if (!graphics) return true; // If device is lost, retry later if (graphics_->IsDeviceLost()) { LOGWARNING("Texture load while device is lost"); dataPending_ = true; return true; } // If over the texture budget, see if materials can be freed to allow textures to be freed CheckTextureBudget(GetTypeStatic()); String texPath, texName, texExt; SplitPath(GetName(), texPath, texName, texExt); SharedPtr<XMLFile> xml(new XMLFile(context_)); if (!xml->Load(source)) return false; LoadParameters(xml); XMLElement textureElem = xml->GetRoot(); XMLElement faceElem = textureElem.GetChild("face"); unsigned faces = 0; while (faceElem && faces < MAX_CUBEMAP_FACES) { String name = faceElem.GetAttribute("name"); String faceTexPath, faceTexName, faceTexExt; SplitPath(name, faceTexPath, faceTexName, faceTexExt); // If path is empty, add the XML file path if (faceTexPath.Empty()) name = texPath + name; SharedPtr<Image> image(cache->GetResource<Image>(name)); Load((CubeMapFace)faces, image); ++faces; faceElem = faceElem.GetNext("face"); } return true; }
VariantVector XMLElement::GetVariantVector() const { VariantVector ret; XMLElement variantElem = GetChild("variant"); while (variantElem) { ret.push_back(variantElem.GetVariant()); variantElem = variantElem.GetNext("variant"); } return ret; }
StringVector XMLElement::GetStringVector() const { StringVector ret; XMLElement stringElem = GetChild("string"); while (stringElem) { ret.push_back(stringElem.GetAttributeCString("value")); stringElem = stringElem.GetNext("string"); } return ret; }
VariantMap XMLElement::GetVariantMap() const { VariantMap ret; XMLElement variantElem = GetChild("variant"); while (variantElem) { StringHash key(variantElem.GetInt("hash")); ret[key] = variantElem.GetVariant(); variantElem = variantElem.GetNext("variant"); } return ret; }
void ShaderPrecache::LoadShaders(Graphics* graphics, Deserializer& source) { LOGDEBUG("Begin precaching shaders"); XMLFile xmlFile(graphics->GetContext()); xmlFile.Load(source); XMLElement shader = xmlFile.GetRoot().GetChild("shader"); while (shader) { String vsDefines = shader.GetAttribute("vsdefines"); String psDefines = shader.GetAttribute("psdefines"); // Check for illegal variations on OpenGL ES and skip them #ifdef GL_ES_VERSION_2_0 if ( #ifndef __EMSCRIPTEN__ vsDefines.Contains("INSTANCED") || #endif (psDefines.Contains("POINTLIGHT") && psDefines.Contains("SHADOW"))) { shader = shader.GetNext("shader"); continue; } #endif ShaderVariation* vs = graphics->GetShader(VS, shader.GetAttribute("vs"), vsDefines); ShaderVariation* ps = graphics->GetShader(PS, shader.GetAttribute("ps"), psDefines); // Set the shaders active to actually compile them graphics->SetShaders(vs, ps); shader = shader.GetNext("shader"); } LOGDEBUG("End precaching shaders"); }
bool PListFile::LoadDict(PListValueMap& dict, const XMLElement& dictElem) { if (!dictElem) return false; XMLElement keyElem = dictElem.GetChild("key"); XMLElement valueElem = keyElem.GetNext(); while (keyElem && valueElem) { String key = keyElem.GetValue(); valueElem = keyElem.GetNext(); PListValue value; if (!LoadValue(value, valueElem)) return false; dict[key] = value; keyElem = valueElem.GetNext("key"); valueElem = keyElem.GetNext(); } return true; }
void Texture::SetParameters(const XMLElement& element) { XMLElement paramElem = element.GetChild(); while (paramElem) { String name = paramElem.GetName(); if (name == "address") { String coord = paramElem.GetAttributeLower("coord"); if (coord.Length() >= 1) { TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u'); String mode = paramElem.GetAttributeLower("mode"); SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode.CString(), addressModeNames, ADDRESS_WRAP)); } } if (name == "border") SetBorderColor(paramElem.GetColor("color")); if (name == "filter") { String mode = paramElem.GetAttributeLower("mode"); SetFilterMode((TextureFilterMode)GetStringListIndex(mode.CString(), filterModeNames, FILTER_DEFAULT)); } if (name == "mipmap") SetNumLevels(paramElem.GetBool("enable") ? 0 : 1); if (name == "quality") { if (paramElem.HasAttribute("low")) SetMipsToSkip(QUALITY_LOW, paramElem.GetInt("low")); if (paramElem.HasAttribute("med")) SetMipsToSkip(QUALITY_MEDIUM, paramElem.GetInt("med")); if (paramElem.HasAttribute("medium")) SetMipsToSkip(QUALITY_MEDIUM, paramElem.GetInt("medium")); if (paramElem.HasAttribute("high")) SetMipsToSkip(QUALITY_HIGH, paramElem.GetInt("high")); } if (name == "srgb") SetSRGB(paramElem.GetBool("enable")); paramElem = paramElem.GetNext(); } }
bool Scene::LoadAsyncXML(File* file) { if (!file) { LOGERROR("Null file for async loading"); return false; } StopAsyncLoading(); SharedPtr<XMLFile> xml(new XMLFile(context_)); if (!xml->Load(*file)) return false; LOGINFO("Loading scene from " + file->GetName()); Clear(); XMLElement rootElement = xml->GetRoot(); // Store own old ID for resolving possible root node references unsigned nodeID = rootElement.GetInt("id"); resolver_.AddNode(nodeID, this); // Load the root level components first if (!Node::LoadXML(rootElement, resolver_, false)) return false; // Then prepare for loading all root level child nodes in the async update XMLElement childNodeElement = rootElement.GetChild("node"); asyncLoading_ = true; asyncProgress_.file_ = file; asyncProgress_.xmlFile_ = xml; asyncProgress_.xmlElement_ = childNodeElement; asyncProgress_.loadedNodes_ = 0; asyncProgress_.totalNodes_ = 0; // Count the amount of child nodes while (childNodeElement) { ++asyncProgress_.totalNodes_; childNodeElement = childNodeElement.GetNext("node"); } return true; }
bool TmxTileLayer2D::Load(const XMLElement& element, const TileMapInfo2D& info) { LoadInfo(element); XMLElement dataElem = element.GetChild("data"); if (!dataElem) { URHO3D_LOGERROR("Could not find data in layer"); return false; } if (dataElem.HasAttribute("encoding") && dataElem.GetAttribute("encoding") != "xml") { URHO3D_LOGERROR("Encoding not support now"); return false; } XMLElement tileElem = dataElem.GetChild("tile"); tiles_.Resize((unsigned)(width_ * height_)); for (int y = 0; y < height_; ++y) { for (int x = 0; x < width_; ++x) { if (!tileElem) return false; int gid = tileElem.GetInt("gid"); if (gid > 0) { SharedPtr<Tile2D> tile(new Tile2D()); tile->gid_ = gid; tile->sprite_ = tmxFile_->GetTileSprite(gid); tile->propertySet_ = tmxFile_->GetTilePropertySet(gid); tiles_[y * width_ + x] = tile; } tileElem = tileElem.GetNext("tile"); } } if (element.HasChild("properties")) LoadPropertySet(element.GetChild("properties")); return true; }
bool PListFile::LoadArray(PListValueVector& array, const XMLElement& arrayElem) { if (!arrayElem) return false; for (XMLElement valueElem = arrayElem.GetChild(); valueElem; valueElem = valueElem.GetNext()) { PListValue value; if (!LoadValue(value, valueElem)) return false; array.Push(value); } return true; }
bool SpriteSheet2D::EndLoadFromXMLFile() { ResourceCache* cache = GetSubsystem<ResourceCache>(); texture_ = cache->GetResource<Texture2D>(loadTextureName_); if (!texture_) { URHO3D_LOGERROR("Could not load texture " + loadTextureName_); loadXMLFile_.Reset(); loadTextureName_.Clear(); return false; } XMLElement rootElem = loadXMLFile_->GetRoot("TextureAtlas"); XMLElement subTextureElem = rootElem.GetChild("SubTexture"); while (subTextureElem) { String name = subTextureElem.GetAttribute("name"); int x = subTextureElem.GetInt("x"); int y = subTextureElem.GetInt("y"); int width = subTextureElem.GetInt("width"); int height = subTextureElem.GetInt("height"); IntRect rectangle(x, y, x + width, y + height); Vector2 hotSpot(0.5f, 0.5f); IntVector2 offset(0, 0); if (subTextureElem.HasAttribute("frameWidth") && subTextureElem.HasAttribute("frameHeight")) { offset.x_ = subTextureElem.GetInt("frameX"); offset.y_ = subTextureElem.GetInt("frameY"); int frameWidth = subTextureElem.GetInt("frameWidth"); int frameHeight = subTextureElem.GetInt("frameHeight"); hotSpot.x_ = ((float)offset.x_ + frameWidth / 2) / width; hotSpot.y_ = 1.0f - ((float)offset.y_ + frameHeight / 2) / height; } DefineSprite(name, rectangle, hotSpot, offset); subTextureElem = subTextureElem.GetNext("SubTexture"); } loadXMLFile_.Reset(); loadTextureName_.Clear(); return true; }
bool Animatable::LoadXML(const XMLElement& source, bool setInstanceDefault) { if (!Serializable::LoadXML(source, setInstanceDefault)) return false; SetObjectAnimation(0); attributeAnimationInfos_.Clear(); XMLElement elem = source.GetChild("objectanimation"); if (elem) { SharedPtr<ObjectAnimation> objectAnimation(new ObjectAnimation(context_)); if (!objectAnimation->LoadXML(elem)) return false; SetObjectAnimation(objectAnimation); } elem = source.GetChild("attributeanimation"); while (elem) { String name = elem.GetAttribute("name"); SharedPtr<ValueAnimation> attributeAnimation(new ValueAnimation(context_)); if (!attributeAnimation->LoadXML(elem)) return false; String wrapModeString = source.GetAttribute("wrapmode"); WrapMode wrapMode = WM_LOOP; for (int i = 0; i <= WM_CLAMP; ++i) { if (wrapModeString == wrapModeNames[i]) { wrapMode = (WrapMode)i; break; } } float speed = elem.GetFloat("speed"); SetAttributeAnimation(name, attributeAnimation, wrapMode, speed); elem = elem.GetNext("attributeanimation"); } return true; }
VariantMap XMLElement::GetVariantMap() const { VariantMap ret; XMLElement variantElem = GetChild("variant"); while (variantElem) { // If this is a manually edited map, user can not be expected to calculate hashes manually. Also accept "name" attribute if (variantElem.HasAttribute("name")) ret[StringHash(variantElem.GetAttribute("name"))] = variantElem.GetVariant(); else if (variantElem.HasAttribute("hash")) ret[StringHash(variantElem.GetUInt("hash"))] = variantElem.GetVariant(); variantElem = variantElem.GetNext("variant"); } return ret; }
void Sound::LoadParameters() { ResourceCache* cache = GetSubsystem<ResourceCache>(); String xmlName = ReplaceExtension(GetName(), ".xml"); if (!cache->Exists(xmlName)) return; XMLFile* file = cache->GetResource<XMLFile>(xmlName); if (!file) return; XMLElement rootElem = file->GetRoot(); XMLElement paramElem = rootElem.GetChild(); while (paramElem) { String name = paramElem.GetName(); if (name == "format" && !compressed_) { if (paramElem.HasAttribute("frequency")) frequency_ = paramElem.GetInt("frequency"); if (paramElem.HasAttribute("sixteenbit")) sixteenBit_ = paramElem.GetBool("sixteenbit"); if (paramElem.HasAttribute("16bit")) sixteenBit_ = paramElem.GetBool("16bit"); if (paramElem.HasAttribute("stereo")) stereo_ = paramElem.GetBool("stereo"); } if (name == "loop") { if (paramElem.HasAttribute("enable")) SetLooped(paramElem.GetBool("enable")); if (paramElem.HasAttribute("start") && paramElem.HasAttribute("end")) SetLoop(paramElem.GetInt("start"), paramElem.GetInt("end")); } paramElem = paramElem.GetNext(); } }
void ShaderPrecache::LoadShaders(Graphics* graphics, Deserializer& source) { LOGDEBUG("Begin precaching shaders"); XMLFile xmlFile(graphics->GetContext()); xmlFile.Load(source); XMLElement shader = xmlFile.GetRoot().GetChild("shader"); while (shader) { ShaderVariation* vs = graphics->GetShader(VS, shader.GetAttribute("vs"), shader.GetAttribute("vsdefines")); ShaderVariation* ps = graphics->GetShader(PS, shader.GetAttribute("ps"), shader.GetAttribute("psdefines")); // Set the shaders active to actually compile them graphics->SetShaders(vs, ps); shader = shader.GetNext("shader"); } LOGDEBUG("End precaching shaders"); }
EditorIconCache::EditorIconCache(Context* context) : Object(context) { ResourceCache* cache = ui::GetSystemUI()->GetSubsystem<ResourceCache>(); auto* icons = cache->GetResource<XMLFile>("UI/EditorIcons.xml"); if (icons == nullptr) return; XMLElement root = icons->GetRoot(); for (XMLElement element = root.GetChild("element"); element.NotNull(); element = element.GetNext("element")) { ea::string type = element.GetAttribute("type"); if (type.empty()) { URHO3D_LOGERROR("EditorIconCache.xml contains icon entry without a \"type\" attribute."); continue; } XPathResultSet texture = element.SelectPrepared(XPathQuery("attribute[@name=\"Texture\"]")); if (texture.Empty()) { URHO3D_LOGERROR("EditorIconCache.xml contains icon entry without a \"Texture\"."); continue; } XPathResultSet rect = element.SelectPrepared(XPathQuery("attribute[@name=\"Image Rect\"]")); if (rect.Empty()) { URHO3D_LOGERROR("EditorIconCache.xml contains icon entry without a \"Image Rect\"."); continue; } IconData data; data.textureRef_ = texture.FirstResult().GetVariantValue(VAR_RESOURCEREF).GetResourceRef(); data.rect_ = rect.FirstResult().GetVariantValue(VAR_INTRECT).GetIntRect(); iconCache_[type] = data; } }
void Sound::LoadParameters() { auto* cache = GetSubsystem<ResourceCache>(); String xmlName = ReplaceExtension(GetName(), ".xml"); SharedPtr<XMLFile> file(cache->GetTempResource<XMLFile>(xmlName, false)); if (!file) return; XMLElement rootElem = file->GetRoot(); LoadMetadataFromXML(rootElem); for (XMLElement paramElem = rootElem.GetChild(); paramElem; paramElem = paramElem.GetNext()) { String name = paramElem.GetName(); if (name == "format" && !compressed_) { if (paramElem.HasAttribute("frequency")) frequency_ = (unsigned)paramElem.GetInt("frequency"); if (paramElem.HasAttribute("sixteenbit")) sixteenBit_ = paramElem.GetBool("sixteenbit"); if (paramElem.HasAttribute("16bit")) sixteenBit_ = paramElem.GetBool("16bit"); if (paramElem.HasAttribute("stereo")) stereo_ = paramElem.GetBool("stereo"); } if (name == "loop") { if (paramElem.HasAttribute("enable")) SetLooped(paramElem.GetBool("enable")); if (paramElem.HasAttribute("start") && paramElem.HasAttribute("end")) SetLoop((unsigned)paramElem.GetInt("start"), (unsigned)paramElem.GetInt("end")); } } }
bool SpriteSheet2D::Load(Deserializer& source) { spriteMapping_.Clear(); SharedPtr<XMLFile> xmlFile(new XMLFile(context_)); if(!xmlFile->Load(source)) { LOGERROR("Could not load sprite sheet"); return false; } SetMemoryUse(source.GetSize()); XMLElement rootElem = xmlFile->GetRoot(); if (!rootElem) { LOGERROR("Invalid sprite sheet"); return false; } if (rootElem.GetName() == "spritesheet") { ResourceCache* cache = GetSubsystem<ResourceCache>(); String textureFileName = rootElem.GetAttribute("texture"); texture_ = cache->GetResource<Texture2D>(textureFileName, false); // If texture not found, try get in current directory if (!texture_) texture_ = cache->GetResource<Texture2D>(GetParentPath(GetName()) + textureFileName); if (!texture_) { LOGERROR("Cound not load texture"); return false; } XMLElement spriteElem = rootElem.GetChild("sprite"); while (spriteElem) { String name = spriteElem.GetAttribute("name"); IntRect rectangle = spriteElem.GetIntRect("rectangle"); Vector2 hotSpot(0.5f, 0.5f); if (spriteElem.HasAttribute("hotspot")) hotSpot = spriteElem.GetVector2("hotspot"); DefineSprite(name, rectangle, hotSpot); spriteElem = spriteElem.GetNext("sprite"); } } // Sparrow Starling texture atlas else if (rootElem.GetName() == "TextureAtlas") { String textureFileName = rootElem.GetAttribute("imagePath"); ResourceCache* cache = GetSubsystem<ResourceCache>(); texture_ = cache->GetResource<Texture2D>(textureFileName, false); // If texture not found, try get in current directory if (!texture_) texture_ = cache->GetResource<Texture2D>(GetParentPath(GetName()) + textureFileName); if (!texture_) { LOGERROR("Cound not load texture"); return false; } XMLElement subTextureElem = rootElem.GetChild("SubTexture"); while (subTextureElem) { String name = subTextureElem.GetAttribute("name"); int x = subTextureElem.GetInt("x"); int y = subTextureElem.GetInt("y"); int width = subTextureElem.GetInt("width"); int height = subTextureElem.GetInt("height"); IntRect rectangle(x, y, x + width, y + height); Vector2 hotSpot(0.5f, 0.5f); if (subTextureElem.HasAttribute("frameWidth") && subTextureElem.HasAttribute("frameHeight")) { int frameX = subTextureElem.GetInt("frameX"); int frameY = subTextureElem.GetInt("frameY"); int frameWidth = subTextureElem.GetInt("frameWidth"); int frameHeight = subTextureElem.GetInt("frameHeight"); hotSpot.x_ = ((float)frameX + frameWidth / 2) / width; hotSpot.y_ = 1.0f - ((float)frameY + frameHeight / 2) / height; } DefineSprite(name, rectangle, hotSpot); subTextureElem = subTextureElem.GetNext("SubTexture"); } } else { LOGERROR("Invalid sprite sheet file"); return false; } return true; }
void WriteOutput(const String& outputFileName, bool exportAnimations, bool rotationsOnly, bool saveMaterialList) { // Begin serialization { File dest(context_); if (!dest.Open(outputFileName, FILE_WRITE)) ErrorExit("Could not open output file " + outputFileName); // ID dest.WriteFileID("UMDL"); // Vertexbuffers dest.WriteUInt(vertexBuffers_.Size()); for (unsigned i = 0; i < vertexBuffers_.Size(); ++i) vertexBuffers_[i].WriteData(dest); // Indexbuffers dest.WriteUInt(indexBuffers_.Size()); for (unsigned i = 0; i < indexBuffers_.Size(); ++i) indexBuffers_[i].WriteData(dest); // Subgeometries dest.WriteUInt(subGeometries_.Size()); for (unsigned i = 0; i < subGeometries_.Size(); ++i) { // Write bone mapping info from the first LOD level. It does not change for further LODs dest.WriteUInt(subGeometries_[i][0].boneMapping_.Size()); for (unsigned k = 0; k < subGeometries_[i][0].boneMapping_.Size(); ++k) dest.WriteUInt(subGeometries_[i][0].boneMapping_[k]); // Lod levels for this subgeometry dest.WriteUInt(subGeometries_[i].Size()); for (unsigned j = 0; j < subGeometries_[i].Size(); ++j) { dest.WriteFloat(subGeometries_[i][j].distance_); dest.WriteUInt((unsigned)subGeometries_[i][j].primitiveType_); dest.WriteUInt(subGeometries_[i][j].vertexBuffer_); dest.WriteUInt(subGeometries_[i][j].indexBuffer_); dest.WriteUInt(subGeometries_[i][j].indexStart_); dest.WriteUInt(subGeometries_[i][j].indexCount_); } } // Morphs dest.WriteUInt(morphs_.Size()); for (unsigned i = 0; i < morphs_.Size(); ++i) morphs_[i].WriteData(dest); // Skeleton dest.WriteUInt(bones_.Size()); for (unsigned i = 0; i < bones_.Size(); ++i) { dest.WriteString(bones_[i].name_); dest.WriteUInt(bones_[i].parentIndex_); dest.WriteVector3(bones_[i].bindPosition_); dest.WriteQuaternion(bones_[i].bindRotation_); dest.WriteVector3(bones_[i].bindScale_); Matrix3x4 offsetMatrix(bones_[i].derivedPosition_, bones_[i].derivedRotation_, bones_[i].derivedScale_); offsetMatrix = offsetMatrix.Inverse(); dest.Write(offsetMatrix.Data(), sizeof(Matrix3x4)); dest.WriteUByte(bones_[i].collisionMask_); if (bones_[i].collisionMask_ & 1) dest.WriteFloat(bones_[i].radius_); if (bones_[i].collisionMask_ & 2) dest.WriteBoundingBox(bones_[i].boundingBox_); } // Bounding box dest.WriteBoundingBox(boundingBox_); // Geometry centers for (unsigned i = 0; i < subGeometryCenters_.Size(); ++i) dest.WriteVector3(subGeometryCenters_[i]); } if (saveMaterialList) { String materialListName = ReplaceExtension(outputFileName, ".txt"); File listFile(context_); if (listFile.Open(materialListName, FILE_WRITE)) { for (unsigned i = 0; i < materialNames_.Size(); ++i) { // Assume the materials will be located inside the standard Materials subdirectory listFile.WriteLine("Materials/" + ReplaceExtension(SanitateAssetName(materialNames_[i]), ".xml")); } } else PrintLine("Warning: could not write material list file " + materialListName); } XMLElement skeletonRoot = skelFile_->GetRoot("skeleton"); if (skeletonRoot && exportAnimations) { // Go through animations XMLElement animationsRoot = skeletonRoot.GetChild("animations"); if (animationsRoot) { XMLElement animation = animationsRoot.GetChild("animation"); while (animation) { ModelAnimation newAnimation; newAnimation.name_ = animation.GetAttribute("name"); newAnimation.length_ = animation.GetFloat("length"); XMLElement tracksRoot = animation.GetChild("tracks"); XMLElement track = tracksRoot.GetChild("track"); while (track) { String trackName = track.GetAttribute("bone"); ModelBone* bone = 0; for (unsigned i = 0; i < bones_.Size(); ++i) { if (bones_[i].name_ == trackName) { bone = &bones_[i]; break; } } if (!bone) ErrorExit("Found animation track for unknown bone " + trackName); AnimationTrack newAnimationTrack; newAnimationTrack.name_ = trackName; if (!rotationsOnly) newAnimationTrack.channelMask_ = CHANNEL_POSITION | CHANNEL_ROTATION; else newAnimationTrack.channelMask_ = CHANNEL_ROTATION; XMLElement keyFramesRoot = track.GetChild("keyframes"); XMLElement keyFrame = keyFramesRoot.GetChild("keyframe"); while (keyFrame) { AnimationKeyFrame newKeyFrame; // Convert from right- to left-handed XMLElement position = keyFrame.GetChild("translate"); float x = position.GetFloat("x"); float y = position.GetFloat("y"); float z = position.GetFloat("z"); Vector3 pos(x, y, -z); XMLElement rotation = keyFrame.GetChild("rotate"); XMLElement axis = rotation.GetChild("axis"); float angle = -rotation.GetFloat("angle") * M_RADTODEG; x = axis.GetFloat("x"); y = axis.GetFloat("y"); z = axis.GetFloat("z"); Vector3 axisVec(x, y, -z); Quaternion rot(angle, axisVec); // Transform from bind-pose relative into absolute pos = bone->bindPosition_ + bone->bindRotation_ * pos; rot = bone->bindRotation_ * rot; newKeyFrame.time_ = keyFrame.GetFloat("time"); newKeyFrame.position_ = pos; newKeyFrame.rotation_ = rot; newAnimationTrack.keyFrames_.Push(newKeyFrame); keyFrame = keyFrame.GetNext("keyframe"); } // Make sure keyframes are sorted from beginning to end Sort(newAnimationTrack.keyFrames_.Begin(), newAnimationTrack.keyFrames_.End(), CompareKeyFrames); // Do not add tracks with no keyframes if (newAnimationTrack.keyFrames_.Size()) newAnimation.tracks_.Push(newAnimationTrack); track = track.GetNext("track"); } // Write each animation into a separate file String animationFileName = outputFileName.Replaced(".mdl", ""); animationFileName += "_" + newAnimation.name_ + ".ani"; File dest(context_); if (!dest.Open(animationFileName, FILE_WRITE)) ErrorExit("Could not open output file " + animationFileName); dest.WriteFileID("UANI"); dest.WriteString(newAnimation.name_); dest.WriteFloat(newAnimation.length_); dest.WriteUInt(newAnimation.tracks_.Size()); for (unsigned i = 0; i < newAnimation.tracks_.Size(); ++i) { AnimationTrack& track = newAnimation.tracks_[i]; dest.WriteString(track.name_); dest.WriteUByte(track.channelMask_); dest.WriteUInt(track.keyFrames_.Size()); for (unsigned j = 0; j < track.keyFrames_.Size(); ++j) { AnimationKeyFrame& keyFrame = track.keyFrames_[j]; dest.WriteFloat(keyFrame.time_); if (track.channelMask_ & CHANNEL_POSITION) dest.WriteVector3(keyFrame.position_); if (track.channelMask_ & CHANNEL_ROTATION) dest.WriteQuaternion(keyFrame.rotation_); if (track.channelMask_ & CHANNEL_SCALE) dest.WriteVector3(keyFrame.scale_); } } animation = animation.GetNext("animation"); PrintLine("Processed animation " + newAnimation.name_); } } } }
bool ParticleEffect::Load(const XMLElement& source) { // Reset to defaults first so that missing parameters in case of a live reload behave as expected material_.Reset(); numParticles_ = DEFAULT_NUM_PARTICLES; updateInvisible_ = false; relative_ = true; scaled_ = true; sorted_ = false; fixedScreenSize_ = false; animationLodBias_ = 0.0f; emitterType_ = EMITTER_SPHERE; emitterSize_ = Vector3::ZERO; directionMin_ = DEFAULT_DIRECTION_MIN; directionMax_ = DEFAULT_DIRECTION_MAX; constantForce_ = Vector3::ZERO; dampingForce_ = 0.0f; activeTime_ = 0.0f; inactiveTime_ = 0.0; emissionRateMin_ = DEFAULT_EMISSION_RATE; emissionRateMax_ = DEFAULT_EMISSION_RATE; sizeMin_ = DEFAULT_PARTICLE_SIZE; sizeMax_ = DEFAULT_PARTICLE_SIZE; timeToLiveMin_ = DEFAULT_TIME_TO_LIVE; timeToLiveMax_ = DEFAULT_TIME_TO_LIVE; velocityMin_ = DEFAULT_VELOCITY; velocityMax_ = DEFAULT_VELOCITY; rotationMin_ = 0.0f; rotationMax_ = 0.0f; rotationSpeedMin_ = 0.0f; rotationSpeedMax_ = 0.0f; sizeAdd_ = 0.0f; sizeMul_ = 1.0f; colorFrames_.Clear(); textureFrames_.Clear(); faceCameraMode_ = FC_ROTATE_XYZ; if (source.IsNull()) { URHO3D_LOGERROR("Can not load particle effect from null XML element"); return false; } if (source.HasChild("material")) { loadMaterialName_ = source.GetChild("material").GetAttribute("name"); // If async loading, can not GetResource() the material. But can do a background request for it if (GetAsyncLoadState() == ASYNC_LOADING) GetSubsystem<ResourceCache>()->BackgroundLoadResource<Material>(loadMaterialName_, true, this); } if (source.HasChild("numparticles")) SetNumParticles((unsigned)source.GetChild("numparticles").GetInt("value")); if (source.HasChild("updateinvisible")) updateInvisible_ = source.GetChild("updateinvisible").GetBool("enable"); if (source.HasChild("relative")) relative_ = source.GetChild("relative").GetBool("enable"); if (source.HasChild("scaled")) scaled_ = source.GetChild("scaled").GetBool("enable"); if (source.HasChild("sorted")) sorted_ = source.GetChild("sorted").GetBool("enable"); if (source.HasChild("fixedscreensize")) fixedScreenSize_ = source.GetChild("fixedscreensize").GetBool("enable"); if (source.HasChild("animlodbias")) SetAnimationLodBias(source.GetChild("animlodbias").GetFloat("value")); if (source.HasChild("emittertype")) { String type = source.GetChild("emittertype").GetAttributeLower("value"); if (type == "point") { // Point emitter type is deprecated, handled as zero sized sphere emitterType_ = EMITTER_SPHERE; emitterSize_ = Vector3::ZERO; } else emitterType_ = (EmitterType)GetStringListIndex(type.CString(), emitterTypeNames, EMITTER_SPHERE); } if (source.HasChild("emittersize")) emitterSize_ = source.GetChild("emittersize").GetVector3("value"); if (source.HasChild("emitterradius")) emitterSize_.x_ = emitterSize_.y_ = emitterSize_.z_ = source.GetChild("emitterradius").GetFloat("value"); if (source.HasChild("direction")) GetVector3MinMax(source.GetChild("direction"), directionMin_, directionMax_); if (source.HasChild("constantforce")) constantForce_ = source.GetChild("constantforce").GetVector3("value"); if (source.HasChild("dampingforce")) dampingForce_ = source.GetChild("dampingforce").GetFloat("value"); if (source.HasChild("activetime")) activeTime_ = source.GetChild("activetime").GetFloat("value"); if (activeTime_ < 0.0f) activeTime_ = M_INFINITY; if (source.HasChild("inactivetime")) inactiveTime_ = source.GetChild("inactivetime").GetFloat("value"); if (inactiveTime_ < 0.0f) inactiveTime_ = M_INFINITY; if (source.HasChild("emissionrate")) GetFloatMinMax(source.GetChild("emissionrate"), emissionRateMin_, emissionRateMax_); if (source.HasChild("interval")) { float intervalMin = 0.0f; float intervalMax = 0.0f; GetFloatMinMax(source.GetChild("interval"), intervalMin, intervalMax); emissionRateMax_ = 1.0f / intervalMin; emissionRateMin_ = 1.0f / intervalMax; } if (source.HasChild("particlesize")) GetVector2MinMax(source.GetChild("particlesize"), sizeMin_, sizeMax_); if (source.HasChild("timetolive")) GetFloatMinMax(source.GetChild("timetolive"), timeToLiveMin_, timeToLiveMax_); if (source.HasChild("velocity")) GetFloatMinMax(source.GetChild("velocity"), velocityMin_, velocityMax_); if (source.HasChild("rotation")) GetFloatMinMax(source.GetChild("rotation"), rotationMin_, rotationMax_); if (source.HasChild("rotationspeed")) GetFloatMinMax(source.GetChild("rotationspeed"), rotationSpeedMin_, rotationSpeedMax_); if (source.HasChild("faceCameraMode")) { String type = source.GetChild("faceCameraMode").GetAttributeLower("value"); faceCameraMode_ = (FaceCameraMode)GetStringListIndex(type.CString(), faceCameraModeNames, FC_ROTATE_XYZ); } if (source.HasChild("sizedelta")) { XMLElement deltaElem = source.GetChild("sizedelta"); if (deltaElem.HasAttribute("add")) sizeAdd_ = deltaElem.GetFloat("add"); if (deltaElem.HasAttribute("mul")) sizeMul_ = deltaElem.GetFloat("mul"); } if (source.HasChild("color")) { ColorFrame colorFrame(source.GetChild("color").GetColor("value")); SetColorFrame(0, colorFrame); } if (source.HasChild("colorfade")) { Vector<ColorFrame> fades; for (XMLElement colorFadeElem = source.GetChild("colorfade"); colorFadeElem; colorFadeElem = colorFadeElem.GetNext("colorfade")) fades.Push(ColorFrame(colorFadeElem.GetColor("color"), colorFadeElem.GetFloat("time"))); SetColorFrames(fades); } if (colorFrames_.Empty()) colorFrames_.Push(ColorFrame(Color::WHITE)); if (source.HasChild("texanim")) { Vector<TextureFrame> animations; for (XMLElement animElem = source.GetChild("texanim"); animElem; animElem = animElem.GetNext("texanim")) { TextureFrame animation; animation.uv_ = animElem.GetRect("uv"); animation.time_ = animElem.GetFloat("time"); animations.Push(animation); } SetTextureFrames(animations); } return true; }
bool Technique::BeginLoad(Deserializer& source) { passes_.Clear(); SetMemoryUse(sizeof(Technique)); SharedPtr<XMLFile> xml(new XMLFile(context_)); if (!xml->Load(source)) return false; XMLElement rootElem = xml->GetRoot(); if (rootElem.HasAttribute("sm3")) isSM3_ = rootElem.GetBool("sm3"); String globalVS = rootElem.GetAttribute("vs"); String globalPS = rootElem.GetAttribute("ps"); String globalVSDefines = rootElem.GetAttribute("vsdefines"); String globalPSDefines = rootElem.GetAttribute("psdefines"); // End with space so that the pass-specific defines can be appended if (!globalVSDefines.Empty()) globalVSDefines += ' '; if (!globalPSDefines.Empty()) globalPSDefines += ' '; bool globalAlphaMask = false; if (rootElem.HasAttribute("alphamask")) globalAlphaMask = rootElem.GetBool("alphamask"); unsigned numPasses = 0; XMLElement passElem = rootElem.GetChild("pass"); while (passElem) { if (passElem.HasAttribute("name")) { StringHash nameHash(passElem.GetAttribute("name")); Pass* newPass = CreatePass(nameHash); ++numPasses; if (passElem.HasAttribute("sm3")) newPass->SetIsSM3(passElem.GetBool("sm3")); // Append global defines only when pass does not redefine the shader if (passElem.HasAttribute("vs")) { newPass->SetVertexShader(passElem.GetAttribute("vs")); newPass->SetVertexShaderDefines(passElem.GetAttribute("vsdefines")); } else { newPass->SetVertexShader(globalVS); newPass->SetVertexShaderDefines(globalVSDefines + passElem.GetAttribute("vsdefines")); } if (passElem.HasAttribute("ps")) { newPass->SetPixelShader(passElem.GetAttribute("ps")); newPass->SetPixelShaderDefines(passElem.GetAttribute("psdefines")); } else { newPass->SetPixelShader(globalPS); newPass->SetPixelShaderDefines(globalPSDefines + passElem.GetAttribute("psdefines")); } if (passElem.HasAttribute("lighting")) { String lighting = passElem.GetAttributeLower("lighting"); newPass->SetLightingMode((PassLightingMode)GetStringListIndex(lighting.CString(), lightingModeNames, LIGHTING_UNLIT)); } if (passElem.HasAttribute("blend")) { String blend = passElem.GetAttributeLower("blend"); newPass->SetBlendMode((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE)); } if (passElem.HasAttribute("depthtest")) { String depthTest = passElem.GetAttributeLower("depthtest"); if (depthTest == "false") newPass->SetDepthTestMode(CMP_ALWAYS); else newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest.CString(), compareModeNames, CMP_LESS)); } if (passElem.HasAttribute("depthwrite")) newPass->SetDepthWrite(passElem.GetBool("depthwrite")); if (passElem.HasAttribute("alphamask")) newPass->SetAlphaMask(passElem.GetBool("alphamask")); else newPass->SetAlphaMask(globalAlphaMask); } else LOGERROR("Missing pass name"); passElem = passElem.GetNext("pass"); } // Calculate memory use now SetMemoryUse(sizeof(Technique) + numPasses * sizeof(Pass)); return true; }
bool Animation::BeginLoad(Deserializer& source) { unsigned memoryUse = sizeof(Animation); // Check ID if (source.ReadFileID() != "UANI") { URHO3D_LOGERROR(source.GetName() + " is not a valid animation file"); return false; } // Read name and length animationName_ = source.ReadString(); animationNameHash_ = animationName_; length_ = source.ReadFloat(); tracks_.Clear(); unsigned tracks = source.ReadUInt(); memoryUse += tracks * sizeof(AnimationTrack); // Read tracks for (unsigned i = 0; i < tracks; ++i) { AnimationTrack* newTrack = CreateTrack(source.ReadString()); newTrack->channelMask_ = source.ReadUByte(); unsigned keyFrames = source.ReadUInt(); newTrack->keyFrames_.Resize(keyFrames); memoryUse += keyFrames * sizeof(AnimationKeyFrame); // Read keyframes of the track for (unsigned j = 0; j < keyFrames; ++j) { AnimationKeyFrame& newKeyFrame = newTrack->keyFrames_[j]; newKeyFrame.time_ = source.ReadFloat(); if (newTrack->channelMask_ & CHANNEL_POSITION) newKeyFrame.position_ = source.ReadVector3(); if (newTrack->channelMask_ & CHANNEL_ROTATION) newKeyFrame.rotation_ = source.ReadQuaternion(); if (newTrack->channelMask_ & CHANNEL_SCALE) newKeyFrame.scale_ = source.ReadVector3(); } } // Optionally read triggers from an XML file ResourceCache* cache = GetSubsystem<ResourceCache>(); String xmlName = ReplaceExtension(GetName(), ".xml"); SharedPtr<XMLFile> file(cache->GetTempResource<XMLFile>(xmlName, false)); if (file) { XMLElement rootElem = file->GetRoot(); XMLElement triggerElem = rootElem.GetChild("trigger"); while (triggerElem) { if (triggerElem.HasAttribute("normalizedtime")) AddTrigger(triggerElem.GetFloat("normalizedtime"), true, triggerElem.GetVariant()); else if (triggerElem.HasAttribute("time")) AddTrigger(triggerElem.GetFloat("time"), false, triggerElem.GetVariant()); triggerElem = triggerElem.GetNext("trigger"); } memoryUse += triggers_.Size() * sizeof(AnimationTriggerPoint); SetMemoryUse(memoryUse); return true; } // Optionally read triggers from a JSON file String jsonName = ReplaceExtension(GetName(), ".json"); SharedPtr<JSONFile> jsonFile(cache->GetTempResource<JSONFile>(jsonName, false)); if (jsonFile) { const JSONValue& rootVal = jsonFile->GetRoot(); JSONArray triggerArray = rootVal.Get("triggers").GetArray(); for (unsigned i = 0; i < triggerArray.Size(); i++) { const JSONValue& triggerValue = triggerArray.At(i); JSONValue normalizedTimeValue = triggerValue.Get("normalizedTime"); if (!normalizedTimeValue.IsNull()) AddTrigger(normalizedTimeValue.GetFloat(), true, triggerValue.GetVariant()); else { JSONValue timeVal = triggerValue.Get("time"); if (!timeVal.IsNull()) AddTrigger(timeVal.GetFloat(), false, triggerValue.GetVariant()); } } memoryUse += triggers_.Size() * sizeof(AnimationTriggerPoint); SetMemoryUse(memoryUse); return true; } SetMemoryUse(memoryUse); return true; }
bool TextureCube::BeginLoad(Deserializer& source) { ResourceCache* cache = context_->resourceCache(); // In headless mode, do not actually load the texture, just return success if (!graphics_) return true; // If device is lost, retry later if (graphics_->IsDeviceLost()) { URHO3D_LOGWARNING("Texture load while device is lost"); dataPending_ = true; return true; } cache->ResetDependencies(this); QString texPath, texName, texExt; SplitPath(GetName(), texPath, texName, texExt); loadParameters_ = (new XMLFile(context_)); if (!loadParameters_->Load(source)) { loadParameters_.Reset(); return false; } loadImages_.clear(); XMLElement textureElem = loadParameters_->GetRoot(); XMLElement imageElem = textureElem.GetChild("image"); // Single image and multiple faces with layout if (imageElem) { QString name = imageElem.GetAttribute("name"); // If path is empty, add the XML file path if (GetPath(name).isEmpty()) name = texPath + name; SharedPtr<Image> image = cache->GetTempResource<Image>(name); if (!image) return false; int faceWidth, faceHeight; loadImages_.resize(MAX_CUBEMAP_FACES); if (image->IsCubemap()) { loadImages_[FACE_POSITIVE_X] = image; loadImages_[FACE_NEGATIVE_X] = loadImages_[FACE_POSITIVE_X]->GetNextSibling(); loadImages_[FACE_POSITIVE_Y] = loadImages_[FACE_NEGATIVE_X]->GetNextSibling(); loadImages_[FACE_NEGATIVE_Y] = loadImages_[FACE_POSITIVE_Y]->GetNextSibling(); loadImages_[FACE_POSITIVE_Z] = loadImages_[FACE_NEGATIVE_Y]->GetNextSibling(); loadImages_[FACE_NEGATIVE_Z] = loadImages_[FACE_POSITIVE_Z]->GetNextSibling(); } else { CubeMapLayout layout = (CubeMapLayout)GetStringListIndex(imageElem.GetAttribute("layout"), cubeMapLayoutNames, CML_HORIZONTAL); switch (layout) { case CML_HORIZONTAL: faceWidth = image->GetWidth() / MAX_CUBEMAP_FACES; faceHeight = image->GetHeight(); loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 0, 0, faceWidth, faceHeight); loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 1, 0, faceWidth, faceHeight); loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 2, 0, faceWidth, faceHeight); loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 3, 0, faceWidth, faceHeight); loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 4, 0, faceWidth, faceHeight); loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 5, 0, faceWidth, faceHeight); break; case CML_HORIZONTALNVIDIA: faceWidth = image->GetWidth() / MAX_CUBEMAP_FACES; faceHeight = image->GetHeight(); for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i) loadImages_[i] = GetTileImage(image, i, 0, faceWidth, faceHeight); break; case CML_HORIZONTALCROSS: faceWidth = image->GetWidth() / 4; faceHeight = image->GetHeight() / 3; loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 0, faceWidth, faceHeight); loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 1, faceWidth, faceHeight); loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 1, 1, faceWidth, faceHeight); loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 1, faceWidth, faceHeight); loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 3, 1, faceWidth, faceHeight); loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 1, 2, faceWidth, faceHeight); break; case CML_VERTICALCROSS: faceWidth = image->GetWidth() / 3; faceHeight = image->GetHeight() / 4; loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 0, faceWidth, faceHeight); loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 1, faceWidth, faceHeight); loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 1, 1, faceWidth, faceHeight); loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 1, faceWidth, faceHeight); loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 1, 2, faceWidth, faceHeight); loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 3, faceWidth, faceHeight); if (loadImages_[FACE_NEGATIVE_Z]) { loadImages_[FACE_NEGATIVE_Z]->FlipVertical(); loadImages_[FACE_NEGATIVE_Z]->FlipHorizontal(); } break; case CML_BLENDER: faceWidth = image->GetWidth() / 3; faceHeight = image->GetHeight() / 2; loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 0, faceWidth, faceHeight); loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 0, faceWidth, faceHeight); loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 0, faceWidth, faceHeight); loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 0, 1, faceWidth, faceHeight); loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 1, faceWidth, faceHeight); loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 2, 1, faceWidth, faceHeight); break; } } } // Face per image else { XMLElement faceElem = textureElem.GetChild("face"); while (faceElem) { QString name = faceElem.GetAttribute("name"); // If path is empty, add the XML file path if (GetPath(name).isEmpty()) name = texPath + name; loadImages_.push_back(cache->GetTempResource<Image>(name)); cache->StoreResourceDependency(this, name); faceElem = faceElem.GetNext("face"); } } // Precalculate mip levels if async loading if (GetAsyncLoadState() == ASYNC_LOADING) { for (unsigned i = 0; i < loadImages_.size(); ++i) { if (loadImages_[i]) loadImages_[i]->PrecalculateLevels(); } } return true; }
bool Technique::BeginLoad(Deserializer& source) { passes_.clear(); cloneTechniques_.clear(); SetMemoryUse(sizeof(Technique)); ea::shared_ptr<XMLFile> xml(context_->CreateObject<XMLFile>()); if (!xml->Load(source)) return false; XMLElement rootElem = xml->GetRoot(); if (rootElem.HasAttribute("desktop")) isDesktop_ = rootElem.GetBool("desktop"); ea::string globalVS = rootElem.GetAttribute("vs"); ea::string globalPS = rootElem.GetAttribute("ps"); ea::string globalVSDefines = rootElem.GetAttribute("vsdefines"); ea::string globalPSDefines = rootElem.GetAttribute("psdefines"); // End with space so that the pass-specific defines can be appended if (!globalVSDefines.empty()) globalVSDefines += ' '; if (!globalPSDefines.empty()) globalPSDefines += ' '; XMLElement passElem = rootElem.GetChild("pass"); while (passElem) { if (passElem.HasAttribute("name")) { Pass* newPass = CreatePass(passElem.GetAttribute("name")); if (passElem.HasAttribute("desktop")) newPass->SetIsDesktop(passElem.GetBool("desktop")); // Append global defines only when pass does not redefine the shader if (passElem.HasAttribute("vs")) { newPass->SetVertexShader(passElem.GetAttribute("vs")); newPass->SetVertexShaderDefines(passElem.GetAttribute("vsdefines")); } else { newPass->SetVertexShader(globalVS); newPass->SetVertexShaderDefines(globalVSDefines + passElem.GetAttribute("vsdefines")); } if (passElem.HasAttribute("ps")) { newPass->SetPixelShader(passElem.GetAttribute("ps")); newPass->SetPixelShaderDefines(passElem.GetAttribute("psdefines")); } else { newPass->SetPixelShader(globalPS); newPass->SetPixelShaderDefines(globalPSDefines + passElem.GetAttribute("psdefines")); } newPass->SetVertexShaderDefineExcludes(passElem.GetAttribute("vsexcludes")); newPass->SetPixelShaderDefineExcludes(passElem.GetAttribute("psexcludes")); if (passElem.HasAttribute("lighting")) { ea::string lighting = passElem.GetAttributeLower("lighting"); newPass->SetLightingMode((PassLightingMode)GetStringListIndex(lighting.c_str(), lightingModeNames, LIGHTING_UNLIT)); } if (passElem.HasAttribute("blend")) { ea::string blend = passElem.GetAttributeLower("blend"); newPass->SetBlendMode((BlendMode)GetStringListIndex(blend.c_str(), blendModeNames, BLEND_REPLACE)); } if (passElem.HasAttribute("cull")) { ea::string cull = passElem.GetAttributeLower("cull"); newPass->SetCullMode((CullMode)GetStringListIndex(cull.c_str(), cullModeNames, MAX_CULLMODES)); } if (passElem.HasAttribute("depthtest")) { ea::string depthTest = passElem.GetAttributeLower("depthtest"); if (depthTest == "false") newPass->SetDepthTestMode(CMP_ALWAYS); else newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest.c_str(), compareModeNames, CMP_LESS)); } if (passElem.HasAttribute("depthwrite")) newPass->SetDepthWrite(passElem.GetBool("depthwrite")); if (passElem.HasAttribute("alphatocoverage")) newPass->SetAlphaToCoverage(passElem.GetBool("alphatocoverage")); } else URHO3D_LOGERROR("Missing pass name"); passElem = passElem.GetNext("pass"); } return true; }
void PropertySet2D::Load(const XMLElement& element) { assert(element.GetName() == "properties"); for (XMLElement propertyElem = element.GetChild("property"); propertyElem; propertyElem = propertyElem.GetNext("property")) nameToValueMapping_[propertyElem.GetAttribute("name")] = propertyElem.GetAttribute("value"); }
bool Menu::LoadXML(const XMLElement& source, XMLFile* styleFile) { // Apply the style first, but only for non-internal elements if (!internal_ && styleFile) { // Use style override if defined, otherwise type name String styleName = source.GetAttribute("style"); if (styleName.Empty()) styleName = GetTypeName(); SetStyle(styleFile, styleName); } // Then load rest of the attributes from the source if (!Serializable::LoadXML(source)) return false; unsigned nextInternalChild = 0; // Load child elements. Internal elements are not to be created as they already exist XMLElement childElem = source.GetChild("element"); while (childElem) { bool internalElem = childElem.GetBool("internal"); bool popupElem = childElem.GetBool("popup"); String typeName = childElem.GetAttribute("type"); if (typeName.Empty()) typeName = "UIElement"; UIElement* child = 0; if (!internalElem) { if (!popupElem) child = CreateChild(ShortStringHash(typeName)); else { // Do not add the popup element as a child even temporarily, as that can break layouts SharedPtr<UIElement> popup = DynamicCast<UIElement>(context_->CreateObject(ShortStringHash(typeName))); if (!popup) LOGERROR("Could not create popup element type " + ShortStringHash(typeName).ToString()); else { child = popup; SetPopup(popup); } } } else { // An internal popup element should already exist if (popupElem) child = popup_; else { for (unsigned i = nextInternalChild; i < children_.Size(); ++i) { if (children_[i]->IsInternal() && children_[i]->GetTypeName() == typeName) { child = children_[i]; nextInternalChild = i + 1; break; } } if (!child) LOGWARNING("Could not find matching internal child element of type " + typeName + " in " + GetTypeName()); } } if (child) { if (!child->LoadXML(childElem, styleFile)) return false; } childElem = childElem.GetNext("element"); } ApplyAttributes(); return true; }
bool ParticleEmitter::Load(XMLFile* file) { if (!file) { LOGERROR("Null particle emitter parameter file"); return false; } XMLElement rootElem = file->GetRoot(); if (!rootElem) { LOGERROR("Particle emitter parameter file does not have a valid root element"); return false; } if (rootElem.HasChild("material")) SetMaterial(GetSubsystem<ResourceCache>()->GetResource<Material>(rootElem.GetChild("material").GetAttribute("name"))); if (rootElem.HasChild("numparticles")) SetNumParticles(rootElem.GetChild("numparticles").GetInt("value")); if (rootElem.HasChild("updateinvisible")) updateInvisible_ = rootElem.GetChild("updateinvisible").GetBool("enable"); if (rootElem.HasChild("relative")) relative_ = rootElem.GetChild("relative").GetBool("enable"); if (rootElem.HasChild("scaled")) scaled_ = rootElem.GetChild("scaled").GetBool("enable"); if (rootElem.HasChild("sorted")) sorted_ = rootElem.GetChild("sorted").GetBool("enable"); if (rootElem.HasChild("animlodbias")) SetAnimationLodBias(rootElem.GetChild("relative").GetFloat("value")); if (rootElem.HasChild("emittertype")) { String type = rootElem.GetChild("emittertype").GetAttributeLower("value"); if (type == "point") { // Point emitter type is deprecated, handled as zero sized sphere emitterType_ = EMITTER_SPHERE; emitterSize_ = Vector3::ZERO; } else if (type == "box") emitterType_ = EMITTER_BOX; else if (type == "sphere") emitterType_ = EMITTER_SPHERE; else LOGERROR("Unknown particle emitter type " + type); } if (rootElem.HasChild("emittersize")) emitterSize_ = rootElem.GetChild("emittersize").GetVector3("value"); if (rootElem.HasChild("emitterradius")) emitterSize_.x_ = emitterSize_.y_ = emitterSize_.z_ = rootElem.GetChild("emitterradius").GetFloat("value"); if (rootElem.HasChild("direction")) GetVector3MinMax(rootElem.GetChild("direction"), directionMin_, directionMax_); if (rootElem.HasChild("constantforce")) constantForce_ = rootElem.GetChild("constantforce").GetVector3("value"); if (rootElem.HasChild("dampingforce")) dampingForce_ = rootElem.GetChild("dampingforce").GetFloat("value"); if (rootElem.HasChild("activetime")) activeTime_ = rootElem.GetChild("activetime").GetFloat("value"); if (activeTime_ < 0.0f) activeTime_ = M_INFINITY; if (rootElem.HasChild("inactivetime")) inactiveTime_ = rootElem.GetChild("inactivetime").GetFloat("value"); if (inactiveTime_ < 0.0f) inactiveTime_ = M_INFINITY; if (rootElem.HasChild("emissionrate")) GetFloatMinMax(rootElem.GetChild("emissionrate"), emissionRateMin_, emissionRateMax_); if (rootElem.HasChild("interval")) { float intervalMin = 0.0f; float intervalMax = 0.0f; GetFloatMinMax(rootElem.GetChild("interval"), intervalMin, intervalMax); emissionRateMax_ = 1.0f / intervalMin; emissionRateMin_ = 1.0f / intervalMax; } if (rootElem.HasChild("particlesize")) GetVector2MinMax(rootElem.GetChild("particlesize"), sizeMin_, sizeMax_); if (rootElem.HasChild("timetolive")) GetFloatMinMax(rootElem.GetChild("timetolive"), timeToLiveMin_, timeToLiveMax_); if (rootElem.HasChild("velocity")) GetFloatMinMax(rootElem.GetChild("velocity"), velocityMin_, velocityMax_); if (rootElem.HasChild("rotation")) GetFloatMinMax(rootElem.GetChild("rotation"), rotationMin_, rotationMax_); if (rootElem.HasChild("rotationspeed")) GetFloatMinMax(rootElem.GetChild("rotationspeed"), rotationSpeedMin_, rotationSpeedMax_); if (rootElem.HasChild("sizedelta")) { XMLElement deltaElem = rootElem.GetChild("sizedelta"); if (deltaElem.HasAttribute("add")) sizeAdd_ = deltaElem.GetFloat("add"); if (deltaElem.HasAttribute("mul")) sizeMul_ = deltaElem.GetFloat("mul"); } if (rootElem.HasChild("color")) SetColor(rootElem.GetChild("color").GetColor("value")); if (rootElem.HasChild("colorfade")) { Vector<ColorFrame> fades; XMLElement colorFadeElem = rootElem.GetChild("colorfade"); while (colorFadeElem) { fades.Push(ColorFrame(colorFadeElem.GetColor("color"), colorFadeElem.GetFloat("time"))); colorFadeElem = colorFadeElem.GetNext("colorfade"); } SetColors(fades); } if (rootElem.HasChild("texanim")) { Vector<TextureFrame> animations; XMLElement animElem = rootElem.GetChild("texanim"); while (animElem) { TextureFrame animation; animation.uv_ = animElem.GetRect("uv"); animation.time_ = animElem.GetFloat("time"); animations.Push(animation); animElem = animElem.GetNext("texanim"); } textureFrames_ = animations; } MarkNetworkUpdate(); return true; }
void RenderPathCommand::Load(const XMLElement& element) { type_ = (RenderCommandType)GetStringListIndex(element.GetAttributeLower("type").CString(), commandTypeNames, CMD_NONE); tag_ = element.GetAttribute("tag"); if (element.HasAttribute("enabled")) enabled_ = element.GetBool("enabled"); if (element.HasAttribute("metadata")) metadata_ = element.GetAttribute("metadata"); switch (type_) { case CMD_CLEAR: if (element.HasAttribute("color")) { clearFlags_ |= CLEAR_COLOR; if (element.GetAttributeLower("color") == "fog") useFogColor_ = true; else clearColor_ = element.GetColor("color"); } if (element.HasAttribute("depth")) { clearFlags_ |= CLEAR_DEPTH; clearDepth_ = element.GetFloat("depth"); } if (element.HasAttribute("stencil")) { clearFlags_ |= CLEAR_STENCIL; clearStencil_ = (unsigned)element.GetInt("stencil"); } break; case CMD_SCENEPASS: pass_ = element.GetAttribute("pass"); sortMode_ = (RenderCommandSortMode)GetStringListIndex(element.GetAttributeLower("sort").CString(), sortModeNames, SORT_FRONTTOBACK); if (element.HasAttribute("marktostencil")) markToStencil_ = element.GetBool("marktostencil"); if (element.HasAttribute("vertexlights")) vertexLights_ = element.GetBool("vertexlights"); break; case CMD_FORWARDLIGHTS: pass_ = element.GetAttribute("pass"); if (element.HasAttribute("uselitbase")) useLitBase_ = element.GetBool("uselitbase"); break; case CMD_LIGHTVOLUMES: case CMD_QUAD: vertexShaderName_ = element.GetAttribute("vs"); pixelShaderName_ = element.GetAttribute("ps"); vertexShaderDefines_ = element.GetAttribute("vsdefines"); pixelShaderDefines_ = element.GetAttribute("psdefines"); if (type_ == CMD_QUAD) { if (element.HasAttribute("blend")) { String blend = element.GetAttributeLower("blend"); blendMode_ = ((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE)); } XMLElement parameterElem = element.GetChild("parameter"); while (parameterElem) { String name = parameterElem.GetAttribute("name"); shaderParameters_[name] = Material::ParseShaderParameterValue(parameterElem.GetAttribute("value")); parameterElem = parameterElem.GetNext("parameter"); } } break; default: break; } // By default use 1 output, which is the viewport outputs_.Resize(1); outputs_[0] = MakePair(String("viewport"), FACE_POSITIVE_X); if (element.HasAttribute("output")) outputs_[0].first_ = element.GetAttribute("output"); if (element.HasAttribute("face")) outputs_[0].second_ = (CubeMapFace)element.GetInt("face"); if (element.HasAttribute("depthstencil")) depthStencilName_ = element.GetAttribute("depthstencil"); // Check for defining multiple outputs XMLElement outputElem = element.GetChild("output"); while (outputElem) { unsigned index = (unsigned)outputElem.GetInt("index"); if (index < MAX_RENDERTARGETS) { if (index >= outputs_.Size()) outputs_.Resize(index + 1); outputs_[index].first_ = outputElem.GetAttribute("name"); outputs_[index].second_ = outputElem.HasAttribute("face") ? (CubeMapFace)outputElem.GetInt("face") : FACE_POSITIVE_X; } outputElem = outputElem.GetNext("output"); } XMLElement textureElem = element.GetChild("texture"); while (textureElem) { TextureUnit unit = TU_DIFFUSE; if (textureElem.HasAttribute("unit")) unit = ParseTextureUnitName(textureElem.GetAttribute("unit")); if (unit < MAX_TEXTURE_UNITS) { String name = textureElem.GetAttribute("name"); textureNames_[unit] = name; } textureElem = textureElem.GetNext("texture"); } }