Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions core/3d/CCMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ static Texture2D* getDummyTexture()
Mesh::Mesh()
: _skin(nullptr)
, _visible(true)
, _isTransparent(false)
, _force2DQueue(false)
, meshIndexFormat(CustomCommand::IndexFormat::U_SHORT)
, _meshIndexData(nullptr)
, _blend(BlendFunc::ALPHA_NON_PREMULTIPLIED)
Expand Down Expand Up @@ -390,12 +388,13 @@ void Mesh::draw(Renderer* renderer,
uint32_t flags,
unsigned int lightMask,
const Vec4& color,
bool forceDepthWrite)
bool forceDepthWrite,
bool wireframe)
{
if (!isVisible())
return;

bool isTransparent = (_isTransparent || color.w < 1.f);
bool isTransparent = (_material->isTransparent() || color.w < 1.f);
float globalZ = isTransparent ? 0 : globalZOrder;
if (isTransparent)
flags |= Node::FLAGS_RENDER_AS_3D;
Expand All @@ -416,8 +415,6 @@ void Mesh::draw(Renderer* renderer,
else
_material->getStateBlock().setDepthWrite(true);

_material->getStateBlock().setBlend(_force2DQueue || isTransparent);

// set default uniforms for Mesh
// 'u_color' and others
const auto scene = Director::getInstance()->getRunningScene();
Expand All @@ -441,7 +438,8 @@ void Mesh::draw(Renderer* renderer,
command.init(globalZ, transform);
command.setSkipBatching(isTransparent);
command.setTransparent(isTransparent);
command.set3D(!_force2DQueue);
command.set3D(!_material->isForce2DQueue());
command.setWireframe(wireframe);
}

_meshIndexData->setPrimitiveType(_material->_drawPrimitive);
Expand Down
11 changes: 3 additions & 8 deletions core/3d/CCMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ class AX_DLL Mesh : public Ref
uint32_t flags,
unsigned int lightMask,
const Vec4& color,
bool forceDepthWrite);
bool forceDepthWrite,
bool wireframe);

/**skin setter*/
void setSkin(MeshSkin* skin);
Expand All @@ -239,11 +240,6 @@ class AX_DLL Mesh : public Ref
*/
void calculateAABB();

/**
* force set this Mesh renderer to 2D render queue
*/
void setForce2DQueue(bool force2D) { _force2DQueue = force2D; }

std::string getTextureFileName() { return _texFile; }

Mesh();
Expand All @@ -257,8 +253,7 @@ class AX_DLL Mesh : public Ref
std::map<NTextureData::Usage, Texture2D*> _textures; // textures that submesh is using
MeshSkin* _skin; // skin
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;
Expand Down
18 changes: 6 additions & 12 deletions core/3d/CCMeshRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ MeshRenderer::MeshRenderer()
, _lightMask(-1)
, _shaderUsingLight(false)
, _forceDepthWrite(false)
, _wireframe(false)
, _usingAutogeneratedGLProgram(true)
, _transparentMaterialHint(false)
{}

MeshRenderer::~MeshRenderer()
Expand Down Expand Up @@ -408,7 +410,7 @@ MeshRenderer* MeshRenderer::createMeshRendererNode(NodeData* nodedata, ModelData
texParams.sAddressMode = textureData->wrapS;
texParams.tAddressMode = textureData->wrapT;
tex->setTexParameters(texParams);
mesh->_isTransparent = (materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr);
_transparentMaterialHint = materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr;
}
}
textureData = materialData->getTextureData(NTextureData::Usage::Normal);
Expand Down Expand Up @@ -512,6 +514,7 @@ void MeshRenderer::genMaterial(bool useLight)
for (auto&& mesh : _meshes)
{
auto material = materials[mesh->getMeshIndexData()->getMeshVertexData()];
material->setTransparent(_transparentMaterialHint);
// keep original state block if exist
auto oldmaterial = mesh->getMaterial();
if (oldmaterial)
Expand Down Expand Up @@ -573,8 +576,7 @@ void MeshRenderer::createNode(NodeData* nodedata, Node* root, const MaterialData
texParams.sAddressMode = textureData->wrapS;
texParams.tAddressMode = textureData->wrapT;
tex->setTexParameters(texParams);
mesh->_isTransparent =
(materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr);
_transparentMaterialHint = materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr;
}
}
textureData = materialData->getTextureData(NTextureData::Usage::Normal);
Expand Down Expand Up @@ -811,7 +813,7 @@ void MeshRenderer::draw(Renderer* renderer, const Mat4& transform, uint32_t flag
for (auto&& mesh : _meshes)
{
mesh->draw(renderer, _globalZOrder, transform, flags, _lightMask, Vec4(color.r, color.g, color.b, color.a),
_forceDepthWrite);
_forceDepthWrite, _wireframe);
}
}

