2015-05-06 04:07:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "MaterialSystemTest.h"
|
2015-05-15 08:24:27 +08:00
|
|
|
|
|
|
|
#include <ctime>
|
2015-06-13 07:47:08 +08:00
|
|
|
#include <spine/spine-cocos2dx.h>
|
2015-05-15 08:24:27 +08:00
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
#include "../testResource.h"
|
2022-10-12 00:15:09 +08:00
|
|
|
#include "axmol.h"
|
2015-08-10 15:51:26 +08:00
|
|
|
#include "ui/CocosGUI.h"
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-05-14 08:39:39 +08:00
|
|
|
static void printProperties(Properties* properties, int indent);
|
2015-05-06 06:59:41 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
// MARK: Tests
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
MaterialSystemTest::MaterialSystemTest()
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
ADD_TEST_CASE(Material_2DEffects);
|
2019-03-15 14:11:50 +08:00
|
|
|
ADD_TEST_CASE(Material_AutoBindings);
|
2015-05-14 08:39:39 +08:00
|
|
|
ADD_TEST_CASE(Material_setTechnique);
|
|
|
|
ADD_TEST_CASE(Material_clone);
|
2022-07-05 14:48:46 +08:00
|
|
|
ADD_TEST_CASE(Material_MultipleMeshRenderer);
|
|
|
|
ADD_TEST_CASE(Material_MeshRendererTest);
|
2015-05-15 08:24:27 +08:00
|
|
|
ADD_TEST_CASE(Material_parsePerformance);
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
std::string MaterialSystemBaseTest::title() const
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
return "Material System";
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
// MARK: Tests start here
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void Material_MeshRendererTest::onEnter()
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
MaterialSystemBaseTest::onEnter();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
|
|
|
mesh->setScale(8.f);
|
|
|
|
mesh->setTexture("MeshRendererTest/boss.png");
|
|
|
|
this->addChild(mesh);
|
|
|
|
mesh->setPositionNormalized(Vec2(0.5f, 0.5f));
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string Material_MeshRendererTest::subtitle() const
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
return "Material System on MeshRenderer";
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void Material_MultipleMeshRenderer::onEnter()
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
MaterialSystemBaseTest::onEnter();
|
|
|
|
|
|
|
|
const char* names[] = {
|
2022-07-05 14:48:46 +08:00
|
|
|
"MeshRendererTest/ReskinGirl.c3b", "MeshRendererTest/ReskinGirl.c3b", "MeshRendererTest/ReskinGirl.c3b",
|
|
|
|
"MeshRendererTest/ReskinGirl.c3b", "MeshRendererTest/ReskinGirl.c3b", "MeshRendererTest/ReskinGirl.c3b",
|
|
|
|
"MeshRendererTest/ReskinGirl.c3b", "MeshRendererTest/ReskinGirl.c3b",
|
2015-05-06 04:07:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const int totalNames = sizeof(names) / sizeof(names[0]);
|
|
|
|
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < totalNames; i++)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create(names[i]);
|
|
|
|
this->addChild(mesh);
|
|
|
|
mesh->setPosition(Vec2((size.width / (totalNames + 1)) * (i + 1), size.height / 4));
|
|
|
|
mesh->setScale(3);
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string Material_MultipleMeshRenderer::subtitle() const
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
return "Sprites with multiple meshes";
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
//
|
2015-06-03 07:56:59 +08:00
|
|
|
// MARK: Material_2DEffects
|
2015-05-12 12:31:33 +08:00
|
|
|
//
|
|
|
|
void Material_2DEffects::onEnter()
|
|
|
|
{
|
|
|
|
MaterialSystemBaseTest::onEnter();
|
|
|
|
|
2015-05-20 04:51:52 +08:00
|
|
|
auto properties = Properties::createNonRefCounted("Materials/2d_effects.material#sample");
|
2015-05-12 12:31:33 +08:00
|
|
|
|
|
|
|
// Print the properties of every namespace within this one.
|
|
|
|
printProperties(properties, 0);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Material* mat1 = Material::createWithProperties(properties);
|
2015-05-12 12:31:33 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto meshBlur = Sprite::create("Images/grossini.png");
|
|
|
|
meshBlur->setPositionNormalized(Vec2(0.2f, 0.5f));
|
|
|
|
this->addChild(meshBlur);
|
|
|
|
meshBlur->setProgramState(mat1->getTechniqueByName("blur")->getPassByIndex(0)->getProgramState());
|
2015-05-12 12:31:33 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto meshOutline = Sprite::create("Images/grossini.png");
|
|
|
|
meshOutline->setPositionNormalized(Vec2(0.4f, 0.5f));
|
|
|
|
this->addChild(meshOutline);
|
|
|
|
meshOutline->setProgramState(mat1->getTechniqueByName("outline")->getPassByIndex(0)->getProgramState());
|
2015-05-12 12:31:33 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto meshNoise = Sprite::create("Images/grossini.png");
|
|
|
|
meshNoise->setPositionNormalized(Vec2(0.6f, 0.5f));
|
|
|
|
this->addChild(meshNoise);
|
|
|
|
meshNoise->setProgramState(mat1->getTechniqueByName("noise")->getPassByIndex(0)->getProgramState());
|
2015-05-12 12:31:33 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto meshEdgeDetect = Sprite::create("Images/grossini.png");
|
|
|
|
meshEdgeDetect->setPositionNormalized(Vec2(0.8f, 0.5f));
|
|
|
|
this->addChild(meshEdgeDetect);
|
|
|
|
meshEdgeDetect->setProgramState(mat1->getTechniqueByName("edge_detect")->getPassByIndex(0)->getProgramState());
|
2015-05-15 08:24:27 +08:00
|
|
|
|
2019-03-15 14:11:50 +08:00
|
|
|
timeUniforms.clear();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
#define FETCH_CCTIME_LOCATION(mesh) \
|
2021-12-28 16:06:23 +08:00
|
|
|
do \
|
|
|
|
{ \
|
2022-07-05 14:48:46 +08:00
|
|
|
auto programState = mesh->getProgramState(); \
|
2021-12-28 16:06:23 +08:00
|
|
|
auto location = programState->getUniformLocation("u_Time"); \
|
|
|
|
timeUniforms.emplace_back(programState, location); \
|
|
|
|
} while (0)
|
2019-03-15 14:11:50 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
FETCH_CCTIME_LOCATION(meshBlur);
|
|
|
|
FETCH_CCTIME_LOCATION(meshOutline);
|
|
|
|
FETCH_CCTIME_LOCATION(meshNoise);
|
|
|
|
FETCH_CCTIME_LOCATION(meshEdgeDetect);
|
2019-03-15 14:11:50 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
schedule(AX_SCHEDULE_SELECTOR(Material_2DEffects::updateCCTimeUniforms));
|
2019-03-15 14:11:50 +08:00
|
|
|
|
2015-05-15 08:24:27 +08:00
|
|
|
// properties is not a "Ref" object
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(properties);
|
2015-05-12 12:31:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Material_2DEffects::subtitle() const
|
|
|
|
{
|
|
|
|
return "Testing effects on Sprite";
|
|
|
|
}
|
|
|
|
|
2019-03-15 14:11:50 +08:00
|
|
|
void Material_2DEffects::updateCCTimeUniforms(float)
|
|
|
|
{
|
|
|
|
float time = Director::getInstance()->getTotalFrames() * Director::getInstance()->getAnimationInterval();
|
|
|
|
Vec4 random(time / 10.0f, time, time * 2.0f, time * 4.0f);
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& loc : timeUniforms)
|
2019-03-15 14:11:50 +08:00
|
|
|
{
|
|
|
|
loc.programState->setUniform(loc.location, &random, sizeof(random));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:43:40 +08:00
|
|
|
//
|
2019-03-15 14:11:50 +08:00
|
|
|
// MARK: Material_AutoBindings
|
2019-03-14 13:43:40 +08:00
|
|
|
//
|
2019-03-15 14:11:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Custom material auto-binding resolver for terrain.
|
|
|
|
*/
|
|
|
|
class EffectAutoBindingResolver : public backend::ProgramState::AutoBindingResolver
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
virtual bool resolveAutoBinding(backend::ProgramState* programState,
|
|
|
|
/* Node* node,*/ std::string_view uniform,
|
|
|
|
std::string_view autoBinding) override;
|
2019-03-15 14:11:50 +08:00
|
|
|
|
|
|
|
void callbackRadius(backend::ProgramState* programState, backend::UniformLocation uniform);
|
|
|
|
void callbackColor(backend::ProgramState* programState, backend::UniformLocation uniform);
|
|
|
|
};
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
bool EffectAutoBindingResolver::resolveAutoBinding(backend::ProgramState* programState,
|
|
|
|
/*Node* node,*/ std::string_view uniform,
|
|
|
|
std::string_view autoBinding)
|
2019-03-15 14:11:50 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (autoBinding.compare("DYNAMIC_RADIUS") == 0)
|
2019-03-15 14:11:50 +08:00
|
|
|
{
|
|
|
|
auto loc = programState->getUniformLocation(uniform);
|
2022-07-15 19:17:01 +08:00
|
|
|
programState->setCallbackUniform(loc, AX_CALLBACK_2(EffectAutoBindingResolver::callbackRadius, this));
|
2019-03-15 14:11:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
else if (autoBinding.compare("OUTLINE_COLOR") == 0)
|
2019-03-15 14:11:50 +08:00
|
|
|
{
|
|
|
|
auto loc = programState->getUniformLocation(uniform);
|
2022-07-15 19:17:01 +08:00
|
|
|
programState->setCallbackUniform(loc, AX_CALLBACK_2(EffectAutoBindingResolver::callbackColor, this));
|
2019-03-15 14:11:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void EffectAutoBindingResolver::callbackRadius(backend::ProgramState* programState, backend::UniformLocation uniform)
|
2019-03-15 14:11:50 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
float f = AXRANDOM_0_1() * 10;
|
2019-03-15 14:11:50 +08:00
|
|
|
programState->setUniform(uniform, &f, sizeof(f));
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void EffectAutoBindingResolver::callbackColor(backend::ProgramState* programState, backend::UniformLocation uniform)
|
2019-03-15 14:11:50 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
float r = AXRANDOM_0_1();
|
|
|
|
float g = AXRANDOM_0_1();
|
|
|
|
float b = AXRANDOM_0_1();
|
2019-03-15 14:11:50 +08:00
|
|
|
Vec3 color(r, g, b);
|
|
|
|
|
|
|
|
programState->setUniform(uniform, &color, sizeof(color));
|
|
|
|
}
|
|
|
|
|
|
|
|
Material_AutoBindings::Material_AutoBindings()
|
|
|
|
{
|
|
|
|
_resolver = new EffectAutoBindingResolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
Material_AutoBindings::~Material_AutoBindings()
|
|
|
|
{
|
|
|
|
delete _resolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Material_AutoBindings::onEnter()
|
|
|
|
{
|
|
|
|
MaterialSystemBaseTest::onEnter();
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// auto properties = Properties::createNonRefCounted("Materials/2d_effects.material#sample");
|
2019-03-15 14:11:50 +08:00
|
|
|
auto properties = Properties::createNonRefCounted("Materials/auto_binding_test.material#sample");
|
|
|
|
|
|
|
|
// Print the properties of every namespace within this one.
|
|
|
|
printProperties(properties, 0);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Material* mat1 = Material::createWithProperties(properties);
|
2019-03-15 14:11:50 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto meshBlur = Sprite::create("Images/grossini.png");
|
|
|
|
meshBlur->setPositionNormalized(Vec2(0.2f, 0.5f));
|
|
|
|
this->addChild(meshBlur);
|
|
|
|
meshBlur->setProgramState(mat1->getTechniqueByName("blur")->getPassByIndex(0)->getProgramState());
|
2019-03-15 14:11:50 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto meshOutline = Sprite::create("Images/grossini.png");
|
|
|
|
meshOutline->setPositionNormalized(Vec2(0.4f, 0.5f));
|
|
|
|
this->addChild(meshOutline);
|
|
|
|
meshOutline->setProgramState(mat1->getTechniqueByName("outline")->getPassByIndex(0)->getProgramState());
|
2019-03-15 14:11:50 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto meshNoise = Sprite::create("Images/grossini.png");
|
|
|
|
meshNoise->setPositionNormalized(Vec2(0.6f, 0.5f));
|
|
|
|
this->addChild(meshNoise);
|
|
|
|
meshNoise->setProgramState(mat1->getTechniqueByName("noise")->getPassByIndex(0)->getProgramState());
|
2019-03-15 14:11:50 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto meshEdgeDetect = Sprite::create("Images/grossini.png");
|
|
|
|
meshEdgeDetect->setPositionNormalized(Vec2(0.8f, 0.5f));
|
|
|
|
this->addChild(meshEdgeDetect);
|
|
|
|
meshEdgeDetect->setProgramState(mat1->getTechniqueByName("edge_detect")->getPassByIndex(0)->getProgramState());
|
2019-03-15 14:11:50 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
_noiseProgramState = meshNoise->getProgramState();
|
2021-12-28 16:06:23 +08:00
|
|
|
_locationTime = _noiseProgramState->getUniformLocation("u_Time");
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
schedule(AX_SCHEDULE_SELECTOR(Material_AutoBindings::updateUniformTime));
|
2019-03-15 14:11:50 +08:00
|
|
|
// properties is not a "Ref" object
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(properties);
|
2019-03-15 14:11:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Material_AutoBindings::subtitle() const
|
|
|
|
{
|
|
|
|
return "Testing auto-bindings uniforms";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Material_AutoBindings::updateUniformTime(float dt)
|
|
|
|
{
|
|
|
|
float time = Director::getInstance()->getTotalFrames() * Director::getInstance()->getAnimationInterval();
|
|
|
|
Vec4 random(time / 10.0f, time, time * 2.0f, time * 4.0f);
|
|
|
|
_noiseProgramState->setUniform(_locationTime, &random, sizeof(random));
|
|
|
|
}
|
2015-06-03 07:56:59 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2015-05-14 08:39:39 +08:00
|
|
|
void Material_setTechnique::onEnter()
|
2015-05-12 12:31:33 +08:00
|
|
|
{
|
|
|
|
MaterialSystemBaseTest::onEnter();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
|
|
|
mesh->setScale(6);
|
|
|
|
this->addChild(mesh);
|
|
|
|
mesh->setPositionNormalized(Vec2(0.5f, 0.5f));
|
|
|
|
_mesh = mesh;
|
2015-05-12 12:31:33 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Material* mat = Material::createWithFilename("Materials/3d_effects.material");
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setMaterial(mat);
|
2015-05-12 12:31:33 +08:00
|
|
|
|
|
|
|
// lights
|
|
|
|
auto light1 = AmbientLight::create(Color3B::RED);
|
|
|
|
addChild(light1);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto light2 = DirectionLight::create(Vec3(-1, 1, 0), Color3B::GREEN);
|
2015-05-12 12:31:33 +08:00
|
|
|
addChild(light2);
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
this->schedule(AX_CALLBACK_1(Material_setTechnique::changeMaterial, this), 1, "cookie");
|
2015-05-12 12:31:33 +08:00
|
|
|
_techniqueState = 0;
|
2015-05-14 08:39:39 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto rot = RotateBy::create(5, Vec3(30.0f, 60.0f, 270.0f));
|
2015-05-14 08:39:39 +08:00
|
|
|
auto repeat = RepeatForever::create(rot);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(repeat);
|
2015-05-12 12:31:33 +08:00
|
|
|
}
|
|
|
|
|
2015-05-14 08:39:39 +08:00
|
|
|
std::string Material_setTechnique::subtitle() const
|
2015-05-12 12:31:33 +08:00
|
|
|
{
|
2015-05-14 08:39:39 +08:00
|
|
|
return "Testing setTechnique()";
|
2015-05-12 12:31:33 +08:00
|
|
|
}
|
|
|
|
|
2015-05-14 08:39:39 +08:00
|
|
|
void Material_setTechnique::changeMaterial(float dt)
|
2015-05-12 12:31:33 +08:00
|
|
|
{
|
|
|
|
// get it from Mesh 0
|
|
|
|
switch (_techniqueState)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
case 0:
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->getMaterial(0)->setTechnique("lit");
|
2021-12-28 16:06:23 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->getMaterial(0)->setTechnique("normal");
|
2021-12-28 16:06:23 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2022-07-05 14:48:46 +08:00
|
|
|
_mesh->getMaterial(0)->setTechnique("outline");
|
2021-12-28 16:06:23 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2015-05-12 12:31:33 +08:00
|
|
|
}
|
|
|
|
|
2019-03-14 13:43:40 +08:00
|
|
|
_techniqueState = (_techniqueState + 1) % 3;
|
2015-05-12 12:31:33 +08:00
|
|
|
}
|
|
|
|
|
2015-05-14 08:39:39 +08:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
void Material_clone::onEnter()
|
|
|
|
{
|
|
|
|
MaterialSystemBaseTest::onEnter();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
|
|
|
mesh->setScale(3);
|
|
|
|
this->addChild(mesh);
|
|
|
|
mesh->setPositionNormalized(Vec2(0.25f, 0.5f));
|
2015-05-14 08:39:39 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Material* mat = Material::createWithFilename("Materials/3d_effects.material");
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setMaterial(mat);
|
2015-05-14 08:39:39 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto rot = RotateBy::create(5, Vec3(360.0f, 240.0f, 120.0f));
|
2015-05-14 08:39:39 +08:00
|
|
|
auto repeat = RepeatForever::create(rot);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(repeat);
|
|
|
|
|
|
|
|
// mesh 2... using same material
|
|
|
|
auto mesh2 = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
|
|
|
mesh2->setScale(3);
|
|
|
|
this->addChild(mesh2);
|
|
|
|
mesh2->setPositionNormalized(Vec2(0.5f, 0.5f));
|
|
|
|
mesh2->setMaterial(mat);
|
|
|
|
mesh2->runAction(repeat->clone());
|
|
|
|
|
|
|
|
// mesh 3... using cloned material
|
|
|
|
auto mesh3 = MeshRenderer::create("MeshRendererTest/boss1.obj");
|
|
|
|
mesh3->setScale(3);
|
|
|
|
this->addChild(mesh3);
|
|
|
|
mesh3->setPositionNormalized(Vec2(0.75f, 0.5f));
|
2015-05-14 08:39:39 +08:00
|
|
|
auto mat2 = mat->clone();
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh3->setMaterial(mat2);
|
|
|
|
mesh3->runAction(repeat->clone());
|
2015-05-14 08:39:39 +08:00
|
|
|
|
|
|
|
// testing clone
|
2022-07-05 14:48:46 +08:00
|
|
|
// should affect both mesh 1 and mesh 2
|
2015-05-14 08:39:39 +08:00
|
|
|
mat->setTechnique("outline");
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
// should affect only mesh 3
|
2021-12-28 16:06:23 +08:00
|
|
|
// mat2->setTechnique("normal");
|
2015-05-14 08:39:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Material_clone::subtitle() const
|
|
|
|
{
|
|
|
|
return "Testing material->clone()";
|
|
|
|
}
|
|
|
|
|
2015-05-15 08:24:27 +08:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2015-08-10 16:12:15 +08:00
|
|
|
const int SHOW_LEBAL_TAG = 114;
|
|
|
|
|
2015-05-15 08:24:27 +08:00
|
|
|
void Material_parsePerformance::onEnter()
|
|
|
|
{
|
|
|
|
MaterialSystemBaseTest::onEnter();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-08-10 16:41:16 +08:00
|
|
|
_maxParsingCoumt = 5e3;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-08-10 15:51:26 +08:00
|
|
|
auto screenSize = Director::getInstance()->getWinSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-08-10 15:51:26 +08:00
|
|
|
ui::Slider* slider = ui::Slider::create();
|
|
|
|
slider->loadBarTexture("cocosui/sliderTrack.png");
|
|
|
|
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
|
|
|
|
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
|
|
|
|
slider->setPercent(50);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-08-10 15:51:26 +08:00
|
|
|
slider->setPosition(Vec2(screenSize.width / 2.0f, screenSize.height / 3.0f));
|
|
|
|
slider->addEventListener([&](Ref* sender, ui::Slider::EventType type) {
|
2015-08-10 16:12:15 +08:00
|
|
|
if (type == ui::Slider::EventType::ON_SLIDEBALL_UP)
|
2015-08-10 15:51:26 +08:00
|
|
|
{
|
|
|
|
ui::Slider* slider = dynamic_cast<ui::Slider*>(sender);
|
2021-12-28 16:06:23 +08:00
|
|
|
float p = slider->getPercent() / 100.0f;
|
2015-08-10 16:12:15 +08:00
|
|
|
slider->setTouchEnabled(false);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Will parsing material %d times", (int)(p * _maxParsingCoumt));
|
2015-08-10 16:12:15 +08:00
|
|
|
Label* label = dynamic_cast<Label*>(this->getChildByTag(SHOW_LEBAL_TAG));
|
2021-12-28 16:06:23 +08:00
|
|
|
if (label)
|
2015-08-10 16:12:15 +08:00
|
|
|
{
|
|
|
|
label->setString("Testing start!");
|
|
|
|
}
|
2015-08-10 15:51:26 +08:00
|
|
|
this->scheduleOnce(
|
2021-12-28 16:06:23 +08:00
|
|
|
[this, p, slider](float) {
|
|
|
|
this->parsingTesting(p * _maxParsingCoumt);
|
|
|
|
slider->setTouchEnabled(true);
|
|
|
|
},
|
|
|
|
1.0, "schedule test parsing");
|
2015-08-10 15:51:26 +08:00
|
|
|
}
|
|
|
|
});
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-08-10 15:51:26 +08:00
|
|
|
addChild(slider);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
auto label = Label::createWithSystemFont(
|
|
|
|
"Max parsing count is 10000, which may crash because of high memory consumption.", "Helvetica", 10);
|
2015-08-10 16:12:15 +08:00
|
|
|
label->setPosition(Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f - 20));
|
|
|
|
addChild(label);
|
|
|
|
label = Label::createWithSystemFont("Slide to test parsing performance", "Helvetica", 10);
|
|
|
|
label->setPosition(Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f));
|
|
|
|
addChild(label);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-08-10 16:12:15 +08:00
|
|
|
label = Label::createWithSystemFont("", "Helvetica", 10);
|
|
|
|
label->setPosition(Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f + 20));
|
|
|
|
label->setTag(SHOW_LEBAL_TAG);
|
|
|
|
addChild(label);
|
2015-08-10 15:51:26 +08:00
|
|
|
}
|
2015-05-15 08:24:27 +08:00
|
|
|
|
2015-08-10 15:51:26 +08:00
|
|
|
void Material_parsePerformance::parsingTesting(unsigned int count)
|
|
|
|
{
|
|
|
|
std::clock_t begin = std::clock();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2016-04-22 11:20:05 +08:00
|
|
|
for (unsigned int i = 0; i < count; i++)
|
2015-05-15 08:24:27 +08:00
|
|
|
{
|
|
|
|
Material::createWithFilename("Materials/2d_effects.material");
|
|
|
|
Material::createWithFilename("Materials/3d_effects.material");
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
std::clock_t end = std::clock();
|
2015-05-15 08:24:27 +08:00
|
|
|
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
|
2021-12-28 16:06:23 +08:00
|
|
|
Label* label = dynamic_cast<Label*>(this->getChildByTag(SHOW_LEBAL_TAG));
|
|
|
|
if (label)
|
2015-08-10 16:12:15 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
std::string str = StringUtils::format("Testing completed! Took: %.3f seconds for parsing material %d times.",
|
|
|
|
elapsed_secs, count);
|
2015-08-10 16:12:15 +08:00
|
|
|
label->setString(str);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Took: %.3f seconds for parsing material %d times.", elapsed_secs, count);
|
2015-08-10 16:12:15 +08:00
|
|
|
}
|
2015-05-15 08:24:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Material_parsePerformance::subtitle() const
|
|
|
|
{
|
|
|
|
return "Testing parsing performance";
|
|
|
|
}
|
2015-06-16 05:12:44 +08:00
|
|
|
|
2015-05-14 08:39:39 +08:00
|
|
|
// MARK: Helper functions
|
|
|
|
|
|
|
|
static void printProperties(Properties* properties, int indent)
|
|
|
|
{
|
|
|
|
// Print the name and ID of the current namespace.
|
|
|
|
const char* spacename = properties->getNamespace();
|
2022-10-13 21:44:42 +08:00
|
|
|
const char* id = properties->getId().data();
|
2015-05-14 08:39:39 +08:00
|
|
|
char chindent[64];
|
2021-12-28 16:06:23 +08:00
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < indent * 2; i++)
|
2015-05-14 08:39:39 +08:00
|
|
|
chindent[i] = ' ';
|
|
|
|
chindent[i] = '\0';
|
|
|
|
|
2015-05-15 08:24:27 +08:00
|
|
|
log("%sNamespace: %s ID: %s\n%s{", chindent, spacename, id, chindent);
|
2015-05-14 08:39:39 +08:00
|
|
|
|
|
|
|
// Print all properties in this namespace.
|
2021-12-28 16:06:23 +08:00
|
|
|
const char* name = properties->getNextProperty();
|
2015-05-14 08:39:39 +08:00
|
|
|
const char* value = NULL;
|
|
|
|
while (name != NULL)
|
|
|
|
{
|
|
|
|
value = properties->getString(name);
|
2015-05-15 08:24:27 +08:00
|
|
|
log("%s%s = %s", chindent, name, value);
|
2015-05-14 08:39:39 +08:00
|
|
|
name = properties->getNextProperty();
|
|
|
|
}
|
|
|
|
|
|
|
|
Properties* space = properties->getNextNamespace();
|
|
|
|
while (space != NULL)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
printProperties(space, indent + 1);
|
2015-05-14 08:39:39 +08:00
|
|
|
space = properties->getNextNamespace();
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
log("%s}\n", chindent);
|
2015-05-14 08:39:39 +08:00
|
|
|
}
|