Refator sampler name in shader

This commit is contained in:
halx99 2022-07-04 21:26:48 +08:00
parent 782e29881f
commit 0b4363bfc0
64 changed files with 510 additions and 481 deletions

View File

@ -481,7 +481,7 @@ void FastTMXLayer::updatePrimitives()
}
vertexLayout->setLayout(sizeof(V3F_C4B_T2F));
_mvpMatrixLocaiton = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_texture");
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_tex0");
pipelineDescriptor.programState->setTexture(_textureLocation, 0, _texture->getBackendTexture());
command->init(_globalZOrder, blendfunc);

View File

@ -109,7 +109,7 @@ bool GridBase::initWithSize(const Vec2& gridSize, Texture2D* texture, bool flipp
_programState = new backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
_mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_texture");
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_tex0");
#define VERTEX_POSITION_SIZE 3
#define VERTEX_TEXCOORD_SIZE 2

View File

@ -232,7 +232,7 @@ bool MotionStreak::setProgramState(backend::ProgramState* programState, bool nee
pipelineDescriptor.programState = _programState;
_mvpMatrixLocaiton = _programState->getUniformLocation("u_MVPMatrix");
_textureLocation = _programState->getUniformLocation("u_texture");
_textureLocation = _programState->getUniformLocation("u_tex0");
auto vertexLayout = _programState->getVertexLayout();
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();

View File

@ -56,7 +56,7 @@ ParticleBatchNode::ParticleBatchNode()
pipelinePS = new backend::ProgramState(program);
_mvpMatrixLocaiton = pipelinePS->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelinePS->getUniformLocation("u_texture");
_textureLocation = pipelinePS->getUniformLocation("u_tex0");
auto layout = pipelinePS->getVertexLayout();
const auto& attributeInfo = pipelinePS->getProgram()->getActiveAttributes();

View File

@ -55,7 +55,7 @@ ParticleSystemQuad::ParticleSystemQuad()
pipelinePS = (new backend::ProgramState(program));
_mvpMatrixLocaiton = pipelinePS->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelinePS->getUniformLocation("u_texture");
_textureLocation = pipelinePS->getUniformLocation("u_tex0");
auto vertexLayout = pipelinePS->getVertexLayout();
const auto& attributeInfo = pipelinePS->getProgram()->getActiveAttributes();
@ -660,7 +660,7 @@ void ParticleSystemQuad::updateParticleQuads()
// And wether if every property's memory of the particle system is continuous,
// for the purpose of improving cache hit rate, we should process only one property in one for-loop.
// It was proved to be effective especially for low-end devices.
if (_isLifeAnimated || _isEmitterAnimated || _isLoopAnimated || _isAnimAllocated)
if ((_isLifeAnimated || _isEmitterAnimated || _isLoopAnimated) && _isAnimAllocated)
{
V3F_C4B_T2F_Quad* quad = startQuad;
unsigned short* cellIndex = _particleData.animCellIndex;

View File

@ -90,7 +90,7 @@ backend::ProgramState* initPipelineDescriptor(cocos2d::CustomCommand& command,
}
locMVP = programState->getUniformLocation("u_MVPMatrix");
locTexture = programState->getUniformLocation("u_texture");
locTexture = programState->getUniformLocation("u_tex0");
return programState;
}

View File

@ -118,7 +118,7 @@ void SpriteBatchNode::setUniformLocation()
{
CCASSERT(_programState, "programState should not be nullptr");
_mvpMatrixLocaiton = _programState->getUniformLocation("u_MVPMatrix");
_textureLocation = _programState->getUniformLocation("u_texture");
_textureLocation = _programState->getUniformLocation("u_tex0");
}
void SpriteBatchNode::setVertexLayout()

View File

@ -298,7 +298,7 @@ bool Bundle3D::loadObj(MeshDatas& meshdatas,
}
// split into submesh according to material
std::map<int, IndexArray> subMeshMap;
std::map<int, std::vector<unsigned short>> subMeshMap;
for (size_t k = 0, size = mesh.material_ids.size(); k < size; ++k)
{
int id = mesh.material_ids[k];
@ -447,7 +447,7 @@ bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
for (unsigned int k = 0; k < meshPartCount; ++k)
{
IndexArray indexArray{CustomCommand::IndexFormat::U_SHORT}; // TODO: _version == 1.3 use U_INT?
std::vector<unsigned short> indexArray;
std::string meshPartid = _binaryReader.readString();
meshData->subMeshIds.push_back(meshPartid);
unsigned int nIndexCount;
@ -457,7 +457,7 @@ bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
goto FAILED;
}
indexArray.resize(nIndexCount);
if (_binaryReader.read(indexArray.data(), 2, nIndexCount) != nIndexCount)
if (_binaryReader.read(&indexArray[0], 2, nIndexCount) != nIndexCount)
{
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
goto FAILED;
@ -599,9 +599,9 @@ bool Bundle3D::loadMeshDatasBinary_0_1(MeshDatas& meshdatas)
return false;
}
IndexArray indices;
std::vector<unsigned short> indices;
indices.resize(nIndexCount);
if (_binaryReader.read(indices.data(), 2, nIndexCount) != nIndexCount)
if (_binaryReader.read(&indices[0], 2, nIndexCount) != nIndexCount)
{
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
CC_SAFE_DELETE(meshdata);
@ -720,9 +720,9 @@ bool Bundle3D::loadMeshDatasBinary_0_2(MeshDatas& meshdatas)
return false;
}
IndexArray indices{CustomCommand::IndexFormat::U_SHORT}; /* TODO: _version == 1.3 use U_INT?*/
std::vector<unsigned short> indices;
indices.resize(nIndexCount);
if (_binaryReader.read(indices.data(), 2, nIndexCount) != nIndexCount)
if (_binaryReader.read(&indices[0], 2, nIndexCount) != nIndexCount)
{
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
CC_SAFE_DELETE(meshdata);
@ -777,7 +777,7 @@ bool Bundle3D::loadMeshDatasJson(MeshDatas& meshdatas)
const rapidjson::Value& mesh_part_array = mesh_data[PARTS];
for (rapidjson::SizeType i = 0, mesh_part_array_size = mesh_part_array.Size(); i < mesh_part_array_size; ++i)
{
IndexArray indexArray;
std::vector<unsigned short> indexArray;
const rapidjson::Value& mesh_part = mesh_part_array[i];
meshData->subMeshIds.push_back(mesh_part[ID].GetString());
// index_number
@ -810,8 +810,8 @@ bool Bundle3D::loadMeshDatasJson(MeshDatas& meshdatas)
}
else
{
meshData->subMeshAABB.push_back(calculateAABB(meshData->vertex, meshData->getPerVertexSize(),
indexArray));
meshData->subMeshAABB.push_back(
calculateAABB(meshData->vertex, meshData->getPerVertexSize(), indexArray));
}
}
meshdatas.meshDatas.push_back(meshData);
@ -1185,16 +1185,15 @@ bool Bundle3D::loadMeshDataJson_0_1(MeshDatas& meshdatas)
unsigned int indexnum = mesh_data_body_array_0[INDEXNUM].GetUint();
// indices
IndexArray indices;
std::vector<unsigned short> indices;
indices.resize(indexnum);
const rapidjson::Value& indices_val_array = mesh_data_body_array_0[INDICES];
for (rapidjson::SizeType i = 0; i < indices_val_array.Size(); ++i)
indices.at<uint16_t>(i) = (unsigned short)indices_val_array[i].GetUint();
indices[i] = (unsigned short)indices_val_array[i].GetUint();
meshdata->subMeshIndices.push_back(indices);
meshdata->subMeshAABB.push_back(
calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
meshdatas.meshDatas.push_back(meshdata);
return true;
}
@ -1240,12 +1239,12 @@ bool Bundle3D::loadMeshDataJson_0_2(MeshDatas& meshdatas)
unsigned int indexnum = mesh_submesh_val[INDEXNUM].GetUint();
// indices
IndexArray indices;
std::vector<unsigned short> indices;
indices.resize(indexnum);
const rapidjson::Value& indices_val_array = mesh_submesh_val[INDICES];
for (rapidjson::SizeType j = 0; j < indices_val_array.Size(); ++j)
indices.at<uint16_t>(j) = (unsigned short)indices_val_array[j].GetUint();
indices[j] = (unsigned short)indices_val_array[j].GetUint();
meshdata->subMeshIndices.push_back(indices);
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
@ -2325,16 +2324,15 @@ Bundle3D::~Bundle3D()
cocos2d::AABB Bundle3D::calculateAABB(const std::vector<float>& vertex,
int stride,
const IndexArray& indices)
const std::vector<unsigned short>& index)
{
AABB aabb;
stride /= 4;
indices.for_each ([&](uint32_t i) {
Vec3 point(vertex[i * stride], vertex[i * stride + 1], vertex[i * stride + 2]);
aabb.updateMinMax(&point, 1);
});
for (const auto& it : index)
{
Vec3 point(vertex[it * stride], vertex[it * stride + 1], vertex[it * stride + 2]);
aabb.updateMinMax(&point, 1);
}
return aabb;
}

View File

@ -113,8 +113,7 @@ public:
const char* mtl_basepath = nullptr);
// calculate aabb
static AABB calculateAABB(const std::vector<float>& vertex,
int stride, const IndexArray& indices);
static AABB calculateAABB(const std::vector<float>& vertex, int stride, const std::vector<unsigned short>& index);
Bundle3D();
virtual ~Bundle3D();

View File

@ -39,140 +39,8 @@
#include "3d/CC3DProgramInfo.h"
#include "yasio/detail/byte_buffer.hpp"
NS_CC_BEGIN
class IndexArray
{
public:
IndexArray() { format(backend::IndexFormat::U_SHORT); }
IndexArray(backend::IndexFormat indexFormat) { format(indexFormat); }
IndexArray(std::initializer_list<uint16_t> rhs) : _buffer(rhs)
{
format(backend::IndexFormat::U_SHORT);
}
IndexArray(std::initializer_list<uint32_t> rhs, std::true_type /*U_INT*/) : _buffer(rhs)
{
format(backend::IndexFormat::U_INT);
}
IndexArray(const IndexArray& rhs) : _stride(rhs._stride), _buffer(rhs._buffer) {}
IndexArray(IndexArray&& rhs) noexcept : _stride(rhs._stride), _buffer(std::move(rhs._buffer)) {}
IndexArray& operator=(const IndexArray& rhs) {
_stride = rhs._stride;
_buffer = rhs._buffer;
return *this;
}
IndexArray& operator=(IndexArray&& rhs) noexcept
{
this->swap(rhs);
return *this;
}
void swap(IndexArray& rhs) {
std::swap(_stride, rhs._stride);
_buffer.swap(rhs._buffer);
}
void clear(backend::IndexFormat format = backend::IndexFormat::UNSPEC)
{
_buffer.clear();
if (format != backend::IndexFormat::UNSPEC)
_stride = format == backend::IndexFormat::U_SHORT ? 2 : 4;
}
/** Pushes back a value if type unsigned short (uint16_t). */
void push_back(uint16_t val)
{
assert(_stride == 2);
_buffer.append_n((uint8_t*)&val, sizeof(val));
}
/** Pushes back a value if type unsigned int (uint32_t). */
void push_back(uint32_t val, std::true_type /*U_INT*/)
{
assert(_stride == 4);
_buffer.append_n((uint8_t*)&val, sizeof(val));
}
/** Inserts a list containing unsigned short (uint16_t) data. */
void insert(uint8_t* where, std::initializer_list<uint16_t> ilist)
{
assert(_stride == 2);
_buffer.insert(where, (uint8_t*)ilist.begin(), (uint8_t*)ilist.end());
}
/** Inserts a list containing unsigned int (uint32_t) data. */
void insert(uint8_t* where, std::initializer_list<uint32_t> ilist, std::true_type)
{
assert(_stride == 4);
_buffer.insert(where, (uint8_t*)ilist.begin(), (uint8_t*)ilist.end());
}
template <typename _Ty>
_Ty& at(size_t idx)
{
assert((sizeof(_Ty) == sizeof(uint16_t) && _stride == 2) ||
(sizeof(_Ty) == sizeof(uint32_t) && _stride == 4));
if (idx < this->size())
return (_Ty&)_buffer[idx * sizeof(_Ty)];
throw std::out_of_range("IndexArray: out of range!");
}
uint8_t* begin() noexcept { return _buffer.begin(); }
uint8_t* end() noexcept { return _buffer.end(); }
const uint8_t* begin() const noexcept { return _buffer.begin(); }
const uint8_t* end() const noexcept { return _buffer.end(); }
uint8_t* data() noexcept { return _buffer.data(); }
const uint8_t* data() const noexcept { return _buffer.data(); }
/** returns the count of indices in the container. */
size_t size() const { return _buffer.size() / _stride; }
/** returns the size of the container in bytes. */
size_t bsize() const { return _buffer.size(); }
/** resizes the count of indices in the container. */
void resize(size_t size) { _buffer.resize(size * _stride); }
/** resizes the container in bytes. */
void bresize(size_t size) { _buffer.resize(size); }
/** returns true if the container is empty. Otherwise, false. */
bool empty() const { return _buffer.empty(); }
/** returns the format of the index array. */
backend::IndexFormat format() const
{
return _stride == 2 ? backend::IndexFormat::U_SHORT : backend::IndexFormat::U_INT;
}
/** sets the format of the index array. */
void format(backend::IndexFormat format)
{
_stride = format == backend::IndexFormat::U_SHORT ? 2 : 4;
}
template <typename _Fty>
void for_each(_Fty cb) const
{
assert(_stride == 2 || _stride == 4);
for (auto it = _buffer.begin(); it != _buffer.end(); it += _stride)
{
uint32_t val = 0;
memcpy(&val, it, _stride);
cb(val);
}
}
protected:
uint8_t _stride;
yasio::byte_buffer _buffer;
};
/**mesh vertex attribute
* @js NA
* @lua NA
@ -260,14 +128,13 @@ struct NodeDatas
}
};
/**mesh data
* @js NA
* @lua NA
*/
struct MeshData
{
using IndexArray = ::cocos2d::IndexArray;
typedef std::vector<unsigned short> IndexArray;
std::vector<float> vertex;
int vertexSizeInFloat;
std::vector<IndexArray> subMeshIndices;
@ -280,7 +147,7 @@ struct MeshData
public:
/**
* Get per vertex size
* @return return the sum size of all vertex attributes.
* @return return the sum of each vertex's all attribute size.
*/
int getPerVertexSize() const
{

View File

@ -113,7 +113,6 @@ Mesh::Mesh()
, _visible(true)
, _isTransparent(false)
, _force2DQueue(false)
, meshIndexFormat(CustomCommand::IndexFormat::U_SHORT)
, _meshIndexData(nullptr)
, _blend(BlendFunc::ALPHA_NON_PREMULTIPLIED)
, _blendDirty(true)
@ -223,13 +222,10 @@ Mesh* Mesh::create(const std::vector<float>& vertices,
meshdata.vertex = vertices;
meshdata.subMeshIndices.push_back(indices);
meshdata.subMeshIds.push_back("");
auto meshvertexdata = MeshVertexData::create(meshdata, indices.format());
auto meshvertexdata = MeshVertexData::create(meshdata);
auto indexData = meshvertexdata->getMeshIndexDataByIndex(0);
auto mesh = create("", indexData);
mesh->setIndexFormat(indices.format());
return mesh;
return create("", indexData);
}
Mesh* Mesh::create(std::string_view name, MeshIndexData* indexData, MeshSkin* skin)
@ -732,18 +728,12 @@ CustomCommand::PrimitiveType Mesh::getPrimitiveType() const
ssize_t Mesh::getIndexCount() const
{
return _meshIndexData->getIndexBuffer()->getSize() /
(meshIndexFormat == CustomCommand::IndexFormat::U_SHORT ? sizeof(uint16_t) : sizeof(uint32_t));
return _meshIndexData->getIndexBuffer()->getSize() / sizeof(uint16_t);
}
CustomCommand::IndexFormat Mesh::getIndexFormat() const
{
return meshIndexFormat;
}
void Mesh::setIndexFormat(CustomCommand::IndexFormat indexFormat)
{
meshIndexFormat = indexFormat;
return CustomCommand::IndexFormat::U_SHORT;
}
backend::Buffer* Mesh::getIndexBuffer() const

View File

@ -64,7 +64,7 @@ class CC_DLL Mesh : public Ref
friend class Sprite3D;
public:
typedef std::vector<unsigned short> IndexArray;
/**create mesh from positions, normals, and so on, single SubMesh*/
static Mesh* create(const std::vector<float>& positions,
const std::vector<float>& normals,
@ -191,12 +191,6 @@ public:
* @lua NA
*/
CustomCommand::IndexFormat getIndexFormat() const;
/**
* set index format
*
* @lua NA
*/
void setIndexFormat(CustomCommand::IndexFormat indexFormat);
/**
* get index buffer
*
@ -259,7 +253,6 @@ protected:
bool _visible; // is the submesh visible
bool _isTransparent; // is this mesh transparent, it is a property of material in fact
bool _force2DQueue; // add this mesh to 2D render queue
CustomCommand::IndexFormat meshIndexFormat;
std::string _name;
MeshIndexData* _meshIndexData;

View File

@ -75,7 +75,7 @@ MeshIndexData::MeshIndexData()
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) {
_indexBuffer->updateData((void*)_indexData.data(), _indexData.bsize());
_indexBuffer->updateData((void*)_indexData.data(), _indexData.size() * sizeof(_indexData[0]));
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
#endif
@ -108,7 +108,7 @@ void MeshVertexData::setVertexData(const std::vector<float>& vertexData)
#endif
}
MeshVertexData* MeshVertexData::create(const MeshData& meshdata, CustomCommand::IndexFormat format)
MeshVertexData* MeshVertexData::create(const MeshData& meshdata)
{
auto vertexdata = new MeshVertexData();
vertexdata->_vertexBuffer = backend::Device::getInstance()->newBuffer(
@ -132,29 +132,28 @@ MeshVertexData* MeshVertexData::create(const MeshData& meshdata, CustomCommand::
bool needCalcAABB = (meshdata.subMeshAABB.size() != meshdata.subMeshIndices.size());
for (size_t i = 0, size = meshdata.subMeshIndices.size(); i < size; ++i)
{
auto& indices = meshdata.subMeshIndices[i];
// auto indexSize = format == CustomCommand::IndexFormat::U_SHORT ? sizeof(uint16_t) : sizeof(uint32_t);
auto& index = meshdata.subMeshIndices[i];
auto indexBuffer = backend::Device::getInstance()->newBuffer(
indices.bsize() /* * indexSize*/, backend::BufferType::INDEX, backend::BufferUsage::STATIC);
index.size() * sizeof(index[0]), backend::BufferType::INDEX, backend::BufferUsage::STATIC);
indexBuffer->autorelease();
#if CC_ENABLE_CACHE_TEXTURE_DATA
indexBuffer->usingDefaultStoredData(false);
#endif
indexBuffer->updateData((void*)indices.data(), indices.bsize() /* * indexSize*/);
indexBuffer->updateData((void*)index.data(), index.size() * sizeof(index[0]));
std::string id = (i < meshdata.subMeshIds.size() ? meshdata.subMeshIds[i] : "");
MeshIndexData* indexdata = nullptr;
if (needCalcAABB)
{
auto aabb = Bundle3D::calculateAABB(meshdata.vertex, meshdata.getPerVertexSize(), indices);
auto aabb = Bundle3D::calculateAABB(meshdata.vertex, meshdata.getPerVertexSize(), index);
indexdata = MeshIndexData::create(id, vertexdata, indexBuffer, aabb);
}
else
indexdata = MeshIndexData::create(id, vertexdata, indexBuffer, meshdata.subMeshAABB[i]);
#if CC_ENABLE_CACHE_TEXTURE_DATA
indexdata->setIndexData(indices);
indexdata->setIndexData(index);
#endif
vertexdata->_indices.pushBack(indexdata);
vertexdata->_indexs.pushBack(indexdata);
}
vertexdata->autorelease();
@ -163,7 +162,7 @@ MeshVertexData* MeshVertexData::create(const MeshData& meshdata, CustomCommand::
MeshIndexData* MeshVertexData::getMeshIndexDataById(std::string_view id) const
{
for (auto it : _indices)
for (auto it : _indexs)
{
if (it->getId() == id)
return it;
@ -194,7 +193,7 @@ MeshVertexData::MeshVertexData()
MeshVertexData::~MeshVertexData()
{
CC_SAFE_RELEASE(_vertexBuffer);
_indices.clear();
_indexs.clear();
_vertexData.clear();
#if CC_ENABLE_CACHE_TEXTURE_DATA
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);

View File

@ -112,7 +112,7 @@ class CC_DLL MeshVertexData : public Ref
public:
/**create*/
static MeshVertexData* create(const MeshData& meshdata, CustomCommand::IndexFormat format = CustomCommand::IndexFormat::U_SHORT);
static MeshVertexData* create(const MeshData& meshdata);
/** get vertexbuffer */
backend::Buffer* getVertexBuffer() const { return _vertexBuffer; }
@ -124,9 +124,9 @@ public:
const MeshVertexAttrib& getMeshVertexAttrib(ssize_t index) const { return _attribs[index]; }
/** get index data count */
ssize_t getMeshIndexDataCount() const { return _indices.size(); }
ssize_t getMeshIndexDataCount() const { return _indexs.size(); }
/** get index data by index */
MeshIndexData* getMeshIndexDataByIndex(int index) const { return _indices.at(index); }
MeshIndexData* getMeshIndexDataByIndex(int index) const { return _indexs.at(index); }
/** get index data by id */
MeshIndexData* getMeshIndexDataById(std::string_view id) const;
@ -144,8 +144,8 @@ public:
protected:
backend::Buffer* _vertexBuffer = nullptr; // vertex buffer
ssize_t _sizePerVertex = -1;
Vector<MeshIndexData*> _indices; // index data
std::vector<MeshVertexAttrib> _attribs; // vertex attributes
Vector<MeshIndexData*> _indexs; // index data
std::vector<MeshVertexAttrib> _attribs; // vertex attributes
int _vertexCount = 0; // vertex count
std::vector<float> _vertexData;

View File

@ -170,7 +170,7 @@ void MotionStreak3D::initCustomCommand()
blend.destinationAlphaBlendFactor = blend.destinationRGBBlendFactor = _blendFunc.dst;
_locMVP = _programState->getUniformLocation("u_MVPMatrix");
_locTexture = _programState->getUniformLocation("u_texture");
_locTexture = _programState->getUniformLocation("u_tex0");
_customCommand.createVertexBuffer(sizeof(VertexData), _vertexData.size(), CustomCommand::BufferUsage::DYNAMIC);
}

View File

@ -112,6 +112,7 @@ void Sprite3D::createAsync(std::string_view modelPath,
sprite->_asyncLoadParam.afterLoadCallback = callback;
sprite->_asyncLoadParam.texPath = texturePath;
sprite->_asyncLoadParam.modelPath = modelPath;
sprite->_asyncLoadParam.modelFullPath = FileUtils::getInstance()->fullPathForFilename(modelPath);
sprite->_asyncLoadParam.callbackParam = callbackparam;
sprite->_asyncLoadParam.materialdatas = new MaterialDatas();
sprite->_asyncLoadParam.meshdatas = new MeshDatas();
@ -120,7 +121,7 @@ void Sprite3D::createAsync(std::string_view modelPath,
AsyncTaskPool::TaskType::TASK_IO, CC_CALLBACK_1(Sprite3D::afterAsyncLoad, sprite),
(void*)(&sprite->_asyncLoadParam), [sprite]() {
sprite->_asyncLoadParam.result =
sprite->loadFromFile(sprite->_asyncLoadParam.modelPath, sprite->_asyncLoadParam.nodeDatas,
sprite->loadFromFile(sprite->_asyncLoadParam.modelFullPath, sprite->_asyncLoadParam.nodeDatas,
sprite->_asyncLoadParam.meshdatas, sprite->_asyncLoadParam.materialdatas);
});
}

View File

@ -272,6 +272,7 @@ protected:
void* callbackParam;
bool result; // sprite load result
std::string modelPath;
std::string modelFullPath;
std::string texPath; //
MeshDatas* meshdatas;
MaterialDatas* materialdatas;

View File

@ -51,9 +51,6 @@ Sprite3DMaterial* Sprite3DMaterial::_vertexLitMaterialSkin = nullptr;
Sprite3DMaterial* Sprite3DMaterial::_diffuseMaterialSkin = nullptr;
Sprite3DMaterial* Sprite3DMaterial::_bumpedDiffuseMaterialSkin = nullptr;
Sprite3DMaterial* Sprite3DMaterial::_quadTextureMaterial = nullptr;
Sprite3DMaterial* Sprite3DMaterial::_quadColorMaterial = nullptr;
backend::ProgramState* Sprite3DMaterial::_unLitMaterialProgState = nullptr;
backend::ProgramState* Sprite3DMaterial::_unLitNoTexMaterialProgState = nullptr;
backend::ProgramState* Sprite3DMaterial::_vertexLitMaterialProgState = nullptr;
@ -66,9 +63,6 @@ backend::ProgramState* Sprite3DMaterial::_vertexLitMaterialSkinProgState = n
backend::ProgramState* Sprite3DMaterial::_diffuseMaterialSkinProgState = nullptr;
backend::ProgramState* Sprite3DMaterial::_bumpedDiffuseMaterialSkinProgState = nullptr;
backend::ProgramState* Sprite3DMaterial::_quadTextureMaterialProgState = nullptr;
backend::ProgramState* Sprite3DMaterial::_quadColorMaterialProgState = nullptr;
void Sprite3DMaterial::createBuiltInMaterial()
{
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::SKINPOSITION_TEXTURE_3D);
@ -135,22 +129,6 @@ void Sprite3DMaterial::createBuiltInMaterial()
{
_bumpedDiffuseMaterialSkin->_type = Sprite3DMaterial::MaterialType::BUMPED_DIFFUSE;
}
program = backend::Program::getBuiltinProgram(backend::ProgramType::QUAD_TEXTURE_2D);
_quadTextureMaterialProgState = new backend::ProgramState(program);
_quadTextureMaterial = new Sprite3DMaterial();
if (_quadTextureMaterial && _quadTextureMaterial->initWithProgramState(_quadTextureMaterialProgState))
{
_quadTextureMaterial->_type = Sprite3DMaterial::MaterialType::QUAD_TEXTURE;
}
program = backend::Program::getBuiltinProgram(backend::ProgramType::QUAD_COLOR_2D);
_quadColorMaterialProgState = new backend::ProgramState(program);
_quadColorMaterial = new Sprite3DMaterial();
if (_quadColorMaterial && _quadColorMaterial->initWithProgramState(_quadColorMaterialProgState))
{
_quadColorMaterial->_type = Sprite3DMaterial::MaterialType::QUAD_COLOR;
}
}
void Sprite3DMaterial::releaseBuiltInMaterial()
@ -236,7 +214,7 @@ Sprite3DMaterial* Sprite3DMaterial::createBuiltInMaterial(MaterialType type, boo
break;
case Sprite3DMaterial::MaterialType::VERTEX_LIT:
CCASSERT(0, "not implemented");
CCASSERT(0, "not implement");
break;
case Sprite3DMaterial::MaterialType::DIFFUSE:
@ -251,14 +229,6 @@ Sprite3DMaterial* Sprite3DMaterial::createBuiltInMaterial(MaterialType type, boo
material = skinned ? _bumpedDiffuseMaterialSkin : _bumpedDiffuseMaterial;
break;
case Sprite3DMaterial::MaterialType::QUAD_TEXTURE:
material = _quadTextureMaterial;
break;
case Sprite3DMaterial::MaterialType::QUAD_COLOR:
material = _quadColorMaterial;
break;
default:
break;
}

View File

@ -63,8 +63,6 @@ public:
DIFFUSE, // diffuse (pixel lighting)
DIFFUSE_NOTEX, // diffuse (without texture)
BUMPED_DIFFUSE, // bumped diffuse
QUAD_TEXTURE, // textured quad material
QUAD_COLOR, // colored quad material (without texture)
// Custom material
CUSTOM, // Create from material file
@ -136,9 +134,6 @@ protected:
static Sprite3DMaterial* _diffuseMaterialSkin;
static Sprite3DMaterial* _bumpedDiffuseMaterialSkin;
static Sprite3DMaterial* _quadTextureMaterial;
static Sprite3DMaterial* _quadColorMaterial;
static backend::ProgramState* _unLitMaterialProgState;
static backend::ProgramState* _unLitNoTexMaterialProgState;
static backend::ProgramState* _vertexLitMaterialProgState;
@ -150,9 +145,6 @@ protected:
static backend::ProgramState* _vertexLitMaterialSkinProgState;
static backend::ProgramState* _diffuseMaterialSkinProgState;
static backend::ProgramState* _bumpedDiffuseMaterialSkinProgState;
static backend::ProgramState* _quadTextureMaterialProgState;
static backend::ProgramState* _quadColorMaterialProgState;
};
/**

View File

@ -829,14 +829,14 @@ void Terrain::cacheUniformAttribLocation()
_lightMapCheckLocation = _programState->getUniformLocation("u_has_light_map");
if (!_alphaMap)
{
_detailMapLocation[0] = _programState->getUniformLocation("u_texture0");
_detailMapLocation[0] = _programState->getUniformLocation("u_tex0");
}
else
{
for (int i = 0; i < _maxDetailMapValue; ++i)
{
char str[20];
sprintf(str, "u_texture%d", i);
sprintf(str, "u_tex%d", i);
_detailMapLocation[i] = _programState->getUniformLocation(str);
}

View File

@ -1303,11 +1303,87 @@ void Director::createStatsLabel()
_drawnVerticesLabel->setIgnoreContentScaleFactor(true);
_drawnVerticesLabel->setScale(scaleFactor);
auto safeOrigin = getSafeAreaRect().origin;
const int height_spacing = (int)(22 / CC_CONTENT_SCALE_FACTOR());
_drawnVerticesLabel->setPosition(Vec2(0, height_spacing * 2.0f) + safeOrigin);
_drawnBatchesLabel->setPosition(Vec2(0, height_spacing * 1.0f) + safeOrigin);
_FPSLabel->setPosition(Vec2(0, height_spacing * 0.0f) + safeOrigin);
setFPSPos();
}
// Set the FPS position on disply like the numbers on numpad
void Director::setFPSPos(FPSPosition fpsPosition)
{
if (_displayStats && _FPSLabel && _drawnBatchesLabel && _drawnVerticesLabel)
{
static Vec2 _fpsPosition = {0, 0};
auto safeOrigin = getSafeAreaRect().origin;
auto safeSize = getSafeAreaRect().size;
const int height_spacing = (int)(22 / CC_CONTENT_SCALE_FACTOR());
switch (fpsPosition)
{
case FPSPosition::BOTTOM_LEFT:
_fpsPosition = Vec2(0, 0);
_drawnVerticesLabel->setAnchorPoint({0, 0});
_drawnBatchesLabel->setAnchorPoint({0, 0});
_FPSLabel->setAnchorPoint({0, 0});
break;
case FPSPosition::CENTER_LEFT:
_fpsPosition = Vec2(0, safeSize.height / 2 - height_spacing * 1.5);
_drawnVerticesLabel->setAnchorPoint({0, 0.0});
_drawnBatchesLabel->setAnchorPoint({0, 0.0});
_FPSLabel->setAnchorPoint({0, 0});
break;
case FPSPosition::TOP_LEFT:
_fpsPosition = Vec2(0, safeSize.height - height_spacing * 3);
_drawnVerticesLabel->setAnchorPoint({0, 0});
_drawnBatchesLabel->setAnchorPoint({0, 0});
_FPSLabel->setAnchorPoint({0, 0});
break;
case FPSPosition::BOTTOM_RIGHT:
_fpsPosition = Vec2(safeSize.width, 0);
_drawnVerticesLabel->setAnchorPoint({1, 0});
_drawnBatchesLabel->setAnchorPoint({1, 0});
_FPSLabel->setAnchorPoint({1, 0});
break;
case FPSPosition::CENTER_RIGHT:
_fpsPosition = Vec2(safeSize.width, safeSize.height / 2 - height_spacing * 1.5);
_drawnVerticesLabel->setAnchorPoint({1, 0.0});
_drawnBatchesLabel->setAnchorPoint({1, 0.0});
_FPSLabel->setAnchorPoint({1, 0.0});
break;
case FPSPosition::TOP_RIGHT:
_fpsPosition = Vec2(safeSize.width, safeSize.height - height_spacing * 3);
_drawnVerticesLabel->setAnchorPoint({1, 0});
_drawnBatchesLabel->setAnchorPoint({1, 0});
_FPSLabel->setAnchorPoint({1, 0});
break;
case FPSPosition::BOTTOM_CENTER:
_fpsPosition = Vec2(safeSize.width / 2, 0);
_drawnVerticesLabel->setAnchorPoint({0.5, 0});
_drawnBatchesLabel->setAnchorPoint({0.5, 0});
_FPSLabel->setAnchorPoint({0.5, 0});
break;
case FPSPosition::CENTER:
_fpsPosition = Vec2(safeSize.width / 2, safeSize.height / 2 - height_spacing * 1.5);
_drawnVerticesLabel->setAnchorPoint({0.5, 0.0});
_drawnBatchesLabel->setAnchorPoint({0.5, 0.0});
_FPSLabel->setAnchorPoint({0.5, 0.0});
break;
case FPSPosition::TOP_CENTER:
_fpsPosition = Vec2(safeSize.width / 2, safeSize.height - height_spacing * 3);
_drawnVerticesLabel->setAnchorPoint({0.5, 0});
_drawnBatchesLabel->setAnchorPoint({0.5, 0});
_FPSLabel->setAnchorPoint({0.5, 0});
break;
default: // FPSPosition::BOTTOM_LEFT
_fpsPosition = Vec2(0, 0);
_drawnVerticesLabel->setAnchorPoint({0, 0});
_drawnBatchesLabel->setAnchorPoint({0, 0});
_FPSLabel->setAnchorPoint({0, 0});
break;
}
_drawnVerticesLabel->setPosition(Vec2(0, height_spacing * 2.0f) + _fpsPosition + safeOrigin);
_drawnBatchesLabel->setPosition(Vec2(0, height_spacing * 1.0f) + _fpsPosition + safeOrigin);
_FPSLabel->setPosition(Vec2(0, height_spacing * 0.0f) + _fpsPosition + safeOrigin);
}
}
#endif // #if !CC_STRIP_FPS

View File

@ -77,6 +77,23 @@ enum class MATRIX_STACK_TYPE
MATRIX_STACK_TEXTURE
};
/**
* @brief FPS position on display like the numbers on the numpad
*/
enum FPSPosition
{
BOTTOM_LEFT = 1,
BOTTOM_CENTER = 2,
BOTTOM_RIGHT = 3,
CENTER_LEFT = 4,
CENTER = 5,
CENTER_RIGHT = 6,
TOP_LEFT = 7,
TOP_CENTER = 8,
TOP_RIGHT = 9
};
/**
@brief Class that creates and handles the main Window and manages how
and when to execute the Scenes.
@ -166,6 +183,11 @@ public:
/** Gets the seconds per frame. */
float getSecondsPerFrame() { return _secondsPerFrame; }
/** Set the FPS position like a numpad [1-9] on display. */
void setFPSPos(FPSPosition FPSposition = FPSPosition::BOTTOM_LEFT);
/** Sets the FPS value. */
/**
* Get the GLView.
* @lua NA

View File

@ -6,16 +6,16 @@
*/
struct RngSeed
{
const unsigned long RNG_RAND_MAX = 4294967295;
const unsigned long RNG_RAND_MAX_SIGNED = 2147483647;
unsigned long _x = 1;
unsigned long _y = 2;
unsigned long _z = 4;
unsigned long _w = 8;
unsigned long _carry = 0;
unsigned long _k = 0;
unsigned long _m = 0;
unsigned long _seed = 0;
const uint32_t RNG_RAND_MAX = 4294967295;
const uint32_t RNG_RAND_MAX_SIGNED = 2147483647;
uint32_t _x = 1;
uint32_t _y = 2;
uint32_t _z = 4;
uint32_t _w = 8;
uint32_t _carry = 0;
uint32_t _k = 0;
uint32_t _m = 0;
uint32_t _seed = 0;
RngSeed() { seed_rand(time(NULL)); }
@ -31,7 +31,7 @@ struct RngSeed
}
// returns an unsigned long random value
unsigned long rand()
uint32_t rand()
{
_x = _x * 69069 + 1;
_y ^= _y << 13;
@ -46,13 +46,13 @@ struct RngSeed
}
// returns a random integer from min to max
int range(int min, int max)
int32_t range(int32_t min, int32_t max)
{
return floor(min + static_cast<float>(rand()) / (static_cast<float>(RNG_RAND_MAX / (max - min))));
}
// returns a random unsigned integer from min to max
unsigned int rangeu(unsigned int min, unsigned int max)
uint32_t rangeu(uint32_t min, uint32_t max)
{
return floor(min + static_cast<float>(rand()) / (static_cast<float>(RNG_RAND_MAX / (max - min))));
}
@ -64,13 +64,13 @@ struct RngSeed
}
// returns a random integer from 0 to max
int max(int max = INT_MAX)
int32_t max(int32_t max = INT_MAX)
{
return floor(0 + static_cast<float>(rand()) / (static_cast<float>(RNG_RAND_MAX / (max - 0))));
}
// returns a random unsigned integer from 0 to max
unsigned int maxu(unsigned int max = UINT_MAX)
uint32_t maxu(uint32_t max = UINT_MAX)
{
return floor(0 + static_cast<float>(rand()) / (static_cast<float>(RNG_RAND_MAX / (max - 0))));
}

View File

@ -53,14 +53,13 @@ public:
using PrimitiveType = backend::PrimitiveType;
/**
Buffer usage of vertex/index buffer. If the contents are not updated every frame,
then STATIC should be used. Otherwise, DYNAMIC should be used.
This flag is not improtant because most GPU drivers ignore it, so it's best left to STATIC.
Buffer usage of vertex/index buffer. If the contents is not updated every frame,
then use STATIC, other use DYNAMIC.
*/
using BufferUsage = backend::BufferUsage;
/**
The index format that determines the size of index data. U_SHORT (65535 vertices) is enough for most
cases, But support for U_INT (4294967295 vertices) has been added.
The index format determine the size for index data. U_SHORT is enough for most
cases.
*/
using IndexFormat = backend::IndexFormat;
@ -209,8 +208,6 @@ TODO: should remove it.
inline IndexFormat getIndexFormat() const { return _indexFormat; }
inline void setIndexFormat(IndexFormat format) { _indexFormat = format; }
/**
* set a callback which will be invoke before rendering
*/

View File

@ -150,7 +150,7 @@ void Pass::initUniformLocations()
_locPMatrix = ps->getUniformLocation("u_PMatrix");
_locNormalMatrix = ps->getUniformLocation("u_NormalMatrix");
_locTexture = ps->getUniformLocation("u_texture");
_locTexture = ps->getUniformLocation("u_tex0");
_locNormalTexture = ps->getUniformLocation("u_normalTex");
_locColor = ps->getUniformLocation("u_color");

View File

@ -107,8 +107,8 @@ public:
void updateMVPUniform(const Mat4& modelView);
void setUniformTexture(uint32_t slot, backend::TextureBackend*); // u_texture
void setUniformNormTexture(uint32_t slot, backend::TextureBackend*); // u_texture
void setUniformTexture(uint32_t slot, backend::TextureBackend*); // u_tex0
void setUniformNormTexture(uint32_t slot, backend::TextureBackend*); // u_tex0
void setUniformColor(const void*, size_t); // ucolor
void setUniformMatrixPalette(const void*, size_t); // u_matrixPalette
@ -155,7 +155,7 @@ private:
backend::UniformLocation _locPMatrix;
backend::UniformLocation _locNormalMatrix;
backend::UniformLocation _locTexture; // u_texture
backend::UniformLocation _locTexture; // u_tex0
backend::UniformLocation _locNormalTexture; // u_normalTex
backend::UniformLocation _locColor; // ucolor

View File

@ -735,7 +735,7 @@ void Texture2D::initProgram()
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE);
_programState = new cocos2d::backend::ProgramState(program);
_mvpMatrixLocation = _programState->getUniformLocation("u_MVPMatrix");
_textureLocation = _programState->getUniformLocation("u_texture");
_textureLocation = _programState->getUniformLocation("u_tex0");
pipelineDescriptor.programState = _programState;

View File

@ -123,8 +123,6 @@ bool ProgramCache::init()
registerProgramFactory(ProgramType::TERRAIN_3D, CC3D_terrain_vert, CC3D_terrain_frag);
registerProgramFactory(ProgramType::PARTICLE_TEXTURE_3D, CC3D_particle_vert, CC3D_particleTexture_frag);
registerProgramFactory(ProgramType::PARTICLE_COLOR_3D, CC3D_particle_vert, CC3D_particleColor_frag);
registerProgramFactory(ProgramType::QUAD_COLOR_2D, CC2D_quad_vert, CC2D_quadColor_frag);
registerProgramFactory(ProgramType::QUAD_TEXTURE_2D, CC2D_quad_vert, CC2D_quadTexture_frag);
registerProgramFactory(ProgramType::HSV, positionTextureColor_vert, hsv_frag);
registerProgramFactory(ProgramType::HSV_DUAL_SAMPLER, positionTextureColor_vert, dualSampler_hsv_frag);

View File

@ -160,8 +160,7 @@ enum class TextureUsage : uint32_t
enum class IndexFormat : uint32_t
{
U_SHORT,
U_INT,
UNSPEC = 0xff,
U_INT
};
enum class VertexStepMode : uint32_t
@ -432,9 +431,6 @@ struct ProgramType
PARTICLE_TEXTURE_3D, // CC3D_particle_vert, CC3D_particleTexture_frag
PARTICLE_COLOR_3D, // CC3D_particle_vert, CC3D_particleColor_frag
QUAD_COLOR_2D, // CC2D_quad_vert, CC2D_quadColor_frag
QUAD_TEXTURE_2D, // CC2D_quad_vert, CC2D_quadTexture_frag
HSV,
HSV_DUAL_SAMPLER,
HSV_ETC1 = HSV_DUAL_SAMPLER,
@ -447,10 +443,10 @@ struct ProgramType
/// built-in uniform name
static const char* UNIFORM_NAME_MVP_MATRIX = "u_MVPMatrix";
static const char* UNIFORM_NAME_TEXTURE = "u_texture";
static const char* UNIFORM_NAME_TEXTURE1 = "u_texture1";
static const char* UNIFORM_NAME_TEXTURE2 = "u_texture2";
static const char* UNIFORM_NAME_TEXTURE3 = "u_texture3";
static const char* UNIFORM_NAME_TEXTURE = "u_tex0";
static const char* UNIFORM_NAME_TEXTURE1 = "u_tex1";
static const char* UNIFORM_NAME_TEXTURE2 = "u_tex2";
static const char* UNIFORM_NAME_TEXTURE3 = "u_tex3";
static const char* UNIFORM_NAME_TEXT_COLOR = "u_textColor";
static const char* UNIFORM_NAME_EFFECT_COLOR = "u_effectColor";
static const char* UNIFORM_NAME_EFFECT_TYPE = "u_effectType";

View File

@ -202,14 +202,14 @@ void ShaderModuleMTL::setBuiltinUniformLocation()
_uniformLocation[Uniform::EFFECT_TYPE] = iter->second.location;
}
/// u_texture
/// u_tex0
iter = _uniformInfos.find(UNIFORM_NAME_TEXTURE);
if (iter != _uniformInfos.end())
{
_uniformLocation[Uniform::TEXTURE] = iter->second.location;
}
/// u_texture1
/// u_tex1
iter = _uniformInfos.find(UNIFORM_NAME_TEXTURE1);
if (iter != _uniformInfos.end())
{

View File

@ -188,11 +188,11 @@ void ProgramGL::computeLocations()
_builtinUniformLocation[Uniform::EFFECT_TYPE].location[1] =
_activeUniformInfos[UNIFORM_NAME_EFFECT_TYPE].bufferOffset;
/// u_texture
/// u_tex0
location = glGetUniformLocation(_program, UNIFORM_NAME_TEXTURE);
_builtinUniformLocation[Uniform::TEXTURE].location[0] = location;
/// u_texture1
/// u_tex1
location = glGetUniformLocation(_program, UNIFORM_NAME_TEXTURE1);
_builtinUniformLocation[Uniform::TEXTURE1].location[0] = location;
}