Expand Down Expand Up @@ -946,14 +948,6 @@ Mesh* MeshRenderer::getMesh() const
return _meshes.at(0);
}

void MeshRenderer::setForce2DQueue(bool force2D)
{
for (const auto& mesh : _meshes)
{
mesh->setForce2DQueue(force2D);
}
}

///////////////////////////////////////////////////////////////////////////////////
MeshRendererCache* MeshRendererCache::_cacheInstance = nullptr;
MeshRendererCache* MeshRendererCache::getInstance()
Expand Down
17 changes: 10 additions & 7 deletions core/3d/CCMeshRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ class AX_DLL MeshRenderer : public Node, public BlendProtocol
void setLightMask(unsigned int mask) { _lightMask = mask; }
unsigned int getLightMask() const { return _lightMask; }

/** enables wireframe rendering mode for this mesh renderer only, this can be very useful for debugging and
understanding generated meshes. */
void setWireframe(bool value) { _wireframe = value; }
bool isWireframe() const { return _wireframe; }

/** render all meshes within this mesh renderer */
virtual void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override;

Expand All @@ -194,15 +199,11 @@ class AX_DLL MeshRenderer : public Node, public BlendProtocol
*/
void setMaterial(Material* material, int meshIndex);

/** Adds a new material to a particular mesh in this mesh renderer.
* if meshIndex == -1, then it will be applied to all the meshes that belong to this mesh renderer.
/** Gets the material of a specific mesh in this mesh renderer.
*
* @param meshIndex Index of the mesh to apply the material to.
* @param meshIndex Index of the mesh to get the material from. 0 is the default index.
*/
Material* getMaterial(int meshIndex) const;

/** force render this mesh renderer in 2D queue. */
void setForce2DQueue(bool force2D);
Material* getMaterial(int meshIndex = 0) const;

/** Get list of meshes used in this mesh renderer. */
const Vector<Mesh*>& getMeshes() const { return _meshes; }
Expand Down Expand Up @@ -265,7 +266,9 @@ class AX_DLL MeshRenderer : public Node, public BlendProtocol
unsigned int _lightMask;
bool _shaderUsingLight; // Is the current shader using lighting?
bool _forceDepthWrite; // Always write to depth buffer
bool _wireframe; // render in wireframe mode
bool _usingAutogeneratedGLProgram;
bool _transparentMaterialHint; // Generate transparent materials when building from files

struct AsyncLoadParam
{
Expand Down
12 changes: 12 additions & 0 deletions core/renderer/CCMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,18 @@ std::string Material::getName() const
return _name;
}

void Material::setTransparent(bool value)
{
_isTransparent = value;
getStateBlock().setBlend(_force2DQueue || _isTransparent);
}

void Material::setForce2DQueue(bool value)
{
_force2DQueue = value;
getStateBlock().setBlend(_force2DQueue || _isTransparent);
}

Material::Material() : _name(""), _currentTechnique(nullptr), _target(nullptr) {}

Material::~Material() {}
Expand Down
24 changes: 24 additions & 0 deletions core/renderer/CCMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,27 @@ class AX_DLL Material : public Ref
*/
axis::backend::PrimitiveType getPrimitiveType() const { return _drawPrimitive; }

