Cocos2d-x3.3RC0之LightTest分析

Cocos2d-x3.3RC0之LightTest分析,第1张

概述1、头文件 //LightTest.hclass LightTest : public Layer{public: CREATE_FUNC(LightTest); LightTest(); virtual ~LightTest(); virtual void update(float delta); void SwitchLight(Ref* 1、头文件
//lightTest.hclass lightTest : public Layer{public:    CREATE_FUNC(lightTest);    lighttest();    virtual ~lighttest();        virtual voID update(float delta);    voID Switchlight(Ref* sender,lightType lightType);//转换光源    private:        voID addSprite();//添加精灵    voID addlights();//添加光源    private:        AmbIEntlight *_ambIEntlight;       //环境光,想四周发射光线,模拟一个屋子的环境,光线来源方向不定。    Directionlight *_directionallight; //方向光,表示一个非常远的光源,投射光线是平行的,用来模拟太阳光    Pointlight *_pointlight;           //点光,从空间某点向四周均匀发射有限长的光线,用来模拟向四周发射状的光    Spotlight *_spotlight;             //聚光,从某点发射一个圆锥形状,用来模拟台灯        Label *_ambIEntlightLabel;    Label *_directionallightLabel;    Label *_pointlightLabel;    Label *_spotlightLabel;};
2、Cpp文件
#include "lightTest.h"//构造函数,初始化除环境光AmbIEntlight之外的三种光为nullptrlightTest::lighttest(): _directionallight(nullptr),_pointlight(nullptr),_spotlight(nullptr){    addSprite();//添加精灵    addlights();//添加光源    scheduleUpdate();//帧更新        //设置摄像机位置    auto s = Director::getInstance()->getWinSize();    //参数1:透视相机的视野,通常在40-60度范围    //参数2:相机的长宽比,通常视窗的宽度除以视窗的高度    //参数3:近点平面距离    //参数4:原点平面距离    auto camera = Camera::createPerspective(60,(GLfloat)s.wIDth/s.height,1.0f,1000.0f);    camera->setCameraFlag(CameraFlag::USER1);//设置相机Flag    camera->setposition3D(Vec3(0.0,100,100));//设置相机3D坐标    //参数1:目标的中心位置,参数2:向上的向量    camera->lookAt(Vec3(0.0,0.0,0.0),Vec3(0.0,1.0,0.0));    addChild(camera);        //添加相机转换的开光,Label控件    TTFConfig ttfConfig("Fonts/arial.ttf",30);    _ambIEntlightLabel = Label::createWithTTF(ttfConfig,"AmbIEnt light ON");    _ambIEntlightLabel->retain();    auto menuItem0 = MenuItemLabel::create(_ambIEntlightLabel,CC_CALLBACK_1(lightTest::Switchlight,this,lightType::AMBIENT));    _directionallightLabel = Label::createWithTTF(ttfConfig,"Directional light OFF");    _directionallightLabel->retain();    auto menuItem1 = MenuItemLabel::create(_directionallightLabel,lightType::DIRECTIONAL));    _pointlightLabel = Label::createWithTTF(ttfConfig,"Point light OFF");    _pointlightLabel->retain();    auto menuItem2 = MenuItemLabel::create(_pointlightLabel,lightType::POINT));    _spotlightLabel = Label::createWithTTF(ttfConfig,"Spot light OFF");    _spotlightLabel->retain();    auto menuItem3 = MenuItemLabel::create(_spotlightLabel,lightType::SPOT));    auto menu = Menu::create(menuItem0,menuItem1,menuItem2,menuItem3,nullptr);    menu->setposition(Vec2::ZERO);    menuItem0->setAnchorPoint(Vec2::ANCHOR_top_left);    menuItem0->setposition( Vec2(VisibleRect::left().x,VisibleRect::top().y-50) );    menuItem1->setAnchorPoint(Vec2::ANCHOR_top_left);    menuItem1->setposition( Vec2(VisibleRect::left().x,VisibleRect::top().y-100) );    menuItem2->setAnchorPoint(Vec2::ANCHOR_top_left);    menuItem2->setposition( Vec2(VisibleRect::left().x,VisibleRect::top().y -150));    menuItem3->setAnchorPoint(Vec2::ANCHOR_top_left);    menuItem3->setposition( Vec2(VisibleRect::left().x,VisibleRect::top().y -200));    addChild(menu);}//析构资源lightTest::~lighttest(){    if (_spotlightLabel)        _spotlightLabel->release();        if (_pointlightLabel)        _pointlightLabel->release();        if (_directionallightLabel)        _directionallightLabel->release();        if (_spotlight)        _spotlight->release();        if (_pointlight)        _pointlight->release();        if (_directionallight)        _directionallight->release();        if (_ambIEntlight)        _ambIEntlight->release();}//添加精灵voID lightTest::addSprite(){    auto s = Director::getInstance()->getWinSize();        {        //创建3D对象,c3b是二进制文件,通过FBX模型文件进行二次转换得到。        //obj:FBX是一种通用导出格式,可通过maya和max导出,maya和max默认导出obj格式文件。        //c3t:通过FBX模型文件转换后生成的Json文件,体积大载入慢,不提倡使用        //c3b:二进制文件,数据内容与c3t一样,体积小,速度快,提倡使用        //注意:.c3b文件的使用需和它对应的贴图拷贝到Resource目录下,否则,运行时找不到资源。        std::string filename = "orc.c3b";        //创建3D精灵对象        auto sprite = Sprite3D::create(filename);        sprite->setRotation3D(Vec3(0.0,180.0,0.0));        sprite->setposition(Vec2(0.0,0.0));        sprite->setScale(2.0);        //创建3D精灵对象        auto sp = Sprite3D::create("axe.c3b");        //通过骨骼名称获得连接点        sprite->getAttachNode("Bip001 R Hand")->addChild(sp);        //创建3D动画        auto animation = Animation3D::create(filename);        if (animation)        {            auto animate = Animate3D::create(animation);            sprite->runAction(RepeatForever::create(animate));//3D精灵执行3D动画        }                addChild(sprite);        //设置精灵的Mask,该值与相机的flag进行&运算为真,则对相机可见        sprite->setCameraMask(2);    }        {        //同上        std::string filename = "sphere.c3b";        auto sprite = Sprite3D::create(filename);        sprite->setposition(Vec2(30.0,0.0));        addChild(sprite);        sprite->setCameraMask(2);    }        {        //同上        std::string filename = "sphere.c3b";        auto sprite = Sprite3D::create(filename);        sprite->setScale(0.5f);        sprite->setposition(Vec2(-50.0,0.0));        addChild(sprite);        sprite->setCameraMask(2);    }        {        //同上        std::string filename = "sphere.c3b";        auto sprite = Sprite3D::create(filename);        sprite->setScale(0.5f);        sprite->setposition(Vec2(-30.0,10.0));        addChild(sprite);        sprite->setCameraMask(2);    }}//添加光源voID lightTest::addlights(){    auto s = Director::getInstance()->getWinSize();     //环境光    _ambIEntlight = AmbIEntlight::create(color3B(200,200,200));    _ambIEntlight->retain();    _ambIEntlight->setEnabled(true);//开启光源    addChild(_ambIEntlight);    _ambIEntlight->setCameraMask(2);//确保相机可见        //方向光,参数1:光源方向.参数2:光源颜色    _directionallight = Directionlight::create(Vec3(-1.0f,-1.0f,0.0f),color3B(200,200));    _directionallight->retain();    _directionallight->setEnabled(false);//光源关闭    addChild(_directionallight);    _directionallight->setCameraMask(2);//确保相机可见        //点光,参数1:光源的位置,参数2:光源的颜色。参数3:光源范围    _pointlight = Pointlight::create(Vec3(0.0f,0.0f,200),10000.0f);    _pointlight->retain();    _pointlight->setEnabled(false);//光源关闭    addChild(_pointlight);    _pointlight->setCameraMask(2);//确保相机可见        //聚光,参数1:方向。参数2:位置。参数3:颜色,参数4:内圆弧度。参数5:外圆弧度。参数6:光源范围    _spotlight = Spotlight::create(Vec3(-1.0f,Vec3(0.0f,0.5,10000.0f);    _spotlight->retain();    _spotlight->setEnabled(false);//光源关闭    addChild(_spotlight);    _spotlight->setCameraMask(2);//确保相机可见        {        auto tintto1 = TintTo::create(4,255);        auto tintto2 = TintTo::create(4,255,0);        auto tintto3 = TintTo::create(4,0);        auto tintto4 = TintTo::create(4,255);        auto seq = Sequence::create(tintto1,tintto2,tintto3,tintto4,nullptr);        _ambIEntlight->runAction(RepeatForever::create(seq));//光源执行渐隐动作    }        {        auto tintto1 = TintTo::create(4,0);        auto tintto2 = TintTo::create(4,255);        auto tintto4 = TintTo::create(4,nullptr);        _directionallight->runAction(RepeatForever::create(seq));//光源执行渐隐动作    }        {        auto tintto1 = TintTo::create(4,255);        auto seq = Sequence::create(tintto2,tintto1,nullptr);        _pointlight->runAction(RepeatForever::create(seq));//光源执行渐隐动作    }        {        auto tintto1 = TintTo::create(4,255);        auto seq = Sequence::create(tintto3,nullptr);        _spotlight->runAction(RepeatForever::create(seq));//光源执行渐隐动作    }}//帧更新voID lightTest::update( float delta ){    static float angleDelta = 0.0;        if (_directionallight)    {        _directionallight->setRotation3D(Vec3(-45.0,-CC_radians_TO_degrees(angleDelta),0.0f));//设置旋转角度,宏是弧度转化为角度    }    //cosf与sinf:float版的cos和sin    if (_pointlight)    {        _pointlight->setpositionX(100.0f * cosf(angleDelta + 2.0 * delta));        _pointlight->setpositionY(100.0f);        _pointlight->setpositionZ(100.0f * sinf(angleDelta + 2.0 * delta));    }        if (_spotlight)    {        _spotlight->setpositionX(100.0f * cosf(angleDelta + 4.0 * delta));        _spotlight->setpositionY(100.0f);        _spotlight->setpositionZ(100.0f * sinf(angleDelta + 4.0 * delta));        _spotlight->setDirection(-Vec3(cosf(angleDelta + 4.0 * delta),sinf(angleDelta + 4.0 * delta)));    }        angleDelta += delta;    //    update(delta);}//光源开关voID lightTest::Switchlight( Ref* sender,lightType lightType ){    switch (lightType)    {        case lightType::AMBIENT:        {            char str[32];            bool isON = !_ambIEntlight->isEnabled();            sprintf(str,"AmbIEnt light %s",isON == true? "ON":"OFF");            _ambIEntlight->setEnabled(isON);            _ambIEntlightLabel->setString(str);        }            break;                    case lightType::DIRECTIONAL:        {            char str[32];            bool isON = !_directionallight->isEnabled();            sprintf(str,"Directional light %s",isON == true? "ON":"OFF");            _directionallight->setEnabled(isON);            _directionallightLabel->setString(str);        }            break;                    case lightType::POINT:        {            char str[32];            bool isON = !_pointlight->isEnabled();            sprintf(str,"Point light %s",isON == true? "ON":"OFF");            _pointlight->setEnabled(isON);            _pointlightLabel->setString(str);        }            break;                    case lightType::SPOT:        {            char str[32];            bool isON = !_spotlight->isEnabled();            sprintf(str,"Spot light %s",isON == true? "ON":"OFF");            _spotlight->setEnabled(isON);            _spotlightLabel->setString(str);        }            break;                    default:            break;    }}
3、使用 在HelloWorldScene.cpp中的 createScene()中,创建lightTest的对象,加入Scene 总结

以上是内存溢出为你收集整理的Cocos2d-x3.3RC0之LightTest分析全部内容,希望文章能够帮你解决Cocos2d-x3.3RC0之LightTest分析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1008856.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存