From be6ca1b6d2b141093b5be40e75158df56a4ea555 Mon Sep 17 00:00:00 2001 From: Nite Luo Date: Tue, 27 Jan 2015 17:05:51 -0800 Subject: [PATCH] add force depth write --- cocos/2d/CCNode.h | 2 +- cocos/3d/CCSprite3D.cpp | 10 ++++++++++ cocos/3d/CCSprite3D.h | 17 +++++++++++++++++ cocos/renderer/CCMeshCommand.cpp | 7 ++++++- cocos/renderer/CCMeshCommand.h | 1 + 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 89f8d8da82..703c81ad32 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -1122,7 +1122,7 @@ public: * * @return An Action pointer */ - Action* runAction(Action* action); + virtual Action* runAction(Action* action); /** * Stops and removes all actions from the running action list . diff --git a/cocos/3d/CCSprite3D.cpp b/cocos/3d/CCSprite3D.cpp index 1c28f4c13c..fdf69f6aef 100644 --- a/cocos/3d/CCSprite3D.cpp +++ b/cocos/3d/CCSprite3D.cpp @@ -741,6 +741,10 @@ void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) } //support tint and fade meshCommand.setDisplayColor(Vec4(color.r, color.g, color.b, color.a)); + if (_forceDepthWrite) + { + meshCommand.setDepthWriteEnabled(true); + } meshCommand.setTransparent(isTransparent); renderer->addCommand(&meshCommand); } @@ -801,6 +805,12 @@ const AABB& Sprite3D::getAABB() const return _aabb; } +Action* Sprite3D::runAction(Action *action) +{ + setForceDepthWrite(true); + return Node::runAction(action); +} + Rect Sprite3D::getBoundingBox() const { AABB aabb = getAABB(); diff --git a/cocos/3d/CCSprite3D.h b/cocos/3d/CCSprite3D.h index cec60085c7..b819c4471d 100644 --- a/cocos/3d/CCSprite3D.h +++ b/cocos/3d/CCSprite3D.h @@ -126,6 +126,22 @@ public: */ const AABB& getAABB() const; + /** + * Executes an action, and returns the action that is executed. For Sprite3D special logic are needed to take care of Fading. + * + * This node becomes the action's target. Refer to Action::getTarget() + * @warning Actions don't retain their target. + * + * @return An Action pointer + */ + virtual Action* runAction(Action* action) override; + + /** + * Force to write to depth buffer, this is useful if you want to achieve effects like fading. + */ + void setForceDepthWrite(bool value) { _forceDepthWrite = value; } + bool isForceDepthWrite() const { return _forceDepthWrite;}; + /** * Returns 2d bounding-box * Note: the bouding-box is just get from the AABB which as Z=0, so that is not very accurate. @@ -200,6 +216,7 @@ protected: bool _aabbDirty; unsigned int _lightMask; bool _shaderUsingLight; // is current shader using light ? + bool _forceDepthWrite; // Always write to depth buffer struct AsyncLoadParam { diff --git a/cocos/renderer/CCMeshCommand.cpp b/cocos/renderer/CCMeshCommand.cpp index 66d9625690..48685f2c64 100644 --- a/cocos/renderer/CCMeshCommand.cpp +++ b/cocos/renderer/CCMeshCommand.cpp @@ -153,6 +153,7 @@ void MeshCommand::setDepthTestEnabled(bool enable) void MeshCommand::setDepthWriteEnabled(bool enable) { + _forceDepthWrite = enable; _depthWriteEnabled = enable; } @@ -166,7 +167,11 @@ void MeshCommand::setTransparent(bool value) _isTransparent = value; //Skip batching for transparent mesh _skipBatching = value; - setDepthWriteEnabled(!_isTransparent); + + if (!_forceDepthWrite) + { + _depthWriteEnabled = true; + } } MeshCommand::~MeshCommand() diff --git a/cocos/renderer/CCMeshCommand.h b/cocos/renderer/CCMeshCommand.h index 867da76d2a..128c294913 100644 --- a/cocos/renderer/CCMeshCommand.h +++ b/cocos/renderer/CCMeshCommand.h @@ -127,6 +127,7 @@ protected: GLenum _cullFace; bool _depthTestEnabled; bool _depthWriteEnabled; + bool _forceDepthWrite; bool _renderStateCullFace; bool _renderStateDepthTest;