/**
* Enable material transparent rendering.
* WARNING: depth testing will not work.
*/
void setTransparent(bool value);

/**
* Is material transparent?
*/
bool isTransparent() const { return _isTransparent; }

/**
* Enable material 2D queue rendering.
*/
void setForce2DQueue(bool value);

/**
* Is material in 2D render queue?
*/
bool isForce2DQueue() const { return _force2DQueue; }

protected:
Material();
~Material();
Expand Down Expand Up @@ -189,6 +210,9 @@ class AX_DLL Material : public Ref
std::unordered_map<std::string, int> _textureSlots;
int _textureSlotIndex = 0;

bool _isTransparent = false; // is this mesh transparent.
bool _force2DQueue = false; // render meshes using this material in 2D render queue.

axis::backend::PrimitiveType _drawPrimitive =
axis::backend::PrimitiveType::TRIANGLE; // primitive draw type for meshes
};
Expand Down
9 changes: 8 additions & 1 deletion core/renderer/CCRenderCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ class AX_DLL RenderCommand
void set3D(bool value) { _is3D = value; }
/**Get the depth by current model view matrix.*/
float getDepth() const { return _depth; }
/**Whether the command should be rendered in wireframe mode.*/
bool isWireframe() const { return _isWireframe; }
/**Set wireframe render mode for this command.*/
void setWireframe(bool value) { _isWireframe = value; }
/// Can use the result to change the descriptor content.
inline PipelineDescriptor& getPipelineDescriptor() { return _pipelineDescriptor; }

Expand Down Expand Up @@ -123,9 +127,12 @@ class AX_DLL RenderCommand
/** Is the command been rendered on 3D pass. */
bool _is3D = false;

/** Depth from the model view matrix.*/
/** Depth from the model view matrix. */
float _depth = 0.f;

/** Polygon render mode set to LINE, which represents wireframe mode. */
bool _isWireframe = false;

Mat4 _mv;

PipelineDescriptor _pipelineDescriptor;
Expand Down
5 changes: 3 additions & 2 deletions core/renderer/CCRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,12 +731,13 @@ void Renderer::drawCustomCommand(RenderCommand* command)
{
_commandBuffer->setIndexBuffer(cmd->getIndexBuffer());
_commandBuffer->drawElements(cmd->getPrimitiveType(), cmd->getIndexFormat(), cmd->getIndexDrawCount(),
cmd->getIndexDrawOffset());
cmd->getIndexDrawOffset(), cmd->isWireframe());
_drawnVertices += cmd->getIndexDrawCount();
}
else
{
_commandBuffer->drawArrays(cmd->getPrimitiveType(), cmd->getVertexDrawStart(), cmd->getVertexDrawCount());
_commandBuffer->drawArrays(cmd->getPrimitiveType(), cmd->getVertexDrawStart(), cmd->getVertexDrawCount(),
cmd->isWireframe());
_drawnVertices += cmd->getVertexDrawCount();
}
_drawnBatches++;
Expand Down
8 changes: 6 additions & 2 deletions core/renderer/backend/CommandBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ class CommandBuffer : public axis::Ref
* @param count For each instance, the number of indexes to draw
* @see `drawElements(PrimitiveType primitiveType, IndexFormat indexType, unsigned int count, unsigned int offset)`
*/
virtual void drawArrays(PrimitiveType primitiveType, std::size_t start, std::size_t count) = 0;
virtual void drawArrays(PrimitiveType primitiveType,
std::size_t start,
std::size_t count,
bool wireframe = false) = 0;

