当前位置: 首页 > news >正文

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

1. kanzi studio设置

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

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

在这里插入图片描述

2. 代码设置

2.1 创建一个自定义节点类

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

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


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 设置离屏渲染到目标纹理上

 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. 动态创建节点

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作为贴图显示
http://www.lryc.cn/news/474834.html

相关文章:

  • IDEA中通义灵码的使用技巧
  • JS中let var 和const区别
  • ansible详细介绍和具体步骤
  • 利用LangChain与LLM打造个性化私有文档搜索系统
  • linux中的软、硬链接
  • Ubuntu 系统、Docker配置、Docker的常用软件配置(下)
  • jdk,openjdk,oraclejdk
  • Docker Hub 镜像加速器
  • DevOps赋能:优化业务价值流的实战策略与路径(上)
  • int的取值范围
  • 图文检索(16):IDC: Boost Text-to-Image Retrieval via Indirect and Direct Connections
  • 企业数字化转型:重识、深思、重启新征程-亿发
  • 仓颉刷题录-字符串数字转换(一)
  • SpringBoot【实用篇】- 配置高级
  • liunx CentOs7安装MQTT服务器(mosquitto)
  • 【银河麒麟高级服务器操作系统】虚拟机lvm分区丢失现象分析及解决建议
  • Android 原子性类型都有哪些
  • MySQL(上)
  • ffplay 实现视频流中音频的延迟
  • CSDN资源变现
  • Rembg模型构建教程
  • 向量的基础知识和矩阵向量的坐标旋转
  • GeoSever发布图层(保姆姬)
  • AI 大模型的发展趋势是怎样的?
  • win10怎么关掉自动更新-小白必看
  • 大学城水电资源管理:Spring Boot解决方案
  • 躺平成长-运营日记-第三天
  • 智慧税务管理:金融企业报税效率与合规性提升
  • linux之网络子系统-用户层接收数据包之同步阻塞方案
  • 【天线&空中农业】花生霉变检测系统源码&数据集全套:改进yolo11-LVMB