using new way to edit shaders

This commit is contained in:
devZoGok
2024-09-08 14:24:44 +03:00
parent 63429dacbe
commit 39dc1afd25
+23 -21
View File
@@ -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 <boost/algorithm/string/find.hpp>
#include <glm.hpp>
#include <glm/gtc/matrix_inverse.hpp>
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<Node*> 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<Mesh*> 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){