2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-17 14:26:57 +08:00
|
|
|
#include "LightTest.h"
|
2014-08-17 21:33:33 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2015-04-09 08:37:30 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
LightTests::LightTests()
|
2014-08-17 21:33:33 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
ADD_TEST_CASE(LightTest);
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
LightTest::LightTest() : _directionalLight(nullptr), _pointLight(nullptr), _spotLight(nullptr)
|
2014-08-17 21:33:33 +08:00
|
|
|
{
|
2014-08-19 16:32:49 +08:00
|
|
|
addSprite();
|
|
|
|
addLights();
|
|
|
|
scheduleUpdate();
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto camera = Camera::createPerspective(60, (float)s.width / s.height, 1.0f, 1000.0f);
|
2014-08-19 16:32:49 +08:00
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
2019-09-24 11:31:35 +08:00
|
|
|
camera->setPosition3D(Vec3(0.0f, 100.0f, 100.0f));
|
|
|
|
camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
|
2014-08-19 16:32:49 +08:00
|
|
|
addChild(camera);
|
|
|
|
|
2014-08-20 16:41:45 +08:00
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 15);
|
2021-12-28 16:06:23 +08:00
|
|
|
_ambientLightLabel = Label::createWithTTF(ttfConfig, "Ambient Light ON");
|
2014-09-18 16:38:35 +08:00
|
|
|
_ambientLightLabel->retain();
|
2021-12-28 16:06:23 +08:00
|
|
|
auto menuItem0 =
|
|
|
|
MenuItemLabel::create(_ambientLightLabel, CC_CALLBACK_1(LightTest::SwitchLight, this, LightType::AMBIENT));
|
|
|
|
_directionalLightLabel = Label::createWithTTF(ttfConfig, "Directional Light OFF");
|
2014-08-26 09:43:11 +08:00
|
|
|
_directionalLightLabel->retain();
|
2021-12-28 16:06:23 +08:00
|
|
|
auto menuItem1 = MenuItemLabel::create(_directionalLightLabel,
|
|
|
|
CC_CALLBACK_1(LightTest::SwitchLight, this, LightType::DIRECTIONAL));
|
|
|
|
_pointLightLabel = Label::createWithTTF(ttfConfig, "Point Light OFF");
|
2014-08-26 09:43:11 +08:00
|
|
|
_pointLightLabel->retain();
|
2021-12-28 16:06:23 +08:00
|
|
|
auto menuItem2 =
|
|
|
|
MenuItemLabel::create(_pointLightLabel, CC_CALLBACK_1(LightTest::SwitchLight, this, LightType::POINT));
|
|
|
|
_spotLightLabel = Label::createWithTTF(ttfConfig, "Spot Light OFF");
|
2014-08-26 09:43:11 +08:00
|
|
|
_spotLightLabel->retain();
|
2021-12-28 16:06:23 +08:00
|
|
|
auto menuItem3 =
|
|
|
|
MenuItemLabel::create(_spotLightLabel, CC_CALLBACK_1(LightTest::SwitchLight, this, LightType::SPOT));
|
2014-10-09 09:55:33 +08:00
|
|
|
auto menu = Menu::create(menuItem0, menuItem1, menuItem2, menuItem3, nullptr);
|
2014-08-19 16:32:49 +08:00
|
|
|
menu->setPosition(Vec2::ZERO);
|
2014-09-18 16:38:35 +08:00
|
|
|
menuItem0->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
2021-12-28 16:06:23 +08:00
|
|
|
menuItem0->setPosition(Vec2(VisibleRect::left().x, VisibleRect::top().y - 50));
|
2014-08-20 16:43:01 +08:00
|
|
|
menuItem1->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
2021-12-28 16:06:23 +08:00
|
|
|
menuItem1->setPosition(Vec2(VisibleRect::left().x, VisibleRect::top().y - 100));
|
2014-08-20 16:43:01 +08:00
|
|
|
menuItem2->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
2021-12-28 16:06:23 +08:00
|
|
|
menuItem2->setPosition(Vec2(VisibleRect::left().x, VisibleRect::top().y - 150));
|
2014-08-20 16:43:01 +08:00
|
|
|
menuItem3->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
2021-12-28 16:06:23 +08:00
|
|
|
menuItem3->setPosition(Vec2(VisibleRect::left().x, VisibleRect::top().y - 200));
|
2014-08-19 16:32:49 +08:00
|
|
|
addChild(menu);
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
2014-09-17 14:26:57 +08:00
|
|
|
LightTest::~LightTest()
|
2014-08-17 21:33:33 +08:00
|
|
|
{
|
2014-08-26 09:43:11 +08:00
|
|
|
if (_spotLightLabel)
|
|
|
|
_spotLightLabel->release();
|
2014-08-25 16:32:15 +08:00
|
|
|
|
2014-08-26 09:43:11 +08:00
|
|
|
if (_pointLightLabel)
|
|
|
|
_pointLightLabel->release();
|
2014-08-25 16:32:15 +08:00
|
|
|
|
2014-08-26 09:43:11 +08:00
|
|
|
if (_directionalLightLabel)
|
|
|
|
_directionalLightLabel->release();
|
2014-08-25 16:32:15 +08:00
|
|
|
|
2014-09-18 16:38:35 +08:00
|
|
|
if (_spotLight)
|
|
|
|
_spotLight->release();
|
2014-08-19 15:51:30 +08:00
|
|
|
|
2014-08-19 16:32:49 +08:00
|
|
|
if (_pointLight)
|
|
|
|
_pointLight->release();
|
2014-08-19 15:51:30 +08:00
|
|
|
|
2014-09-18 16:38:35 +08:00
|
|
|
if (_directionalLight)
|
|
|
|
_directionalLight->release();
|
|
|
|
|
|
|
|
if (_ambientLight)
|
|
|
|
_ambientLight->release();
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
2014-09-17 14:26:57 +08:00
|
|
|
std::string LightTest::title() const
|
2014-08-17 21:33:33 +08:00
|
|
|
{
|
2014-08-19 15:51:30 +08:00
|
|
|
return "Light Test";
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
2014-09-17 14:26:57 +08:00
|
|
|
void LightTest::addSprite()
|
2014-08-19 15:51:30 +08:00
|
|
|
{
|
2014-08-19 16:32:49 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2014-08-28 10:16:15 +08:00
|
|
|
//{
|
2022-07-05 14:48:46 +08:00
|
|
|
// std::string fileName = "MeshRendererTest/plane.c3b";
|
|
|
|
// auto sprite = MeshRenderer::create(fileName);
|
2014-08-28 10:16:15 +08:00
|
|
|
// sprite->setRotation3D(Vec3(-90.0, 0.0, 0.0));
|
|
|
|
// sprite->setScale(5.0f);
|
|
|
|
// sprite->setPosition(Vec2(0.0, -50.0));
|
|
|
|
// addChild(sprite);
|
|
|
|
// sprite->setCameraMask(2);
|
|
|
|
//}
|
2014-08-19 16:32:49 +08:00
|
|
|
|
2014-08-26 09:43:11 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/orc.c3b";
|
|
|
|
auto mesh = MeshRenderer::create(fileName);
|
|
|
|
mesh->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
mesh->setPosition(Vec2(0.0f, 0.0f));
|
|
|
|
mesh->setScale(2.0);
|
|
|
|
auto m = MeshRenderer::create("MeshRendererTest/axe.c3b");
|
|
|
|
mesh->getAttachNode("Bip001 R Hand")->addChild(m);
|
2014-08-26 09:43:11 +08:00
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(RepeatForever::create(animate));
|
2014-08-26 09:43:11 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
addChild(mesh);
|
|
|
|
mesh->setCameraMask(2);
|
2014-08-26 09:43:11 +08:00
|
|
|
}
|
2014-08-19 16:32:49 +08:00
|
|
|
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/sphere.c3b";
|
|
|
|
auto sprite = MeshRenderer::create(fileName);
|
2019-09-24 11:31:35 +08:00
|
|
|
sprite->setPosition(Vec2(30.0f, 0.0f));
|
2014-08-19 16:32:49 +08:00
|
|
|
addChild(sprite);
|
|
|
|
sprite->setCameraMask(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/sphere.c3b";
|
|
|
|
auto sprite = MeshRenderer::create(fileName);
|
2014-08-19 16:32:49 +08:00
|
|
|
sprite->setScale(0.5f);
|
2019-09-24 11:31:35 +08:00
|
|
|
sprite->setPosition(Vec2(-50.0f, 0.0f));
|
2014-08-19 16:32:49 +08:00
|
|
|
addChild(sprite);
|
|
|
|
sprite->setCameraMask(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/sphere.c3b";
|
|
|
|
auto sprite = MeshRenderer::create(fileName);
|
2014-08-19 16:32:49 +08:00
|
|
|
sprite->setScale(0.5f);
|
2019-09-24 11:31:35 +08:00
|
|
|
sprite->setPosition(Vec2(-30.0f, 10.0f));
|
2014-08-19 16:32:49 +08:00
|
|
|
addChild(sprite);
|
|
|
|
sprite->setCameraMask(2);
|
|
|
|
}
|
2014-08-19 15:51:30 +08:00
|
|
|
}
|
|
|
|
|
2014-09-17 14:26:57 +08:00
|
|
|
void LightTest::addLights()
|
2014-08-19 15:51:30 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-09-28 16:02:12 +08:00
|
|
|
_ambientLight = AmbientLight::create(Color3B(200, 200, 200));
|
2014-09-18 16:38:35 +08:00
|
|
|
_ambientLight->retain();
|
|
|
|
_ambientLight->setEnabled(true);
|
|
|
|
addChild(_ambientLight);
|
|
|
|
_ambientLight->setCameraMask(2);
|
|
|
|
|
2014-09-28 16:02:12 +08:00
|
|
|
_directionalLight = DirectionLight::create(Vec3(-1.0f, -1.0f, 0.0f), Color3B(200, 200, 200));
|
2014-08-20 16:43:01 +08:00
|
|
|
_directionalLight->retain();
|
2014-09-18 16:38:35 +08:00
|
|
|
_directionalLight->setEnabled(false);
|
2014-08-20 16:43:01 +08:00
|
|
|
addChild(_directionalLight);
|
2014-08-25 13:58:15 +08:00
|
|
|
_directionalLight->setCameraMask(2);
|
2014-08-19 16:32:49 +08:00
|
|
|
|
2014-09-28 16:02:12 +08:00
|
|
|
_pointLight = PointLight::create(Vec3(0.0f, 0.0f, 0.0f), Color3B(200, 200, 200), 10000.0f);
|
2014-08-20 16:43:01 +08:00
|
|
|
_pointLight->retain();
|
|
|
|
_pointLight->setEnabled(false);
|
|
|
|
addChild(_pointLight);
|
2014-08-25 13:58:15 +08:00
|
|
|
_pointLight->setCameraMask(2);
|
2014-08-19 16:32:49 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
_spotLight =
|
|
|
|
SpotLight::create(Vec3(-1.0f, -1.0f, 0.0f), Vec3(0.0f, 0.0f, 0.0f), Color3B(200, 200, 200), 0.0, 0.5, 10000.0f);
|
2014-08-19 16:32:49 +08:00
|
|
|
_spotLight->retain();
|
|
|
|
_spotLight->setEnabled(false);
|
|
|
|
addChild(_spotLight);
|
2014-08-25 13:58:15 +08:00
|
|
|
_spotLight->setCameraMask(2);
|
|
|
|
|
2014-09-18 16:38:35 +08:00
|
|
|
{
|
|
|
|
auto tintto1 = TintTo::create(4, 0, 0, 255);
|
|
|
|
auto tintto2 = TintTo::create(4, 0, 255, 0);
|
|
|
|
auto tintto3 = TintTo::create(4, 255, 0, 0);
|
|
|
|
auto tintto4 = TintTo::create(4, 255, 255, 255);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto seq = Sequence::create(tintto1, tintto2, tintto3, tintto4, nullptr);
|
2014-09-18 16:38:35 +08:00
|
|
|
_ambientLight->runAction(RepeatForever::create(seq));
|
|
|
|
}
|
|
|
|
|
2014-08-25 13:58:15 +08:00
|
|
|
{
|
|
|
|
auto tintto1 = TintTo::create(4, 255, 0, 0);
|
|
|
|
auto tintto2 = TintTo::create(4, 0, 255, 0);
|
|
|
|
auto tintto3 = TintTo::create(4, 0, 0, 255);
|
|
|
|
auto tintto4 = TintTo::create(4, 255, 255, 255);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto seq = Sequence::create(tintto1, tintto2, tintto3, tintto4, nullptr);
|
2014-08-25 13:58:15 +08:00
|
|
|
_directionalLight->runAction(RepeatForever::create(seq));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto tintto1 = TintTo::create(4, 255, 0, 0);
|
|
|
|
auto tintto2 = TintTo::create(4, 0, 255, 0);
|
|
|
|
auto tintto3 = TintTo::create(4, 0, 0, 255);
|
|
|
|
auto tintto4 = TintTo::create(4, 255, 255, 255);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto seq = Sequence::create(tintto2, tintto1, tintto3, tintto4, nullptr);
|
2014-08-25 13:58:15 +08:00
|
|
|
_pointLight->runAction(RepeatForever::create(seq));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto tintto1 = TintTo::create(4, 255, 0, 0);
|
|
|
|
auto tintto2 = TintTo::create(4, 0, 255, 0);
|
|
|
|
auto tintto3 = TintTo::create(4, 0, 0, 255);
|
|
|
|
auto tintto4 = TintTo::create(4, 255, 255, 255);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto seq = Sequence::create(tintto3, tintto2, tintto1, tintto4, nullptr);
|
2014-08-25 13:58:15 +08:00
|
|
|
_spotLight->runAction(RepeatForever::create(seq));
|
|
|
|
}
|
2014-08-19 15:51:30 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void LightTest::update(float delta)
|
2014-08-19 15:51:30 +08:00
|
|
|
{
|
2014-08-19 16:32:49 +08:00
|
|
|
static float angleDelta = 0.0;
|
2014-09-18 16:38:35 +08:00
|
|
|
|
2014-08-20 16:43:01 +08:00
|
|
|
if (_directionalLight)
|
2014-09-18 16:38:35 +08:00
|
|
|
{
|
2014-08-20 16:43:01 +08:00
|
|
|
_directionalLight->setRotation3D(Vec3(-45.0, -CC_RADIANS_TO_DEGREES(angleDelta), 0.0f));
|
2014-09-18 16:38:35 +08:00
|
|
|
}
|
2014-08-20 16:43:01 +08:00
|
|
|
|
|
|
|
if (_pointLight)
|
|
|
|
{
|
2014-08-25 16:32:15 +08:00
|
|
|
_pointLight->setPositionX(100.0f * cosf(angleDelta + 2.0 * delta));
|
2014-08-20 16:43:01 +08:00
|
|
|
_pointLight->setPositionY(100.0f);
|
2014-08-25 16:32:15 +08:00
|
|
|
_pointLight->setPositionZ(100.0f * sinf(angleDelta + 2.0 * delta));
|
2014-08-20 16:43:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_spotLight)
|
|
|
|
{
|
2014-08-25 16:32:15 +08:00
|
|
|
_spotLight->setPositionX(100.0f * cosf(angleDelta + 4.0 * delta));
|
2014-08-20 16:43:01 +08:00
|
|
|
_spotLight->setPositionY(100.0f);
|
2014-08-25 16:32:15 +08:00
|
|
|
_spotLight->setPositionZ(100.0f * sinf(angleDelta + 4.0 * delta));
|
|
|
|
_spotLight->setDirection(-Vec3(cosf(angleDelta + 4.0 * delta), 1.0, sinf(angleDelta + 4.0 * delta)));
|
2014-08-20 16:43:01 +08:00
|
|
|
}
|
2014-08-19 15:51:30 +08:00
|
|
|
|
2014-08-19 16:32:49 +08:00
|
|
|
angleDelta += delta;
|
2014-08-19 15:51:30 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
TestCase::update(delta);
|
2014-08-19 15:51:30 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void LightTest::SwitchLight(Ref* sender, LightType lightType)
|
2014-08-19 15:51:30 +08:00
|
|
|
{
|
2014-08-19 16:32:49 +08:00
|
|
|
switch (lightType)
|
|
|
|
{
|
2014-09-18 16:38:35 +08:00
|
|
|
case LightType::AMBIENT:
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
|
|
|
char str[32];
|
|
|
|
bool isON = !_ambientLight->isEnabled();
|
|
|
|
sprintf(str, "Ambient Light %s", isON == true ? "ON" : "OFF");
|
|
|
|
_ambientLight->setEnabled(isON);
|
|
|
|
_ambientLightLabel->setString(str);
|
|
|
|
}
|
|
|
|
break;
|
2014-09-18 16:38:35 +08:00
|
|
|
|
|
|
|
case LightType::DIRECTIONAL:
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
|
|
|
char str[32];
|
|
|
|
bool isON = !_directionalLight->isEnabled();
|
|
|
|
sprintf(str, "Directional Light %s", isON == true ? "ON" : "OFF");
|
|
|
|
_directionalLight->setEnabled(isON);
|
|
|
|
_directionalLightLabel->setString(str);
|
|
|
|
}
|
|
|
|
break;
|
2014-08-19 16:32:49 +08:00
|
|
|
|
2014-09-18 16:38:35 +08:00
|
|
|
case LightType::POINT:
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
|
|
|
char str[32];
|
|
|
|
bool isON = !_pointLight->isEnabled();
|
|
|
|
sprintf(str, "Point Light %s", isON == true ? "ON" : "OFF");
|
|
|
|
_pointLight->setEnabled(isON);
|
|
|
|
_pointLightLabel->setString(str);
|
|
|
|
}
|
|
|
|
break;
|
2014-08-19 16:32:49 +08:00
|
|
|
|
2014-09-18 16:38:35 +08:00
|
|
|
case LightType::SPOT:
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
|
|
|
char str[32];
|
|
|
|
bool isON = !_spotLight->isEnabled();
|
|
|
|
sprintf(str, "Spot Light %s", isON == true ? "ON" : "OFF");
|
|
|
|
_spotLight->setEnabled(isON);
|
|
|
|
_spotLightLabel->setString(str);
|
|
|
|
}
|
|
|
|
break;
|
2014-08-19 16:32:49 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-08-19 15:51:30 +08:00
|
|
|
}
|