Post Effect Problem with cocos2d-x 3.7

Hi,

I’m trying to add post effect on 3D scenes with 2D rendering.

What I have done is:
I rendered 3D scene with the class RenderTexture, but Depth test has never been running.
Instead of using the class, I tried to call Depth test with setting ‘true’ at setDepthTest() function in Director class,
and Depth test was enabled at the time.
But this time, Depth test was enabled with not only 3D rendering but also 2D, and UI became invisible.
(It happened both windows and android, with cocos2d-x 3.7)

Here, are what I want to know:

Is it possible to render the mixed-file of 2D and 3D scenes with using RenderTexture command?

If 1) is YES, how could I solve the problems?

How I constructed is codes delow:

// HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
		// there's no 'id' in cpp, so we recommend returning the class instance pointer
		static cocos2d::Scene* createScene();

		// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
		virtual bool init();
		
		// a selector callback
		void menuCloseCallback(cocos2d::Ref* pSender);
		
		// implement the "static create()" method manually
		CREATE_FUNC(HelloWorld);

	NS_CC::RenderTexture* renderTexture;
	NS_CC::Node* root;
	NS_CC::Node* menu;

	virtual void visit( cocos2d::Renderer *renderer, const cocos2d::Mat4& parentTransform, uint32_t parentFlags )override;
};

#endif // __HELLOWORLD_SCENE_H__

// HelloWorldScene.cpp

#include "HelloWorldScene.h"
USING_NS_CC;

Scene* HelloWorld::createScene()
{
		auto scene = Scene::create();
		auto layer = HelloWorld::create();
		scene->addChild(layer);

		return scene;
}

bool HelloWorld::init()
{
		if ( !Layer::init() )
		{
				return false;
		}

		Size visibleSize = Director::getInstance()->getVisibleSize();
		Vec2 origin = Director::getInstance()->getVisibleOrigin();

		auto closeItem = MenuItemImage::create(
																					 "CloseNormal.png",
																					 "CloseSelected.png",
																					 CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

	closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
																origin.y + closeItem->getContentSize().height/2));

		auto menu = Menu::create(closeItem, NULL);
		menu->setPosition(Vec2::ZERO);
	this->menu = menu;
	menu->setGlobalZOrder(100.f);
		this->addChild(menu);

	// create root-node of scenes
	root = Node::create();
	addChild( root, 1 );

	// create render texture
	auto windsize = _director->getWinSize( );
	auto node = Node::create( );
	node->setPosition3D( Vec3( 0.f, 5.f, 100.f ) );
	addChild( node );
	auto camera = Camera::createPerspective( 60.f, windsize.width / windsize.height, 0.1f, 10000.f );
	camera->setCameraFlag( CameraFlag::USER1 );
	camera->setPosition3D( Vec3( 0.f, 0.f, 0.f ) );
	camera->lookAt( Vec3( 0.f, 7.f, 0.f ) );
	node->addChild( camera );

	renderTexture = RenderTexture::create( visibleSize.width, visibleSize.height, Texture2D::PixelFormat::RGBA8888, GL_DEPTH_COMPONENT16 );
	renderTexture->setAnchorPoint( Vec2::ANCHOR_BOTTOM_LEFT );
	renderTexture->setPosition( visibleSize * 0.5f );
	renderTexture->setKeepMatrix( true );
	addChild( renderTexture );


	// add 2D sprite with scenes
	auto sprite = Sprite::create( "HelloWorld.png" );
	sprite->setPositionY( -6.f );
	sprite->setScale( 0.3f );
	root->addChild( sprite );

	// add 3D sprite with scenes
	auto model = Sprite3D::create( "chara/ma001_00.c3b" );
	auto animation = Animation3D::create( "chara/aa001_53.c3b" );
	auto animate = Animate3D::create( animation );
	root->addChild( model, 1 );
	model->setScale( 3.4f );
	model->runAction( RepeatForever::create( animate ) );
	root->setCameraMask( 2 );

	camera->runAction( RepeatForever::create( Sequence::createWithTwoActions( MoveTo::create( 9.f, Vec3( -50.f, 8.f, -35.f ) ), MoveTo::create( 9.f, Vec3( 50.f, 8.f, -35.f ) ) ) ) );

	_director->setDepthTest( true );

		return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
	renderTexture->getSprite()->setColor( Color3B::GREEN );
}

void HelloWorld::visit( Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags )
{

	auto camera = Camera::getVisitingCamera( );
	bool visibleByCamera = camera->getCameraFlag() == CameraFlag::USER1;

	if (visibleByCamera)
	{
		// add GroupCommand on default render queue
		// -- group 1 --
		// add RenderTexture::onClear on RenderTexture::onBegin in the negavive group of render queue
		renderTexture->setGlobalZOrder( -1.f );
		renderTexture->beginWithClear( 0.f, 0.f, 0.f, 1.f, 1.f, 0.f );
		// add draw command of sprite 3D and sprite 2D
		root->visit( renderer, parentTransform, parentFlags );
		// add RenderTexture::onEnd on positive group
		renderTexture->setGlobalZOrder( 1.f );
		renderTexture->end();
	}
	else
	{
		static CustomCommand clear;
		clear.func = []{
				Director::getInstance()->setDepthTest(false);
		};
		clear.init(0.f	);
		renderer->addCommand( &clear );

		// add renderTexture and menu command on default render queue
		renderTexture->setGlobalZOrder( 0.f );
		renderTexture->visit( renderer, parentTransform, parentFlags );
		menu->visit( renderer, parentTransform, parentFlags );

		static CustomCommand clear2;
		clear2.func = []{
				Director::getInstance()->setDepthTest(true);
		};
		clear2.init(0.f	);
		renderer->addCommand( &clear2 );
	}
}
1 Like