const string testFragmentSource(""
"uniform mediump vec4 Diffuse;\n"
"uniform mediump vec4 Emissive;\n"
"uniform mediump vec4 DirectionalLightColor[3];\n"
"uniform mediump vec3 DirectionalLightDirection[3];\n"
"varying mediump vec3 vNormal;\n"
"void main()\n"
"{"
" precision lowp float;\n"
" vec4 color = vec4(0.0);\n"
" vec3 nor = normalize(vNormal);\n"
" for (int it = 0; it < 3; ++it)\n"
" {\n"
" float angle = max(dot(nor, normalize(DirectionalLightDirection[it])), 0.0);\n"
" color += angle * (DirectionalLightColor[it] * Diffuse);\n"
" }\n"
" gl_FragColor = Emissive + color;\n"
"}");
const string testVertexSource(""
"attribute vec3 kzPosition;\n"
"attribute vec3 kzNormal;\n"
"uniform highp mat4 kzNormalMatrix;\n"
"uniform highp mat4 kzProjectionCameraWorldMatrix;\n"
"varying mediump vec3 vNormal;\n"
"void main()\n"
"{\n"
" precision mediump float;\n"
" vNormal = (kzNormalMatrix * vec4(kzNormal, 1.0)).xyz;\n"
" gl_Position = kzProjectionCameraWorldMatrix * vec4(kzPosition.xyz, 1.0);\n"
"}");
createInfo.
vertexFormat.emplace_back(
"kzPosition", VertexAttribute::SemanticPosition, 0u, GraphicsElementTypeFLOAT, 1u, 3u, 0, 0u);
createInfo.
vertexFormat.emplace_back(
"kzNormal", VertexAttribute::SemanticNormal, 0u, GraphicsElementTypeFLOAT, 1u, 3u, 1, 0u);
createInfo.
addUniform(
"kzProjectionCameraWorldMatrix", PropertyDataTypeMatrix4x4, 1u, nullopt, ShaderProgram::UniformBindingTypeDefault, ShaderProgram::FixedUniform::ProjectionCameraWorldMatrix,
getFixedUniformFunction(
"kzProjectionCameraWorldMatrix"), ShaderProgram::UniformTransformationPassThrough);
createInfo.
addUniform(
"kzNormalMatrix", PropertyDataTypeMatrix4x4, 1u, nullopt, ShaderProgram::UniformBindingTypeDefault, ShaderProgram::FixedUniform::NormalMatrix,
getFixedUniformFunction(
"kzProjectionCameraWorldMatrix"), ShaderProgram::UniformTransformationPassThrough);
createInfo.
addUniform(StandardMaterial::DiffuseProperty);
createInfo.
addUniform(StandardMaterial::EmissiveProperty);
createInfo.
addUniform(
"DirectionalLightColor", PropertyDataTypeColor, 3u,
AbstractPropertyType(Light::DirectionalLightColorProperty), ShaderProgram::UniformBindingTypeDefault, nullopt,
nullptr, ShaderProgram::UniformTransformationPassThrough);
createInfo.
addUniform(
"DirectionalLightDirection", PropertyDataTypeVector3, 3u,
AbstractPropertyType(Light::DirectionalLightDirectionProperty), ShaderProgram::UniformBindingTypeDefault, nullopt,
nullptr, ShaderProgram::UniformTransformationPassThrough);
{
optional<BindingLoadInfo> info =
createDefaultRenderValueBinding(domain,
AbstractPropertyType(StandardMaterial::DiffuseProperty), StandardMaterial::DiffuseProperty.getUnqualifiedName(), PropertyDataTypeColor, 1u, ShaderProgram::UniformTransformationPassThrough);
createInfo.
bindings.emplace_back(info->binding, info->targetDataType, info->targetRenderValue);
}
{
optional<BindingLoadInfo> info =
createDefaultRenderValueBinding(domain,
AbstractPropertyType(StandardMaterial::EmissiveProperty), StandardMaterial::EmissiveProperty.getUnqualifiedName(), PropertyDataTypeColor, 1u, ShaderProgram::UniformTransformationPassThrough);
createInfo.
bindings.emplace_back(info->binding, info->targetDataType, info->targetRenderValue);
}
{
createInfo.
bindings.emplace_back(info->binding, info->targetDataType, info->targetRenderValue);
}
{
createInfo.
bindings.emplace_back(info->binding, info->targetDataType, info->targetRenderValue);
}
MaterialSharedPtr material = Material::create(domain,
"Example material", shaderProgram);
When you want to override binding information, instead of relying on the default states for the bindings, create the bindings manually. To manually create bindings for shaders:
const string testFragmentSource(""
"uniform mediump vec4 Diffuse;\n"
"uniform mediump vec4 DirectionalLightColor[3];\n"
"uniform mediump vec3 DirectionalLightDirection[3];\n"
"varying mediump vec3 vNormal;\n"
"void main()\n"
"{"
" precision lowp float;\n"
" vec4 color = vec4(0.0);\n"
" vec3 nor = normalize(vNormal);\n"
" for (int it = 0; it < 3; ++it)\n"
" {\n"
" float angle = max(dot(nor, normalize(DirectionalLightDirection[it])), 0.0);\n"
" color += angle * (DirectionalLightColor[it] * Diffuse);\n"
" }\n"
" gl_FragColor = color;\n"
"}");
const string testVertexSource(""
"attribute vec3 kzPosition;\n"
"attribute vec3 kzNormal;\n"
"uniform highp mat4 kzNormalMatrix;\n"
"uniform highp mat4 kzProjectionCameraWorldMatrix;\n"
"varying mediump vec3 vNormal;\n"
"void main()\n"
"{\n"
" precision mediump float;\n"
" vNormal = (kzNormalMatrix * vec4(kzNormal, 1.0)).xyz;\n"
" gl_Position = kzProjectionCameraWorldMatrix * vec4(kzPosition.xyz, 1.0);\n"
"}");
createInfo.
vertexFormat.emplace_back(
"kzPosition", VertexAttribute::SemanticPosition, 0u, GraphicsElementTypeFLOAT, 1u, 3u, 0, 0u);
createInfo.
vertexFormat.emplace_back(
"kzNormal", VertexAttribute::SemanticNormal, 0u, GraphicsElementTypeFLOAT, 1u, 3u, 1, 0u);
createInfo.
addUniform(
"kzProjectionCameraWorldMatrix", PropertyDataTypeMatrix4x4, 1u, nullopt, ShaderProgram::UniformBindingTypeDefault, ShaderProgram::FixedUniform::ProjectionCameraWorldMatrix,
getFixedUniformFunction(
"kzProjectionCameraWorldMatrix"), ShaderProgram::UniformTransformationPassThrough);
createInfo.
addUniform(
"kzNormalMatrix", PropertyDataTypeMatrix4x4, 1u, nullopt, ShaderProgram::UniformBindingTypeDefault, ShaderProgram::FixedUniform::NormalMatrix,
getFixedUniformFunction(
"kzProjectionCameraWorldMatrix"), ShaderProgram::UniformTransformationPassThrough);
createInfo.
addUniform(StandardMaterial::DiffuseProperty);
createInfo.
addUniform(
"DirectionalLightColor", PropertyDataTypeColor, 3u,
AbstractPropertyType(Light::DirectionalLightColorProperty), ShaderProgram::UniformBindingTypeDefault, nullopt,
nullptr, ShaderProgram::UniformTransformationPassThrough);
createInfo.
addUniform(
"DirectionalLightDirection", PropertyDataTypeVector3, 3u,
AbstractPropertyType(Light::DirectionalLightDirectionProperty), ShaderProgram::UniformBindingTypeDefault, nullopt,
nullptr, ShaderProgram::UniformTransformationPassThrough);
{
valueSources.push_back(RenderPropertyExpressionValueSource::create(".", StandardMaterial::DiffuseProperty));
valueSources.push_back(RenderPropertyExpressionValueSource::create(".", StandardMaterial::EmissiveProperty));
operations.emplace_back(BindingExpressionArgumentTypeSourceRegister0, PropertyFieldWhole,
BindingExpressionArgumentTypeSourceRegister1, PropertyFieldWhole,
KZU_EXPRESSION_VALIDATOR_OPERATION_MULTIPLY, 0, PropertyFieldWhole);
vector<Variant>(), 1u);
createInfo.
bindings.emplace_back(binding, PropertyDataTypeColor, StandardMaterial::DiffuseProperty.getUnqualifiedName());
}
{
sources.emplace_back(ObjectPropertyExpressionValueSource::create("##RenderPass", DrawObjectsRenderPass::DirectionalLightsProperty));
vector<Variant> constants;
constants.emplace_back(3);
processors.push_back(RangeBindingProcessor::create(domain, BindingExpressionArgumentTypeTemporaryRegister1, Light::DirectionalLightColorProperty, 2u));
processors.push_back(RangeBindingProcessor::create(domain, BindingExpressionArgumentTypeTemporaryRegister1, Node::OpacityProperty, 3u));
operations.emplace_back(BindingExpressionArgumentTypeSourceRegister0, PropertyFieldWhole,
BindingExpressionArgumentTypeConstantRegister0, PropertyFieldWhole,
KZU_EXPRESSION_VALIDATOR_OPERATION_CREATE_VIEW, 1u, PropertyFieldWhole);
operations.emplace_back(BindingExpressionArgumentTypeTemporaryRegister2, PropertyFieldWhole,
BindingExpressionArgumentTypeTemporaryRegister3, PropertyFieldWhole,
KZU_EXPRESSION_VALIDATOR_OPERATION_MULTIPLY, 0u, PropertyFieldWhole);
BindingSourcePtr bindingSource = ExpressionBindingSource::create(operations, processors, sources,
constants, 4u);
createInfo.
bindings.emplace_back(binding, PropertyDataTypeColor, Light::DirectionalLightColorProperty.getUnqualifiedName());
}
{
EXPECT_EQ(true, static_cast<bool>(info));
createInfo.
bindings.emplace_back(info->binding, info->targetDataType, info->targetRenderValue);
}
MaterialSharedPtr material = Material::create(domain,
"Example material", shaderProgram);