// Copyright (c) 2023 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #include "SimpleBillboardedShader.hpp" #include "scene/components/display/mesh/MeshRenderer.hpp" #include "scene/components/display/Camera.hpp" using namespace Dawn; void SimpleBillboardedShaderProgram::compile() { #if DAWN_OPENGL_GLSL this->compileShader( { { "aPos", 0 }, { "aTexCoord", 1 } }, // Vertex Shader "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" "layout (location = 1) in vec2 aTexCoord;\n" "uniform mat4 u_Proj;\n" "uniform mat4 u_View;\n" "uniform mat4 u_Model;\n" "out vec2 o_TextCoord;\n" "void main() {\n" "vec3 camRight = vec3(u_View[0][0], u_View[1][0], u_View[2][0]);\n" "vec3 camUp = vec3(u_View[0][1], u_View[1][1], u_View[2][1]);\n" "vec3 billboardPos = u_View[3].xyz + aPos.x * camRight + aPos.y * camUp;\n" "gl_Position = u_Proj * u_View * u_Model * vec4(billboardPos, 1.0);\n" "o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n" "}", // Fragment Shader "#version 330 core\n" "in vec2 o_TextCoord;\n" "out vec4 o_Color;\n" "uniform vec4 u_Color;\n" "uniform bool u_HasTexture;\n" "uniform sampler2D u_Text;\n" "void main() {\n" "if(u_HasTexture) {\n" "o_Color = texture(u_Text, o_TextCoord) * u_Color;\n" "} else {\n" "o_Color = u_Color;" "}\n" "}\n" ); #endif this->paramProjection = this->getParameterByName("u_Proj"); this->paramView = this->getParameterByName("u_View"); this->paramModel = this->getParameterByName("u_Model"); this->paramColor = this->getParameterByName("u_Color"); this->paramTexture = this->getParameterByName("u_Text"); this->paramHasTexture = this->getParameterByName("u_HasTexture"); } void SimpleBillboardedShader::compile() { this->program.compile(); } std::vector SimpleBillboardedShader::getPassItems( Mesh *mesh, Material *material, Camera *camera ) { auto simpleMaterial = dynamic_cast(material); assertNotNull(simpleMaterial); struct ShaderPassItem onlyPass; onlyPass.mesh = mesh; onlyPass.shaderProgram = &program; onlyPass.colorValues[program.paramColor] = simpleMaterial->color; onlyPass.matrixValues[program.paramModel] = material->transform->getWorldTransform(); onlyPass.matrixValues[program.paramView] = camera->transform->getWorldTransform(); onlyPass.matrixValues[program.paramProjection] = camera->getProjection(); onlyPass.renderFlags = ( RENDER_MANAGER_RENDER_FLAG_BLEND | RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST ); if(simpleMaterial->texture != nullptr) { onlyPass.boolValues[program.paramHasTexture] = true; onlyPass.textureSlots[0] = simpleMaterial->texture; onlyPass.textureValues[program.paramTexture] = 0; } else { onlyPass.boolValues[program.paramHasTexture] = false; } std::vector passes; passes.push_back(onlyPass); return passes; }