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);