创建超出屏幕大小的RenderTexture时有部分区域画不了

举个例子,设置的屏幕大小和分辨率都是800480,然后我创建一个700600的RenderTexture,那么会有一部分区域调用visit是印不到RenderTexture上的,这个区域大小是700*120(会比120小一点,取决于调用visit的节点的大小),而且这个区域是在RenderTexture的顶部。上图好理解一些。

PS:我的cocos2dx版本是3.1.1

求解析或者解决方案啊!!!

同求:7::7::7:

:12: :12: :12:

直接上代或者测试例~

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);

bool touchBegin (cocos2d::Touch *pTouch, cocos2d::Event *pEvent);
void touchMoved (cocos2d::Touch *pTouch, cocos2d::Event *pEvent);
void touchEnded (cocos2d::Touch *pTouch, cocos2d::Event *pEvent);

void drawLine (cocos2d::Vec2 &start, cocos2d::Vec2 &end);
virtual void onEnter ();
virtual void onExit ();

CREATE_FUNC(HelloWorld);

private:
cocos2d::Sprite *m_pBrush;
cocos2d::Vectorcocos2d::Sprite* _brushs;
};

#endif // HELLOWORLD_SCENE_H

HelloWorldScene.cpp

#include “HelloWorldScene.h”
#include “iostream”

USING_NS_CC;
using namespace std;

Scene* HelloWorld::createScene()
{
// ‘scene’ is an autorelease object
auto scene = Scene::create();

// 'layer' is an autorelease object
auto layer = HelloWorld::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;

}

// on “init” you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}

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

auto renderTexture = RenderTexture::create (visibleSize.width - 100, visibleSize.height + 400);
this->addChild (renderTexture);
renderTexture->setTag (1001);
renderTexture->setPosition (visibleSize.width / 2, visibleSize.height / 2 - 200);
renderTexture->clear (255, 0, 0, 255);

Sprite * sprite = Sprite::create("CloseNormal.png");
float y = visibleSize.height - 400 + sprite->getContentSize ().height;

auto drawNode = DrawNode::create ();
this->addChild (drawNode);
drawNode->drawSegment (Vec2 (0, y), Vec2 (visibleSize.width, y),
    2.0f, Color4F::BLUE);

return true;

}

void HelloWorld::onEnter ()
{
Layer::onEnter ();
auto listener = EventListenerTouchOneByOne::create ();
listener->onTouchBegan = ] (Touch *pTouch, Event *pEvent)
{
return true;
};
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::touchMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority (listener, this);
}

void HelloWorld::onExit ()
{
Layer::onExit ();

}

void HelloWorld::touchMoved (Touch *pTouch, Event *pEvent)
{
auto start = pTouch->getLocation();
start.y += 400;
auto end = pTouch->getPreviousLocation();
end.y += 400;
drawLine (start, end);
}

void HelloWorld::drawLine (cocos2d::Vec2 &start, cocos2d::Vec2 &end)
{
auto renderTexture = dynamic_cast<RenderTexture*>(this->getChildByTag (1001));
renderTexture->begin();
float distance = start.getDistance(end);
if (distance > 1)
{
int d = (int)distance;
_brushs.clear();
for(int i = 0; i < d; ++i)
{
Sprite * sprite = Sprite::create(“CloseNormal.png”);
_brushs.pushBack(sprite);
}
for (int i = 0; i < d; i++)
{
float difx = end.x - start.x;
float dify = end.y - start.y;
float delta = (float)i / distance;
_brushs.at(i)->setPosition(Vec2(start.x + (difx * delta), start.y + (dify * delta)));

        _brushs.at(i)->visit();
    }
}

renderTexture->end();

}

void HelloWorld::touchEnded (Touch *pTouch, Event *pEvent)
{

}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox(“You pressed the close button. Windows Store Apps do not implement a close button.”,“Alert”);
return;
#endif

Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}

效果如下图:

触摸滑动在蓝线之下的能正常画出线来,触摸滑动的范围在蓝线之上的就画不出来了。

你的rendertexture本来就没包含这块地方,当然不会画出来,没明白你的需求

怎么没包含?我都特意clear将RenderTexture的范围变红色了,没看到上面也变红色了吗?需求和这个Demo差不多,就是画线,但是需要RenderTexture能超出屏幕范围。

明白了,我记得某个地方看到过,现在sprite绘制会判断是否在屏幕范围内,如果不在屏幕范围内,不进行绘制,你可能要去修改下源码或者求助大佬们了

看了下,应该就是在Sprite::draw这里了,renderer->checkVisibility(transform, _contentSize)

噢,我看看,谢谢啦!

兄弟,你搞定了么? 我也碰到相同的问题了,但是貌似改这个checkVisibility没用啊

把这句代码注释掉就可以了。_insideBounds = transformUpdated ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;

1赞

多谢!等下试试看!