#include <maya/MStatus.h>
#include <maya/MFnPlugin.h>
#include <maya/MFnMesh.h>
#include <maya/MDrawRegistry.h>
#include <maya/MPxVertexBufferGenerator.h>
#include <maya/MHWGeometry.h>
{
public:
MyCustomBufferGenerator() {}
~MyCustomBufferGenerator() override {}
bool getSourceIndexing(
const MObject&
object,
{
if (!status) return false;
int faceNum = 0;
if (numPolys <= 0)
return false;
for(int i = 0; i < numPolys; ++i)
{
int faceColorID = faceNum%3;
for (int x = 0; x < vertexCount; ++x)
{
vertToFaceVertIDs.
append(faceColorID);
}
faceNum++;
}
return true;
}
bool getSourceStreams(
const MObject&
object,
{
return false;
}
void createVertexStream(
const MObject&
object,
{
return;
return;
return;
if (!status) return;
unsigned int vertexCount = indices.
length();
if (vertexCount <= 0)
return;
float* buffer = (
float*)vertexBuffer.
acquire(vertexCount,
true );
float* start = buffer;
for(unsigned int i = 0; i < vertexCount; ++i)
{
*(buffer++) = 1.0f;
*(buffer++) = (float)indices[i];
}
}
};
{
public:
MyCustomBufferGenerator2() {}
~MyCustomBufferGenerator2() override {}
bool getSourceIndexing(
const MObject&
object,
{
if (!status) return false;
mesh.getVertices(vertexCount, vertexList);
for(
unsigned int i = 0; i < vertexList.
length(); ++i)
vertices.
append( (
unsigned int)vertexList[i] );
return true;
}
bool getSourceStreams(
const MObject&
object,
{
sourceStreams.
append(
"Position" );
sourceStreams.
append(
"Normal" );
return true;
}
void createVertexStream(
const MObject&
object,
{
return;
if (dimension != 4 && dimension != 3)
return;
return;
if (!status) return;
unsigned int vertexCount = indices.
length();
if (vertexCount <= 0)
return;
return;
if(positionDimension != 3 && positionDimension != 4)
return;
return;
if(normalDimension != 3 && normalDimension != 4)
return;
float* positionBuffer = (
float*)positionStream->
map();
if(positionBuffer)
{
float* normalBuffer = (
float*)normalStream->
map();
if(normalBuffer)
{
void* compositeBuffer = vertexBuffer.
acquire(vertexCount,
true );
if(compositeBuffer)
{
void* compositeBufferStart = compositeBuffer;
float* compositeBufferAsFloat = (float*)compositeBuffer;
int* compositeBufferAsInt = (int*)compositeBuffer;
for(unsigned int i = 0; i < vertexCount; ++i)
{
{
*(compositeBufferAsFloat++) = *(positionBuffer + 1);
*(compositeBufferAsFloat++) = *(positionBuffer + 2);
*(compositeBufferAsFloat++) = *(normalBuffer);
if(dimension == 4)
*(compositeBufferAsFloat++) = *(normalBuffer + 2);
}
{
*(compositeBufferAsInt++) = (int)(*(positionBuffer + 1) * 255);
*(compositeBufferAsInt++) = (int)(*(positionBuffer + 2) * 255);
*(compositeBufferAsInt++) = (int)(*(normalBuffer) * 255);
if(dimension == 4)
*(compositeBufferAsInt++) = (int)(*(normalBuffer + 2) * 255);
}
positionBuffer += positionDimension;
normalBuffer += normalDimension;
}
vertexBuffer.
commit(compositeBufferStart);
}
}
}
}
};
{
return new MyCustomBufferGenerator();
}
{
return new MyCustomBufferGenerator2();
}
static void getCustomSemantics(
MString& customSemantic,
MString& customSemantic2)
{
if( renderer ) {
if( renderer->
drawAPI() == MHWRender::kDirectX11 ) {
customSemantic = "myCustomStream";
customSemantic2 = "myCustomStreamB";
}
else if( renderer->
drawAPI() == MHWRender::kOpenGLCoreProfile ) {
customSemantic = "myCustomStream";
customSemantic2 = "myCustomStreamB";
}
else if( renderer->
drawAPI() == MHWRender::kOpenGL ) {
customSemantic = "ATTR8";
customSemantic2 = "ATTR7";
}
}
}
{
MString customSemantic, customSemantic2;
getCustomSemantics(customSemantic, customSemantic2);
return MS::kSuccess;
}
{
MString customSemantic, customSemantic2;
getCustomSemantics(customSemantic, customSemantic2);
return MS::kSuccess;
}