Quantcast
Channel: Cocos Forums - Latest topics
Viewing all articles
Browse latest Browse all 17070

Best practices for displaying generated geometry

$
0
0

@shadowphiar wrote:

Hello,
I've been wondering if it's possible to generate your own geometry for a Sprite3D instead of loading it from a model file?

I've constructed an example which runs without generating runtime errors, but never actually appears to draw anything. (I see an increase in the number of draw calls and vertices in the debug stats, but I don't see my cube on the screen). Have I missed something obvious?

Size size = Director::getInstance()->getWinSize();
camera = Camera::createPerspective(30.0f, size.width / size.height, 1.0f, 1000.0f);
camera->setPosition3D(Vec3(0.0f, 80.0f, 80.0f));
camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
camera->setCameraFlag(CameraFlag::USER1);
this->addChild(camera);

camera->setBackgroundBrush(CameraBackgroundBrush::createSkyboxBrush("skybox/posx.jpg", "skybox/negx.jpg","skybox/posy.jpg", "skybox/negy.jpg","skybox/posz.jpg", "skybox/negz.jpg"));

auto sprite3d = Sprite3D::create();
sprite3d->setPosition3D(Vec3(0.0,0.0,0));
sprite3d->setCameraMask((unsigned short)CameraFlag::USER1);

std::vector< MeshVertexAttrib > attribs = {
    { 3, GL_FLOAT, GLProgram::VERTEX_ATTRIB_POSITION,   3*sizeof(float) },
    { 3, GL_FLOAT, GLProgram::VERTEX_ATTRIB_NORMAL,     3*sizeof(float) },
    { 3, GL_FLOAT, GLProgram::VERTEX_ATTRIB_TANGENT,    3*sizeof(float) },
    { 3, GL_FLOAT, GLProgram::VERTEX_ATTRIB_BINORMAL,   3*sizeof(float) },
    { 2, GL_FLOAT, GLProgram::VERTEX_ATTRIB_TEX_COORD,  2*sizeof(float) }
};
int perVertexSizeInFloat =  14;  // 3+3+3+3+2

float x=0;
float y=0;
float z=0;
float s=20;
std::vector< float > vertices = {
    // position  normal  tangent binormal uv
    // +x
    x+s,y-s,z-s, 1,0,0,  0,0,-1, 0,-1,0, 0,0,
    x+s,y+s,z-s, 1,0,0,  0,0,-1, 0,-1,0, 1,0,
    x+s,y-s,z+s, 1,0,0,  0,0,-1, 0,-1,0, 0,1,
    x+s,y+s,z+s, 1,0,0,  0,0,-1, 0,-1,0, 1,1,
    // +y
    x-s,y+s,z-s, 0,1,0,  1,0,0,  0,0,1,  0,0,
    x+s,y+s,z-s, 0,1,0,  1,0,0,  0,0,1,  1,0,
    x-s,y+s,z+s, 0,1,0,  1,0,0,  0,0,1,  0,1,
    x+s,y+s,z+s, 0,1,0,  1,0,0,  0,0,1,  1,1,
    // +z
    x-s,y-s,z+s, 0,0,1,  0,0,-1, 1,0,0,  0,0,
    x+s,y-s,z+s, 0,0,1,  0,0,-1, 1,0,0,  1,0,
    x-s,y+s,z+s, 0,0,1,  0,0,-1, 1,0,0,  0,1,
    x+s,y+s,z+s, 0,0,1,  0,0,-1, 1,0,0,  1,1,
    // -x
    x-s,y-s,z-s, -1,0,0, 0,0,1,  0,-1,0, 0,0,
    x-s,y+s,z-s, -1,0,0, 0,0,1,  0,-1,0, 1,0,
    x-s,y-s,z+s, -1,0,0, 0,0,1,  0,-1,0, 0,1,
    x-s,y+s,z+s, -1,0,0, 0,0,1,  0,-1,0, 1,1,
    // -y
    x-s,y-s,z-s, 0,-1,0, -1,0,0, 0,0,1,  0,0,
    x+s,y-s,z-s, 0,-1,0, -1,0,0, 0,0,1,  1,0,
    x-s,y-s,z+s, 0,-1,0, -1,0,0, 0,0,1,  0,1,
    x+s,y-s,z+s, 0,-1,0, -1,0,0, 0,0,1,  1,1,
    // -z
    x-s,y-s,z-s, 0,0,-1,  0,0,1, 1,0,0,  0,0,
    x+s,y-s,z-s, 0,0,-1,  0,0,1, 1,0,0,  1,0,
    x-s,y+s,z-s, 0,0,-1,  0,0,1, 1,0,0,  0,1,
    x+s,y+s,z-s, 0,0,-1,  0,0,1, 1,0,0,  1,1,
};
MeshData::IndexArray indices = { 0, 0, 1, 2, 3, 3,  //+x
                                 4, 4, 5, 6, 7, 7,  //+y
                                 8, 8, 9, 10,11,11, //+z
                                 12,12,13,14,15,15, //-x
                                 16,16,17,18,19,19, //-y
                                 20,20,21,22,23,23  //-z
};

auto mesh = Mesh::create(vertices, perVertexSizeInFloat, indices, attribs);
sprite3d->addMesh(mesh);

sprite3d->setMaterial(Sprite3DMaterial::createBuiltInMaterial(Sprite3DMaterial::MaterialType::UNLIT, false));
sprite3d->setTexture("HelloWorld.png");

this->addChild(sprite3d);

(Background: Each level of my game will contain a bunch of similar objects which each contain a single mesh, and can share one material and texture, and never move relative to one another. I believe it should be possible to draw them all with a single call and substantially improve mobile performance. Is this a valid approach, or will cocos at some point be able to batch my Sprite3D objects together automatically, and save me the trouble of implementing it myself?)

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 17070

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>