View File

@ -73,7 +73,4 @@ NS_CC_BEGIN
#include "renderer/shaders/hsv.frag"
#include "renderer/shaders/dualSampler_hsv.frag"
#include "renderer/shaders/quad.vert"
#include "renderer/shaders/quad.frag"
NS_CC_END

View File

@ -78,10 +78,6 @@ extern CC_DLL const char* CC3D_skybox_vert;
extern CC_DLL const char* CC3D_terrain_frag;
extern CC_DLL const char* CC3D_terrain_vert;
extern CC_DLL const char* CC2D_quadTexture_frag;
extern CC_DLL const char* CC2D_quadColor_frag;
extern CC_DLL const char* CC2D_quad_vert;
extern CC_DLL const char* hsv_frag;
extern CC_DLL const char* dualSampler_hsv_frag;
NS_CC_END

View File

@ -98,7 +98,7 @@ uniform vec4 u_color;
uniform sampler2D u_normalTex;
#endif
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
vec3 computeLighting(vec3 normalVector, vec3 lightDirection, vec3 lightColor, float attenuation)
{
@ -172,9 +172,9 @@ void main(void)
#endif
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
gl_FragColor = texture2D(u_texture, TextureCoordOut) * u_color * combinedColor;
gl_FragColor = texture2D(u_tex0, TextureCoordOut) * u_color * combinedColor;
#else
gl_FragColor = texture2D(u_texture, TextureCoordOut) * u_color;
gl_FragColor = texture2D(u_tex0, TextureCoordOut) * u_color;
#endif
}

