(11) Cocos2d-x3.3RC0之LightTest分析

前端之家收集整理的这篇文章主要介绍了(11) Cocos2d-x3.3RC0之LightTest分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、头文件

  1. //LightTest.h
  2. classLightTest:publicLayer
  3. {
  4. public:
  5. CREATE_FUNC(LightTest);
  6. LightTest();
  7. virtual~LightTest();
  8. virtualvoidupdate(floatdelta);
  9. voidSwitchLight(Ref*sender,LightTypelightType);//转换光源
  10. private:
  11. voidaddSprite();//添加精灵
  12. voidaddLights();//添加光源
  13. private:
  14. AmbientLight*_ambientLight;//环境光,想四周发射光线,模拟一个屋子的环境,光线来源方向不定。
  15. DirectionLight*_directionalLight;//方向光,表示一个非常远的光源,投射光线是平行的,用来模拟太阳光
  16. PointLight*_pointLight;//点光,从空间某点向四周均匀发射有限长的光线,用来模拟向四周发射状的光
  17. SpotLight*_spotLight;//聚光,从某点发射一个圆锥形状,用来模拟台灯
  18. Label*_ambientLightLabel;
  19. Label*_directionalLightLabel;
  20. Label*_pointLightLabel;
  21. Label*_spotLightLabel;
  22. };

