mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
Merge pull request #25 from caryliu1999/v2.3.0-youyou-3dModel
Fix renderer error in shadow stage.
This commit is contained in:
@@ -59,7 +59,7 @@ public:
|
||||
_data.clear();
|
||||
}
|
||||
|
||||
const T* getData(const int index) const
|
||||
const T* getData(size_t index) const
|
||||
{
|
||||
if (index >= _count)
|
||||
{
|
||||
@@ -70,7 +70,7 @@ public:
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
const int getLength() const
|
||||
const size_t getLength() const
|
||||
{
|
||||
return _count;
|
||||
}
|
||||
@@ -117,7 +117,7 @@ public:
|
||||
_count -= 1;
|
||||
}
|
||||
private:
|
||||
int _count = 0;
|
||||
size_t _count = 0;
|
||||
RecycleFunc _func;
|
||||
std::vector<T*> _data;
|
||||
|
||||
|
||||
@@ -135,7 +135,7 @@ void BaseRenderer::render(const View& view, const Scene* scene)
|
||||
std::vector<StageItem> stageItems;
|
||||
for (const auto& stage : view.stages)
|
||||
{
|
||||
for (int i = 0, len = _drawItems->getLength(); i < len; i++)
|
||||
for (size_t i = 0, len = _drawItems->getLength(); i < len; i++)
|
||||
{
|
||||
const DrawItem* item = _drawItems->getData(i);
|
||||
auto tech = item->effect->getTechnique(stage);
|
||||
@@ -158,7 +158,7 @@ void BaseRenderer::render(const View& view, const Scene* scene)
|
||||
|
||||
// render stages
|
||||
std::unordered_map<std::string, StageCallback>::iterator foundIter;
|
||||
for (int i = 0, len = _stageInfos->getLength(); i < len; i++)
|
||||
for (size_t i = 0, len = _stageInfos->getLength(); i < len; i++)
|
||||
{
|
||||
const StageInfo* stageInfo = _stageInfos->getData(i);
|
||||
foundIter = _stage2fn.find(stageInfo->stage);
|
||||
|
||||
@@ -30,10 +30,6 @@ RENDERER_BEGIN
|
||||
|
||||
Camera::Camera()
|
||||
{
|
||||
_cachedView.color = _color;
|
||||
_cachedView.depth = _depth;
|
||||
_cachedView.clearFlags = _clearFlags;
|
||||
_cachedView.stencil = _stencil;
|
||||
}
|
||||
|
||||
Camera::~Camera()
|
||||
@@ -47,8 +43,6 @@ void Camera::setFrameBuffer(FrameBuffer* framebuffer)
|
||||
RENDERER_SAFE_RELEASE(_framebuffer);
|
||||
_framebuffer = framebuffer;
|
||||
RENDERER_SAFE_RETAIN(_framebuffer);
|
||||
|
||||
_cachedView.frameBuffer = _framebuffer;
|
||||
}
|
||||
|
||||
void Camera::setWorldMatrix(const Mat4& worldMatrix)
|
||||
@@ -64,25 +58,21 @@ void Camera::setWorldMatrix(const Mat4& worldMatrix)
|
||||
void Camera::setColor(float r, float g, float b, float a)
|
||||
{
|
||||
_color.set(r, g, b, a);
|
||||
_cachedView.color = _color;
|
||||
}
|
||||
|
||||
void Camera::setDepth(float depth)
|
||||
{
|
||||
_depth = depth;
|
||||
_cachedView.depth = _depth;
|
||||
}
|
||||
|
||||
void Camera::setStencil(int stencil)
|
||||
{
|
||||
_stencil = stencil;
|
||||
_cachedView.stencil = _stencil;
|
||||
}
|
||||
|
||||
void Camera::setClearFlags(uint8_t flags )
|
||||
{
|
||||
_clearFlags = flags;
|
||||
_cachedView.clearFlags = _clearFlags;
|
||||
}
|
||||
|
||||
void Camera::setRect(float x, float y, float w, float h)
|
||||
@@ -93,7 +83,6 @@ void Camera::setRect(float x, float y, float w, float h)
|
||||
void Camera::setStages(const std::vector<std::string>& stages)
|
||||
{
|
||||
_stages = stages;
|
||||
_cachedView.stages = _stages;
|
||||
}
|
||||
|
||||
|
||||
@@ -110,41 +99,47 @@ void Camera::setNode(NodeProxy* node)
|
||||
}
|
||||
}
|
||||
|
||||
const View& Camera::extractView( int width, int height)
|
||||
void Camera::extractView(View& out, int width, int height)
|
||||
{
|
||||
// can optimize it if node's position is not changed
|
||||
|
||||
// rect
|
||||
_cachedView.rect.set(_rect.x * width,
|
||||
out.rect.set(_rect.x * width,
|
||||
_rect.y * height,
|
||||
_rect.w * width,
|
||||
_rect.h * height);
|
||||
|
||||
// clear opts
|
||||
out.color = _color;
|
||||
out.depth = _depth;
|
||||
out.stencil = _stencil;
|
||||
out.clearFlags = _clearFlags;
|
||||
|
||||
// view matrix
|
||||
//REFINE:
|
||||
_node->getWorldRT(&_worldRTInv);
|
||||
_cachedView.matView.set(_worldRTInv.getInversed());
|
||||
out.matView.set(_worldRTInv.getInversed());
|
||||
|
||||
// projecton matrix
|
||||
float aspect = (float)width / height;
|
||||
if (ProjectionType::PERSPECTIVE == _projection)
|
||||
Mat4::createPerspective(_fov / 3.1415926 * 180, aspect, _near, _far, &_cachedView.matProj);
|
||||
Mat4::createPerspective(_fov / 3.1415926 * 180, aspect, _near, _far, &out.matProj);
|
||||
else
|
||||
{
|
||||
float x = _orthoHeight * aspect;
|
||||
float y = _orthoHeight;
|
||||
Mat4::createOrthographicOffCenter(-x, x, -y, y, _near, _far, &_cachedView.matProj);
|
||||
Mat4::createOrthographicOffCenter(-x, x, -y, y, _near, _far, &out.matProj);
|
||||
}
|
||||
|
||||
// view projection
|
||||
Mat4::multiply(_cachedView.matProj, _cachedView.matView, &_cachedView.matViewProj);
|
||||
_cachedView.matInvViewPorj.set(_cachedView.matViewProj.getInversed());
|
||||
Mat4::multiply(out.matProj, out.matView, &out.matViewProj);
|
||||
out.matInvViewPorj.set(out.matViewProj.getInversed());
|
||||
|
||||
// stages & framebuffer
|
||||
out.stages = _stages;
|
||||
out.frameBuffer = _framebuffer;
|
||||
|
||||
// culling mask
|
||||
_cachedView.cullingMask = _cullingMask;
|
||||
_cachedView.cullingByID = true;
|
||||
|
||||
return _cachedView;
|
||||
out.cullingMask = _cullingMask;
|
||||
out.cullingByID = true;
|
||||
}
|
||||
|
||||
Vec3& Camera::screenToWorld(Vec3& out, const Vec3& screenPos, int width, int height) const
|
||||
|
||||
@@ -177,7 +177,7 @@ public:
|
||||
/**
|
||||
* @brief Extracts the camera info to view.
|
||||
*/
|
||||
const View& extractView(int width, int height);
|
||||
void extractView(View& view, int width, int height);
|
||||
/**
|
||||
* @brief Transform a screen position to world in the current camera projection.
|
||||
*/
|
||||
@@ -228,8 +228,7 @@ private:
|
||||
|
||||
Mat4 _worldRTInv;
|
||||
Vec3 _worldPos;
|
||||
|
||||
View _cachedView;
|
||||
|
||||
int _priority = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -83,8 +83,16 @@ void ForwardRenderer::render(Scene* scene)
|
||||
updateLights(scene);
|
||||
scene->sortCameras();
|
||||
auto& cameras = scene->getCameras();
|
||||
for (auto camera : cameras)
|
||||
BaseRenderer::render(camera->extractView(_width, _height), scene);
|
||||
for (auto& camera : cameras)
|
||||
{
|
||||
View* view = requestView();
|
||||
camera->extractView(*view, _width, _height);
|
||||
}
|
||||
|
||||
for (size_t i = 0, len = _views->getLength(); i < len; ++i) {
|
||||
const View* view = _views->getData(i);
|
||||
BaseRenderer::render(*view, scene);
|
||||
}
|
||||
|
||||
scene->removeModels();
|
||||
}
|
||||
@@ -99,8 +107,9 @@ void ForwardRenderer::renderCamera(Camera* camera, Scene* scene)
|
||||
width = fb->getWidth();
|
||||
height = fb->getHeight();
|
||||
}
|
||||
View view = camera->extractView(width, height);
|
||||
BaseRenderer::render(view, scene);
|
||||
View* view = requestView();
|
||||
camera->extractView(*view, width, height);
|
||||
BaseRenderer::render(*view, scene);
|
||||
|
||||
scene->removeModels();
|
||||
}
|
||||
@@ -367,6 +376,7 @@ void ForwardRenderer::drawItems(const std::vector<StageItem>& items)
|
||||
slots.reserve(count);
|
||||
for (auto item : items)
|
||||
{
|
||||
shadowMaps.clear();
|
||||
for(int i = 0; i < count; i++)
|
||||
{
|
||||
Light* light = _shadowLights.at(i);
|
||||
|
||||
@@ -197,7 +197,7 @@ void Light::generateShadowMap(DeviceGraphics* device)
|
||||
Texture2D::Options options;
|
||||
options.width = _shadowResolution;
|
||||
options.height = _shadowResolution;
|
||||
// options.format = Texture2D::Format::RGBA8;
|
||||
options.glFormat = GL_RGBA;
|
||||
options.wrapS = Texture2D::WrapMode::CLAMP;
|
||||
options.wrapT = Texture2D::WrapMode::CLAMP;
|
||||
_shadowMap->init(device, options);
|
||||
|
||||
@@ -1568,6 +1568,7 @@ bool ccvaluevector_to_EffectPass(const cocos2d::ValueVector& v, cocos2d::Vector<
|
||||
bool depthTest = false, depthWrite = false;
|
||||
cocos2d::renderer::DepthFunc depthFunc = cocos2d::renderer::DepthFunc::LESS;
|
||||
|
||||
bool stencilTest = false;
|
||||
uint8_t stencilMaskFront = 0xff, stencilWriteMaskFront = 0xff, stencilMaskBack = 0xff, stencilWriteMaskBack = 0xff;
|
||||
uint32_t stencilRefFront = 0, stencilRefBack = 0;
|
||||
cocos2d::renderer::StencilFunc stencilFuncFront = cocos2d::renderer::StencilFunc::ALWAYS, stencilFuncBack = cocos2d::renderer::StencilFunc::ALWAYS;
|
||||
@@ -1594,6 +1595,11 @@ bool ccvaluevector_to_EffectPass(const cocos2d::ValueVector& v, cocos2d::Vector<
|
||||
depthFunc = static_cast<cocos2d::renderer::DepthFunc>(state.at("depthFunc").asUnsignedInt());
|
||||
}
|
||||
|
||||
if(state.find("stencilTest") != state.end())
|
||||
{
|
||||
stencilTest = state.at("stencilTest").asBool();
|
||||
}
|
||||
|
||||
// front
|
||||
if(state.find("stencilFuncFront") != state.end())
|
||||
{
|
||||
@@ -1667,10 +1673,13 @@ bool ccvaluevector_to_EffectPass(const cocos2d::ValueVector& v, cocos2d::Vector<
|
||||
}
|
||||
|
||||
cobj->setDepth(depthTest, depthWrite, depthFunc); // depth func
|
||||
// stencil front
|
||||
cobj->setStencilFront(stencilFuncFront, stencilRefFront, stencilMaskFront, stencilFailOpFront, stencilZFailOpFront, stencilZPassOpFront, stencilWriteMaskFront);
|
||||
// stencil back
|
||||
cobj->setStencilBack(stencilFuncBack, stencilRefBack, stencilMaskBack, stencilFailOpBack, stencilZFailOpBack, stencilZPassOpBack, stencilWriteMaskBack);
|
||||
|
||||
if (stencilTest) {
|
||||
// stencil front
|
||||
cobj->setStencilFront(stencilFuncFront, stencilRefFront, stencilMaskFront, stencilFailOpFront, stencilZFailOpFront, stencilZPassOpFront, stencilWriteMaskFront);
|
||||
// stencil back
|
||||
cobj->setStencilBack(stencilFuncBack, stencilRefBack, stencilMaskBack, stencilFailOpBack, stencilZFailOpBack, stencilZPassOpBack, stencilWriteMaskBack);
|
||||
}
|
||||
}
|
||||
|
||||
cobj->autorelease();
|
||||
|
||||
@@ -562,15 +562,24 @@ SE_BIND_FUNC(js_renderer_Effect_init);
|
||||
static bool js_renderer_CustomProperties_setProperty(se::State& s)
|
||||
{
|
||||
cocos2d::renderer::CustomProperties* cobj = (cocos2d::renderer::CustomProperties*)s.nativeThisObject();
|
||||
CC_UNUSED bool ok = true;
|
||||
SE_PRECONDITION2(cobj, false, "js_renderer_CustomProperties_setProperty : Invalid Native Object");
|
||||
const auto& args = s.args();
|
||||
std::string arg0;
|
||||
cocos2d::renderer::Technique::Parameter arg1;
|
||||
ok &= seval_to_std_string(args[0], &arg0);
|
||||
ok &= seval_to_TechniqueParameter(args[1], &arg1);
|
||||
SE_PRECONDITION2(ok, false, "js_renderer_CustomProperties_setProperty : Error processing arguments");
|
||||
cobj->setProperty(arg0, arg1);
|
||||
return true;
|
||||
size_t argc = args.size();
|
||||
CC_UNUSED bool ok = true;
|
||||
if (argc == 3) {
|
||||
std::string arg0;
|
||||
ok &= seval_to_std_string(args[0], &arg0);
|
||||
SE_PRECONDITION2(ok, false, "js_renderer_CustomProperties_setProperty : Name Error");
|
||||
std::uint8_t arg1;
|
||||
ok &= seval_to_uint8(args[1], &arg1);
|
||||
cocos2d::renderer::Technique::Parameter arg2(arg0, static_cast<cocos2d::renderer::Technique::Parameter::Type>(arg1));
|
||||
ok &= seval_to_TechniqueParameter_not_constructor(args[2], &arg2);
|
||||
SE_PRECONDITION2(ok, false, "js_renderer_CustomProperties_setProperty : Error processing arguments");
|
||||
cobj->setProperty(arg0, arg2);
|
||||
return true;
|
||||
}
|
||||
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 2);
|
||||
return false;
|
||||
}
|
||||
SE_BIND_FUNC(js_renderer_CustomProperties_setProperty);
|
||||
|
||||
@@ -616,6 +625,8 @@ bool jsb_register_renderer_manual(se::Object* global)
|
||||
|
||||
// Effect
|
||||
__jsb_cocos2d_renderer_Effect_proto->defineFunction("init", _SE(js_renderer_Effect_init));
|
||||
// CustomProperties
|
||||
__jsb_cocos2d_renderer_CustomProperties_proto->defineFunction("setProperty", _SE(js_renderer_CustomProperties_setProperty));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user