Cocos2d-x的3D特性

2015年03月21日 15:34 0 点赞 0 评论 更新于 2025-11-21 13:31

一、Sprite3D概述

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

示例代码

以下是一个创建Sprite3D对象并设置其缩放和纹理的示例:

// 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支持以下三种格式:

  1. obj格式:obj是通过3ds Max或Maya导出的一种格式。Sprite3D可以直接使用该格式的资源创建对象。不过,obj格式不支持动画。
  2. c3t(Cocos 3D文本)格式:c3t是通过fbv - conv工具从FBX格式转换而来的一种Json格式。这种格式易于读取,方便开发者检测数据模型。但c3t文件体积较大,因此不建议在游戏的最终版本中使用。
  3. 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系统

在Mac OS X系统中,使用以下命令:

$ cd COCOS2DX_ROOT/tools/fbx-conv/mac
$ ./fbx-conv [-a|-b|-t] FBXFile

Windows系统

在Windows系统中,使用以下命令:

cd COCOS2DX_ROOT/tools/fbx-conv/windows
fbx-conv [-a|-b|-t] FBXFile

选项说明

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

四、3D动画(Animation 3D)

要运行Sprite3D的动画,可以使用Animation3DAnimate3D类。Animation3D可以使用“c3b”或“c3t”格式的文件创建,而Animate3D则通过Animation3D对象创建。由于Animate3DActionInterval的子类,所以可以使用runAction()方法来运行动画,这与AnimationAnimate之间的关系类似。在testcpp中有一个名为“Testing Animate3D”的示例。

示例代码

// 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);
int rand2 = std::rand();
if (rand2 % 3 == 1)
{
animate->setSpeed(animate->getSpeed() + CCRANDOM_0_1());
}
else if (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中找到相关代码。

示例代码

// 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");

作者信息

feifeila

feifeila

共发布了 3994 篇文章