2、Cpp文件

  1. #include"LightTest.h"
  2. //构造函数,初始化除环境光AmbientLight之外的三种光为nullptr
  3. LightTest::LightTest()
  4. :_directionalLight(nullptr)
  5. ,_pointLight(nullptr)
  6. ,_spotLight(nullptr)
  7. {
  8. addSprite();//添加精灵
  9. addLights();//添加光源
  10. scheduleUpdate();//帧更新
  11. //设置摄像机位置
  12. autos=Director::getInstance()->getWinSize();
  13. //参数1:透视相机的视野,通常在40-60度范围
  14. //参数2:相机的长宽比,通常视窗的宽度除以视窗的高度
  15. //参数3:近点平面距离
  16. //参数4:原点平面距离
  17. autocamera=Camera::createPerspective(60,(GLfloat)s.width/s.height,1.0f,1000.0f);
  18. camera->setCameraFlag(CameraFlag::USER1);//设置相机Flag
  19. camera->setPosition3D(Vec3(0.0,100,100));//设置相机3D坐标
  20. //参数1:目标的中心位置,参数2:向上的向量
  21. camera->lookAt(Vec3(0.0,0.0,0.0),Vec3(0.0,1.0,0.0));
  22. addChild(camera);
  23. //添加相机转换的开光,Label控件
  24. TTFConfigttfConfig("fonts/arial.ttf",30);
  25. _ambientLightLabel=Label::createWithTTF(ttfConfig,"AmbientLightON");
  26. _ambientLightLabel->retain();
  27. automenuItem0=MenuItemLabel::create(_ambientLightLabel,CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::AMBIENT));
  28. _directionalLightLabel=Label::createWithTTF(ttfConfig,"DirectionalLightOFF");
  29. _directionalLightLabel->retain();
  30. automenuItem1=MenuItemLabel::create(_directionalLightLabel,LightType::DIRECTIONAL));
  31. _pointLightLabel=Label::createWithTTF(ttfConfig,"PointLightOFF");
  32. _pointLightLabel->retain();
  33. automenuItem2=MenuItemLabel::create(_pointLightLabel,LightType::POINT));
  34. _spotLightLabel=Label::createWithTTF(ttfConfig,"SpotLightOFF");
  35. _spotLightLabel->retain();
  36. automenuItem3=MenuItemLabel::create(_spotLightLabel,LightType::SPOT));
  37. automenu=Menu::create(menuItem0,menuItem1,menuItem2,menuItem3,nullptr);
  38. menu->setPosition(Vec2::ZERO);
  39. menuItem0->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
  40. menuItem0->setPosition(Vec2(VisibleRect::left().x,VisibleRect::top().y-50));
  41. menuItem1->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
  42. menuItem1->setPosition(Vec2(VisibleRect::left().x,VisibleRect::top().y-100));
  43. menuItem2->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
  44. menuItem2->setPosition(Vec2(VisibleRect::left().x,VisibleRect::top().y-150));
  45. menuItem3->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
  46. menuItem3->setPosition(Vec2(VisibleRect::left().x,VisibleRect::top().y-200));
  47. addChild(menu);
  48. }
  49. //析构资源
  50. LightTest::~LightTest()
  51. {
  52. if(_spotLightLabel)
  53. _spotLightLabel->release();
  54. if(_pointLightLabel)
  55. _pointLightLabel->release();
  56. if(_directionalLightLabel)
  57. _directionalLightLabel->release();
  58. if(_spotLight)
  59. _spotLight->release();
  60. if(_pointLight)
  61. _pointLight->release();
  62. if(_directionalLight)
  63. _directionalLight->release();
  64. if(_ambientLight)
  65. _ambientLight->release();
  66. }
  67. voidLightTest::addSprite()
  68. //创建3D对象,c3b是二进制文件,通过FBX模型文件进行二次转换得到。
  69. //obj:FBX是一种通用导出格式,可通过maya和max导出,maya和max默认导出obj格式文件
  70. //c3t:通过FBX模型文件转换后生成的Json文件,体积大载入慢,不提倡使用
  71. //c3b:二进制文件,数据内容与c3t一样,体积小,速度快,提倡使用
  72. //注意:.c3b文件的使用需和它对应的贴图拷贝到Resource目录下,否则,运行时找不到资源。
  73. std::stringfileName="orc.c3b";
  74. //创建3D精灵对象
  75. autosprite=Sprite3D::create(fileName);
  76. sprite->setRotation3D(Vec3(0.0,180.0,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> sprite->setPosition(Vec2(0.0,0.0));
  77. sprite->setScale(2.0);
  78. //创建3D精灵对象
  79. autosp=Sprite3D::create("axe.c3b");
  80. //通过骨骼名称获得连接点
  81. sprite->getAttachNode("Bip001RHand")->addChild(sp);
  82. //创建3D动画
  83. autoanimation=Animation3D::create(fileName);
  84. if(animation)
  85. autoanimate=Animate3D::create(animation);
  86. sprite->runAction(RepeatForever::create(animate));//3D精灵执行3D动画
  87. addChild(sprite);
  88. //设置精灵的Mask,该值与相机的flag进行&运算为真,则对相机可见
  89. sprite->setCameraMask(2);
  90. //同上
  91. std::stringfileName="sphere.c3b";
  92. sprite->setPosition(Vec2(30.0,248); margin:0px!important; padding:0px 3px 0px 10px!important"> sprite->setCameraMask(2);
  93. //同上
  94. std::stringfileName="sphere.c3b";
  95. autosprite=Sprite3D::create(fileName);
  96. sprite->setScale(0.5f);
  97. sprite->setPosition(Vec2(-50.0,248); margin:0px!important; padding:0px 3px 0px 10px!important"> sprite->setPosition(Vec2(-30.0,10.0));
  98. voidLightTest::addLights()
  99. autos=Director::getInstance()->getWinSize();
  100. //环境光
  101. _ambientLight=AmbientLight::create(Color3B(200,200,200));
  102. _ambientLight->retain();
  103. _ambientLight->setEnabled(true);//开启光源
  104. addChild(_ambientLight);
  105. _ambientLight->setCameraMask(2);//确保相机可见
  106. //方向光,参数1:光源方向.参数2:光源颜色
  107. _directionalLight=DirectionLight::create(Vec3(-1.0f,-1.0f,0.0f),Color3B(200,200));
  108. _directionalLight->retain();
  109. _directionalLight->setEnabled(false);//光源关闭
  110. addChild(_directionalLight);
  111. _directionalLight->setCameraMask(2);//确保相机可见
  112. //点光,参数1:光源的位置,参数2:光源的颜色。参数3:光源范围
  113. _pointLight=PointLight::create(Vec3(0.0f,0.0f,200),10000.0f);
  114. _pointLight->retain();
  115. _pointLight->setEnabled(//光源关闭
  116. addChild(_pointLight);
  117. _pointLight->setCameraMask(2);//聚光,参数1:方向。参数2:位置。参数3:颜色,参数4:内圆弧度。参数5:外圆弧度。参数6:光源范围
  118. _spotLight=SpotLight::create(Vec3(-1.0f,Vec3(0.0f,0.5,10000.0f);
  119. _spotLight->retain();
  120. _spotLight->setEnabled( addChild(_spotLight);
  121. _spotLight->setCameraMask(2); autotintto1=TintTo::create(4,255);
  122. autotintto2=TintTo::create(4,0);
  123. autotintto3=TintTo::create(4,0);
  124. autotintto4=TintTo::create(4,255);
  125. autoseq=Sequence::create(tintto1,tintto2,tintto3,tintto4,248); margin:0px!important; padding:0px 3px 0px 10px!important"> _ambientLight->runAction(RepeatForever::create(seq));//光源执行渐隐动作
  126. autotintto1=TintTo::create(4,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> autotintto2=TintTo::create(4,248); margin:0px!important; padding:0px 3px 0px 10px!important"> autotintto3=TintTo::create(4,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> autotintto4=TintTo::create(4,248); margin:0px!important; padding:0px 3px 0px 10px!important"> autoseq=Sequence::create(tintto1,nullptr);
  127. _directionalLight->runAction(RepeatForever::create(seq));//光源执行渐隐动作
  128. autoseq=Sequence::create(tintto2,tintto1,248); margin:0px!important; padding:0px 3px 0px 10px!important"> _pointLight->runAction(RepeatForever::create(seq)); autoseq=Sequence::create(tintto3,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> _spotLight->runAction(RepeatForever::create(seq));voidLightTest::update(floatdelta)
  129. staticfloatangleDelta=0.0;
  130. if(_directionalLight)
  131. _directionalLight->setRotation3D(Vec3(-45.0,-CC_RADIANS_TO_DEGREES(angleDelta),0.0f));//设置旋转角度,宏是弧度转化为角度
  132. //cosf与sinf:float版的cos和sin
  133. if(_pointLight)
  134. _pointLight->setPositionX(100.0f*cosf(angleDelta+2.0*delta));
  135. _pointLight->setPositionY(100.0f);
  136. _pointLight->setPositionZ(100.0f*sinf(angleDelta+2.0*delta));
  137. if(_spotLight)
  138. _spotLight->setPositionX(100.0f*cosf(angleDelta+4.0*delta));
  139. _spotLight->setPositionY(100.0f);
  140. _spotLight->setPositionZ(100.0f*sinf(angleDelta+4.0*delta));
  141. _spotLight->setDirection(-Vec3(cosf(angleDelta+4.0*delta),sinf(angleDelta+4.0*delta)));
  142. angleDelta+=delta;
  143. //update(delta);
  144. //光源开关
  145. voidLightTest::SwitchLight(Ref*sender,LightTypelightType)
  146. switch(lightType)
  147. caseLightType::AMBIENT:
  148. charstr[32];
  149. boolisON=!_ambientLight->isEnabled();
  150. sprintf(str,"AmbientLight%s",isON==true?"ON":"OFF");
  151. _ambientLight->setEnabled(isON);
  152. _ambientLightLabel->setString(str);
  153. break;
  154. caseLightType::DIRECTIONAL:
  155. boolisON=!_directionalLight->isEnabled();
  156. "DirectionalLight%s",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> _directionalLight->setEnabled(isON);
  157. _directionalLightLabel->setString(str);
  158. caseLightType::POINT:
  159. boolisON=!_pointLight->isEnabled();
  160. "PointLight%s",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> _pointLight->setEnabled(isON);
  161. _pointLightLabel->setString(str);
  162. caseLightType::SPOT:
  163. boolisON=!_spotLight->isEnabled();
  164. "SpotLight%s",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> _spotLight->setEnabled(isON);
  165. _spotLightLabel->setString(str);
  166. default:
  167. break;
  168. }

3、使用

在HelloWorldScene.cpp中的createScene()中,创建LightTest的对象,加入Scene
原文链接:https://www.f2er.com/cocos2dx/346192.html

猜你在找的Cocos2d-x相关文章