View File

@ -31,10 +31,10 @@ varying mediump vec2 TextureCoordOut;
varying vec2 TextureCoordOut;
#endif
uniform vec4 u_color;
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
void main(void)
{
gl_FragColor = texture2D(u_texture, TextureCoordOut) * u_color;
gl_FragColor = texture2D(u_tex0, TextureCoordOut) * u_color;
}
)";

View File

@ -34,11 +34,11 @@ varying vec2 TextureCoordOut;
#endif
uniform vec4 u_color;
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
void main(void)
{
gl_FragColor = texture2D(u_texture, TextureCoordOut) * ColorOut * u_color;
gl_FragColor = texture2D(u_tex0, TextureCoordOut) * ColorOut * u_color;
}
)";

View File

@ -38,10 +38,10 @@ uniform int u_has_alpha;
uniform int u_has_light_map;
#endif
uniform sampler2D u_alphaMap;
uniform sampler2D u_texture0;
uniform sampler2D u_texture1;
uniform sampler2D u_texture2;
uniform sampler2D u_texture3;
uniform sampler2D u_tex0;
uniform sampler2D u_tex1;
uniform sampler2D u_tex2;
uniform sampler2D u_tex3;
uniform sampler2D u_lightMap;
uniform float u_detailSize[4];
uniform vec3 u_lightDir;
@ -58,14 +58,14 @@ if(u_has_light_map<=0)
float lightFactor = dot(-u_lightDir,v_normal);
if(u_has_alpha<=0)
{
gl_FragColor = texture2D(u_texture0, v_texCoord)*lightColor*lightFactor;
gl_FragColor = texture2D(u_tex0, v_texCoord)*lightColor*lightFactor;
}else
{
vec4 blendFactor =texture2D(u_alphaMap,v_texCoord);
vec4 color = vec4(0.0,0.0,0.0,0.0);
color = texture2D(u_texture0, v_texCoord*u_detailSize[0])*blendFactor.r +
texture2D(u_texture1, v_texCoord*u_detailSize[1])*blendFactor.g + texture2D(u_texture2, v_texCoord*u_detailSize[2])*blendFactor.b
+ texture2D(u_texture3, v_texCoord*u_detailSize[3])*(1.0 - blendFactor.a);
color = texture2D(u_tex0, v_texCoord*u_detailSize[0])*blendFactor.r +
texture2D(u_tex1, v_texCoord*u_detailSize[1])*blendFactor.g + texture2D(u_tex2, v_texCoord*u_detailSize[2])*blendFactor.b
+ texture2D(u_tex3, v_texCoord*u_detailSize[3])*(1.0 - blendFactor.a);
gl_FragColor = vec4(color.rgb*lightColor.rgb*lightFactor, 1.0);
}
}

