2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
#include "MeshRendererTest.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "DrawNode3D.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "2d/CameraBackgroundBrush.h"
|
|
|
|
#include "3d/MeshMaterial.h"
|
|
|
|
#include "3d/MotionStreak3D.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "extensions/Particle3D/PU/PUParticleSystem3D.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include "../testResource.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererTests::MeshRendererTests()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
ADD_TEST_CASE(MeshRendererBasicTest);
|
2023-07-28 14:13:01 +08:00
|
|
|
ADD_TEST_CASE(MeshRendererStaticInstancingBasicTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererDynamicInstancingBasicTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererPreallocatedInstancingBufferTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererInstancingStressTest);
|
2022-07-05 14:48:46 +08:00
|
|
|
ADD_TEST_CASE(MeshRendererHitTest);
|
|
|
|
ADD_TEST_CASE(AsyncLoadMeshRendererTest);
|
2019-11-23 20:27:39 +08:00
|
|
|
// // 3DEffect use custom shader which is not supported on WP8/WinRT yet.
|
2022-07-05 14:48:46 +08:00
|
|
|
ADD_TEST_CASE(MeshRendererEffectTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererUVAnimationTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererFakeShadowTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererBasicToonShaderTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererLightMapTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererWithSkinTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererWithSkinOutlineTest);
|
2019-11-23 20:27:39 +08:00
|
|
|
ADD_TEST_CASE(Animate3DTest);
|
|
|
|
ADD_TEST_CASE(AttachmentTest);
|
2022-07-05 14:48:46 +08:00
|
|
|
ADD_TEST_CASE(MeshRendererReskinTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererWithOBBPerformanceTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererMirrorTest);
|
2019-11-23 20:27:39 +08:00
|
|
|
ADD_TEST_CASE(QuaternionTest);
|
2022-07-05 14:48:46 +08:00
|
|
|
ADD_TEST_CASE(MeshRendererEmptyTest);
|
|
|
|
ADD_TEST_CASE(UseCaseMeshRenderer);
|
|
|
|
ADD_TEST_CASE(MeshRendererForceDepthTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererCubeMapTest);
|
2019-11-23 20:27:39 +08:00
|
|
|
ADD_TEST_CASE(NodeAnimationTest);
|
|
|
|
ADD_TEST_CASE(Issue9767);
|
2022-07-05 14:48:46 +08:00
|
|
|
ADD_TEST_CASE(MeshRendererClippingTest);
|
2019-11-23 20:27:39 +08:00
|
|
|
ADD_TEST_CASE(Animate3DCallbackTest);
|
|
|
|
ADD_TEST_CASE(CameraBackgroundClearTest);
|
2022-07-05 14:48:46 +08:00
|
|
|
ADD_TEST_CASE(MeshRendererVertexColorTest);
|
2019-11-23 20:27:39 +08:00
|
|
|
ADD_TEST_CASE(MotionStreak3DTest);
|
2022-07-05 14:48:46 +08:00
|
|
|
ADD_TEST_CASE(MeshRendererPropertyTest);
|
|
|
|
ADD_TEST_CASE(MeshRendererNormalMappingTest);
|
2019-11-23 20:27:39 +08:00
|
|
|
ADD_TEST_CASE(Issue16155Test);
|
|
|
|
};
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2022-07-05 14:48:46 +08:00
|
|
|
// MeshRendererTestDemo
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererTestDemo::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "No title";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererTestDemo::~MeshRendererTestDemo()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshMaterial::releaseCachedMaterial();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2022-07-05 14:48:46 +08:00
|
|
|
// MeshRendererForceDepthTest
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererForceDepthTest::MeshRendererForceDepthTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-10-18 19:17:36 +08:00
|
|
|
auto orc = ax::MeshRenderer::create("MeshRendererTest/orc.c3b");
|
2019-11-23 20:27:39 +08:00
|
|
|
orc->setScale(5);
|
|
|
|
orc->setPositionNormalized(Vec2(.5f, .3f));
|
|
|
|
orc->setPositionZ(40);
|
|
|
|
orc->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
orc->setGlobalZOrder(-1);
|
|
|
|
|
|
|
|
addChild(orc);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto ship = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
2019-11-23 20:27:39 +08:00
|
|
|
ship->setScale(5);
|
2022-07-05 14:48:46 +08:00
|
|
|
ship->setTexture("MeshRendererTest/boss.png");
|
2019-11-23 20:27:39 +08:00
|
|
|
ship->setPositionNormalized(Vec2(.5f, .5f));
|
|
|
|
ship->setRotation3D(Vec3(90.0f, 0.0f, 0.0f));
|
|
|
|
ship->setForceDepthWrite(true);
|
|
|
|
|
|
|
|
addChild(ship);
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererForceDepthTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "Force Depth Write Error Test";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererForceDepthTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "Ship should always appear behind orc";
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2022-07-05 14:48:46 +08:00
|
|
|
// MeshRendererEmptyTest
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererEmptyTest::MeshRendererEmptyTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto s = MeshRenderer::create();
|
2019-11-23 20:27:39 +08:00
|
|
|
s->setPositionNormalized(Vec2(.5f, .5f));
|
|
|
|
auto l = Label::create();
|
|
|
|
l->setString("Test");
|
|
|
|
s->addChild(l);
|
|
|
|
addChild(s);
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererEmptyTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Testing MeshRenderer Container";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererEmptyTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "MeshRenderer can act as containers for 2D objects";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2022-07-05 14:48:46 +08:00
|
|
|
// MeshRendererBasicTest
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererBasicTest::MeshRendererBasicTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(MeshRendererBasicTest::onTouchesEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(Vec2(s.width / 2, s.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererBasicTest::addNewMeshWithCoords(Vec2 p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
// int idx = (int)(AXRANDOM_0_1() * 1400.0f / 100.0f);
|
2021-12-31 12:12:40 +08:00
|
|
|
// int x = (idx%5) * 85;
|
|
|
|
// int y = (idx/5) * 121;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// //option 1: load a obj that contain the texture in it
|
2022-07-05 14:48:46 +08:00
|
|
|
// auto mesh = MeshRenderer::create("mesh3dTest/scene01.obj");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// option 2: load obj and assign the texture
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
|
|
|
mesh->setScale(3.f);
|
|
|
|
mesh->setTexture("MeshRendererTest/boss.png");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
//
|
2022-10-18 19:17:36 +08:00
|
|
|
// mesh->setEffect(ax::EFFECT_OUTLINE);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add to scene
|
2022-07-05 14:48:46 +08:00
|
|
|
addChild(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setPosition(Vec2(p.x, p.y));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
ActionInterval* action;
|
2022-07-16 10:43:05 +08:00
|
|
|
float random = AXRANDOM_0_1();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (random < 0.20)
|
|
|
|
action = ScaleBy::create(3, 2);
|
|
|
|
else if (random < 0.40)
|
|
|
|
action = RotateBy::create(3, 360);
|
|
|
|
else if (random < 0.60)
|
|
|
|
action = Blink::create(1, 3);
|
|
|
|
else if (random < 0.8)
|
|
|
|
action = TintBy::create(2, 0, -255, -255);
|
|
|
|
else
|
|
|
|
action = FadeOut::create(2);
|
|
|
|
auto action_back = action->reverse();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto seq = Sequence::create(action, action_back, nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(RepeatForever::create(seq));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererBasicTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&touch : touches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(location);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererBasicTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Testing MeshRenderer";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererBasicTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Tap screen to add more meshes";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-28 14:13:01 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// MeshRendererStaticInstancingBasicTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
MeshRendererStaticInstancingBasicTest::MeshRendererStaticInstancingBasicTest()
|
|
|
|
{
|
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
|
|
|
mesh->setScale(3.f);
|
|
|
|
mesh->setTexture("MeshRendererTest/boss.png");
|
|
|
|
|
|
|
|
auto& s = Director::getInstance()->getWinSize();
|
|
|
|
mesh->setPosition(s.width / 2, s.height / 2);
|
|
|
|
|
|
|
|
mesh->enableInstancing(ax::MeshMaterial::InstanceMaterialType::UNLIT_INSTANCE);
|
|
|
|
mesh->setDynamicInstancing(false); // false by default
|
|
|
|
|
|
|
|
FastRNG r{};
|
|
|
|
|
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
{
|
|
|
|
auto inst = Node::create();
|
|
|
|
inst->setPosition(70 * r.rangef(), 50 * r.rangef());
|
|
|
|
inst->setRotation(r.maxf(360));
|
|
|
|
mesh->addInstanceChild(inst, false); // false by default parameter
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh->shrinkToFitInstances();
|
|
|
|
|
|
|
|
addChild(mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MeshRendererStaticInstancingBasicTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Static Instancing";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MeshRendererStaticInstancingBasicTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "1000 instances of the same mesh";
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// MeshRendererDynamicInstancingBasicTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
MeshRendererDynamicInstancingBasicTest::MeshRendererDynamicInstancingBasicTest()
|
|
|
|
{
|
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
|
|
|
mesh->setScale(3.f);
|
|
|
|
mesh->setTexture("MeshRendererTest/boss.png");
|
|
|
|
|
|
|
|
auto& s = Director::getInstance()->getWinSize();
|
|
|
|
mesh->setPosition(s.width / 2, s.height / 2);
|
|
|
|
|
|
|
|
mesh->enableInstancing(ax::MeshMaterial::InstanceMaterialType::UNLIT_INSTANCE);
|
|
|
|
mesh->setDynamicInstancing(true);
|
|
|
|
|
|
|
|
FastRNG r{};
|
|
|
|
|
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
{
|
|
|
|
auto inst = Node::create();
|
|
|
|
inst->setPosition(70 * r.rangef(), 50 * r.rangef());
|
|
|
|
auto rot = RepeatForever::create(RotateBy::create(r.rangef(0.5, 2), Vec3(0, 0, 360)));
|
|
|
|
inst->runAction(rot);
|
|
|
|
mesh->addInstanceChild(inst, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh->shrinkToFitInstances();
|
|
|
|
|
|
|
|
addChild(mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MeshRendererDynamicInstancingBasicTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Dynamic Instancing";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MeshRendererDynamicInstancingBasicTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "1000 instances of the same mesh with actions";
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// MeshRendererPreallocatedInstancingBufferTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
MeshRendererPreallocatedInstancingBufferTest::MeshRendererPreallocatedInstancingBufferTest()
|
|
|
|
{
|
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
|
|
|
mesh->setScale(3.f);
|
|
|
|
mesh->setTexture("MeshRendererTest/boss.png");
|
|
|
|
|
|
|
|
auto& s = Director::getInstance()->getWinSize();
|
|
|
|
mesh->setPosition(s.width / 2, s.height / 2);
|
|
|
|
|
|
|
|
mesh->enableInstancing(ax::MeshMaterial::InstanceMaterialType::UNLIT_INSTANCE, 1000);
|
|
|
|
mesh->setDynamicInstancing(true);
|
|
|
|
|
|
|
|
FastRNG r{};
|
|
|
|
|
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
{
|
|
|
|
auto inst = Node::create();
|
|
|
|
inst->setPosition(70 * r.rangef(), 50 * r.rangef());
|
|
|
|
auto rot = RepeatForever::create(RotateBy::create(r.rangef(0.5, 2), Vec3(0, 0, 360)));
|
|
|
|
inst->runAction(rot);
|
|
|
|
mesh->addInstanceChild(inst, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
addChild(mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MeshRendererPreallocatedInstancingBufferTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Preallocated Instancing";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MeshRendererPreallocatedInstancingBufferTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "1000 instances of the same mesh with actions";
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// MeshRendererInstancingStressTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
MeshRendererInstancingStressTest::MeshRendererInstancingStressTest()
|
|
|
|
{
|
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
|
|
|
mesh->setScale(3.f);
|
|
|
|
mesh->setTexture("MeshRendererTest/boss.png");
|
|
|
|
|
|
|
|
auto& s = Director::getInstance()->getWinSize();
|
|
|
|
mesh->setPosition(s.width / 2, s.height / 2);
|
|
|
|
|
|
|
|
mesh->enableInstancing(ax::MeshMaterial::InstanceMaterialType::UNLIT_INSTANCE, 10000);
|
|
|
|
mesh->setDynamicInstancing(false); // false by default
|
|
|
|
|
|
|
|
FastRNG r{};
|
|
|
|
|
|
|
|
for (int i = 0; i < 10000; i++)
|
|
|
|
{
|
|
|
|
auto inst = Node::create();
|
|
|
|
inst->setPosition(70 * r.rangef(), 50 * r.rangef());
|
|
|
|
inst->setRotation(r.maxf(360));
|
|
|
|
mesh->addInstanceChild(inst, false); // false by default parameter
|
|
|
|
}
|
|
|
|
|
|
|
|
addChild(mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MeshRendererInstancingStressTest::title() const
|
|
|
|
{
|
|
|
|
return "Stress Testing Instancing";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MeshRendererInstancingStressTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "10000 instances of the same mesh";
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2022-07-05 14:48:46 +08:00
|
|
|
// MeshRendererUVAnimationTest
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererUVAnimationTest::MeshRendererUVAnimationTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// the offset use to translating texture
|
2019-11-23 20:27:39 +08:00
|
|
|
_cylinder_texture_offset = 0;
|
2021-12-31 12:12:40 +08:00
|
|
|
_shining_duration = 0;
|
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// use custom camera
|
2019-11-23 20:27:39 +08:00
|
|
|
auto camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1f, 200);
|
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// create cylinder
|
2022-07-05 14:48:46 +08:00
|
|
|
auto cylinder = MeshRenderer::create("MeshRendererTest/cylinder.c3b");
|
|
|
|
auto mat = MeshMaterial::createWithFilename("MeshRendererTest/UVAnimation.material");
|
2021-12-31 12:12:40 +08:00
|
|
|
_state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
|
2019-11-23 20:27:39 +08:00
|
|
|
cylinder->setMaterial(mat);
|
|
|
|
|
|
|
|
this->addChild(cylinder);
|
|
|
|
this->setCameraMask(2);
|
|
|
|
this->addChild(camera);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// adjust cylinder's position & rotation
|
2019-11-23 20:27:39 +08:00
|
|
|
cylinder->setPosition3D(Vec3(0.0f, -15.0f, -50.0f));
|
|
|
|
cylinder->setRotation3D(Vec3(-90.0f, 0.0f, 0.0f));
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// the callback function update cylinder's texcoord
|
2022-07-16 10:43:05 +08:00
|
|
|
schedule(AX_SCHEDULE_SELECTOR(MeshRendererUVAnimationTest::cylinderUpdate));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2021-12-31 12:12:40 +08:00
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [=](EventCustom*) {
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mat = MeshMaterial::createWithFilename("MeshRendererTest/UVAnimation.material");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
cylinder->setMaterial(mat);
|
|
|
|
_state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
|
2021-12-31 12:12:40 +08:00
|
|
|
});
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererUVAnimationTest::~MeshRendererUVAnimationTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererUVAnimationTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "Testing UV Animation";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererUVAnimationTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererUVAnimationTest::cylinderUpdate(float dt)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// callback function to update cylinder's texcoord
|
2019-11-23 20:27:39 +08:00
|
|
|
static bool fade_in = true;
|
2021-12-31 12:12:40 +08:00
|
|
|
_cylinder_texture_offset += 0.3 * dt;
|
2019-11-23 20:27:39 +08:00
|
|
|
_cylinder_texture_offset = (_cylinder_texture_offset > 1) ? 0 : _cylinder_texture_offset;
|
|
|
|
if (fade_in)
|
|
|
|
{
|
|
|
|
_shining_duration += 0.5 * dt;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (_shining_duration > 1)
|
|
|
|
fade_in = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_shining_duration -= 0.5 * dt;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (_shining_duration < 0)
|
|
|
|
fade_in = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// pass the result to shader
|
|
|
|
auto offsetLoc = _state->getUniformLocation("offset");
|
2019-11-23 20:27:39 +08:00
|
|
|
auto durationLoc = _state->getUniformLocation("duration");
|
|
|
|
_state->setUniform(offsetLoc, &_cylinder_texture_offset, sizeof(_cylinder_texture_offset));
|
|
|
|
_state->setUniform(durationLoc, &_shining_duration, sizeof(_shining_duration));
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2022-07-05 14:48:46 +08:00
|
|
|
// MeshRendererFakeShadowTest
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererFakeShadowTest::MeshRendererFakeShadowTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesBegan = AX_CALLBACK_2(MeshRendererFakeShadowTest::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = AX_CALLBACK_2(MeshRendererFakeShadowTest::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(MeshRendererFakeShadowTest::onTouchesEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
auto layer = Layer::create();
|
|
|
|
addChild(layer, 0);
|
2021-12-31 12:12:40 +08:00
|
|
|
// create Camera
|
2019-11-23 20:27:39 +08:00
|
|
|
_camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1f, 200);
|
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
_camera->setPosition3D(Vec3(0.0f, 20.0f, 25.0f));
|
|
|
|
_camera->setRotation3D(Vec3(-60.0f, 0.0f, 0.0f));
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// create a plane
|
2022-07-05 14:48:46 +08:00
|
|
|
_plane = MeshRenderer::create("MeshRendererTest/plane.c3t");
|
2019-11-23 20:27:39 +08:00
|
|
|
_plane->setRotation3D(Vec3(90.0f, 0.0f, 0.0f));
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mat = MeshMaterial::createWithFilename("MeshRendererTest/FakeShadow.material");
|
2021-12-31 12:12:40 +08:00
|
|
|
_state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
|
2019-11-23 20:27:39 +08:00
|
|
|
_plane->setMaterial(mat);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto location = _state->getUniformLocation("u_model_matrix");
|
2019-11-23 20:27:39 +08:00
|
|
|
auto transform = _plane->getNodeToWorldTransform();
|
|
|
|
_state->setUniform(location, transform.m, sizeof(transform.m));
|
|
|
|
|
|
|
|
layer->addChild(_plane);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// create the orc
|
2022-07-05 14:48:46 +08:00
|
|
|
_orc = MeshRenderer::create("MeshRendererTest/orc.c3b");
|
2019-11-23 20:27:39 +08:00
|
|
|
_orc->setScale(0.2f);
|
|
|
|
_orc->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
_orc->setPosition3D(Vec3(0.0f, 0.0f, 10.0f));
|
|
|
|
_targetPos = _orc->getPosition3D();
|
2021-12-31 12:12:40 +08:00
|
|
|
location = _state->getUniformLocation("u_target_pos");
|
2019-11-23 20:27:39 +08:00
|
|
|
_state->setUniform(location, &_targetPos, sizeof(_targetPos));
|
|
|
|
layer->addChild(_orc);
|
|
|
|
layer->addChild(_camera);
|
|
|
|
layer->setCameraMask(2);
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
schedule(AX_SCHEDULE_SELECTOR(MeshRendererFakeShadowTest::updateCamera), 0.0f);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2021-12-31 12:12:40 +08:00
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom*) {
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mat = MeshMaterial::createWithFilename("MeshRendererTest/FakeShadow.material");
|
2021-12-31 12:12:40 +08:00
|
|
|
_state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
|
2019-11-23 20:27:39 +08:00
|
|
|
_plane->setMaterial(mat);
|
|
|
|
backend::UniformLocation loc = _state->getUniformLocation("u_model_matrix");
|
2021-12-31 12:12:40 +08:00
|
|
|
auto trans = _plane->getNodeToWorldTransform();
|
2019-11-23 20:27:39 +08:00
|
|
|
_state->setUniform(loc, &trans, sizeof(trans));
|
2021-12-31 12:12:40 +08:00
|
|
|
loc = _state->getUniformLocation("u_target_pos");
|
2019-11-23 20:27:39 +08:00
|
|
|
auto pos = _orc->getPosition3D();
|
|
|
|
_state->setUniform(loc, &pos, sizeof(pos));
|
2021-12-31 12:12:40 +08:00
|
|
|
});
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererFakeShadowTest::~MeshRendererFakeShadowTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererFakeShadowTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "fake shadow effect";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererFakeShadowTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "touch the screen to move around";
|
|
|
|
}
|
|
|
|
|
2022-10-18 19:17:36 +08:00
|
|
|
void MeshRendererFakeShadowTest::Move(ax::Ref* sender, int value)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_orc->setPositionX(_orc->getPositionX() + value);
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererFakeShadowTest::updateCamera(float fDelta)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
updateState(fDelta);
|
|
|
|
if (isState(_curState, State_Move))
|
|
|
|
{
|
|
|
|
move3D(fDelta);
|
|
|
|
if (isState(_curState, State_Rotate))
|
|
|
|
{
|
|
|
|
Vec3 curPos = _orc->getPosition3D();
|
|
|
|
|
|
|
|
Vec3 newFaceDir = _targetPos - curPos;
|
2021-12-31 12:12:40 +08:00
|
|
|
newFaceDir.y = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
newFaceDir.normalize();
|
|
|
|
Vec3 up;
|
|
|
|
_orc->getNodeToWorldTransform().getUpVector(&up);
|
|
|
|
up.normalize();
|
|
|
|
Vec3 right;
|
|
|
|
Vec3::cross(-newFaceDir, up, &right);
|
|
|
|
right.normalize();
|
|
|
|
Vec3 pos = Vec3(0, 0, 0);
|
|
|
|
Mat4 mat;
|
|
|
|
mat.m[0] = right.x;
|
|
|
|
mat.m[1] = right.y;
|
|
|
|
mat.m[2] = right.z;
|
|
|
|
mat.m[3] = 0.0f;
|
|
|
|
|
|
|
|
mat.m[4] = up.x;
|
|
|
|
mat.m[5] = up.y;
|
|
|
|
mat.m[6] = up.z;
|
|
|
|
mat.m[7] = 0.0f;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
mat.m[8] = newFaceDir.x;
|
|
|
|
mat.m[9] = newFaceDir.y;
|
2019-11-23 20:27:39 +08:00
|
|
|
mat.m[10] = newFaceDir.z;
|
|
|
|
mat.m[11] = 0.0f;
|
|
|
|
|
|
|
|
mat.m[12] = pos.x;
|
|
|
|
mat.m[13] = pos.y;
|
|
|
|
mat.m[14] = pos.z;
|
|
|
|
mat.m[15] = 1.0f;
|
|
|
|
_orc->setAdditionalTransform(&mat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererFakeShadowTest::move3D(float elapsedTime)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
Vec3 curPos = _orc->getPosition3D();
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec3 newFaceDir = _targetPos - curPos;
|
2021-12-31 12:12:40 +08:00
|
|
|
newFaceDir.y = 0.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
newFaceDir.normalize();
|
|
|
|
Vec3 offset = newFaceDir * 25.0f * elapsedTime;
|
|
|
|
curPos += offset;
|
|
|
|
_orc->setPosition3D(curPos);
|
|
|
|
offset.x = offset.x;
|
|
|
|
offset.z = offset.z;
|
2021-12-31 12:12:40 +08:00
|
|
|
// pass the newest orc position
|
2019-11-23 20:27:39 +08:00
|
|
|
auto location = _state->getUniformLocation("u_target_pos");
|
|
|
|
auto position = _orc->getPosition3D();
|
|
|
|
_state->setUniform(location, &position, sizeof(position));
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererFakeShadowTest::updateState(float elapsedTime)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Vec3 curPos = _orc->getPosition3D();
|
|
|
|
Vec3 curFaceDir;
|
|
|
|
_orc->getNodeToWorldTransform().getForwardVector(&curFaceDir);
|
|
|
|
curFaceDir = -curFaceDir;
|
|
|
|
curFaceDir.normalize();
|
|
|
|
Vec3 newFaceDir = _targetPos - curPos;
|
2021-12-31 12:12:40 +08:00
|
|
|
newFaceDir.y = 0.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
newFaceDir.normalize();
|
|
|
|
float cosAngle = std::fabs(Vec3::dot(curFaceDir, newFaceDir) - 1.0f);
|
2021-12-31 12:12:40 +08:00
|
|
|
float dist = curPos.distanceSquared(_targetPos);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (dist <= 4.0f)
|
|
|
|
{
|
|
|
|
if (cosAngle <= 0.01f)
|
|
|
|
_curState = State_Idle;
|
|
|
|
else
|
|
|
|
_curState = State_Rotate;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cosAngle > 0.01f)
|
|
|
|
_curState = State_Rotate | State_Move;
|
|
|
|
else
|
|
|
|
_curState = State_Move;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
bool MeshRendererFakeShadowTest::isState(unsigned int state, unsigned int bit) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return (state & bit) == bit;
|
|
|
|
}
|
|
|
|
|
2022-10-18 19:17:36 +08:00
|
|
|
void MeshRendererFakeShadowTest::onTouchesBegan(const std::vector<Touch*>& touches, ax::Event* event) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-18 19:17:36 +08:00
|
|
|
void MeshRendererFakeShadowTest::onTouchesMoved(const std::vector<Touch*>& touches, ax::Event* event) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-18 19:17:36 +08:00
|
|
|
void MeshRendererFakeShadowTest::onTouchesEnded(const std::vector<Touch*>& touches, ax::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&item : touches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto touch = item;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto location = touch->getLocationInView();
|
|
|
|
if (_camera)
|
|
|
|
{
|
|
|
|
if (_orc)
|
|
|
|
{
|
|
|
|
Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
|
|
|
|
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
nearP = _camera->unproject(nearP);
|
|
|
|
farP = _camera->unproject(farP);
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec3 dir(farP - nearP);
|
|
|
|
float dist = 0.0f;
|
2021-12-31 12:12:40 +08:00
|
|
|
float ndd = Vec3::dot(Vec3(0, 1, 0), dir);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (ndd == 0)
|
|
|
|
dist = 0.0f;
|
|
|
|
float ndo = Vec3::dot(Vec3(0, 1, 0), nearP);
|
2021-12-31 12:12:40 +08:00
|
|
|
dist = (0 - ndo) / ndd;
|
|
|
|
Vec3 p = nearP + dist * dir;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (p.x > 100)
|
|
|
|
p.x = 100;
|
|
|
|
if (p.x < -100)
|
|
|
|
p.x = -100;
|
|
|
|
if (p.z > 100)
|
|
|
|
p.z = 100;
|
|
|
|
if (p.z < -100)
|
|
|
|
p.z = -100;
|
|
|
|
|
|
|
|
_targetPos = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2022-07-05 14:48:46 +08:00
|
|
|
// MeshRendererBasicToonShaderTest
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererBasicToonShaderTest::MeshRendererBasicToonShaderTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto _camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1f, 200);
|
2019-11-23 20:27:39 +08:00
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
// create a teapot
|
2022-07-05 14:48:46 +08:00
|
|
|
auto teapot = MeshRenderer::create("MeshRendererTest/teapot.c3b");
|
|
|
|
auto mat = MeshMaterial::createWithFilename("MeshRendererTest/BasicToon.material");
|
2021-12-31 12:12:40 +08:00
|
|
|
_state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
|
2019-11-23 20:27:39 +08:00
|
|
|
teapot->setMaterial(mat);
|
|
|
|
|
|
|
|
teapot->setPosition3D(Vec3(0.0f, -5.0f, -20.0f));
|
|
|
|
teapot->setRotation3D(Vec3(-90.0f, 180.0f, 0.0f));
|
|
|
|
auto rotate_action = RotateBy::create(1.5f, Vec3(0.0f, 30.0f, 0.0f));
|
|
|
|
teapot->runAction(RepeatForever::create(rotate_action));
|
|
|
|
addChild(teapot);
|
|
|
|
addChild(_camera);
|
|
|
|
setCameraMask(2);
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2021-12-31 12:12:40 +08:00
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [=](EventCustom*) {
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mat = MeshMaterial::createWithFilename("MeshRendererTest/BasicToon.material");
|
2021-12-31 12:12:40 +08:00
|
|
|
_state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
|
2019-11-23 20:27:39 +08:00
|
|
|
teapot->setMaterial(mat);
|
2021-12-31 12:12:40 +08:00
|
|
|
});
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererBasicToonShaderTest::~MeshRendererBasicToonShaderTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererBasicToonShaderTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "basic toon shader test";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererBasicToonShaderTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2022-07-05 14:48:46 +08:00
|
|
|
// MeshRendererLightMapTest
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererLightMapTest::MeshRendererLightMapTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// the assets are from the OpenVR demo
|
|
|
|
// get the visible size.
|
2019-11-23 20:27:39 +08:00
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
_camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1f, 200);
|
2019-11-23 20:27:39 +08:00
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
_camera->setPosition3D(Vec3(0.0f, 25.0f, 15.0f));
|
|
|
|
_camera->setRotation3D(Vec3(-35.0f, 0.0f, 0.0f));
|
2022-07-05 14:48:46 +08:00
|
|
|
auto LightMapScene = MeshRenderer::create("MeshRendererTest/LightMapScene.c3b");
|
2019-11-23 20:27:39 +08:00
|
|
|
LightMapScene->setScale(0.1f);
|
|
|
|
addChild(LightMapScene);
|
|
|
|
addChild(_camera);
|
|
|
|
setCameraMask(2);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add a point light
|
2019-11-23 20:27:39 +08:00
|
|
|
auto light = PointLight::create(Vec3(35, 75, -20.5), Color3B(255, 255, 255), 150);
|
|
|
|
addChild(light);
|
2021-12-31 12:12:40 +08:00
|
|
|
// set the ambient light
|
2019-11-23 20:27:39 +08:00
|
|
|
auto ambient = AmbientLight::create(Color3B(55, 55, 55));
|
|
|
|
addChild(ambient);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// create a listener
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesMoved = AX_CALLBACK_2(MeshRendererLightMapTest::onTouchesMoved, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererLightMapTest::~MeshRendererLightMapTest() {}
|
|
|
|
std::string MeshRendererLightMapTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "light map test";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererLightMapTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "drag the screen to move around";
|
|
|
|
}
|
|
|
|
|
2022-10-18 19:17:36 +08:00
|
|
|
void MeshRendererLightMapTest::onTouchesMoved(const std::vector<ax::Touch*>& touches, ax::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (touches.size() == 1)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
float delta = Director::getInstance()->getDeltaTime();
|
|
|
|
auto touch = touches[0];
|
|
|
|
auto location = touch->getLocation();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto PreviousLocation = touch->getPreviousLocation();
|
2021-12-31 12:12:40 +08:00
|
|
|
Point newPos = PreviousLocation - location;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Vec3 cameraDir;
|
|
|
|
Vec3 cameraRightDir;
|
|
|
|
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
|
|
|
|
cameraDir.normalize();
|
|
|
|
cameraDir.y = 0;
|
|
|
|
_camera->getNodeToWorldTransform().getRightVector(&cameraRightDir);
|
|
|
|
cameraRightDir.normalize();
|
|
|
|
cameraRightDir.y = 0;
|
2021-12-31 12:12:40 +08:00
|
|
|
Vec3 cameraPos = _camera->getPosition3D();
|
|
|
|
cameraPos += cameraDir * newPos.y * delta;
|
|
|
|
cameraPos += cameraRightDir * newPos.x * delta;
|
2019-11-23 20:27:39 +08:00
|
|
|
_camera->setPosition3D(cameraPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2022-07-05 14:48:46 +08:00
|
|
|
// MeshRendererHitTest
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererHitTest::MeshRendererHitTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh1 = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh1->setScale(4.f);
|
|
|
|
mesh1->setTexture("MeshRendererTest/boss.png");
|
|
|
|
mesh1->setPosition(Vec2(s.width / 2, s.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add to scene
|
2022-07-05 14:48:46 +08:00
|
|
|
addChild(mesh1);
|
|
|
|
mesh1->runAction(RepeatForever::create(RotateBy::create(3.0f, 360.0f)));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh2 = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh2->setScale(4.f);
|
|
|
|
mesh2->setTexture("MeshRendererTest/boss.png");
|
|
|
|
mesh2->setPosition(Vec2(s.width / 2, s.height / 2));
|
|
|
|
mesh2->setAnchorPoint(Vec2(0.5f, 0.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add to scene
|
2022-07-05 14:48:46 +08:00
|
|
|
addChild(mesh2);
|
|
|
|
mesh2->runAction(RepeatForever::create(RotateBy::create(3.0f, -360.0f)));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
// Make mesh1 touchable
|
2019-11-23 20:27:39 +08:00
|
|
|
auto listener1 = EventListenerTouchOneByOne::create();
|
|
|
|
listener1->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener1->onTouchBegan = [](Touch* touch, Event* event) {
|
2022-07-05 14:48:46 +08:00
|
|
|
auto target = static_cast<MeshRenderer*>(event->getCurrentTarget());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Rect rect = target->getBoundingBox();
|
|
|
|
if (rect.containsPoint(touch->getLocation()))
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
log("mesh3d began... x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
|
2019-11-23 20:27:39 +08:00
|
|
|
target->setOpacity(100);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
listener1->onTouchMoved = [](Touch* touch, Event* event) {
|
2022-07-05 14:48:46 +08:00
|
|
|
auto target = static_cast<MeshRenderer*>(event->getCurrentTarget());
|
2019-11-23 20:27:39 +08:00
|
|
|
target->setPosition(target->getPosition() + touch->getDelta());
|
|
|
|
};
|
|
|
|
|
|
|
|
listener1->onTouchEnded = [=](Touch* touch, Event* event) {
|
2022-07-05 14:48:46 +08:00
|
|
|
auto target = static_cast<MeshRenderer*>(event->getCurrentTarget());
|
|
|
|
log("mesh3d onTouchesEnded.. ");
|
2019-11-23 20:27:39 +08:00
|
|
|
target->setOpacity(255);
|
|
|
|
};
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, mesh1);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), mesh2);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererHitTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Testing MeshRenderer Touch in 2D";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererHitTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Tap MeshRenderer and Drag";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererEffectTest::MeshRendererEffectTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(Vec2(s.width / 2, s.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(MeshRendererEffectTest::onTouchesEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2021-12-31 12:12:40 +08:00
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom*) {
|
2022-07-05 14:48:46 +08:00
|
|
|
auto material = MeshMaterial::createWithFilename("MeshRendererTest/outline.material");
|
2019-11-23 20:27:39 +08:00
|
|
|
material->setTechnique("outline_noneskinned");
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&mesh : _meshes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setMaterial(material->clone());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
});
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererEffectTest::~MeshRendererEffectTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererEffectTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Testing MeshRenderer";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererEffectTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "MeshRenderer with effects";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererEffectTest::addNewMeshWithCoords(Vec2 p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// option 2: load obj and assign the texture
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
|
|
|
auto material = MeshMaterial::createWithFilename("MeshRendererTest/outline.material");
|
2019-11-23 20:27:39 +08:00
|
|
|
material->setTechnique("outline_noneskinned");
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setMaterial(material);
|
|
|
|
mesh->setScale(6.f);
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getTextureCache()->removeUnusedTextures();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add to scene
|
2022-07-05 14:48:46 +08:00
|
|
|
addChild(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setPosition(Vec2(p.x, p.y));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
ActionInterval* action;
|
2022-07-16 10:43:05 +08:00
|
|
|
float random = AXRANDOM_0_1();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (random < 0.20)
|
|
|
|
action = ScaleBy::create(3, 2);
|
|
|
|
else if (random < 0.40)
|
|
|
|
action = RotateBy::create(3, 360);
|
|
|
|
else if (random < 0.60)
|
|
|
|
action = Blink::create(1, 3);
|
|
|
|
else if (random < 0.8)
|
|
|
|
action = TintBy::create(2, 0, -255, -255);
|
|
|
|
else
|
|
|
|
action = FadeOut::create(2);
|
|
|
|
auto action_back = action->reverse();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto seq = Sequence::create(action, action_back, nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(RepeatForever::create(seq));
|
2022-08-08 13:18:33 +08:00
|
|
|
_meshes.emplace_back(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererEffectTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&touch : touches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(location);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
AsyncLoadMeshRendererTest::AsyncLoadMeshRendererTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
_paths.emplace_back("MeshRendererTest/girl.c3b");
|
|
|
|
_paths.emplace_back("MeshRendererTest/orc.c3b");
|
|
|
|
_paths.emplace_back("MeshRendererTest/ReskinGirl.c3b");
|
|
|
|
_paths.emplace_back("MeshRendererTest/axe.c3b");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 15);
|
2022-07-05 14:48:46 +08:00
|
|
|
auto label1 = Label::createWithTTF(ttfConfig, "AsyncLoad MeshRenderer");
|
2021-12-31 12:12:40 +08:00
|
|
|
auto item1 =
|
2022-07-16 10:43:05 +08:00
|
|
|
MenuItemLabel::create(label1, AX_CALLBACK_1(AsyncLoadMeshRendererTest::menuCallback_asyncLoadMesh, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
item1->setPosition(s.width * .5f, s.height * .8f);
|
|
|
|
|
|
|
|
auto pMenu1 = Menu::create(item1, nullptr);
|
|
|
|
pMenu1->setPosition(Vec2(0.0f, 0.0f));
|
|
|
|
this->addChild(pMenu1, 10);
|
|
|
|
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setTag(101);
|
|
|
|
this->addChild(node);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
menuCallback_asyncLoadMesh(nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
AsyncLoadMeshRendererTest::~AsyncLoadMeshRendererTest() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string AsyncLoadMeshRendererTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Testing MeshRenderer::createAsync";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string AsyncLoadMeshRendererTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void AsyncLoadMeshRendererTest::menuCallback_asyncLoadMesh(Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// Note that you must stop the tasks before leaving the scene.
|
2019-11-23 20:27:39 +08:00
|
|
|
AsyncTaskPool::getInstance()->stopTasks(AsyncTaskPool::TaskType::TASK_IO);
|
|
|
|
|
|
|
|
auto node = getChildByTag(101);
|
2022-07-05 14:48:46 +08:00
|
|
|
node->removeAllChildren(); // remove all loaded mesh
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// remove cache data
|
2022-07-12 21:31:54 +08:00
|
|
|
MeshRendererCache::getInstance()->removeAllMeshRenderData();
|
2021-09-05 20:45:32 +08:00
|
|
|
int32_t index = 0;
|
2021-12-31 12:12:40 +08:00
|
|
|
for (const auto& path : _paths)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
MeshRenderer::createAsync(path, AX_CALLBACK_2(AsyncLoadMeshRendererTest::asyncLoad_Callback, this), (void*)index++);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void AsyncLoadMeshRendererTest::asyncLoad_Callback(MeshRenderer* mesh, void* param)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto index = static_cast<int>((uintptr_t)param);
|
|
|
|
auto node = getChildByTag(101);
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
float width = s.width / _paths.size();
|
|
|
|
Vec2 point(width * (0.5f + index), s.height / 2.f);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setPosition(point);
|
|
|
|
node->addChild(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererWithSkinTest::MeshRendererWithSkinTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(MeshRendererWithSkinTest::onTouchesEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
// switch animation quality. In fact, you can set the mesh3d out of frustum to Animate3DQuality::QUALITY_NONE, it
|
2021-12-31 12:12:40 +08:00
|
|
|
// can save a lot of cpu time
|
2019-11-23 20:27:39 +08:00
|
|
|
MenuItemFont::setFontName("fonts/arial.ttf");
|
|
|
|
MenuItemFont::setFontSize(15);
|
|
|
|
_animateQuality = (int)Animate3DQuality::QUALITY_LOW;
|
2021-12-31 12:12:40 +08:00
|
|
|
_menuItem = MenuItemFont::create(getAnimationQualityMessage(),
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_CALLBACK_1(MeshRendererWithSkinTest::switchAnimationQualityCallback, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
_menuItem->setColor(Color3B(0, 200, 20));
|
|
|
|
auto menu = Menu::create(_menuItem, NULL);
|
|
|
|
menu->setPosition(Vec2::ZERO);
|
|
|
|
_menuItem->setPosition(VisibleRect::left().x + 50, VisibleRect::top().y - 70);
|
|
|
|
addChild(menu, 1);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
_meshes.clear();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(Vec2(s.width / 2, s.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererWithSkinTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Testing MeshRenderer";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererWithSkinTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Tap screen to add more mesh3D";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithSkinTest::addNewMeshWithCoords(Vec2 p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/orc.c3b";
|
|
|
|
auto mesh = MeshRenderer::create(fileName);
|
|
|
|
mesh->setScale(3);
|
|
|
|
mesh->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
mesh->setPosition(Vec2(p.x, p.y));
|
|
|
|
addChild(mesh);
|
2022-08-08 13:18:33 +08:00
|
|
|
_meshes.emplace_back(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
bool inverse = (std::rand() % 3 == 0);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int rand2 = std::rand();
|
2019-11-23 20:27:39 +08:00
|
|
|
float speed = 1.0f;
|
|
|
|
if (rand2 % 3 == 1)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
speed = animate->getSpeed() + AXRANDOM_0_1();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else if (rand2 % 3 == 2)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
speed = animate->getSpeed() - 0.5 * AXRANDOM_0_1();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
animate->setSpeed(inverse ? -speed : speed);
|
|
|
|
animate->setTag(110);
|
|
|
|
animate->setQuality((Animate3DQuality)_animateQuality);
|
|
|
|
auto repeat = RepeatForever::create(animate);
|
|
|
|
repeat->setTag(110);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(repeat);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererWithSkinTest::getAnimationQualityMessage() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_animateQuality == (int)Animate3DQuality::QUALITY_NONE)
|
|
|
|
return "None Quality";
|
|
|
|
else if (_animateQuality == (int)Animate3DQuality::QUALITY_LOW)
|
|
|
|
return "Low Quality";
|
|
|
|
else if (_animateQuality == (int)Animate3DQuality::QUALITY_HIGH)
|
|
|
|
return "High Quality";
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithSkinTest::switchAnimationQualityCallback(Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
++_animateQuality;
|
|
|
|
if (_animateQuality > (int)Animate3DQuality::QUALITY_HIGH)
|
|
|
|
_animateQuality = (int)Animate3DQuality::QUALITY_NONE;
|
|
|
|
|
|
|
|
_menuItem->setString(getAnimationQualityMessage());
|
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&iter : _meshes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
RepeatForever* repAction = dynamic_cast<RepeatForever*>(iter->getActionByTag(110));
|
2021-12-31 12:12:40 +08:00
|
|
|
Animate3D* animate3D = dynamic_cast<Animate3D*>(repAction->getInnerAction());
|
2019-11-23 20:27:39 +08:00
|
|
|
animate3D->setQuality((Animate3DQuality)_animateQuality);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithSkinTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&touch : touches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(location);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererWithSkinOutlineTest::MeshRendererWithSkinOutlineTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(MeshRendererWithSkinOutlineTest::onTouchesEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(Vec2(s.width / 2, s.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2021-12-31 12:12:40 +08:00
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom*) {
|
2022-07-05 14:48:46 +08:00
|
|
|
auto material = MeshMaterial::createWithFilename("MeshRendererTest/outline.material");
|
2019-11-23 20:27:39 +08:00
|
|
|
material->setTechnique("outline_skinned");
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&mesh : _meshes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setMaterial(material->clone());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
});
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
|
|
|
|
#endif
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererWithSkinOutlineTest::~MeshRendererWithSkinOutlineTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererWithSkinOutlineTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Testing MeshRenderer for skinned outline";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererWithSkinOutlineTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Tap screen to add more mesh3D";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithSkinOutlineTest::addNewMeshWithCoords(Vec2 p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/orc.c3b";
|
|
|
|
auto mesh = MeshRenderer::create(fileName);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto material = MeshMaterial::createWithFilename("MeshRendererTest/outline.material");
|
2019-11-23 20:27:39 +08:00
|
|
|
material->setTechnique("outline_skinned");
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setMaterial(material);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setScale(3);
|
|
|
|
mesh->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
addChild(mesh);
|
|
|
|
mesh->setPosition(Vec2(p.x, p.y));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
bool inverse = (std::rand() % 3 == 0);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int rand2 = std::rand();
|
2019-11-23 20:27:39 +08:00
|
|
|
float speed = 1.0f;
|
|
|
|
if (rand2 % 3 == 1)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
speed = animate->getSpeed() + AXRANDOM_0_1();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else if (rand2 % 3 == 2)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
speed = animate->getSpeed() - 0.5 * AXRANDOM_0_1();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
animate->setSpeed(inverse ? -speed : speed);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(RepeatForever::create(animate));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithSkinOutlineTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&touch : touches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(location);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Animate3DTest::Animate3DTest()
|
2022-07-05 14:48:46 +08:00
|
|
|
: _mesh(nullptr), _swim(nullptr), _hurt(nullptr), _elapseTransTime(0.f), _moveAction(nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
addMeshRenderer();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(Animate3DTest::onTouchesEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
Animate3DTest::~Animate3DTest()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_moveAction);
|
|
|
|
AX_SAFE_RELEASE(_hurt);
|
|
|
|
AX_SAFE_RELEASE(_swim);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Animate3DTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Animate3D";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Animate3DTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Touch to beat the tortoise";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animate3DTest::update(float dt)
|
|
|
|
{
|
|
|
|
if (_state == State::HURT_TO_SWIMMING)
|
|
|
|
{
|
|
|
|
_elapseTransTime += dt;
|
|
|
|
|
|
|
|
if (_elapseTransTime >= Animate3D::getTransitionTime())
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->stopAction(_hurt);
|
2019-11-23 20:27:39 +08:00
|
|
|
_state = State::SWIMMING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_state == State::SWIMMING_TO_HURT)
|
|
|
|
{
|
|
|
|
_elapseTransTime += dt;
|
|
|
|
if (_elapseTransTime >= Animate3D::getTransitionTime())
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->stopAction(_swim);
|
2019-11-23 20:27:39 +08:00
|
|
|
_state = State::HURT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void Animate3DTest::addMeshRenderer()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/tortoise.c3b";
|
|
|
|
auto mesh = MeshRenderer::create(fileName);
|
|
|
|
mesh->setScale(0.1f);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setPosition(Vec2(s.width * 4.f / 5.f, s.height / 2.f));
|
|
|
|
addChild(mesh);
|
|
|
|
_mesh = mesh;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation, 0.f, 1.933f);
|
2021-12-31 12:12:40 +08:00
|
|
|
_swim = RepeatForever::create(animate);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(_swim);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_swim->retain();
|
|
|
|
_hurt = Animate3D::create(animation, 1.933f, 2.8f);
|
|
|
|
_hurt->retain();
|
|
|
|
_state = State::SWIMMING;
|
|
|
|
}
|
|
|
|
|
|
|
|
_moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f));
|
|
|
|
_moveAction->retain();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto seq =
|
2022-07-16 10:43:05 +08:00
|
|
|
Sequence::create(_moveAction, CallFunc::create(AX_CALLBACK_0(Animate3DTest::reachEndCallBack, this)), nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
seq->setTag(100);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(seq);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Animate3DTest::reachEndCallBack()
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->stopActionByTag(100);
|
|
|
|
auto inverse = MoveTo::create(4.f, Vec2(s.width - _mesh->getPositionX(), s.height / 2.f));
|
2019-11-23 20:27:39 +08:00
|
|
|
inverse->retain();
|
|
|
|
_moveAction->release();
|
|
|
|
_moveAction = inverse;
|
2021-12-31 12:12:40 +08:00
|
|
|
auto rot = RotateBy::create(1.f, Vec3(0.f, 180.f, 0.f));
|
|
|
|
auto seq = Sequence::create(rot, _moveAction,
|
2022-07-16 10:43:05 +08:00
|
|
|
CallFunc::create(AX_CALLBACK_0(Animate3DTest::reachEndCallBack, this)), nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
seq->setTag(100);
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->runAction(seq);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Animate3DTest::renewCallBack()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// rerun swim action
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->runAction(_swim);
|
2021-12-31 12:12:40 +08:00
|
|
|
_state = State::HURT_TO_SWIMMING;
|
2019-11-23 20:27:39 +08:00
|
|
|
_elapseTransTime = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animate3DTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&touch : touches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
if (_mesh)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
float len = (_mesh->getPosition() - location).length();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (len < 40)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// hurt the tortoise
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_state == State::SWIMMING)
|
|
|
|
{
|
|
|
|
_elapseTransTime = 0.0f;
|
2021-12-31 12:12:40 +08:00
|
|
|
_state = State::SWIMMING_TO_HURT;
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->stopAction(_hurt);
|
|
|
|
_mesh->runAction(_hurt);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto delay = DelayTime::create(_hurt->getDuration() - Animate3D::getTransitionTime());
|
2021-12-31 12:12:40 +08:00
|
|
|
auto seq = Sequence::create(
|
2022-07-16 10:43:05 +08:00
|
|
|
delay, CallFunc::create(AX_CALLBACK_0(Animate3DTest::renewCallBack, this)), nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
seq->setTag(101);
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->runAction(seq);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
AttachmentTest::AttachmentTest() : _hasWeapon(false), _mesh(nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(Vec2(s.width / 2, s.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(AttachmentTest::onTouchesEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
}
|
|
|
|
std::string AttachmentTest::title() const
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Testing MeshRenderer Attachment";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
std::string AttachmentTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "touch to switch weapon";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void AttachmentTest::addNewMeshWithCoords(Vec2 p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/orc.c3b";
|
|
|
|
auto mesh = MeshRenderer::create(fileName);
|
|
|
|
mesh->setScale(5);
|
|
|
|
mesh->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
addChild(mesh);
|
|
|
|
mesh->setPosition(Vec2(p.x, p.y));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// test attach
|
2022-07-05 14:48:46 +08:00
|
|
|
auto sp = MeshRenderer::create("MeshRendererTest/axe.c3b");
|
|
|
|
mesh->getAttachNode("Bip001 R Hand")->addChild(sp);
|
2019-11-23 20:27:39 +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));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh = mesh;
|
2019-11-23 20:27:39 +08:00
|
|
|
_hasWeapon = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttachmentTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
|
|
|
{
|
|
|
|
if (_hasWeapon)
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->removeAllAttachNode();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto sp = MeshRenderer::create("MeshRendererTest/axe.c3b");
|
|
|
|
_mesh->getAttachNode("Bip001 R Hand")->addChild(sp);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
_hasWeapon = !_hasWeapon;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererReskinTest::MeshRendererReskinTest() : _mesh(nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(Vec2(s.width / 2, s.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(MeshRendererReskinTest::onTouchesEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 20);
|
|
|
|
auto label1 = Label::createWithTTF(ttfConfig, "Hair");
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item1 = MenuItemLabel::create(label1, AX_CALLBACK_1(MeshRendererReskinTest::menuCallback_reSkin, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label2 = Label::createWithTTF(ttfConfig, "Glasses");
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item2 = MenuItemLabel::create(label2, AX_CALLBACK_1(MeshRendererReskinTest::menuCallback_reSkin, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label3 = Label::createWithTTF(ttfConfig, "Coat");
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item3 = MenuItemLabel::create(label3, AX_CALLBACK_1(MeshRendererReskinTest::menuCallback_reSkin, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label4 = Label::createWithTTF(ttfConfig, "Pants");
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item4 = MenuItemLabel::create(label4, AX_CALLBACK_1(MeshRendererReskinTest::menuCallback_reSkin, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label5 = Label::createWithTTF(ttfConfig, "Shoes");
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item5 = MenuItemLabel::create(label5, AX_CALLBACK_1(MeshRendererReskinTest::menuCallback_reSkin, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
item1->setPosition(Vec2(VisibleRect::left().x + 50, VisibleRect::bottom().y + item1->getContentSize().height * 4));
|
|
|
|
item2->setPosition(Vec2(VisibleRect::left().x + 50, VisibleRect::bottom().y + item1->getContentSize().height * 5));
|
|
|
|
item3->setPosition(Vec2(VisibleRect::left().x + 50, VisibleRect::bottom().y + item1->getContentSize().height * 6));
|
|
|
|
item4->setPosition(Vec2(VisibleRect::left().x + 50, VisibleRect::bottom().y + item1->getContentSize().height * 7));
|
|
|
|
item5->setPosition(Vec2(VisibleRect::left().x + 50, VisibleRect::bottom().y + item1->getContentSize().height * 8));
|
|
|
|
item1->setUserData((void*)SkinType::HAIR);
|
|
|
|
item2->setUserData((void*)SkinType::GLASSES);
|
|
|
|
item3->setUserData((void*)SkinType::UPPER_BODY);
|
|
|
|
item4->setUserData((void*)SkinType::PANTS);
|
|
|
|
item5->setUserData((void*)SkinType::SHOES);
|
|
|
|
auto pMenu1 = Menu::create(item1, item2, item3, item4, item5, nullptr);
|
|
|
|
pMenu1->setPosition(Vec2(0.0f, 0.0f));
|
|
|
|
this->addChild(pMenu1, 10);
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererReskinTest::menuCallback_reSkin(Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-09-05 20:45:32 +08:00
|
|
|
auto index = static_cast<int>((uintptr_t)(((MenuItemLabel*)sender)->getUserData()));
|
2019-11-23 20:27:39 +08:00
|
|
|
if (index < (int)SkinType::MAX_TYPE)
|
|
|
|
{
|
|
|
|
_curSkin[index] = (_curSkin[index] + 1) % _skins[index].size();
|
|
|
|
applyCurSkin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererReskinTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Testing MeshRenderer Reskin";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererReskinTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererReskinTest::addNewMeshWithCoords(Vec2 p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/ReskinGirl.c3b";
|
|
|
|
auto mesh = MeshRenderer::create(fileName);
|
|
|
|
mesh->setScale(4);
|
|
|
|
mesh->setRotation3D(Vec3(0.0f, 0.0f, 0.0f));
|
|
|
|
addChild(mesh);
|
|
|
|
mesh->setPosition(Vec2(p.x, p.y - 60));
|
2019-11-23 20:27:39 +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));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh = mesh;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto& body = _skins[(int)SkinType::UPPER_BODY];
|
2022-08-08 13:18:33 +08:00
|
|
|
body.emplace_back("Girl_UpperBody01");
|
|
|
|
body.emplace_back("Girl_UpperBody02");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto& pants = _skins[(int)SkinType::PANTS];
|
2022-08-08 13:18:33 +08:00
|
|
|
pants.emplace_back("Girl_LowerBody01");
|
|
|
|
pants.emplace_back("Girl_LowerBody02");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto& shoes = _skins[(int)SkinType::SHOES];
|
2022-08-08 13:18:33 +08:00
|
|
|
shoes.emplace_back("Girl_Shoes01");
|
|
|
|
shoes.emplace_back("Girl_Shoes02");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto& hair = _skins[(int)SkinType::HAIR];
|
2022-08-08 13:18:33 +08:00
|
|
|
hair.emplace_back("Girl_Hair01");
|
|
|
|
hair.emplace_back("Girl_Hair02");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto& face = _skins[(int)SkinType::FACE];
|
2022-08-08 13:18:33 +08:00
|
|
|
face.emplace_back("Girl_Face01");
|
|
|
|
face.emplace_back("Girl_Face02");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto& hand = _skins[(int)SkinType::HAND];
|
2022-08-08 13:18:33 +08:00
|
|
|
hand.emplace_back("Girl_Hand01");
|
|
|
|
hand.emplace_back("Girl_Hand02");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto& glasses = _skins[(int)SkinType::GLASSES];
|
2022-08-08 13:18:33 +08:00
|
|
|
glasses.emplace_back("");
|
|
|
|
glasses.emplace_back("Girl_Glasses01");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
memset(_curSkin, 0, (int)SkinType::MAX_TYPE * sizeof(int));
|
|
|
|
|
|
|
|
applyCurSkin();
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererReskinTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererReskinTest::applyCurSkin()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
for (ssize_t i = 0; i < _mesh->getMeshCount(); i++)
|
2021-12-31 12:12:40 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = _mesh->getMeshByIndex(static_cast<int>(i));
|
2019-11-23 20:27:39 +08:00
|
|
|
bool isVisible = false;
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int j = 0; j < (int)SkinType::MAX_TYPE; j++)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
if (mesh->getName() == _skins[j].at(_curSkin[j]))
|
|
|
|
{
|
|
|
|
isVisible = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->getMeshByIndex(static_cast<int>(i))->setVisible(isVisible);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererWithOBBPerformanceTest::MeshRendererWithOBBPerformanceTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesBegan = AX_CALLBACK_2(MeshRendererWithOBBPerformanceTest::onTouchesBegan, this);
|
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(MeshRendererWithOBBPerformanceTest::onTouchesEnded, this);
|
|
|
|
listener->onTouchesMoved = AX_CALLBACK_2(MeshRendererWithOBBPerformanceTest::onTouchesMoved, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
initDrawBox();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(Vec2(s.width / 2, s.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
MenuItemFont::setFontName("fonts/arial.ttf");
|
|
|
|
MenuItemFont::setFontSize(65);
|
2022-07-16 10:43:05 +08:00
|
|
|
auto decrease = MenuItemFont::create(" - ", AX_CALLBACK_1(MeshRendererWithOBBPerformanceTest::delOBBCallback, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
decrease->setColor(Color3B(0, 200, 20));
|
2022-07-16 10:43:05 +08:00
|
|
|
auto increase = MenuItemFont::create(" + ", AX_CALLBACK_1(MeshRendererWithOBBPerformanceTest::addOBBCallback, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
increase->setColor(Color3B(0, 200, 20));
|
|
|
|
|
|
|
|
auto menu = Menu::create(decrease, increase, nullptr);
|
|
|
|
menu->alignItemsHorizontally();
|
|
|
|
menu->setPosition(Vec2(s.width / 2, s.height - 65));
|
|
|
|
addChild(menu, 1);
|
|
|
|
|
|
|
|
TTFConfig ttfCount("fonts/Marker Felt.ttf", 30);
|
|
|
|
_labelCubeCount = Label::createWithTTF(ttfCount, "0 cubes");
|
|
|
|
_labelCubeCount->setColor(Color3B(0, 200, 20));
|
|
|
|
_labelCubeCount->setPosition(Vec2(s.width / 2, s.height - 90));
|
|
|
|
addChild(_labelCubeCount);
|
|
|
|
_hasCollider = false;
|
|
|
|
addOBBCallback(nullptr);
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererWithOBBPerformanceTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "OBB Collision Performance Test";
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererWithOBBPerformanceTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::addNewOBBWithCoords(Vec2 p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Vec3 extents = Vec3(10, 10, 10);
|
|
|
|
AABB aabb(-extents, extents);
|
2021-12-31 12:12:40 +08:00
|
|
|
auto obb = OBB(aabb);
|
2019-11-23 20:27:39 +08:00
|
|
|
obb._center = Vec3(p.x, p.y, 0);
|
2022-08-08 13:18:33 +08:00
|
|
|
_obb.emplace_back(obb);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::onTouchesBegan(const std::vector<Touch*>& touches, Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
for (const auto& touch : touches)
|
|
|
|
{
|
|
|
|
auto location = touch->getLocationInView();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto obbSize = _obb.size();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (obbSize)
|
|
|
|
{
|
|
|
|
_intersetList.clear();
|
|
|
|
Ray ray;
|
|
|
|
calculateRayByLocationInView(&ray, location);
|
|
|
|
for (decltype(obbSize) i = 0; i < obbSize; i++)
|
|
|
|
{
|
|
|
|
if (ray.intersects(_obb[i]))
|
|
|
|
{
|
|
|
|
_intersetList.insert((int)i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
for (const auto& touch : touches)
|
|
|
|
{
|
|
|
|
auto location = touch->getLocation();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto obbSize = _obb.size();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
for (decltype(obbSize) i = 0; i < obbSize; i++)
|
|
|
|
{
|
|
|
|
if (_intersetList.find((int)i) != _intersetList.end())
|
|
|
|
_obb[i]._center = Vec3(location.x, location.y, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::update(float dt)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
char szText[16];
|
2021-09-02 14:38:23 +08:00
|
|
|
sprintf(szText, "%lu cubes", static_cast<unsigned long>(_obb.size()));
|
2019-11-23 20:27:39 +08:00
|
|
|
_labelCubeCount->setString(szText);
|
|
|
|
|
|
|
|
if (_drawDebug)
|
|
|
|
{
|
|
|
|
_drawDebug->clear();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
Mat4 mat = _mesh->getNodeToWorldTransform();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_obbt = _obbtOri;
|
|
|
|
_obbt.transform(mat);
|
|
|
|
|
|
|
|
Vec3 corners[8] = {};
|
|
|
|
_obbt.getCorners(corners);
|
|
|
|
_drawDebug->drawCube(corners, Color4F(0, 0, 1, 1));
|
|
|
|
}
|
|
|
|
if (_obb.size() > 0)
|
|
|
|
{
|
|
|
|
_drawOBB->clear();
|
|
|
|
auto obbSize = _obb.size();
|
|
|
|
for (decltype(obbSize) i = 0; i < obbSize; i++)
|
|
|
|
{
|
|
|
|
Vec3 corners[8] = {};
|
|
|
|
_obb[i].getCorners(corners);
|
|
|
|
_drawOBB->drawCube(corners, _obbt.intersects(_obb[i]) ? Color4F(1, 0, 0, 1) : Color4F(0, 1, 0, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::initDrawBox()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_drawOBB = DrawNode3D::create();
|
|
|
|
addChild(_drawOBB);
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::addNewMeshWithCoords(Vec2 p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/tortoise.c3b";
|
|
|
|
auto mesh = MeshRenderer::create(fileName);
|
|
|
|
AABB aabb = mesh->getAABB();
|
2021-12-31 12:12:40 +08:00
|
|
|
_obbt = OBB(aabb);
|
|
|
|
_obbtOri = _obbt;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setScale(0.1f);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setPosition(Vec2(s.width * 4.f / 5.f, s.height / 2.f));
|
|
|
|
addChild(mesh);
|
|
|
|
_mesh = mesh;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation, 0.f, 1.933f);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(RepeatForever::create(animate));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f));
|
|
|
|
_moveAction->retain();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto seq = Sequence::create(
|
2022-07-16 10:43:05 +08:00
|
|
|
_moveAction, CallFunc::create(AX_CALLBACK_0(MeshRendererWithOBBPerformanceTest::reachEndCallBack, this)), nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
seq->setTag(100);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(seq);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_drawDebug = DrawNode3D::create();
|
|
|
|
addChild(_drawDebug);
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::reachEndCallBack()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->stopActionByTag(100);
|
|
|
|
auto inverse = MoveTo::create(4.f, Vec2(s.width - _mesh->getPositionX(), s.height / 2.f));
|
2019-11-23 20:27:39 +08:00
|
|
|
inverse->retain();
|
|
|
|
_moveAction->release();
|
|
|
|
_moveAction = inverse;
|
2021-12-31 12:12:40 +08:00
|
|
|
auto rot = RotateBy::create(1.0f, Vec3(0.f, 180.f, 0.f));
|
|
|
|
auto seq = Sequence::create(rot, _moveAction,
|
2022-07-16 10:43:05 +08:00
|
|
|
CallFunc::create(AX_CALLBACK_0(MeshRendererWithOBBPerformanceTest::reachEndCallBack, this)),
|
2021-12-31 12:12:40 +08:00
|
|
|
nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
seq->setTag(100);
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->runAction(seq);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::addOBBCallback(Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
addOBBWithCount(10);
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::addOBBWithCount(float value)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
for (int i = 0; i < value; i++)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
Vec2 randompos = Vec2(AXRANDOM_0_1() * Director::getInstance()->getWinSize().width,
|
|
|
|
AXRANDOM_0_1() * Director::getInstance()->getWinSize().height);
|
2021-12-31 12:12:40 +08:00
|
|
|
Vec3 extents = Vec3(10, 10, 10);
|
2019-11-23 20:27:39 +08:00
|
|
|
AABB aabb(-extents, extents);
|
2021-12-31 12:12:40 +08:00
|
|
|
auto obb = OBB(aabb);
|
2019-11-23 20:27:39 +08:00
|
|
|
obb._center = Vec3(randompos.x, randompos.y, 0);
|
2022-08-08 13:18:33 +08:00
|
|
|
_obb.emplace_back(obb);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::delOBBCallback(Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
delOBBWithCount(10);
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::delOBBWithCount(float value)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_obb.size() >= 10)
|
|
|
|
{
|
|
|
|
_obb.erase(_obb.begin(), _obb.begin() + value);
|
|
|
|
_drawOBB->clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::unproject(const Mat4& viewProjection, const Size* viewport, Vec3* src, Vec3* dst)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
assert(dst);
|
|
|
|
|
|
|
|
assert(viewport->width != 0.0f && viewport->height != 0.0f);
|
|
|
|
Vec4 screen(src->x / viewport->width, ((viewport->height - src->y)) / viewport->height, src->z, 1.0f);
|
|
|
|
|
|
|
|
screen.x = screen.x * 2.0f - 1.0f;
|
|
|
|
screen.y = screen.y * 2.0f - 1.0f;
|
|
|
|
screen.z = screen.z * 2.0f - 1.0f;
|
|
|
|
|
|
|
|
viewProjection.getInversed().transformVector(screen, &screen);
|
|
|
|
|
|
|
|
if (screen.w != 0.0f)
|
|
|
|
{
|
|
|
|
screen.x /= screen.w;
|
|
|
|
screen.y /= screen.w;
|
|
|
|
screen.z /= screen.w;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst->set(screen.x, screen.y, screen.z);
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererWithOBBPerformanceTest::calculateRayByLocationInView(Ray* ray, const Vec2& location)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto dir = Director::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto view = dir->getWinSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Vec3 src = Vec3(location.x, location.y, -1);
|
|
|
|
Vec3 nearPoint;
|
|
|
|
unproject(mat, &view, &src, &nearPoint);
|
|
|
|
|
|
|
|
src = Vec3(location.x, location.y, 1);
|
|
|
|
Vec3 farPoint;
|
|
|
|
unproject(mat, &view, &src, &farPoint);
|
|
|
|
|
|
|
|
Vec3 direction;
|
|
|
|
Vec3::subtract(farPoint, nearPoint, &direction);
|
|
|
|
direction.normalize();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ray->_origin = nearPoint;
|
2019-11-23 20:27:39 +08:00
|
|
|
ray->_direction = direction;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererMirrorTest::MeshRendererMirrorTest() : _mesh(nullptr), _mirrorMesh(nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(Vec2(s.width / 2, s.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererMirrorTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "MeshRenderer Mirror Test";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererMirrorTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererMirrorTest::addNewMeshWithCoords(Vec2 p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/orc.c3b";
|
|
|
|
auto mesh = MeshRenderer::create(fileName);
|
|
|
|
mesh->setScale(5);
|
|
|
|
mesh->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
addChild(mesh);
|
|
|
|
mesh->setPosition(Vec2(p.x - 80, p.y));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// test attach
|
2022-07-05 14:48:46 +08:00
|
|
|
auto sp = MeshRenderer::create("MeshRendererTest/axe.c3b");
|
|
|
|
mesh->getAttachNode("Bip001 R Hand")->addChild(sp);
|
2019-11-23 20:27:39 +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));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh = mesh;
|
2019-11-23 20:27:39 +08:00
|
|
|
_hasWeapon = true;
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
// create mirror MeshRenderer
|
|
|
|
mesh = MeshRenderer::create(fileName);
|
|
|
|
mesh->setScale(5);
|
|
|
|
mesh->setScaleX(-5);
|
|
|
|
mesh->setCullFace(CullFaceSide::FRONT);
|
|
|
|
mesh->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
addChild(mesh);
|
|
|
|
mesh->setPosition(Vec2(p.x + 80, p.y));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// test attach
|
2022-07-05 14:48:46 +08:00
|
|
|
sp = MeshRenderer::create("MeshRendererTest/axe.c3b");
|
|
|
|
mesh->getAttachNode("Bip001 R Hand")->addChild(sp);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(RepeatForever::create(animate));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
_mirrorMesh = mesh;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
QuaternionTest::QuaternionTest() : _arcSpeed(AX_DEGREES_TO_RADIANS(90)), _radius(100.f), _accAngle(0.f)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(Vec2(s.width / 2.f, s.height / 2.f));
|
2019-11-23 20:27:39 +08:00
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
std::string QuaternionTest::title() const
|
|
|
|
{
|
|
|
|
return "Test Rotation With Quaternion";
|
|
|
|
}
|
|
|
|
std::string QuaternionTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void QuaternionTest::addNewMeshWithCoords(Vec2 p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/tortoise.c3b";
|
|
|
|
auto mesh = MeshRenderer::create(fileName);
|
|
|
|
mesh->setScale(0.1f);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setPosition(Vec2(s.width / 2.f + _radius * cosf(_accAngle), s.height / 2.f + _radius * sinf(_accAngle)));
|
|
|
|
addChild(mesh);
|
|
|
|
_mesh = mesh;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation, 0.f, 1.933f);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(RepeatForever::create(animate));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuaternionTest::update(float delta)
|
|
|
|
{
|
|
|
|
_accAngle += delta * _arcSpeed;
|
|
|
|
const float pi = (float)M_PI;
|
|
|
|
if (_accAngle >= 2 * pi)
|
|
|
|
_accAngle -= 2 * pi;
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->setPosition(Vec2(s.width / 2.f + _radius * cosf(_accAngle), s.height / 2.f + _radius * sinf(_accAngle)));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Quaternion quat;
|
|
|
|
Quaternion::createFromAxisAngle(Vec3(0.f, 0.f, 1.f), _accAngle - pi * 0.5f, &quat);
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->setRotationQuat(quat);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
UseCaseMeshRenderer::UseCaseMeshRenderer() : _caseIdx(0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
_useCaseTitles[0] = "transparent 3d mesh and 2d mesh";
|
2019-11-23 20:27:39 +08:00
|
|
|
_useCaseTitles[1] = "ui - 3d - ui";
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto itemPrev = MenuItemImage::create("Images/b1.png", "Images/b2.png", [&](Ref* sender) {
|
2019-11-23 20:27:39 +08:00
|
|
|
_caseIdx--;
|
|
|
|
if (_caseIdx < 0)
|
|
|
|
_caseIdx = (int)USECASE::MAX_CASE_NUM - 1;
|
|
|
|
this->switchCase();
|
|
|
|
});
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto itemNext = MenuItemImage::create("Images/f1.png", "Images/f2.png", [&](Ref* sender) {
|
2019-11-23 20:27:39 +08:00
|
|
|
_caseIdx++;
|
|
|
|
if (_caseIdx >= (int)USECASE::MAX_CASE_NUM)
|
|
|
|
_caseIdx = 0;
|
|
|
|
this->switchCase();
|
|
|
|
});
|
|
|
|
|
|
|
|
auto menu = Menu::create(itemPrev, itemNext, nullptr);
|
|
|
|
menu->alignItemsHorizontally();
|
|
|
|
menu->setScale(0.5);
|
|
|
|
menu->setAnchorPoint(Vec2(0.0f, 0.0f));
|
|
|
|
menu->setPosition(Vec2(s.width / 2, 70.0f));
|
|
|
|
|
|
|
|
_label = Label::create();
|
|
|
|
_label->setPosition(s.width * 0.5f, s.height * 0.8f);
|
|
|
|
addChild(_label);
|
|
|
|
|
|
|
|
addChild(menu);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// setup camera
|
2019-11-23 20:27:39 +08:00
|
|
|
auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f);
|
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
camera->setPosition3D(Vec3(0.f, 30.f, 100.f));
|
|
|
|
camera->lookAt(Vec3(0.f, 0.f, 0.f));
|
|
|
|
addChild(camera);
|
|
|
|
|
|
|
|
switchCase();
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string UseCaseMeshRenderer::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "Use Case For 2D + 3D";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string UseCaseMeshRenderer::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void UseCaseMeshRenderer::switchCase()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
removeChildByTag(101);
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
_label->setString(_useCaseTitles[_caseIdx]);
|
2022-07-05 14:48:46 +08:00
|
|
|
if (_caseIdx == 0) // use case 1, 3d transparent mesh + 2d mesh
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string filename = "MeshRendererTest/girl.c3b";
|
|
|
|
auto mesh = MeshRenderer::create(filename);
|
|
|
|
mesh->setScale(0.15f);
|
2019-11-23 20:27:39 +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));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto circleBack = MeshRenderer::create();
|
|
|
|
auto circle = Sprite::create("MeshRendererTest/circle.png");
|
2019-11-23 20:27:39 +08:00
|
|
|
circleBack->setScale(0.5f);
|
|
|
|
circleBack->addChild(circle);
|
|
|
|
circle->runAction(RepeatForever::create(RotateBy::create(3, Vec3(0.f, 0.f, 360.f))));
|
|
|
|
|
|
|
|
circleBack->setRotation3D(Vec3(-90.0f, -90.0f, 0.0f));
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto pos = mesh->getPosition3D();
|
2019-11-23 20:27:39 +08:00
|
|
|
circleBack->setPosition3D(Vec3(pos.x, pos.y, pos.z - 1));
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setOpacity(250);
|
|
|
|
mesh->setCameraMask(2);
|
2019-11-23 20:27:39 +08:00
|
|
|
circleBack->setCameraMask(2);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setTag(3);
|
2019-11-23 20:27:39 +08:00
|
|
|
circleBack->setTag(2);
|
|
|
|
|
|
|
|
auto node = Node::create();
|
2022-07-05 14:48:46 +08:00
|
|
|
node->addChild(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
node->addChild(circleBack);
|
|
|
|
node->setTag(101);
|
|
|
|
addChild(node);
|
|
|
|
|
|
|
|
scheduleUpdate();
|
|
|
|
update(0.f);
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else if (_caseIdx == 1) // use case 2, ui - 3d - ui, last ui should on the top
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto layer = LayerColor::create(Color4B(0, 0, 100, 255), s.width / 2.f, s.height / 2.f);
|
|
|
|
layer->setPosition(s.width * 0.25f, s.height * 0.25f);
|
|
|
|
layer->setGlobalZOrder(-1);
|
|
|
|
addChild(layer);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string filename = "MeshRendererTest/girl.c3b";
|
|
|
|
auto mesh = MeshRenderer::create(filename);
|
|
|
|
mesh->setScale(0.5f);
|
2019-11-23 20:27:39 +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));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setPosition(s.width * 0.25f, s.height * 0.125f);
|
|
|
|
layer->addChild(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 15);
|
|
|
|
auto label1 = Label::createWithTTF(ttfConfig, "Message");
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item1 = MenuItemLabel::create(label1, AX_CALLBACK_1(UseCaseMeshRenderer::menuCallback_Message, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label2 = Label::createWithTTF(ttfConfig, "Message");
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item2 = MenuItemLabel::create(label2, AX_CALLBACK_1(UseCaseMeshRenderer::menuCallback_Message, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
item1->setPosition(Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f,
|
|
|
|
s.height * 0.5f - item1->getContentSize().height));
|
|
|
|
item2->setPosition(Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f,
|
|
|
|
s.height * 0.5f - item1->getContentSize().height * 2.f));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto pMenu1 = Menu::create(item1, item2, nullptr);
|
|
|
|
pMenu1->setPosition(Vec2(0.0f, 0.0f));
|
|
|
|
layer->addChild(pMenu1);
|
|
|
|
|
|
|
|
layer->setTag(101);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void UseCaseMeshRenderer::menuCallback_Message(Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto layer = getChildByTag(101);
|
|
|
|
auto message = layer->getChildByTag(102); // message layer
|
2019-11-23 20:27:39 +08:00
|
|
|
if (message)
|
|
|
|
layer->removeChild(message);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// create a new message layer on the top
|
2021-12-31 12:12:40 +08:00
|
|
|
auto s = layer->getContentSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto messagelayer = LayerColor::create(Color4B(100, 100, 0, 255));
|
|
|
|
messagelayer->setContentSize(Size(s.width * 0.5f, s.height * 0.5f));
|
|
|
|
messagelayer->setPosition(Vec2(s.width * 0.25f, s.height * 0.25f));
|
|
|
|
auto label = Label::create();
|
|
|
|
label->setString("This Message Layer \n Should Be On Top");
|
|
|
|
label->setPosition(Vec2(s.width * 0.25f, s.height * 0.25f));
|
|
|
|
messagelayer->addChild(label);
|
|
|
|
messagelayer->setTag(102);
|
|
|
|
layer->addChild(messagelayer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void UseCaseMeshRenderer::update(float delta)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_caseIdx == 0)
|
|
|
|
{
|
|
|
|
static float accAngle = 0.f;
|
2022-07-16 10:43:05 +08:00
|
|
|
accAngle += delta * AX_DEGREES_TO_RADIANS(60);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
float radius = 30.f;
|
|
|
|
float x = cosf(accAngle) * radius, z = sinf(accAngle) * radius;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto node = getChildByTag(101);
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh3d = node->getChildByTag(3);
|
2021-12-31 12:12:40 +08:00
|
|
|
auto circle = node->getChildByTag(2);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh3d->setPositionX(x);
|
|
|
|
mesh3d->setPositionZ(z);
|
2019-11-23 20:27:39 +08:00
|
|
|
circle->setPositionX(x);
|
|
|
|
circle->setPositionZ(z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////
|
|
|
|
// Node Frame Animation
|
2021-12-31 12:12:40 +08:00
|
|
|
NodeAnimationTest::NodeAnimationTest() : _vectorIndex(0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto itemPrev = MenuItemImage::create("Images/b1.png", "Images/b2.png", [&](Ref* sender) {
|
2022-07-05 14:48:46 +08:00
|
|
|
_meshes[_vectorIndex]->setVisible(false);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
int tIndex = _vectorIndex - 1;
|
|
|
|
if (tIndex < 0)
|
2022-07-05 14:48:46 +08:00
|
|
|
_vectorIndex = (int)_meshes.size() - 1;
|
2019-11-23 20:27:39 +08:00
|
|
|
else
|
|
|
|
_vectorIndex--;
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
_meshes[_vectorIndex]->setVisible(true);
|
2019-11-23 20:27:39 +08:00
|
|
|
});
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto itemNext = MenuItemImage::create("Images/f1.png", "Images/f2.png", [&](Ref* sender) {
|
2022-07-05 14:48:46 +08:00
|
|
|
_meshes[_vectorIndex]->setVisible(false);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
int tIndex = _vectorIndex + 1;
|
2022-07-05 14:48:46 +08:00
|
|
|
if (tIndex >= static_cast<int>(_meshes.size()))
|
2019-11-23 20:27:39 +08:00
|
|
|
_vectorIndex = 0;
|
|
|
|
else
|
|
|
|
_vectorIndex++;
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
_meshes[_vectorIndex]->setVisible(true);
|
2019-11-23 20:27:39 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
auto menu = Menu::create(itemPrev, itemNext, nullptr);
|
|
|
|
menu->alignItemsHorizontally();
|
|
|
|
menu->setScale(0.5f);
|
|
|
|
menu->setAnchorPoint(Vec2(0.0f, 0.0f));
|
|
|
|
menu->setPosition(Vec2(s.width / 2, 70.0f));
|
|
|
|
addChild(menu);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(Vec2(s.width / 2.f, s.height / 2.f));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
std::string NodeAnimationTest::title() const
|
|
|
|
{
|
|
|
|
return "Node Animation Test";
|
|
|
|
}
|
|
|
|
std::string NodeAnimationTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Jumping animation";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void NodeAnimationTest::addNewMeshWithCoords(Vec2 p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
// add jumping ball
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string fileName = "MeshRendererTest/ball.c3b";
|
|
|
|
auto mesh = MeshRenderer::create(fileName);
|
|
|
|
mesh->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
mesh->setScale(3);
|
|
|
|
mesh->setPosition(Vec2(s.width / 2.f, s.height / 3.f));
|
|
|
|
mesh->setTexture("MeshRendererTest/teapot.png");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto light1 = PointLight::create(Vec3(s.width * 0.2f, s.height * 0.8f, 100.0f), Color3B(200, 200, 200), 10000.0f);
|
|
|
|
addChild(light1);
|
|
|
|
auto light2 = AmbientLight::create(Color3B(100, 100, 100));
|
|
|
|
addChild(light2);
|
|
|
|
|
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
2021-12-31 12:12:40 +08:00
|
|
|
auto act = RepeatForever::create(animate);
|
2019-11-23 20:27:39 +08:00
|
|
|
act->setTag(0);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(act);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
addChild(mesh);
|
2022-08-08 13:18:33 +08:00
|
|
|
_meshes.emplace_back(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// add jumping orc
|
2022-07-05 14:48:46 +08:00
|
|
|
fileName = "MeshRendererTest/orc_jump.c3t";
|
|
|
|
mesh = MeshRenderer::create(fileName);
|
|
|
|
mesh->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
mesh->setScale(3);
|
|
|
|
mesh->setPosition(Vec2(s.width / 2.f, s.height / 3.f));
|
|
|
|
mesh->setVisible(false);
|
2019-11-23 20:27:39 +08:00
|
|
|
animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
2021-12-31 12:12:40 +08:00
|
|
|
auto act = RepeatForever::create(animate);
|
2019-11-23 20:27:39 +08:00
|
|
|
act->setTag(0);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(act);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
addChild(mesh);
|
2022-08-08 13:18:33 +08:00
|
|
|
_meshes.emplace_back(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererCubeMapTest::MeshRendererCubeMapTest() : _textureCube(nullptr), _skyBox(nullptr), _teapot(nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
addNewMeshWithCoords(Vec2(s.width / 2, s.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererCubeMapTest::~MeshRendererCubeMapTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
_teapot->release();
|
|
|
|
_skyBox->release();
|
|
|
|
_textureCube->release();
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererCubeMapTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "CubeMap & Skybox Test";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererCubeMapTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererCubeMapTest::addNewMeshWithCoords(Vec2 p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
_camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 10, 1000);
|
2019-11-23 20:27:39 +08:00
|
|
|
_camera->setPosition3D(Vec3(0.f, 0.f, 50.f));
|
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesMoved = AX_CALLBACK_2(MeshRendererCubeMapTest::onTouchesMoved, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
// create a teapot
|
2022-07-05 14:48:46 +08:00
|
|
|
_teapot = MeshRenderer::create("MeshRendererTest/teapot.c3b");
|
2019-11-23 20:27:39 +08:00
|
|
|
_teapot->retain();
|
|
|
|
|
|
|
|
// create the second texture for cylinder
|
2022-07-05 14:48:46 +08:00
|
|
|
_textureCube = TextureCube::create("MeshRendererTest/skybox/left.jpg", "MeshRendererTest/skybox/right.jpg",
|
|
|
|
"MeshRendererTest/skybox/top.jpg", "MeshRendererTest/skybox/bottom.jpg",
|
|
|
|
"MeshRendererTest/skybox/front.jpg", "MeshRendererTest/skybox/back.jpg");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_textureCube->retain();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// set texture parameters
|
2019-11-23 20:27:39 +08:00
|
|
|
Texture2D::TexParams tRepeatParams;
|
2021-12-31 12:12:40 +08:00
|
|
|
tRepeatParams.magFilter = backend::SamplerFilter::LINEAR;
|
|
|
|
tRepeatParams.minFilter = backend::SamplerFilter::LINEAR;
|
2019-11-23 20:27:39 +08:00
|
|
|
tRepeatParams.sAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
|
|
|
tRepeatParams.tAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
|
|
|
_textureCube->setTexParameters(tRepeatParams);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mat = MeshMaterial::createWithFilename("MeshRendererTest/CubeMap.material");
|
2019-11-23 20:27:39 +08:00
|
|
|
auto state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
|
|
|
|
_teapot->setMaterial(mat);
|
|
|
|
|
|
|
|
// pass the texture sampler to our custom shader
|
|
|
|
auto cubeTexLoc = state->getUniformLocation("u_cubeTex");
|
|
|
|
state->setTexture(cubeTexLoc, 0, _textureCube->getBackendTexture());
|
|
|
|
|
|
|
|
_teapot->setPosition3D(Vec3(0.0f, -5.0f, 0.0f));
|
|
|
|
_teapot->setRotation3D(Vec3(-90.0f, 180.0f, 0.0f));
|
|
|
|
|
|
|
|
auto rotate_action = RotateBy::create(1.5, Vec3(0.0f, 30.0f, 0.0f));
|
|
|
|
_teapot->runAction(RepeatForever::create(rotate_action));
|
|
|
|
|
|
|
|
addChild(_teapot);
|
|
|
|
|
|
|
|
{
|
|
|
|
// config skybox
|
|
|
|
_skyBox = Skybox::create();
|
|
|
|
_skyBox->retain();
|
|
|
|
|
|
|
|
_skyBox->setTexture(_textureCube);
|
|
|
|
addChild(_skyBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
addChild(_camera);
|
|
|
|
setCameraMask(2);
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2021-12-31 12:12:40 +08:00
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom*) {
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_textureCube);
|
2022-07-05 14:48:46 +08:00
|
|
|
_textureCube = TextureCube::create("MeshRendererTest/skybox/left.jpg", "MeshRendererTest/skybox/right.jpg",
|
|
|
|
"MeshRendererTest/skybox/top.jpg", "MeshRendererTest/skybox/bottom.jpg",
|
|
|
|
"MeshRendererTest/skybox/front.jpg", "MeshRendererTest/skybox/back.jpg");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_textureCube->retain();
|
2021-12-31 12:12:40 +08:00
|
|
|
// set texture parameters
|
2019-11-23 20:27:39 +08:00
|
|
|
Texture2D::TexParams tRepeatParams;
|
2021-12-31 12:12:40 +08:00
|
|
|
tRepeatParams.magFilter = backend::SamplerFilter::LINEAR;
|
|
|
|
tRepeatParams.minFilter = backend::SamplerFilter::LINEAR;
|
|
|
|
tRepeatParams.sAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
|
|
|
tRepeatParams.tAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
2019-11-23 20:27:39 +08:00
|
|
|
_textureCube->setTexParameters(tRepeatParams);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mat = MeshMaterial::createWithFilename("MeshRendererTest/CubeMap.material");
|
2019-11-23 20:27:39 +08:00
|
|
|
auto state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
|
|
|
|
_teapot->setMaterial(mat);
|
|
|
|
_skyBox->setTexture(_textureCube);
|
|
|
|
// pass the texture sampler to our custom shader
|
|
|
|
auto cubeTexLoc = state->getUniformLocation("u_cubeTex");
|
|
|
|
state->setTexture(cubeTexLoc, 0, _textureCube->getBackendTexture());
|
2021-12-31 12:12:40 +08:00
|
|
|
});
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-10-18 19:17:36 +08:00
|
|
|
void MeshRendererCubeMapTest::onTouchesMoved(const std::vector<Touch*>& touches, ax::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (touches.size())
|
|
|
|
{
|
|
|
|
auto touch = touches[0];
|
|
|
|
auto delta = touch->getDelta();
|
|
|
|
|
|
|
|
static float _angle = 0.f;
|
2022-07-16 10:43:05 +08:00
|
|
|
_angle -= AX_DEGREES_TO_RADIANS(delta.x);
|
2019-11-23 20:27:39 +08:00
|
|
|
_camera->setPosition3D(Vec3(50.0f * sinf(_angle), 0.0f, 50.0f * cosf(_angle)));
|
|
|
|
_camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Issue9767::Issue9767()
|
|
|
|
{
|
|
|
|
_shaderType = Issue9767::ShaderType::SHADER_TEX;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
|
|
|
mesh->setScale(3.f);
|
|
|
|
mesh->setTexture("MeshRendererTest/boss.png");
|
|
|
|
addChild(mesh);
|
|
|
|
mesh->setPosition(Vec2(s.width / 2, s.height / 2));
|
|
|
|
_mesh = mesh;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 15);
|
|
|
|
auto label1 = Label::createWithTTF(ttfConfig, "switch shader");
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item1 = MenuItemLabel::create(label1, AX_CALLBACK_1(Issue9767::menuCallback_SwitchShader, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
item1->setPosition(
|
|
|
|
Vec2(s.width * 0.9f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto pMenu1 = Menu::create(item1, nullptr);
|
|
|
|
pMenu1->setPosition(Vec2(0.0f, 0.0f));
|
|
|
|
addChild(pMenu1);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Issue9767::~Issue9767() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-18 19:17:36 +08:00
|
|
|
void Issue9767::menuCallback_SwitchShader(ax::Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_programState);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_shaderType == Issue9767::ShaderType::SHADER_TEX)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
_shaderType = Issue9767::ShaderType::SHADER_COLOR;
|
|
|
|
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_3D);
|
2019-11-23 20:27:39 +08:00
|
|
|
_programState = new backend::ProgramState(program);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
_shaderType = Issue9767::ShaderType::SHADER_TEX;
|
|
|
|
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_3D);
|
2019-11-23 20:27:39 +08:00
|
|
|
_programState = new backend::ProgramState(program);
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->setProgramState(_programState);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Issue9767::title() const
|
|
|
|
{
|
|
|
|
return "Issue9767: test setGLProgram";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Issue9767::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererClippingTest::MeshRendererClippingTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
auto stencil = Sprite::create("Images/close.png");
|
2022-07-05 14:48:46 +08:00
|
|
|
auto clipMeshRenderer = ClippingNode::create();
|
|
|
|
clipMeshRenderer->setStencil(stencil);
|
|
|
|
this->addChild(clipMeshRenderer);
|
|
|
|
clipMeshRenderer->setScale(3.0f);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh3D = MeshRenderer::create("MeshRendererTest/orc.c3b");
|
|
|
|
mesh3D->setScale(1.0f);
|
|
|
|
mesh3D->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
clipMeshRenderer->addChild(mesh3D); // 5
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
clipMeshRenderer->setPosition(Vec2(size.width / 2, size.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto seq = Sequence::create(ScaleTo::create(2.f, 3), ScaleTo::create(2.f, 0.5f), NULL);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh3D->runAction(RepeatForever::create(seq));
|
|
|
|
auto animation = Animation3D::create("MeshRendererTest/orc.c3b");
|
2021-12-31 12:12:40 +08:00
|
|
|
auto animate = Animate3D::create(animation);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh3D->runAction(RepeatForever::create(animate));
|
2022-08-06 16:17:55 +08:00
|
|
|
mesh3D->getMaterial()->setForce2DQueue(true);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererClippingTest::~MeshRendererClippingTest() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererClippingTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "MeshRenderer Clipping Test";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererClippingTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
Animate3DCallbackTest::Animate3DCallbackTest()
|
|
|
|
{
|
|
|
|
FileUtils::getInstance()->addSearchPath("Particle3D/materials");
|
|
|
|
FileUtils::getInstance()->addSearchPath("Particle3D/scripts");
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2022-07-05 14:48:46 +08:00
|
|
|
_meshRenderer = MeshRenderer::create("MeshRendererTest/ReskinGirl.c3b");
|
|
|
|
_meshRenderer->setPosition(Vec2(s.width / 2.0f, s.height / 3.0f));
|
|
|
|
_meshRenderer->setScale(3.0f);
|
|
|
|
_meshRenderer->setRotation3D(Vec3(0.0f, 90.0f, 0.0f));
|
|
|
|
this->addChild(_meshRenderer);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
_meshRenderer->getMeshByName("Girl_UpperBody02")->setVisible(false);
|
|
|
|
_meshRenderer->getMeshByName("Girl_LowerBody02")->setVisible(false);
|
|
|
|
_meshRenderer->getMeshByName("Girl_Shoes02")->setVisible(false);
|
|
|
|
_meshRenderer->getMeshByName("Girl_Hair02")->setVisible(false);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto rootps = PUParticleSystem3D::create("explosionSystem.pu");
|
|
|
|
rootps->stopParticleSystem();
|
|
|
|
rootps->setScale(4.0f);
|
|
|
|
this->addChild(rootps, 0, 100);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto animation = Animation3D::create("MeshRendererTest/ReskinGirl.c3b");
|
2019-11-23 20:27:39 +08:00
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
2022-07-05 14:48:46 +08:00
|
|
|
_meshRenderer->runAction(RepeatForever::create(animate));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
ValueMap valuemap0;
|
|
|
|
animate->setKeyFrameUserInfo(275, valuemap0);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerCustom::create(Animate3DDisplayedNotification, [&](EventCustom* event) {
|
2019-11-23 20:27:39 +08:00
|
|
|
auto info = (Animate3D::Animate3DDisplayedEventInfo*)event->getUserData();
|
|
|
|
auto node = getChildByTag(100);
|
|
|
|
if (node)
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mat = _meshRenderer->getNodeToWorldTransform() *
|
|
|
|
_meshRenderer->getSkeleton()->getBoneByName("Bip01 R Hand")->getWorldMat();
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setPosition3D(Vec3(mat.m[12] + 100, mat.m[13], mat.m[14]));
|
|
|
|
((PUParticleSystem3D*)node)->startParticleSystem();
|
|
|
|
}
|
|
|
|
|
2022-10-18 19:17:36 +08:00
|
|
|
ax::log("frame %d", info->frame);
|
2019-11-23 20:27:39 +08:00
|
|
|
});
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Animate3DCallbackTest::~Animate3DCallbackTest() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
std::string Animate3DCallbackTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Animate3D Callback";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Animate3DCallbackTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererVertexColorTest::MeshRendererVertexColorTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/box_VertexCol.c3t");
|
|
|
|
mesh->setPosition(Vec2(0.0f, 0.0f));
|
|
|
|
mesh->setScale(1.0f);
|
|
|
|
mesh->setCameraMask(2);
|
|
|
|
auto mat = MeshMaterial::createWithFilename("MeshRendererTest/VertexColor.material");
|
|
|
|
mesh->setMaterial(mat);
|
|
|
|
mesh->runAction(RepeatForever::create(RotateBy::create(1.0f, Vec3(10.0f, 50.0f, 10.0f))));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
this->addChild(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// setup camera
|
2019-11-23 20:27:39 +08:00
|
|
|
auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f);
|
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
camera->setPosition3D(Vec3(0.0f, 0.0f, 10.f));
|
|
|
|
camera->lookAt(Vec3(0.f, 0.f, 0.f));
|
|
|
|
addChild(camera);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2021-12-31 12:12:40 +08:00
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [=](EventCustom*) {
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mat = MeshMaterial::createWithFilename("MeshRendererTest/VertexColor.material");
|
|
|
|
mesh->setMaterial(mat);
|
2021-12-31 12:12:40 +08:00
|
|
|
});
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererVertexColorTest::~MeshRendererVertexColorTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererVertexColorTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "Testing Vertex Color";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererVertexColorTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
CameraBackgroundClearTest::CameraBackgroundClearTest()
|
|
|
|
{
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 20);
|
2021-12-31 12:12:40 +08:00
|
|
|
auto label1 = Label::createWithTTF(ttfConfig, "Clear Mode");
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item1 = MenuItemLabel::create(label1, AX_CALLBACK_1(CameraBackgroundClearTest::switch_CameraClearMode, this));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
item1->setPosition(Vec2(VisibleRect::left().x + 50, VisibleRect::bottom().y + item1->getContentSize().height * 4));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto pMenu1 = Menu::create(item1, nullptr);
|
2021-12-31 12:12:40 +08:00
|
|
|
pMenu1->setPosition(Vec2(0.0f, 0.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
this->addChild(pMenu1, 10);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
// setup camera
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
_camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f);
|
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
_camera->setPosition3D(Vec3(0.f, 30.f, 100.f));
|
|
|
|
_camera->lookAt(Vec3(0.f, 0.f, 0.f));
|
|
|
|
addChild(_camera);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/orc.c3b");
|
|
|
|
addChild(mesh);
|
|
|
|
mesh->setCameraMask(2);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_label = Label::createWithTTF(ttfConfig, "Depth Clear Brush");
|
|
|
|
addChild(_label);
|
2021-12-31 12:12:40 +08:00
|
|
|
_label->setPosition(s.width / 2.f, VisibleRect::top().y * 0.8f);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-10-18 19:17:36 +08:00
|
|
|
void CameraBackgroundClearTest::switch_CameraClearMode(ax::Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto brush = _camera->getBackgroundBrush();
|
2019-11-23 20:27:39 +08:00
|
|
|
CameraBackgroundBrush::BrushType type = CameraBackgroundBrush::BrushType::NONE;
|
|
|
|
if (!brush)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("No brash found!");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
type = brush->getBrushType();
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (type == CameraBackgroundBrush::BrushType::NONE)
|
|
|
|
{
|
|
|
|
_camera->setBackgroundBrush(CameraBackgroundBrush::createDepthBrush(1.f));
|
|
|
|
_label->setString("Depth Clear Brush");
|
|
|
|
// Test brush valid when set by user scene setting
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Background brush valid status is : %s", _camera->isBrushValid() ? "true" : "false");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else if (type == CameraBackgroundBrush::BrushType::DEPTH)
|
|
|
|
{
|
|
|
|
_camera->setBackgroundBrush(CameraBackgroundBrush::createColorBrush(Color4F(1.f, 0.f, 0.f, 1.f), 1.f));
|
|
|
|
_label->setString("Color Clear Brush");
|
|
|
|
}
|
|
|
|
else if (type == CameraBackgroundBrush::BrushType::COLOR)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
_camera->setBackgroundBrush(CameraBackgroundBrush::createSkyboxBrush(
|
2022-07-05 14:48:46 +08:00
|
|
|
"MeshRendererTest/skybox/left.jpg", "MeshRendererTest/skybox/right.jpg", "MeshRendererTest/skybox/top.jpg",
|
|
|
|
"MeshRendererTest/skybox/bottom.jpg", "MeshRendererTest/skybox/front.jpg", "MeshRendererTest/skybox/back.jpg"));
|
2019-11-23 20:27:39 +08:00
|
|
|
_label->setString("Skybox Clear Brush");
|
|
|
|
}
|
|
|
|
else if (type == CameraBackgroundBrush::BrushType::SKYBOX)
|
|
|
|
{
|
|
|
|
_camera->setBackgroundBrush(CameraBackgroundBrush::createNoneBrush());
|
|
|
|
_label->setString("None Clear Brush");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CameraBackgroundClearTest::title() const
|
|
|
|
{
|
|
|
|
return "Camera Background Clear Brush";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CameraBackgroundClearTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
MotionStreak3DTest::MotionStreak3DTest()
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f);
|
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
camera->setPosition3D(Vec3(0.f, 50.f, 200.f));
|
|
|
|
camera->lookAt(Vec3(0.f, 0.f, 0.f));
|
|
|
|
addChild(camera);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/orc.c3b");
|
|
|
|
mesh->setPosition(20.f, 0.f);
|
|
|
|
addChild(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto streak = MotionStreak3D::create(1.0f, 1.0f, 5.f, Color3B(255, 255, 0), "Images/Icon.png");
|
|
|
|
addChild(streak);
|
|
|
|
|
|
|
|
setCameraMask(2);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh = mesh;
|
2019-11-23 20:27:39 +08:00
|
|
|
_streak = streak;
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
std::string MotionStreak3DTest::title() const
|
|
|
|
{
|
|
|
|
return "MotionStreak3D Test";
|
|
|
|
}
|
|
|
|
std::string MotionStreak3DTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void MotionStreak3DTest::update(float delta)
|
|
|
|
{
|
|
|
|
static float t = 0;
|
|
|
|
t += delta;
|
|
|
|
float angle = t * M_PI;
|
2021-12-31 12:12:40 +08:00
|
|
|
float r = 20.f;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->setPosition3D(Vec3(r * cosf(angle), 0, r * sinf(angle)));
|
|
|
|
_streak->setPosition3D(_mesh->getPosition3D());
|
2019-11-23 20:27:39 +08:00
|
|
|
_streak->setSweepAxis(Vec3(cosf(angle), 0, sinf(angle)));
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererNormalMappingTest::MeshRendererNormalMappingTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/sphere.c3b");
|
|
|
|
mesh->setPosition(Vec2(-30.0f, 0.0f));
|
|
|
|
mesh->setRotation3D(Vec3(90.0f, 0.0f, 0.0f));
|
|
|
|
mesh->setScale(2.0);
|
|
|
|
mesh->setCameraMask(2);
|
|
|
|
mesh->setTexture("MeshRendererTest/brickwork-texture.jpg");
|
|
|
|
addChild(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int maxAttributes = Configuration::getInstance()->getMaxAttributes();
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(maxAttributes > 8, "attributes supported must be greater than 8");
|
2019-11-23 20:27:39 +08:00
|
|
|
if (maxAttributes > 8)
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/sphere_bumped.c3b");
|
|
|
|
mesh->setPosition(Vec2(30.0f, 0.0f));
|
|
|
|
mesh->setRotation3D(Vec3(90.0f, 0.0f, 0.0f));
|
|
|
|
mesh->setScale(20.0);
|
|
|
|
mesh->setCameraMask(2);
|
|
|
|
mesh->setTexture("MeshRendererTest/brickwork-texture.jpg");
|
|
|
|
addChild(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// setup camera
|
2019-11-23 20:27:39 +08:00
|
|
|
auto camera = Camera::createPerspective(60.0, s.width / s.height, 1.0f, 1000.f);
|
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
camera->setPosition3D(Vec3(0.f, 0.f, 100.f));
|
|
|
|
camera->lookAt(Vec3(0.f, 0.f, 0.f));
|
|
|
|
addChild(camera);
|
|
|
|
|
|
|
|
PointLight* light = PointLight::create(Vec3(0.0, 0.0, 0.0), Color3B(255, 255, 255), 1000);
|
|
|
|
light->setTag(100);
|
|
|
|
addChild(light);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererNormalMappingTest::update(float dt)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
static float angle = 0.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
static bool reverseDir = false;
|
2021-12-31 12:12:40 +08:00
|
|
|
static float radius = 100.0f;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto light = static_cast<PointLight*>(getChildByTag(100));
|
|
|
|
light->setPosition3D(Vec3(radius * cos(angle), 0.0f, radius * sin(angle)));
|
2021-12-31 12:12:40 +08:00
|
|
|
if (reverseDir)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
angle -= 0.01f;
|
|
|
|
if (angle < 0.0)
|
|
|
|
reverseDir = false;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
angle += 0.01f;
|
|
|
|
if (3.14159 < angle)
|
|
|
|
reverseDir = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererNormalMappingTest::~MeshRendererNormalMappingTest() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererNormalMappingTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "NormalMapping Test";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererNormalMappingTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererPropertyTest::MeshRendererPropertyTest()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f);
|
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
camera->setPosition3D(Vec3(0.f, 50.f, 200.f));
|
|
|
|
camera->lookAt(Vec3(0.f, 0.f, 0.f));
|
|
|
|
addChild(camera);
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh = MeshRenderer::create("MeshRendererTest/orc.c3b");
|
|
|
|
_mesh->setPosition(20.f, 0.f);
|
|
|
|
_mesh->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
_meshTex = _mesh->getMesh()->getTexture();
|
2019-11-23 20:27:39 +08:00
|
|
|
_texFile = _meshTex->getPath();
|
2022-07-05 14:48:46 +08:00
|
|
|
addChild(_mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
setCameraMask(2);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
////listener->onTouchesEnded = AX_CALLBACK_2(MeshRendererReskinTest::onTouchesEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
//_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 20);
|
|
|
|
|
|
|
|
auto label1 = Label::createWithTTF(ttfConfig, "Print Mesh Name");
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item1 = MenuItemLabel::create(label1, AX_CALLBACK_1(MeshRendererPropertyTest::printMeshName, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label2 = Label::createWithTTF(ttfConfig, "Remove Used Texture");
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item2 = MenuItemLabel::create(label2, AX_CALLBACK_1(MeshRendererPropertyTest::removeUsedTexture, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label3 = Label::createWithTTF(ttfConfig, "Reset");
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item3 = MenuItemLabel::create(label3, AX_CALLBACK_1(MeshRendererPropertyTest::resetTexture, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
item1->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 4));
|
|
|
|
item2->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 5));
|
|
|
|
item3->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 6));
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto pMenu1 = Menu::create(item1, item2, item3, nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
pMenu1->setPosition(Vec2(0.0f, 0.0f));
|
|
|
|
this->addChild(pMenu1, 10);
|
|
|
|
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererPropertyTest::title() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "MeshRendererPropertyTest Test";
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string MeshRendererPropertyTest::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererPropertyTest::update(float delta) {}
|
2022-10-18 19:17:36 +08:00
|
|
|
void MeshRendererPropertyTest::printMeshName(ax::Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("MeshName Begin");
|
2022-07-05 14:48:46 +08:00
|
|
|
Vector<Mesh*> meshes = _mesh->getMeshes();
|
2021-12-31 12:12:40 +08:00
|
|
|
for (Mesh* mesh : meshes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
log("MeshName: %s ", mesh->getName().data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("MeshName End");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-10-18 19:17:36 +08:00
|
|
|
void MeshRendererPropertyTest::removeUsedTexture(ax::Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_meshTex != nullptr)
|
|
|
|
{
|
|
|
|
Director::getInstance()->getTextureCache()->removeTexture(_meshTex);
|
2022-07-05 14:48:46 +08:00
|
|
|
this->refreshMeshRender();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 19:17:36 +08:00
|
|
|
void MeshRendererPropertyTest::resetTexture(ax::Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_meshTex != nullptr)
|
|
|
|
{
|
|
|
|
_meshTex = Director::getInstance()->getTextureCache()->addImage(_texFile);
|
2022-07-05 14:48:46 +08:00
|
|
|
this->refreshMeshRender();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRendererPropertyTest::refreshMeshRender()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
Vector<Mesh*> meshes = _mesh->getMeshes();
|
2019-11-23 20:27:39 +08:00
|
|
|
for (Mesh* mesh : meshes)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string file = mesh->getTextureFileName();
|
2019-11-23 20:27:39 +08:00
|
|
|
Texture2D* cacheTex = Director::getInstance()->getTextureCache()->getTextureForKey(file);
|
|
|
|
if (cacheTex == nullptr)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
unsigned char data[] = {255, 0, 0, 255}; // 1*1 red picture
|
|
|
|
Image* image = new Image();
|
2019-11-23 20:27:39 +08:00
|
|
|
image->initWithRawData(data, sizeof(data), 1, 1, sizeof(unsigned char));
|
|
|
|
cacheTex = Director::getInstance()->getTextureCache()->addImage(image, "/dummyTexture");
|
|
|
|
image->release();
|
|
|
|
}
|
2022-10-18 19:17:36 +08:00
|
|
|
mesh->setTexture(cacheTex, ax::NTextureData::Usage::Diffuse, false);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Issue16155Test
|
|
|
|
//
|
|
|
|
Issue16155Test::Issue16155Test()
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/orc.c3b");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
int rcBefore = mesh->getMeshByIndex(0)->getTexture()->getReferenceCount();
|
|
|
|
addChild(mesh);
|
|
|
|
removeChild(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-18 19:17:36 +08:00
|
|
|
ax::log("Issue 16155: Ref count:%d. Run this test again. RC should be the same", rcBefore);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Issue16155Test::title() const
|
|
|
|
{
|
|
|
|
return "Issue16155 Test";
|
|
|
|
}
|
|
|
|
std::string Issue16155Test::subtitle() const
|
|
|
|
{
|
|
|
|
return "Should not leak texture. See console";
|
|
|
|
}
|