diff --git a/node.cpp b/node.cpp index d57cd3e..3560121 100755 --- a/node.cpp +++ b/node.cpp @@ -1,19 +1,24 @@ #include "root.h" #include "bone.h" #include "mesh.h" -#include "particleEmitter.h" -#include "light.h" #include "text.h" -#include "material.h" +#include "light.h" #include "matrix.h" +#include "material.h" #include "skeleton.h" +#include "shaderAsset.h" +#include "particleEmitter.h" +#include "assetManager.h" #include "animationController.h" +#include + #include #include using namespace std; using namespace glm; +using namespace boost; namespace vb01{ Node::Node(Vector3 pos, Quaternion orientation, Vector3 scale, string name, Animatable::Type type) : Animatable(type, name){ @@ -245,6 +250,7 @@ namespace vb01{ lights.push_back(light); light->onAttached(this); updateShaders(); + } void Node::removeLight(int id){ @@ -439,27 +445,23 @@ namespace vb01{ void Node::updateShaders(){ Root *root = Root::getSingleton(); + AssetManager *am = AssetManager::getSingleton(); + ShaderAsset *sa = (ShaderAsset*)am->getAsset(root->getLibPath() + "texture.frag"); + string shaderStr = sa->shaderString; + int numLights = root->getNumLights(); - Node *rootNode = root->getRootNode(); - vector descendants; - rootNode->getDescendants(descendants); - descendants.push_back(rootNode); + string str1 = "const int numLights = " + to_string(numLights > 0 ? numLights : 1) + ";"; + int a1 = std::distance(shaderStr.begin(), find_nth(shaderStr, "\n", 0).begin()); + int a2 = std::distance(shaderStr.begin(), find_nth(shaderStr, "\n", 1).begin()); + shaderStr.replace(a1 + 1, a2 - a1 - 1, str1); - for(Node *n : descendants){ - vector meshes = n->getMeshes(); + string str2 = "const bool checkLights = " + string(numLights > 0 ? "true" : "false") + ";"; + a1 = std::distance(shaderStr.begin(), find_nth(shaderStr, "\n", 1).begin()); + a2 = std::distance(shaderStr.begin(), find_nth(shaderStr, "\n", 2).begin()); + shaderStr.replace(a1 + 1, a2 - a1 - 1, str2); - for(Mesh *m : meshes){ - Material *mat = m->getMaterial(); - - if(mat){ - int numLights = root->getNumLights(); - string str1 = "const int numLights = " + to_string(numLights > 0 ? numLights : 1) + ";"; - mat->getShader()->editShader(Shader::FRAGMENT_SHADER, 1, str1); - string str2 = "const bool checkLights = " + string(numLights > 0 ? "true" : "false") + ";"; - mat->getShader()->editShader(Shader::FRAGMENT_SHADER, 2, str2); - } - } - } + ShaderAsset sa2(sa->path, shaderStr); + am->editAsset(sa->path, sa2); } void Node::setOrientation(Quaternion q){