Sprite3D与Sprite相似,只不过它是3D的。你可以使用内置的create函数初始化一个Sprite3D对象。

如:

1
2
3
4
5
6
7
8
// test case: cpp-tests->Sprite3DTest->Testing Sprite3D
// source: cocos2d-x/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp
// function: Sprite3DBasicTest::addNewSpriteWithCoords(Vec2 p)
 
//create a Sprite3D by the 'boss1.obj' and set its scale and texture
auto sprite = Sprite3D::create("Sprite3DTest/boss1.obj");
sprite->setScale(3.f);
sprite->setTexture("Sprite3DTest/boss.png");

作为Node的子类,Sprite3D可以像精灵一样手动转换或使用Actions。

Sprite3D支持的格式

目前 Sprite3D 支持以下三种格式

  • obj:obj是通过3ds Max或Maya导出的一种格式。Sprite3D可以通过该格式的资源立即被创建出来。不过obj格式不支持动画。
  • c3t(Cocos 3D文本):c3t是通过fbv-conv工具从FBX格式转换而来的一种Json格式。c3t格式可以很容易地读取,这也意味着它能够通过发展中的c3t格式文件检测你的数据模型。但是,c3t是一个大文件格式,所以我们不建议在最终版本的游戏中使用它。因为,我们还有另一种可用格式——c3b。
  • c3b(Cocos 3D二进制):c3b是一个二进制文件。它也是通过fbx-conv工具转换而来的一种格式,同样使用FBX格式文件转换得来。它不能被读取,但它比c3t体积小、运行速度快。大多数时候我们都会选择c3t格式开发或调试,然而在游戏中则使用c3b格式。

fbv-conv的用法

使用c3t或c3b格式时,我们都需要fbx-conv工具。它可以把FBX格式转换为更加友好的运行时格式。在Cocos2d-x v3.2中,你可以在cocos2d-x-3.2rc0/tools/fbx-conv 目录下找到该工具。

  • Mac OS X
1
2
$ cd COCOS2DX_ROOT/tools/fbx-conv/mac
$ ./fbx-conv [-a|-b|-t] FBXFile
  • Windows
1
2
cd COCOS2DX_ROOT/tools/fbx-conv/windows
fbx-conv [-a|-b|-t] FBXFile

选项

  • -a:输出文本和二进制格式
  • -b:输出二进制格式
  • -t:输出文本格式

Animation 3D

运行Sprite3D的动画我们可以使用 Animation3D 和 Animate3D类。Animation3D可以用“c3b”或“c3t”格式的文件创建,Animate3D则通过Animation3D对象创建。Animate3D是ActionInterval的子类,所以你可以使用runAction()运行它。这就像是Animation与Animate之间的关系一样。在testcpp中有一个名为Testing Animate3D的例子。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// test case: cpp-tests->Sprite3DTest
// source: cocos2d-x/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp
// function:  Sprite3DWithSkinTest::addNewSpriteWithCoords(Vec2 p)
 
std::string fileName ="Sprite3DTest/orc.c3b";     
auto sprite = Sprite3D::create(fileName);
sprite->setScale(3);
sprite->setRotation3D(Vec3(0,180,0));
addChild(sprite);
sprite->setPosition( Vec2( p.x, p.y) );
 
//Create animation3D by the same file of its Spirte3D
auto animation = Animation3D::create(fileName);
if(animation)
    {
    //create the Animate3D
    auto animate = Animate3D::create(animation);
    if(std::rand() %3 == 0)
        {
            animate->setPlayBack(true);       
            intrand2 = std::rand();
            if(rand2 % 3 == 1)
                {
                    animate->setSpeed(animate->getSpeed() + CCRANDOM_0_1());
                }
            elseif(rand2 % 3 == 2)
            {
                animate->setSpeed(animate->getSpeed() - 0.5 * CCRANDOM_0_1());
            }
 
       //Run Animate
       sprite->runAction(RepeatForever::create(animate));       
}

Sprite3D特效

Sprite3D特效这一功能将改变引擎大量的核心代码,所以它目前还不被v3.2支持。不过,在EarthWarrior3D(它将教你如何创建自己的特效)中有一个实现了3D轮廓特效的示例。开发者可以在testCpp中找到这个代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// test case: cpp-tests->Sprite3DTest->Sprite3D with effects
// source: cocos2d-x/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp
// function: Sprite3DEffectTest::addNewSpriteWithCoords(Vec2 p)
 
//create a 'Sprite3D' by the 'boss1.obj' and add the effect3D outline to the sprite3D
 
//EffectSprite3D is an subclass of 'Sprite3D' which can add or set an effect to the sprite3D
auto sprite = EffectSprite3D::createFromObjFileAndTexture("Sprite3DTest/boss1.obj","Sprite3DTest/boss.png");   
0