cocos2d-X 3.X中3D模型不显示&&LocalZOrder和GlobalZOrder关系 - 不迷阿狸

时间:2024-02-19 21:45:47

cocos2d-X 3.X中3D模型不显示&&LocalZOrder和GlobalZOrder关系

如果3d模型正确的话,但是加载后不能显示,大多数是因为被2d的Layer挡住了。这个时候无论怎么设置localzorder都没用,只要设置globalzorder就好。

1.将2dLayer的GlobalZOrder设置为<=0;

游戏背景代码如下:

Sprite* backGround = Sprite::create(PIC_PATH+std::string("gameBackground.png"));
    backGround->setPosition(Point(480,270));
    backGround->setScale(1.2f);
    backGround->setGlobalZOrder(0);

2.将3d模型的GlobalZOrder设置为>0;

坦克模型代码如下:

sp3 = Sprite3D::create(OBJ_PATH + std::string("3Dtank.obj"),OBJ_PATH+std::string("3Dtank.png"));
    sp3->setScale(0.075);
    sp3->setGlobalZOrder(2);
    sp3->setPosition(Point(origin.x + s.width / 3 + 30, origin.y + 130));
    this->addChild(sp3, 5);

运行效果图:

 

localzorder和globalzorder的区别与联系

cocos2dx 3.x中的setzorder为了兼容2.x调用的是setlocalzorder,源代码如下:

virtual void setLocalZOrder(int localZOrder);

    CC_DEPRECATED_ATTRIBUTE virtual void setZOrder(int localZOrder) { setLocalZOrder(localZOrder); }

  3.x中的localZorder和2.x中的zorder意思相同。

而globalzorder是3.x中新添加的,其源码声明如下:

/**
     Defines the order in which the nodes are renderer.
     Nodes that have a Global Z Order lower, are renderer first.
     
     In case two or more nodes have the same Global Z Order, the order is not guaranteed.
     The only exception if the Nodes have a Global Z Order == 0. In that case, the Scene Graph order is used.
     
     By default, all nodes have a Global Z Order = 0. That means that by default, the Scene Graph order is used to render the nodes.
     
     Global Z Order is useful when you need to render nodes in an order different than the Scene Graph order.
     
     Limitations: Global Z Order can\'t be used by Nodes that have SpriteBatchNode as one of their ancestors.
     And if ClippingNode is one of the ancestors, then "global Z order" will be relative to the ClippingNode.

     @see `setLocalZOrder()`
     @see `setVertexZ()`

     @since v3.0
     *
     * @param globalZOrder The global Z order value.
     */
    virtual void setGlobalZOrder(float globalZOrder);

如注释所说,每个Node的GlobalZorder默认为0。当node的Global Z Order == 0时,按照localzorder顺序进行渲染。

所以每个node的渲染顺序应该先按照GlobalZorder顺序,默认为0的node按照localZorder顺序。

当涉及到3D模型时,使用GlobalZorder设置渲染顺序。

2D的层,就使用普通的addchild(sprite*,localzorder);即可