在kanzi 3.9.8里使用API创建自定义材质

1. kanzi studio设置

1.1 创建一个纹理贴图,起名Render Target Texture

1.2 创建一个Image节点,使用该贴图

2. 代码设置

2.1 创建一个自定义节点类

c++ 复制代码
class mynode2d : public Node2D
{
public:
virtual void renderOverride(Renderer3D& renderer, CompositionStack& compositionStack, const optional< Matrix3x3 >& baseTransform);
}

2.2 实现函数renderOverride,创建shader和材质

c++ 复制代码
const char* shaderCodeVertex = "attribute vec3 kzPosition;\n\
attribute vec2 kzTextureCoordinate0;\n\
varying vec2 vTexCoord;\n\
varying vec4 ParticleColor;\n\
uniform highp mat4 projectMatrix;\n\
uniform highp mat4 modelMatrix;\n\
uniform vec2 offset;\n\
uniform vec4 color;\n\
uniform float scale;\n\
void main()\n\
{\n\
    precision mediump float;\n\
    //float scale = 10.0f;\n\
    vTexCoord = kzTextureCoordinate0;\n\
    ParticleColor = color;\n\
    gl_Position =  vec4(kzPosition.xyz, 1.0);\n\
}";

const char* shaderCodeFragment = "\n\
uniform sampler2D Texture;\n\
uniform int hasTexture;\n\
varying vec2 vTexCoord;\n\
varying vec4 ParticleColor;\n\
void main()\n\
{\n\
    precision lowp float;\n\
    //vec2 uv = vTexCoord * 2.0 - 1.0;\n\
    if(1==hasTexture){\n\
        vec4 color = texture2D(Texture, vTexCoord);\n\
        gl_FragColor.rgba = color * ParticleColor;\n\
    }\n\
    else{\n\
        gl_FragColor.rgba = ParticleColor;\n\
    }\n\
    gl_FragColor.rgba = vec4(1.0, 1.0, 0.0, 1.0);\n\
}";

ShaderProgram::CreateInfoShaderSourc
es createInfoShaderSources(shaderCodeVertex, shaderCodeFragment);
        ShaderAttributeCollection shaderAttributeCollection;
        ShaderVertexAttribute kzPosition("kzPosition", VertexAttribute::SemanticPosition, 0, GraphicsElementTypeFLOAT, 1, 3, 0, 0);
        ShaderVertexAttribute kzTextureCoordinate0("kzTextureCoordinate0", VertexAttribute::SemanticTextureCoordinate, 0, GraphicsElementTypeFLOAT, 1, 2, 1, 0);

        shaderAttributeCollection.push_back(kzPosition);
        shaderAttributeCollection.push_back(kzTextureCoordinate0);

        createInfoShaderSources.vertexFormat = shaderAttributeCollection;
        //createInfoShaderSources.addUniform(DynamicPropertyType<Matrix4x4>("kzProjectionCameraWorldMatrix"),  ShaderProgram::UniformTransformationPassThrough);

        ShaderProgram::CreateInfo::Status status = createInfoShaderSources.validate(*m_renderer);

        if (status == ShaderProgram::CreateInfo::StatusValid) {
           // m_shaderProgram = new ShaderProgram(getDomain(), createInfoShaderSources, "shaderProgram");
        }
        else {
            printf("status error=%d\n", status);
            return;
        }
        
  ShaderProgramSharedPtr m_shaderProgram = ShaderProgram::create(getDomain(), createInfoShaderSources, "shaderProgram");
        m_shaderProgramP = m_shaderProgram.get();

		//创建材质
        MaterialSharedPtr material = Material::create(getDomain(), "Example material", m_shaderProgram);

		//创建材质画刷
        MaterialBrushSharedPtr mm = MaterialBrush::create(getDomain(), "mm");
        mm->setMaterial(material);
		//设置背景刷
        setProperty(Node2D::BackgroundBrushProperty, mm);

2.3 设置离屏渲染到目标纹理上

c++ 复制代码
 kanzi::TextureSharedPtr m_texture = getResourceManager()->acquireResource<Texture>("kzb://mtk_demo/Textures/Render Target Texture");
    NativeFramebufferHandle NativeFramebufferHandle1 = m_texture->getNativeFramebufferHandle();

    if (NativeFramebufferHandle1 != 0) {
        m_renderer->setActiveFramebuffer(NativeFramebufferHandle1);

        unsigned int oldshader = m_renderer->getActiveShaderHandle();
        
        m_shaderProgramP->bind();
        
        m_renderer->setClearColor(ColorRGBA(1.0, 1.0, 0.0, 0.0));
        m_renderer->addClearTarget(GraphicsClearTargetColor0);
        m_renderer->addClearTarget(GraphicsClearTargetDepth);

        m_renderer->clear();

        m_renderer->flush();

        m_renderer->bindFramebuffer(0);

        m_renderer->setActiveFramebuffer(old);

    }
    else {
        kzLogDebug(("render NativeFramebufferHandle1={}", NativeFramebufferHandle1));
    }

3. 动态创建节点

c++ 复制代码
virtual void onProjectLoaded() KZ_OVERRIDE
{
mynode2dSharedPtr n = mynode2d::create(getDomain(), "mynode");
Node2DSharedPtr r = getRoot();
if (r) {
r->addChild(n);
}
}

4. 运行效果

  • 自定义节点mynode使用了自定义材质作为背景刷
  • Image使用Render Target Texture作为贴图显示
相关推荐
无忧.芙桃11 小时前
C++语言原理与实践(二十三):哈希表的实现
c++·链表·哈希算法·散列表·哈希表
AvocadoZeroOneOI14 小时前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
whitelbwwww16 小时前
cuBLAS--CUDA高性能线性代数计算库--第一部分
c++·线性代数·gpu算力·cuda c++
eggwyw18 小时前
C++设计模式之解释器模式
开发语言·c++
我不会插花弄玉1 天前
13.多路转接epoll【由浅入深-网络】
网络·c++·epoll
郝学胜-神的一滴1 天前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
西西弗Sisyphus1 天前
C++ explicit 关键字工程实践
开发语言·c++
AIgorithmGEEK2 天前
std::unordered_map 与 std::unordered_set:接口、算法技巧与权衡 (速查)
c++·算法·哈希表·负载因子·桶·unordered_map·unordered_set
一拳一个呆瓜2 天前
【STL】头文件:<cmath>
c++·stl
草莓熊Lotso2 天前
【C++ 基于微服务的即时通讯系统】ElasticSearch 用户数据管理实现
linux·数据库·c++·elasticsearch·微服务·架构