关于下面的文档,能提供一个插件DEMO吗?
https://docs.kanzi.com/3.9.8/en/working-with/meshes/meshes-api.html?highlight=writevertexattribute
我根据以上链接创建的工程,没办法正常的绘制。
请问是什么原因呢?
工程链接下载:
#ifndef DRAWBOX_HPP
#define DRAWBOX_HPP
drawbox.hpp// Use kanzi.hpp only when you are learning to develop Kanzi applications.drawbox.cpp
// To improve compilation time in production projects, include only the header files of the Kanzi functionality you are using.
#include <kanzi/kanzi.hpp>
class DrawBox;
typedef kanzi::shared_ptr<DrawBox> DrawBoxSharedPtr;
// The template component.
class DRAWBOX_API DrawBox : public kanzi::Model3D
{
public:
static kanzi::PropertyType<kanzi::ResourceSharedPtr> MaterialProperty;
static kanzi::PropertyType<float> DensityProperty;
KZ_METACLASS_BEGIN(DrawBox, Model3D, "CustomComponentType")
KZ_METACLASS_PROPERTY_TYPE(MaterialProperty)
KZ_METACLASS_PROPERTY_TYPE(DensityProperty)
KZ_METACLASS_END()
// Creates the property editor info for the DrawBox.
static kanzi::PropertyTypeEditorInfoSharedPtr makeEditorInfo();
// Creates a DrawBox.
static DrawBoxSharedPtr create(kanzi::Domain* domain, kanzi::string_view name);
void onAttached() override;
void onDetached() override;
void renderOverride(kanzi::Renderer3D& renderer, kanzi::RenderEntry3D& renderState) override;
virtual void onNodePropertyChanged(kanzi::AbstractPropertyType propertyType, kanzi::PropertyNotificationReason reason) KZ_OVERRIDE;
protected:
// Constructor.
explicit DrawBox(kanzi::Domain* domain, kanzi::string_view name):
kanzi::Model3D(domain, name),
m_cachedMaterial(nullptr),
m_density(0)
{
}
// Initializes the created Kanzi Engine plugin.
// Kanzi node classes typically have a static create() member function, which creates the instance of a node,
// initializes it, and returns a shared pointer to the instance. To initialize an instance of a Kanzi Engine
// plugin, in the create() function call the initialize() function on the instance of that Kanzi Engine plugin.
// You must initialize a node in the initialize() function, not in the constructor.
void initialize();
//const RenderEntryParameterContainer* generateRenderStatesOverride(DrawObjectsRenderPass& drawPass) override;
void drawGraph();
private:
kanzi::MaterialSharedPtr m_cachedMaterial;
RenderEntryParameterContainer m_renderEntry;
float m_density;
};
#endif
#include "drawbox.hpp"
using namespace kanzi;
// Creates the property editor info for the DrawBox.
PropertyTypeEditorInfoSharedPtr DrawBox::makeEditorInfo()
{
return PropertyTypeEditorInfoSharedPtr(
KZ_DECLARE_EDITOR_METADATA(
metadata.tooltip = "Description for DrawBox.";
metadata["FrequentlyAddedProperties"] = "InstancingEffect.InstancingMaterial; InstancingEffect.Density";
));
}
PropertyType<ResourceSharedPtr> DrawBox::MaterialProperty(
kzMakeFixedString("InstancingEffect.InstancingMaterial"),
ResourceSharedPtr(),
0,
false,
KZ_DECLARE_EDITOR_METADATA(
metadata.displayName = "Instancing Material";
metadata.category = "Instancing Effect";
metadata.host = "InstancingEffect:freq";
metadata.editor = "MaterialComboBox.PropertyGridEditorWithEmpty";
metadata.valueProvider = "ProjectObject:Material";
)
);
PropertyType<float> DrawBox::DensityProperty(
kzMakeFixedString("InstancingEffect.Density"),
float(0.0f),
0,
false,
KZ_DECLARE_EDITOR_METADATA
(
metadata.displayName = "Density";
metadata.category = "Instancing Effect";
metadata.host = "InstancingEffect:freq";
metadata.lowerBound = "0.0";
metadata.upperBound = "10.0";
metadata.step = "0.01";
metadata.valueProvider = "Float";
)
);
// Creates a DrawBox.
DrawBoxSharedPtr DrawBox::create(Domain* domain, string_view name)
{
auto enginePlugin = DrawBoxSharedPtr(new DrawBox(domain, name));
enginePlugin->initialize();
return enginePlugin;
}
// Initializes the created Kanzi Engine plugin.
// Kanzi node classes typically have a static create() member function, which creates the instance of a node,
// initializes it, and returns a shared pointer to the instance. To initialize an instance of a Kanzi Engine
// plugin, in the create() function call the initialize() function on the instance of that Kanzi Engine plugin.
// You must initialize a node in the initialize() function, not in the constructor.
void DrawBox::initialize()
{
// Initialize base class.
Model3D::initialize();
drawGraph();
}
void DrawBox::onAttached()
{
Model3D::onAttached();
}
void DrawBox::onDetached()
{
Model3D::onDetached();
//m_instancingMaterial.reset();
//m_instancingMesh.reset();
}
void DrawBox::renderOverride(Renderer3D& renderer, RenderEntry3D& renderState)
{
renderOverride(renderer, renderState);
if (renderState.bindRenderState(renderer))
{
renderState.bindGeometry(renderer);
renderer.getCoreRenderer()->drawBuffers();
}
}
void DrawBox::onNodePropertyChanged(AbstractPropertyType propertyType, PropertyNotificationReason reason)
{
if (propertyType == MaterialProperty)
{
m_cachedMaterial = dynamic_pointer_cast<Material>(getProperty(MaterialProperty));
}
else if(propertyType == DensityProperty)
{
m_density = getProperty(DensityProperty);
}
Model3D::onNodePropertyChanged(propertyType, reason);
}
//kanzi::Node3D::RenderEntryParameterContainer* DrawBox::generateRenderStatesOverride(DrawObjectsRenderPass& drawPass)
////const RenderEntryParameterContainer* DrawBox::generateRenderStatesOverride(DrawObjectsRenderPass& drawPass)
//{
// Matrix4x4 cam = drawPass.getCameraMatrix();
// Matrix4x4 project = drawPass.getProjectionMatrix();
//
// return &m_renderEntry;
//}
void DrawBox::drawGraph()
{
// Define a CreateInfo structure and set it to keep the data after uploading it to the GPU.
Mesh::CreateInfo createInfo;
createInfo.memoryType = GPUResource::GpuAndRam;
// Describe the format for each vertex. In this example, every vertex has a position in 3D space,
// texture coordinates pointing to 2D texture space, and normal and tangent data in 3D space.
// Add a three-component position to the vertex format description.
// Use the standard Kanzi name kzPosition.
MeshVertexAttribute position("kzPosition", VertexAttribute::SemanticPosition, 0, GraphicsDataTypeSFLOAT32, 3, ~0u);
createInfo.vertexFormat.push_back(position);
// Add a two-component texture coordinate to the vertex format description.
// Texture coordinates enable you to use textures on a mesh.
// Use the standard Kanzi name kzTextureCoordinate0.
MeshVertexAttribute textureCoordinate("kzTextureCoordinate0", VertexAttribute::SemanticTextureCoordinate, 0, GraphicsDataTypeSFLOAT32, 2, ~0u);
createInfo.vertexFormat.push_back(textureCoordinate);
// Add three-component normals to the vertex format description.
// Normals enable you to use lighting on a mesh.
// Use the standard Kanzi name kzNormal.
MeshVertexAttribute normal("kzNormal", VertexAttribute::SemanticNormal, 0, GraphicsDataTypeSFLOAT32, 3, ~0u);
createInfo.vertexFormat.push_back(normal);
// Add three-component tangents to the vertex format description.
// Tangents enable you to use normal maps on a mesh.
// Use the standard Kanzi name kzTangent.
MeshVertexAttribute tangent("kzTangent", VertexAttribute::SemanticTangent, 0, GraphicsDataTypeSFLOAT32, 3, ~0u);
createInfo.vertexFormat.push_back(tangent);
// Now that you added all components to the vertex format description, update the offsets, strides, and vertex size.
updateVertexAttributeOffsetsAndVertexSize(createInfo);
updateVertexAttributeStrides(createInfo);
// Create a cluster. Two triangles define a face. Since a cube has six faces, you must create 36 indices
// (2 * 3 * 6 = 36). In this example, the entire mesh uses a single material, which is why you need only
// one cluster.
m_cachedMaterial = static_pointer_cast<Material>(getResourceManager()->acquireResource("kzb://drawbox/Materials/ColorTextureMaterial"));
//m_cachedMaterial->getProperty("Texture");
//m_cachedMaterial->setTexture();
Mesh::CreateInfo::Cluster cluster(GraphicsPrimitiveTypeTriangles, 36, IndexBufferTypeUInt16, m_cachedMaterial, "ColorTextureMaterial");
createInfo.clusters.push_back(cluster);
// Create the vertices. Four vertices define one face of a cube. Since a cube has six faces, you must
// create 24 vertices (4 * 6 = 24). Here you create vertex faces with normals that point to the same
// direction. This makes the faces of a cube appear flat. You cannot reuse vertex positions because the
// normals for each face are separate.
{
createInfo.vertexCount = 24;
size_t dataSize = createInfo.vertexCount * createInfo.vertexSize;
createInfo.vertexData.resize(dataSize);
// Define the positions of the cube corners.
// This example first defines the back face, then the front face.
const Vector3 cornerPositions[] =
{
Vector3(1.0f, 1.0f, 1.0f),
Vector3(-1.0f, 1.0f, 1.0f),
Vector3(-1.0f, -1.0f, 1.0f),
Vector3(1.0f, -1.0f, 1.0f),
Vector3(1.0f, -1.0f, -1.0f),
Vector3(1.0f, 1.0f, -1.0f),
Vector3(-1.0f, 1.0f, -1.0f),
Vector3(-1.0f, -1.0f, -1.0f)
};
// Define vertex indices for all faces.
const Vector2 UVs[] =
{
Vector2(1.0f, 1.0f),
Vector2(0.0f, 1.0f),
Vector2(0.0f, 0.0f),
Vector2(1.0f, 0.0f)
};
// Define vertex indices for all faces.
const unsigned int faceIndices[][4] =
{
// Front.
{ 0, 1, 2, 3 },
// Right.
{ 5, 0, 3, 4 },
// Back.
{ 6, 5, 4, 7 },
// Left.
{ 1, 6, 7, 2 },
// Top.
{ 5, 6, 1, 0 },
// Bottom.
{ 3, 2, 7, 4 }
};
// Define the normals for each face.
const Vector3 faceNormals[] =
{
Vector3(0.0f, 0.0f, 1.0f),
Vector3(1.0f, 0.0f, 0.0f),
Vector3(0.0f, 0.0f, -1.0f),
Vector3(-1.0f, 0.0f, 0.0f),
Vector3(0.0f, 1.0f, 0.0f),
Vector3(0.0f, -1.0f, 0.0f)
};
// Define tangents for each face.
const Vector3 faceTangent[] =
{
Vector3(1.0f, 0.0f, 0.0f),
Vector3(0.0f, 0.0f, -1.0f),
Vector3(-1.0f, 0.0f, 0.0f),
Vector3(0.0f, 0.0f, 1.0f),
Vector3(1.0f, 0.0f, 0.0f),
Vector3(1.0f, 0.0f, 0.0f)
};
uint16_t vertexIndex = 0;
// To generate the vertex data, loop the data arrays that you defined above.
for (unsigned int face = 0; face < 6; ++face)
{
// Add two triangles.
uint16_t* indexData = reinterpret_cast<uint16_t*>(&createInfo.clusters[0].indexData[0]);
unsigned int faceBaseIndex = face * 6;
// The first three vertices of a face define the first triangle...
indexData[faceBaseIndex + 0] = vertexIndex + 0;
indexData[faceBaseIndex + 1] = vertexIndex + 1;
indexData[faceBaseIndex + 2] = vertexIndex + 2;
// ...and the second triangle uses the two last vertices of the face with the first vertex.
// These two triangles create a quad.
indexData[faceBaseIndex + 3] = vertexIndex + 2;
indexData[faceBaseIndex + 4] = vertexIndex + 3;
indexData[faceBaseIndex + 5] = vertexIndex + 0;
// Write vertices for the face.
for (unsigned int faceCorner = 0; faceCorner < 4; ++faceCorner, ++vertexIndex)
{
unsigned int boxCorner = faceIndices[face][faceCorner];
// Write the correct vertex for this corner of each face.
writeVertexAttribute(createInfo, vertexIndex, 0, cornerPositions[boxCorner]);
// UVs for each face are the same.
writeVertexAttribute(createInfo, vertexIndex, 1, UVs[faceCorner]);
// Normal is constant throughout the face.
writeVertexAttribute(createInfo, vertexIndex, 2, faceNormals[face]);
// Tangent is constant throughout the face.
writeVertexAttribute(createInfo, vertexIndex, 3, faceTangent[face]);
}
}
}
// Create a Box and set it as the bounding volume of the mesh.
// Position the box in the center of the mesh. Set the dimensions of the box along each axis
// to 2.0f, that is, from point (-1, -1, -1) to point (1, 1, 1) in the local coordinate space
// of the mesh.
createInfo.boundingBox = Box::fromCenterAndSize(Vector3(0.0f, 0.0f, 0.0f), Vector3(2.0f, 2.0f, 2.0f));
Domain* domain =getDomain();
// Create a mesh from the data that you defined in the CreateInfo.
MeshSharedPtr mesh = Mesh::create(domain, createInfo, "box");
setMesh(mesh);
setRenderMode(RenderModeDefault);
//setRenderMode(RenderModeCustom);
//kzLogDebug(("========��ʼ�����ddd=========="));
}
渲染结果:
由结果可知,我们想通过插件来绘制MESH 并没有绘制出来
0
-
Good Day,
The post is being reviewed, and we will reply to you soon.0 -
Good Day,
Can you describe what you are trying to achieve? It may be easier, and simpler to just import a 3d mesh, instead of creating it with the API.
If you have a reference image of the feature you are looking to achieve I may be able to assist further.0
Please sign in to leave a comment.
Comments
2 comments