Material update, before changing prefabs

This commit is contained in:
2023-01-19 19:29:01 -08:00
parent 985218b9bc
commit e60002b8dc
25 changed files with 439 additions and 716 deletions

View File

@ -10,61 +10,4 @@
using namespace Dawn;
Material::Material(SceneItem *item) : SceneItemComponent(item) {
this->shader = item->scene->game->renderManager.getDefaultShader();
this->updateShaderParameters();
}
void Material::updateShaderParameters() {
this->colorValues.clear();
this->boolValues.clear();
this->parameters = this->shader->getParameters();
this->shader->setDefaultParameters(this);
// We do need to validate these params at some point to make sure that the
// shader has actually bound them.
}
void Material::setShaderParameters() {
auto it = this->parameters.begin();
while(it != this->parameters.end()) {
switch(it->second) {
case SHADER_PARAMETER_TYPE_COLOR:
this->shader->setColor(it->first, this->colorValues[it->first]);
break;
case SHADER_PARAMETER_TYPE_MATRIX:
this->shader->setMatrix(it->first, this->matrixValues[it->first]);
break;
case SHADER_PARAMETER_TYPE_BOOLEAN:
this->shader->setBoolean(it->first, this->boolValues[it->first]);
break;
case SHADER_PARAMETER_TYPE_VECTOR3:
this->shader->setVector3(it->first, this->vec3Values[it->first]);
break;
case SHADER_PARAMETER_TYPE_TEXTURE:
this->shader->setTexture(it->first, this->textureValues[it->first]);
break;
case SHADER_PARAMETER_TYPE_FLOAT:
this->shader->setFloat(it->first, this->floatValues[it->first]);
break;
default:
throw "An unsupported or invalid shader parameter type was supplied.";
}
++it;
}
}
Shader * Material::getShader() {
return this->shader;
}
void Material::setShader(Shader * shader) {
this->shader = shader;
this->updateShaderParameters();
}

View File

@ -8,56 +8,20 @@
#include "display/shader/Shader.hpp"
namespace Dawn {
class Shader;
class Material : public SceneItemComponent {
private:
Shader *shader = nullptr;
/**
* Internal method that will be invoked to go through and update all of
* the shader parameters whenever the shader is swapped out.
*/
void updateShaderParameters();
public:
std::map<shaderparameter_t, enum ShaderParameterType> parameters;
std::map<shaderparameter_t, struct Color> colorValues;
std::map<shaderparameter_t, bool_t> boolValues;
std::map<shaderparameter_t, glm::mat4> matrixValues;
std::map<shaderparameter_t, glm::vec3> vec3Values;
std::map<shaderparameter_t, Texture*> textureValues;
std::map<shaderparameter_t, float_t> floatValues;
/**
* Material component constructor.
*
* @param item Scene Item this component belongs to.
*/
Material(SceneItem *item);
/**
* Return the shader this material is currently using.
* Returns the shader that this material uses.
*
* @return Shader pointer to the currently bound shader.
* @return Shader that belongs to this material.
*/
Shader * getShader();
/**
* Sets the shader for the material to use. This will also clear and
* update all of the parameters from the shaders' default parameter list.
*
* @param shader Shader to set.
*/
void setShader(Shader * shader);
/**
* Protected method that can be called, likely by the render pipeline, to
* set and update all of the shader parameters from this material on to
* the shader.
*
* This method assumes that the shader has already been bound.
*/
void setShaderParameters();
virtual Shader * getShader() = 0;
};
}

View File

@ -1,60 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/components/display/Material.hpp"
#include "scene/SceneItem.hpp"
namespace Dawn {
template<class T>
class ShaderInterface : public SceneItemComponent {
public:
/**
* ShaderInterface to provide a common interface language for all shaders
* that can be referenced by scene components.
*
* @param i SceneItem this Shader interface belongs to.
*/
ShaderInterface(SceneItem *i) : SceneItemComponent(i) {}
/**
* Sets the shader for this shader interface to use. Will also update the
* underlying material for you.
*
* @param shader Shader to use for this interface.
*/
void setShader(T *shader) {
assertNotNull(shader);
this->getMaterial()->setShader(shader);
}
/**
* Returns the shader, assuming that the material has the correct shader.
*
* @return Pointer to the shader.
*/
T * getShader() {
auto material = dynamic_cast<T*>(this->getMaterial()->getShader());
return material;
}
/**
* Returns the material attached to this scene item.
*
* @return The attached material.
*/
Material * getMaterial() {
auto mat = this->item->getComponent<Material>();
assertNotNull(mat);
return mat;
}
std::vector<SceneItemComponent*> getDependencies() override {
return std::vector<SceneItemComponent*>{
this->item->getComponent<Material>()
};
}
};
}