#include "test_pixel_shader.h" #include "device/dvc_device.h" #include "device/dvc_uniform_buffer.h" #include "asset/dvc_texture.h" #include "shader/dvc_shader_functions.h" #include "pipe/dvc_render_pipe.h" using namespace davinci; const char* TestPixelShader::getName(void) const { return "test"; } void TestPixelShader::prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) { m_pipe = pipe; } void TestPixelShader::process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color) { const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer(); const VertexDesc::Element* v_normal_Element = veretexDesc.getElement("v_normal"); const fVector3* v_normal = (const fVector3*)(pixelData + (v_normal_Element->offset / sizeof(float32_t))); const VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv"); const fVector2* v_uv = (const fVector2*)(pixelData + (v_uv_Element->offset / sizeof(float32_t))); const VertexDesc::Element* v_toCamera_Element = veretexDesc.getElement("v_toCamera"); const fVector3* v_toCamera = (const fVector3*)(pixelData + (v_toCamera_Element->offset / sizeof(float32_t))); const VertexDesc::Element* v_toLight_Element = veretexDesc.getElement("v_toLight"); const fVector3* v_toLight = (const fVector3*)(pixelData + (v_toLight_Element->offset / sizeof(float32_t))); const VertexDesc::Element* v_tangent_Element = veretexDesc.getElement("v_tangent"); const fVector3* v_tangent = (const fVector3*)(pixelData + (v_tangent_Element->offset / sizeof(float32_t))); const VertexDesc::Element* v_binormal_Element = veretexDesc.getElement("v_binormal"); const fVector3* v_binormal = (const fVector3*)(pixelData + (v_binormal_Element->offset / sizeof(float32_t))); const VertexDesc::Element* v_wposition_Element = veretexDesc.getElement("v_wposition"); const fVector3* v_wposition = (const fVector3*)(pixelData + (v_wposition_Element->offset / sizeof(float32_t))); fMatrix3 tbnMatrix = fMatrix3( v_tangent->x, v_binormal->x, v_normal->x, v_tangent->y, v_binormal->y, v_normal->y, v_tangent->z, v_binormal->z, v_normal->z); davinci::fVector3 light0_position = davinci::fVector3::ZERO; if (davinci::fVector3* p_light0_position = (davinci::fVector3*)(uniformBuffer.getAttribute("light0:position"))) { light0_position = *p_light0_position; } //davinci::fVector3 camera_position = davinci::fVector3::ZERO; //if (davinci::fVector3* p_camera_position = (davinci::fVector3*)(uniformBuffer.getAttribute("camera:eye_pos"))) //{ // camera_position = *p_camera_position; //} fVector3 lp = light0_position - *v_wposition; fVector3 vLight = lp.normalise() * tbnMatrix; fVector3 u_ambientColor = fVector3(0.2, 0.2, 0.2); fVector3 u_matColor = fVector3(1.0, 1.0, 1.0); fVector3 u_lightColor = fVector3(1.0, 1.0, 1.0); fVector4 u_textureColor = shader::texture2D(*m_pipe, "material:pbr:base_color_texture", *v_uv, fVector4::WHITE); const fVector4 defaultNormal = fVector4(0.5f, 0.5f, 1.0f, 1.0f); fVector4 u_normalColor = shader::texture2D(*m_pipe, "material:pbr:normal_texture", *v_uv, defaultNormal); //---------------- fVector3 vN = fVector3::UNIT_Z;// u_normalColor.xyz() * 2.0f - fVector3::ONE; vN.normalise(); //vector vL = normalize(v_toLight); fVector3 vL =vLight; vL.normalise(); //vector vV = normalize(v_toCamera); fVector3 vV = *v_toCamera; vV.normalise(); float ndotl = vL.dotProduct(vN); //vector vR = 2 * dot(vL, vN)*vN - vL; fVector3 vR = 2 * ndotl * vN - vL; float rdotv = vR.dotProduct(vV); //vector vH = normalize(vL + vV); //fVector3 vH = vL + vV; //vH.normalise(); //color colAmbient = u_ambientColor*u_matColor; fVector3 colAmbient = u_ambientColor * u_matColor; //float ndotl = max(dot(vN, vL), 0); //float32_t ndotl = std::max(vN.dotProduct(vL), 0.0f); //color colDiff = ndotl * u_matColor * u_lightColor; fVector3 colDiff = shader::max(0.0f, ndotl) * u_matColor* u_lightColor; //float32_t spec = std::max(vR.dotProduct(vV), 0.0f)*1.0; //color colSpec = step(0, ndotl) * pow(spec, 64) * u_matColor * u_lightColor; const float _m = 1.0f; float spec = shader::step(0.0f, ndotl) * shader::pow(shader::max(0.0f, rdotv), 32.f); fVector3 colSpec = spec * u_matColor* u_lightColor; //color final = colAmbient + colDiff + colSpec; fVector3 final = fVector3::BLACK;// (colAmbient + colDiff + colSpec)* u_textureColor.xyz(); final += colAmbient; final += colDiff; final += colSpec; final *= u_textureColor.xyz(); //fVector3 final = (colAmbient +colDiff +colSpec)*u_textureColor.xyz(); //color = fVector4::RED; color = fVector4(final.saturate(), 1.0f);// fVector4::RED; } const char* SolidColorPixelShader::getName(void) const { return "solid"; } void SolidColorPixelShader::prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) { } void SolidColorPixelShader::process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color) { color = fVector4::RED; }