veMaterial* veDeferredRenderPipeline::createSpotLightMaterial(veLight *light)
{
    auto material = new veMaterial;
    auto technique = new veTechnique;
    auto pass0 = new vePass;
    auto pass1 = new vePass;
    material->addTechnique(technique);
    technique->addPass(pass0);
    technique->addPass(pass1);
    
    pass0->setRenderPass(vePass::FORWARD_PASS);
    pass0->depthTest() = true;
    pass0->depthWrite() = false;
    pass0->stencilTest() = true;
    pass0->cullFace() = false;
    pass0->stencilFunc() = { GL_ALWAYS, 0, 0, GL_ALWAYS, 0, 0 };
    pass0->stencilOp() = { GL_KEEP, GL_DECR_WRAP, GL_KEEP, GL_KEEP, GL_INCR_WRAP, GL_KEEP };
    pass0->blendFunc() = veBlendFunc::DISABLE;
    pass0->setShader(new veShader(veShader::VERTEX_SHADER, WORLD_BASED_LIGHT_VERTEX_SHADER));
    pass0->setShader(new veShader(veShader::FRAGMENT_SHADER, "layout(location=0) out vec4 fragColor;void main(){}"));
    pass0->addUniform(new veUniform("u_ModelViewProjectMat", MVP_MATRIX));
    pass0->setApplyFunctionCallback([]() {
        glClear(GL_STENCIL_BUFFER_BIT);
        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    });
    
    initLightCommomParams(light, pass1);
    pass1->setRenderPass(vePass::FORWARD_PASS);
    pass1->depthTest() = false;
    pass1->depthWrite() = false;
    pass1->stencilTest() = true;
    pass1->cullFace() = true;
    pass1->cullFaceMode() = GL_FRONT;
    pass1->blendFunc().src = GL_ONE;
    pass1->blendFunc().dst = GL_ONE;
    pass1->blendEquation() = GL_FUNC_ADD;
    pass1->stencilFunc() = { GL_NOTEQUAL, 0, 0xFF, GL_NOTEQUAL, 0, 0xFF };
    pass1->setShader(new veShader(veShader::VERTEX_SHADER, WORLD_BASED_LIGHT_VERTEX_SHADER));
    pass1->setShader(new veShader(veShader::FRAGMENT_SHADER, SPOT_LIGHT_FRAGMENT_SHADER));
    pass1->addUniform(new veUniform("u_ModelViewProjectMat", MVP_MATRIX));
    pass1->addUniform(new veUniform("u_lightDirection", veVec3(0.0f)));
    pass1->addUniform(new veUniform("u_lightPosition", veVec3(0.0f)));
    pass1->addUniform(new veUniform("u_lightMatrix", veMat4::IDENTITY));
    pass1->addUniform(new veUniform("u_lightARI", 0.0f));
    pass1->addUniform(new veUniform("u_lightInnerAngleCos", 0.0f));
    pass1->addUniform(new veUniform("u_lightOuterAngleCos", 0.0f));
    pass1->addUniform(new veUniform("u_shadowTex", 4));
    pass1->setApplyFunctionCallback([]() {
        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    });
    
    return material;
}
veMaterial* veDeferredRenderPipeline::createDirectionalLightMaterial(veLight *light)
{
    auto material = new veMaterial;
    auto technique = new veTechnique;
    auto pass = new vePass;
    material->addTechnique(technique);
    technique->addPass(pass);
    
    initLightCommomParams(light, pass);
    pass->setRenderPass(vePass::FORWARD_PASS);
    pass->depthTest() = false;
    pass->depthWrite() = false;
    pass->stencilTest() = false;
    pass->cullFace() = true;
    pass->cullFaceMode() = GL_BACK;
    pass->blendFunc().src = GL_ONE;
    pass->blendFunc().dst = GL_ONE;
    pass->blendEquation() = GL_FUNC_ADD;
    pass->setShader(new veShader(veShader::VERTEX_SHADER, SCREEN_BASED_LIGHT_VERTEX_SHADER));
    pass->setShader(new veShader(veShader::FRAGMENT_SHADER, DIRECTIONAL_LIGHT_FRAGMENT_SHADER));
    pass->addUniform(new veUniform("u_lightDirection", veVec3::ZERO));
    pass->addUniform(new veUniform("u_lightMatrixs", &veMat4::IDENTITY, 1));
    pass->addUniform(new veUniform("u_shadowCascadedLevels", 0.f));
    pass->addUniform(new veUniform("u_shadowCascadedCount", 0.f));
    pass->addUniform(new veUniform("u_shadowTex", 4));
    
    return material;
}
veMaterial* veDeferredRenderPipeline::createIBLightMaterial(veLight *light)
{
    auto material = new veMaterial;
    auto technique = new veTechnique;
    auto pass = new vePass;
    material->addTechnique(technique);
    technique->addPass(pass);
    
    initLightCommomParams(light, pass);
    pass->setRenderPass(vePass::FORWARD_PASS);
    pass->depthTest() = false;
    pass->depthWrite() = false;
    pass->stencilTest() = false;
    pass->cullFace() = true;
    pass->cullFaceMode() = GL_BACK;
    pass->blendFunc().src = GL_ONE;
    pass->blendFunc().dst = GL_ONE;
    pass->blendEquation() = GL_FUNC_ADD;
    pass->setShader(new veShader(veShader::VERTEX_SHADER, SCREEN_BASED_LIGHT_VERTEX_SHADER));
    pass->setShader(new veShader(veShader::FRAGMENT_SHADER, IB_LIGHT_FRAGMENT_SHADER));
    pass->addUniform(new veUniform("u_lightDirection", veVec3::ZERO));
    pass->addUniform(new veUniform("u_specularMipMapCount", 1.0f));
    pass->addUniform(new veUniform("u_diffuseLighting", 4));
    pass->addUniform(new veUniform("u_specularLighting", 5));
    
    
    return material;
}
Exemple #4
0
Material::Material(Material_t tag)
    : tag(tag),
      vertexShader(this),
      fragmentShader(this),
      index0AttributeName(this)
{
    mId = Material::MaterialIdCount++;

    mUuid = Math::generateUUID();

    mName = "";

    side() = kFrontSide;

    opacity() = 1.0f;
    transparent() = false;

    blending() = kNormalBlending;

    blendSrc() = kSrcAlphaFactor;
    blendDst() = kOneMinusSrcAlphaFactor;
    blendEquation() = kAddEquation;

    depthTest() = true;
    depthWrite() = true;

    polygonOffset() = false;
    polygonOffsetFactor() = 0;
    polygonOffsetUnits() = 0;

    // mAlphaTest = 0;

    overdraw() = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer

    visible() = true;

    needsUpdate() = true;

    // By default, bind position to attribute index 0. In WebGL, attribute 0
    // should always be used to avoid potentially expensive emulation.
    index0AttributeName("position");

    linewidth() = 1.0f;
    metal() = false;
    perPixel() = true;
    shading() = kNoShading;
	vertexColors() = kNoColors;
    wireframe() = false;
    wireframeLinewidth() = 1.0f;
}
veMaterial* veDeferredRenderPipeline::createAmbientLightMaterial()
{
    auto material = new veMaterial;
    auto technique = new veTechnique;
    auto pass = new vePass;
    material->addTechnique(technique);
    technique->addPass(pass);
    
    pass->setRenderPass(vePass::FORWARD_PASS);
    pass->depthTest() = false;
    pass->depthWrite() = false;
    pass->stencilTest() = false;
    pass->cullFace() = true;
    pass->cullFaceMode() = GL_BACK;
    pass->blendFunc().src = GL_ONE;
    pass->blendFunc().dst = GL_ONE;
    pass->blendEquation() = GL_FUNC_ADD;
    pass->setShader(new veShader(veShader::VERTEX_SHADER, SCREEN_BASED_LIGHT_VERTEX_SHADER));
    pass->setShader(new veShader(veShader::FRAGMENT_SHADER, AMBIENT_LIGHT_FRAGMENT_SHADER));
    pass->addUniform(new veUniform("u_ambient", veVec3::ZERO));
    pass->addUniform(new veUniform("u_RT1", 0));
    
    return material;
}