/**
* Draw primitives with an index list.
Expand All @@ -167,7 +170,8 @@ class CommandBuffer : public axis::Ref
virtual void drawElements(PrimitiveType primitiveType,
IndexFormat indexType,
std::size_t count,
std::size_t offset) = 0;
std::size_t offset,
bool wireframe = false) = 0;

/**
* Do some resources release.
Expand Down
9 changes: 7 additions & 2 deletions core/renderer/backend/metal/CommandBufferMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ class CommandBufferMTL : public CommandBuffer
* @param start For each instance, the first index to draw
* @param count For each instance, the number of indexes to draw
* @see `drawElements(PrimitiveType primitiveType, IndexFormat indexType, unsigned int count, unsigned int offset)`
*
* TODO: Implement a wireframe mode for METAL devices. Refer to: https://forums.ogre3d.org/viewtopic.php?t=95089
*/
virtual void drawArrays(PrimitiveType primitiveType, std::size_t start, std::size_t count) override;
virtual void drawArrays(PrimitiveType primitiveType, std::size_t start, std::size_t count, bool wireframe) override;

/**
* Draw primitives with an index list.
Expand All @@ -154,11 +156,14 @@ class CommandBufferMTL : public CommandBuffer
* @param offset Byte offset within indexBuffer to start reading indexes from.
* @see `setIndexBuffer(Buffer* buffer)`
* @see `drawArrays(PrimitiveType primitiveType, unsigned int start, unsigned int count)`
*
* TODO: Implement a wireframe mode for METAL devices. Refer to: https://forums.ogre3d.org/viewtopic.php?t=95089
*/
virtual void drawElements(PrimitiveType primitiveType,
IndexFormat indexType,
std::size_t count,
std::size_t offset) override;
std::size_t offset,
bool wireframe) override;

/**
* Do some resources release.
Expand Down
5 changes: 3 additions & 2 deletions core/renderer/backend/metal/CommandBufferMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ inline int clamp(int value, int min, int max)
[_mtlIndexBuffer retain];
}

void CommandBufferMTL::drawArrays(PrimitiveType primitiveType, std::size_t start, std::size_t count)
void CommandBufferMTL::drawArrays(PrimitiveType primitiveType, std::size_t start, std::size_t count, bool wireframe /* unused */)
{
prepareDrawing();
[_mtlRenderEncoder drawPrimitives:toMTLPrimitive(primitiveType) vertexStart:start vertexCount:count];
Expand All @@ -299,7 +299,8 @@ inline int clamp(int value, int min, int max)
void CommandBufferMTL::drawElements(PrimitiveType primitiveType,
IndexFormat indexType,
std::size_t count,
std::size_t offset)
std::size_t offset,
bool wireframe /* unused */)
{
prepareDrawing();
[_mtlRenderEncoder drawIndexedPrimitives:toMTLPrimitive(primitiveType)
Expand Down
22 changes: 19 additions & 3 deletions core/renderer/backend/opengl/CommandBufferGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,40 @@ void CommandBufferGL::setProgramState(ProgramState* programState)
_programState = programState;
}

void CommandBufferGL::drawArrays(PrimitiveType primitiveType, std::size_t start, std::size_t count)
void CommandBufferGL::drawArrays(PrimitiveType primitiveType, std::size_t start, std::size_t count, bool wireframe)
{
prepareDrawing();
#ifdef AX_USE_GL // glPolygonMode is only supported in Desktop OpenGL
if (wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
#else
if (wireframe) primitiveType = PrimitiveType::LINE;
#endif
glDrawArrays(UtilsGL::toGLPrimitiveType(primitiveType), start, count);

#ifdef AX_USE_GL // glPolygonMode is only supported in Desktop OpenGL
if (wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
#endif
cleanResources();
}

void CommandBufferGL::drawElements(PrimitiveType primitiveType,
IndexFormat indexType,
std::size_t count,
std::size_t offset)
std::size_t offset,
bool wireframe)
{
prepareDrawing();
#ifdef AX_USE_GL // glPolygonMode is only supported in Desktop OpenGL
if (wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
#else
if (wireframe) primitiveType = PrimitiveType::LINE;
#endif
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer->getHandler());
glDrawElements(UtilsGL::toGLPrimitiveType(primitiveType), count, UtilsGL::toGLIndexType(indexType),
(GLvoid*)offset);
CHECK_GL_ERROR_DEBUG();
#ifdef AX_USE_GL // glPolygonMode is only supported in Desktop OpenGL
if (wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
#endif
cleanResources();
}

Expand Down
Loading