View File

@ -31,11 +31,11 @@ const char* dualSampler_frag = R"(
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform sampler2D u_texture;
uniform sampler2D u_texture1;
uniform sampler2D u_tex0;
uniform sampler2D u_tex1;
void main() {
vec4 texColor = vec4(texture2D(u_texture, v_texCoord).rgb, texture2D(u_texture1, v_texCoord).r);
vec4 texColor = vec4(texture2D(u_tex0, v_texCoord).rgb, texture2D(u_tex1, v_texCoord).r);
texColor.rgb *= texColor.a; // Premultiply with Alpha channel

View File

@ -31,13 +31,13 @@ const char* dualSampler_gray_frag = R"(
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform sampler2D u_texture;
uniform sampler2D u_texture1;
uniform sampler2D u_tex0;
uniform sampler2D u_tex1;
void main()
{
vec4 texColor = texture2D(u_texture, v_texCoord);
texColor.a = texture2D(u_texture1, v_texCoord).r;
vec4 texColor = texture2D(u_tex0, v_texCoord);
texColor.a = texture2D(u_tex1, v_texCoord).r;
texColor.rgb *= texColor.a; // premultiply alpha channel
texColor = v_fragmentColor * texColor;

View File

@ -6,8 +6,8 @@ precision mediump float;
varying vec2 v_texCoord;
varying vec4 v_fragmentColor;
uniform sampler2D u_texture;
uniform sampler2D u_texture1;
uniform sampler2D u_tex0;
uniform sampler2D u_tex1;
// HSV matrix
uniform mat3 u_mix_hsv;
@ -35,7 +35,7 @@ vec3 hsv2rgb(vec3 c)
void main()
{
vec4 texColor = vec4(texture2D(u_texture, v_texCoord).rgb, texture2D(u_texture1, v_texCoord).r);
vec4 texColor = vec4(texture2D(u_tex0, v_texCoord).rgb, texture2D(u_tex1, v_texCoord).r);
texColor.rgb *= texColor.a; // Premultiply with Alpha channel
vec3 rgbColor = u_mix_hsv * texColor.rgb;

View File

@ -6,7 +6,7 @@ precision mediump float;
varying vec2 v_texCoord;
varying vec4 v_fragmentColor;
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
// HSV matrix
uniform mat3 u_mix_hsv;
@ -34,7 +34,7 @@ vec3 hsv2rgb(vec3 c)
void main()
{
vec4 pixColor = texture2D(u_texture, v_texCoord); // * v_fragmentColor;
vec4 pixColor = texture2D(u_tex0, v_texCoord); // * v_fragmentColor;
vec3 rgbColor = u_mix_hsv * pixColor.rgb;
float sum = pixColor.r + pixColor.g + pixColor.b;

View File

@ -33,11 +33,11 @@ varying vec2 v_texCoord;
uniform vec4 u_effectColor;
uniform vec4 u_textColor;
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
void main()
{
float dist = texture2D(u_texture, v_texCoord).a;
float dist = texture2D(u_tex0, v_texCoord).a;
//TODO: Implementation 'fwidth' for glsl 1.0
//float width = fwidth(dist);
//assign width for constant will lead to a little bit fuzzy,it's temporary measure.

View File

@ -32,11 +32,11 @@ varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform vec4 u_textColor;
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
void main()
{
vec4 color = texture2D(u_texture, v_texCoord);
vec4 color = texture2D(u_tex0, v_texCoord);
//the texture use dual channel 16-bit output for distance_map
//float dist = color.b+color.g/256.0;
// the texture use single channel 8-bit output for distance_map

View File

@ -32,12 +32,12 @@ varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform vec4 u_textColor;
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
void main()
{
gl_FragColor = v_fragmentColor * vec4(u_textColor.rgb,// RGB from uniform
u_textColor.a * texture2D(u_texture, v_texCoord).a// A from texture & uniform
u_textColor.a * texture2D(u_tex0, v_texCoord).a// A from texture & uniform
);
}
)";

View File

@ -35,7 +35,7 @@ varying vec2 v_texCoord;
uniform vec4 u_effectColor;
uniform vec4 u_textColor;
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
#ifdef GL_ES
uniform lowp int u_effectType; // 0: None (Draw text), 1: Outline, 2: Shadow
@ -45,7 +45,7 @@ uniform int u_effectType;
void main()
{
vec4 sample = texture2D(u_texture, v_texCoord);
vec4 sample = texture2D(u_tex0, v_texCoord);
// fontAlpha == 1 means the area of solid text (without edge)
// fontAlpha == 0 means the area outside text, including outline area
// fontAlpha == (0, 1) means the edge of text

View File

@ -32,11 +32,11 @@ varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform vec4 u_textColor;
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
void main()
{
vec4 color = texture2D(u_texture, v_texCoord);
vec4 color = texture2D(u_tex0, v_texCoord);
//the texture use dual channel 16-bit output for distance_map
//float dist = color.b+color.g/256.0;
// the texture use single channel 8-bit output for distance_map

View File

@ -31,10 +31,10 @@ varying mediump vec2 v_texCoord;
varying vec2 v_texCoord;
#endif
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
void main()
{
gl_FragColor = texture2D(u_texture, v_texCoord);
gl_FragColor = texture2D(u_tex0, v_texCoord);
}
)";

View File

@ -32,10 +32,10 @@ varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
void main()
{
gl_FragColor = v_fragmentColor * texture2D(u_texture, v_texCoord);
gl_FragColor = v_fragmentColor * texture2D(u_tex0, v_texCoord);
}
)";

View File

@ -32,11 +32,11 @@ varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform float u_alpha_value;
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
void main()
{
vec4 texColor = texture2D(u_texture, v_texCoord);
vec4 texColor = texture2D(u_tex0, v_texCoord);
// mimic: glAlphaFunc(GL_GREATER)
// pass if ( incoming_pixel >= u_alpha_value ) => fail if incoming_pixel < u_alpha_value

View File

@ -1,58 +0,0 @@
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
https://adxeproject.github.io/
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.
****************************************************************************/
const char* CC2D_quadTexture_frag = R"(
#ifdef GL_ES
varying mediump vec2 TextureCoordOut;
varying mediump vec4 ColorOut;
#else
varying vec4 ColorOut;
varying vec2 TextureCoordOut;
#endif
uniform vec4 u_color;
uniform sampler2D u_texture;
void main(void)
{
gl_FragColor = texture2D(u_texture, TextureCoordOut) * ColorOut * u_color;
}
)";
const char* CC2D_quadColor_frag = R"(
#ifdef GL_ES
varying mediump vec4 ColorOut;
#else
varying vec4 ColorOut;
#endif
uniform vec4 u_color;
void main(void)
{
gl_FragColor = ColorOut * u_color;
}
)";

View File

@ -1,44 +0,0 @@
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
https://adxeproject.github.io/
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.
****************************************************************************/
const char* CC2D_quad_vert = R"(
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord;
varying vec2 TextureCoordOut;
varying vec4 ColorOut;
uniform mat4 u_PMatrix;
void main()
{
ColorOut = a_color;
TextureCoordOut = a_texCoord;
TextureCoordOut.y = 1.0 - TextureCoordOut.y;
gl_Position = u_PMatrix * a_position;
}
)";

View File

@ -31,11 +31,11 @@ precision mediump float;
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
void main(void)
{
vec4 c = texture2D(u_texture, v_texCoord);
vec4 c = texture2D(u_tex0, v_texCoord);
c = v_fragmentColor * c;
gl_FragColor.xyz = vec3(0.2126*c.r + 0.7152*c.g + 0.0722*c.b);
gl_FragColor.w = c.w;

View File

@ -80,8 +80,8 @@ varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif
uniform sampler2D u_texture; // Y sample
uniform sampler2D u_texture1; // UV sample
uniform sampler2D u_tex0; // Y sample
uniform sampler2D u_tex1; // UV sample
uniform vec2 uv_scale;
uniform float out_w;
@ -105,13 +105,13 @@ void main()
/* For dual sampler */
//vec2 tXY = v_texCoord;
//YUV.x = texture2D(u_texture, tXY).x;
//YUV.x = texture2D(u_tex0, tXY).x;
//tXY.y += 0.015625; // why needs adjust 1.0/64 ?
//YUV.yz = texture2D(u_texture1, tXY).xw;
//YUV.yz = texture2D(u_tex1, tXY).xw;
/* For single sampler */
vec2 tXY = v_texCoord * uv_scale;
YUV.x = texture2D(u_texture, tXY).x;
YUV.x = texture2D(u_tex0, tXY).x;
tXY.y *= 0.5;
tXY.y += 2.0 / 3.0;
@ -120,8 +120,8 @@ void main()
float UPos = ((UVOffs * uv_scale.x) + 0.5) / out_w;
float VPos = ((UVOffs * uv_scale.x) + 1.5) / out_w;
YUV.y = texture2D(u_texture, vec2(UPos, tXY.y)).x;
YUV.z = texture2D(u_texture, vec2(VPos, tXY.y)).x;
YUV.y = texture2D(u_tex0, vec2(UPos, tXY.y)).x;
YUV.z = texture2D(u_tex0, vec2(VPos, tXY.y)).x;
/* Convert YUV to RGB */
vec4 OutColor;
@ -144,8 +144,8 @@ varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif
uniform sampler2D u_texture; // Y sample
uniform sampler2D u_texture1; // UV sample
uniform sampler2D u_tex0; // Y sample
uniform sampler2D u_tex1; // UV sample
uniform vec2 uv_scale;
uniform float out_w; // texture width
@ -170,13 +170,13 @@ void main()
vec3 YUV;
/* For dual sampler */
YUV.yz = texture2D(u_texture1, tXY).yw;
YUV.x = texture2D(u_texture, tXY).x;
YUV.yz = texture2D(u_tex1, tXY).yw;
YUV.x = texture2D(u_tex0, tXY).x;
/* For single sampler */
//YUV.yz = texture2D(u_texture, tXY).yw;
//YUV.yz = texture2D(u_tex0, tXY).yw;
//
//vec4 YUY2P = texture2D(u_texture, tXY);
//vec4 YUY2P = texture2D(u_tex0, tXY);
//float Pos = v_texCoord.x * out_w;
//YUV.x = floor(mod(Pos, 2.0)) == 0.0 ? YUY2P.z : YUY2P.x;

View File

@ -188,22 +188,49 @@ void SimpleSnake::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
{
switch (keyCode)
{
case cocos2d::EventKeyboard::KeyCode::KEY_LEFT_ARROW:
case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
if (dir != 2)
dir = 1;
break;
case cocos2d::EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
if (dir != 1)
dir = 2;
break;
case cocos2d::EventKeyboard::KeyCode::KEY_UP_ARROW:
case EventKeyboard::KeyCode::KEY_UP_ARROW:
if (dir != 3)
dir = 0;
break;
case cocos2d::EventKeyboard::KeyCode::KEY_DOWN_ARROW:
case EventKeyboard::KeyCode::KEY_DOWN_ARROW:
if (dir != 0)
dir = 3;
break;
case EventKeyboard::KeyCode::KEY_1:
Director::getInstance()->setFPSPos(FPSPosition::BOTTOM_LEFT);
break;
case EventKeyboard::KeyCode::KEY_4:
Director::getInstance()->setFPSPos(FPSPosition::CENTER_LEFT);
break;
case EventKeyboard::KeyCode::KEY_7:
Director::getInstance()->setFPSPos(FPSPosition::TOP_LEFT);
break;
case EventKeyboard::KeyCode::KEY_8:
Director::getInstance()->setFPSPos(FPSPosition::TOP_CENTER);
break;
case EventKeyboard::KeyCode::KEY_9:
Director::getInstance()->setFPSPos(FPSPosition::TOP_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_6:
Director::getInstance()->setFPSPos(FPSPosition::CENTER_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_3:
Director::getInstance()->setFPSPos(FPSPosition::BOTTOM_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_2:
Director::getInstance()->setFPSPos(FPSPosition::BOTTOM_CENTER);
break;
case EventKeyboard::KeyCode::KEY_5:
Director::getInstance()->setFPSPos(FPSPosition::CENTER);
break;
default:
break;
}

View File

@ -1420,23 +1420,23 @@ static bool ImGui_ImplAdxe_createShaderPrograms()
"#ifdef GL_ES\n"
" precision mediump float;\n"
"#endif\n"
"uniform sampler2D u_texture;\n"
"uniform sampler2D u_tex0;\n"
"varying vec2 v_texCoord;\n"
"varying vec4 v_fragmentColor;\n"
"void main()\n"
"{\n"
" gl_FragColor = v_fragmentColor * texture2D(u_texture, v_texCoord);\n"
" gl_FragColor = v_fragmentColor * texture2D(u_tex0, v_texCoord);\n"
"}\n";
auto fragment_shader_font =
"#ifdef GL_ES\n"
" precision mediump float;\n"
"#endif\n"
"uniform sampler2D u_texture;\n"
"uniform sampler2D u_tex0;\n"
"varying vec2 v_texCoord;\n"
"varying vec4 v_fragmentColor;\n"
"void main()\n"
"{\n"
" float a = texture2D(u_texture, v_texCoord).a;\n"
" float a = texture2D(u_tex0, v_texCoord).a;\n"
" gl_FragColor = vec4(v_fragmentColor.rgb, v_fragmentColor.a * a);\n"
"}\n";

View File

@ -238,7 +238,7 @@ bool Particle3DQuadRender::initQuadRender(std::string_view texFile)
_locColor = _programState->getUniformLocation("u_color");
_locPMatrix = _programState->getUniformLocation("u_PMatrix");
_locTexture = _programState->getUniformLocation("u_texture");
_locTexture = _programState->getUniformLocation("u_tex0");
_meshCommand.setTransparent(true);
_meshCommand.setSkipBatching(true);

View File

@ -674,7 +674,7 @@ void PUBillboardChain::init(std::string_view texFile)
_locColor = _programState->getUniformLocation("u_color");
_locPMatrix = _programState->getUniformLocation("u_PMatrix");
_locTexture = _programState->getUniformLocation("u_texture");
_locTexture = _programState->getUniformLocation("u_tex0");
_meshCommand.setTransparent(true);
_meshCommand.setSkipBatching(true);

View File

@ -659,7 +659,7 @@ bool PUParticle3DEntityRender::initRender(std::string_view texFile)
_locColor = _programState->getUniformLocation("u_color");
_locPMatrix = _programState->getUniformLocation("u_PMatrix");
_locTexture = _programState->getUniformLocation("u_texture");
_locTexture = _programState->getUniformLocation("u_tex0");
_meshCommand.setTransparent(true);
_meshCommand.setSkipBatching(true);

View File

@ -16851,6 +16851,67 @@ int lua_cocos2dx_Director_drawScene(lua_State* tolua_S)
return 0;
}
int lua_cocos2dx_Director_setFPSPos(lua_State* tolua_S)
{
int argc = 0;
cocos2d::Director* cobj = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertype(tolua_S,1,"cc.Director",0,&tolua_err)) goto tolua_lerror;
#endif
cobj = (cocos2d::Director*)tolua_tousertype(tolua_S,1,0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_Director_setFPSPos'", nullptr);
return 0;
}
#endif
argc = lua_gettop(tolua_S)-1;
if (argc == 0)
{
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_Director_setFPSPos'", nullptr);
return 0;
}
cobj->setFPSPos();
lua_settop(tolua_S, 1);
return 1;
}
if (argc == 1)
{
cocos2d::FPSPosition arg0;
ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0, "cc.Director:setFPSPos");
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_Director_setFPSPos'", nullptr);
return 0;
}
cobj->setFPSPos(arg0);
lua_settop(tolua_S, 1);
return 1;
}
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.Director:setFPSPos",argc, 0);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_Director_setFPSPos'.",&tolua_err);
#endif
return 0;
}
int lua_cocos2dx_Director_restart(lua_State* tolua_S)
{
int argc = 0;
@ -17914,6 +17975,7 @@ int lua_register_cocos2dx_Director(lua_State* tolua_S)
tolua_function(tolua_S,"setNotificationNode",lua_cocos2dx_Director_setNotificationNode);
tolua_function(tolua_S,"setChildrenIndexerEnabled",lua_cocos2dx_Director_setChildrenIndexerEnabled);
tolua_function(tolua_S,"drawScene",lua_cocos2dx_Director_drawScene);
tolua_function(tolua_S,"setFPSPos",lua_cocos2dx_Director_setFPSPos);
tolua_function(tolua_S,"restart",lua_cocos2dx_Director_restart);
tolua_function(tolua_S,"popScene",lua_cocos2dx_Director_popScene);
tolua_function(tolua_S,"loadIdentityMatrix",lua_cocos2dx_Director_loadIdentityMatrix);

View File

@ -2406,6 +2406,7 @@ int register_all_cocos2dx(lua_State* tolua_S);
#endif // __cocos2dx_h__

View File

@ -105,7 +105,7 @@ backend::ProgramState* SkeletonBatch::updateCommandPipelinePS(SkeletonCommand* c
command->_locMVP = currentState->getUniformLocation("u_MVPMatrix");
command->_locTexture = currentState->getUniformLocation("u_texture");
command->_locTexture = currentState->getUniformLocation("u_tex0");
}
return currentState;
}

View File

@ -81,13 +81,13 @@ namespace {
\n#ifdef GL_ES\n
precision lowp float;
\n#endif\n
uniform sampler2D u_texture;
uniform sampler2D u_tex0;
varying vec4 v_light;
varying vec4 v_dark;
varying vec2 v_texCoord;
void main() {
vec4 texColor = texture2D(u_texture, v_texCoord);
vec4 texColor = texture2D(u_tex0, v_texCoord);
float alpha = texColor.a * v_light.a;
gl_FragColor.a = alpha;
gl_FragColor.rgb = ((texColor.a - 1.0) * v_dark.a + 1.0 - texColor.rgb) * v_dark.rgb + texColor.rgb * v_light.rgb;
@ -101,7 +101,7 @@ namespace {
static void updateProgramStateLayout(backend::ProgramState* programState) {
__locPMatrix = programState->getUniformLocation("u_PMatrix");
__locTexture = programState->getUniformLocation("u_texture");
__locTexture = programState->getUniformLocation("u_tex0");
auto layout = programState->getVertexLayout();

View File

@ -35,6 +35,92 @@
USING_NS_CC;
namespace {
class TextButton : public Label {
public:
static TextButton *
create(std::string_view text, const std::function<void(TextButton *)> &onTriggered) {
auto ret = new TextButton();
TTFConfig ttfconfig("fonts/arial.ttf", 25);
if (ret->setTTFConfig(ttfconfig)) {
ret->setString(text);
ret->_onTriggered = onTriggered;
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
void setEnabled(bool enabled) {
_enabled = enabled;
if (_enabled) {
this->setColor(Color3B::WHITE);
} else {
this->setColor(Color3B::GRAY);
}
}
private:
TextButton() : _onTriggered(nullptr), _enabled(true) {
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(TextButton::onTouchBegan, this);
listener->onTouchEnded = CC_CALLBACK_2(TextButton::onTouchEnded, this);
listener->onTouchCancelled = CC_CALLBACK_2(TextButton::onTouchCancelled, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
bool touchHits(Touch *touch) {
auto hitPos = this->convertToNodeSpace(touch->getLocation());
if (hitPos.x >= 0 && hitPos.y >= 0 && hitPos.x <= _contentSize.width &&
hitPos.y <= _contentSize.height) {
return true;
}
return false;
}
bool onTouchBegan(Touch *touch, Event *event) {
auto hits = touchHits(touch);
if (hits) {
scaleButtonTo(0.95f);
}
return hits;
}
void onTouchEnded(Touch *touch, Event *event) {
if (_enabled) {
auto hits = touchHits(touch);
if (hits && _onTriggered) {
_onTriggered(this);
}
}
scaleButtonTo(1);
}
void onTouchCancelled(Touch *touch, Event *event) { scaleButtonTo(1); }
void scaleButtonTo(float scale) {
auto action = ScaleTo::create(0.05f, scale);
action->setTag(10000);
stopActionByTag(10000);
runAction(action);
}
std::function<void(TextButton *)> _onTriggered;
bool _enabled;
};
}
EventDispatcherTests::EventDispatcherTests()
{
ADD_TEST_CASE(TouchableSpriteTest);
@ -458,6 +544,7 @@ void LabelKeyboardEventTest::onEnter()
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
#if defined(CC_PLATFORM_PC)
auto statusLabel = Label::createWithSystemFont("No keyboard event received!", "", 20);
statusLabel->setPosition(origin + Vec2(size.width / 2, size.height / 2));
addChild(statusLabel);
@ -468,6 +555,39 @@ void LabelKeyboardEventTest::onEnter()
sprintf(buf, "Key %d was pressed!", (int)keyCode);
auto label = static_cast<Label*>(event->getCurrentTarget());
label->setString(buf);
switch (keyCode)
{
case EventKeyboard::KeyCode::KEY_1:
Director::getInstance()->setFPSPos(FPSPosition::BOTTOM_LEFT);
break;
case EventKeyboard::KeyCode::KEY_4:
Director::getInstance()->setFPSPos(FPSPosition::CENTER_LEFT);
break;
case EventKeyboard::KeyCode::KEY_7:
Director::getInstance()->setFPSPos(FPSPosition::TOP_LEFT);
break;
case EventKeyboard::KeyCode::KEY_8:
Director::getInstance()->setFPSPos(FPSPosition::TOP_CENTER);
break;
case EventKeyboard::KeyCode::KEY_9:
Director::getInstance()->setFPSPos(FPSPosition::TOP_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_6:
Director::getInstance()->setFPSPos(FPSPosition::CENTER_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_3:
Director::getInstance()->setFPSPos(FPSPosition::BOTTOM_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_2:
Director::getInstance()->setFPSPos(FPSPosition::BOTTOM_CENTER);
break;
case EventKeyboard::KeyCode::KEY_5:
Director::getInstance()->setFPSPos(FPSPosition::CENTER);
break;
default:
break;
}
};
listener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event) {
@ -478,6 +598,31 @@ void LabelKeyboardEventTest::onEnter()
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, statusLabel);
#else
auto& layerSize = this->getContentSize();
static FPSPosition posType = FPSPosition::BOTTOM_LEFT;
posType = FPSPosition::BOTTOM_LEFT;
auto playPrev = TextButton::create("Show Fps Prev Pos", [=](TextButton* button) {
if (posType > FPSPosition::BOTTOM_LEFT)
{
posType = static_cast<FPSPosition>((int)posType - 1);
Director::getInstance()->setFPSPos(posType);
}
});
playPrev->setPosition(layerSize.width * 0.35f, layerSize.height * 0.5f);
addChild(playPrev);
auto playNext = TextButton::create("Show Fps Next Pos", [=](TextButton* button) {
if (posType < FPSPosition::TOP_RIGHT)
{
posType = static_cast<FPSPosition>((int)posType + 1);
Director::getInstance()->setFPSPos(posType);
}
});
playNext->setPosition(layerSize.width * 0.65f, layerSize.height * 0.5f);
addChild(playNext);
Director::getInstance()->setFPSPos(FPSPosition::BOTTOM_LEFT);
#endif
}
std::string LabelKeyboardEventTest::title() const
@ -487,7 +632,11 @@ std::string LabelKeyboardEventTest::title() const
std::string LabelKeyboardEventTest::subtitle() const
{
return "Please click keyboard\n(Only available on Desktop, Android\nand Windows Universal Apps)";
#if defined(CC_PLATFORM_PC)
return "Please click keyboard\n[1-9] Sets Fps position like on numpad!";
#else
return "Set FPS position like numpad [1-9]";
#endif
}
// SpriteAccelerationEventTest

View File

@ -772,7 +772,7 @@ bool ShaderMultiTexture::init()
auto programState = new backend::ProgramState(program);
_sprite->setProgramState(programState);
SET_TEXTURE(programState, "u_texture1", 1, right->getTexture()->getBackendTexture());
SET_TEXTURE(programState, "u_tex1", 1, right->getTexture()->getBackendTexture());
SET_UNIFORM(programState, "u_interpolate", 0.5f);
// slider
@ -803,5 +803,5 @@ void ShaderMultiTexture::changeTexture(Ref*)
Sprite* right = dynamic_cast<Sprite*>(getChildByTag(rightSpriteTag));
right->setTexture(texture);
auto programState = _sprite->getProgramState();
SET_TEXTURE(programState, "u_texture1", 1, right->getTexture()->getBackendTexture());
SET_TEXTURE(programState, "u_tex1", 1, right->getTexture()->getBackendTexture());
}