mirror of https://github.com/axmolengine/axmol.git
Merge pull request #10608 from super626/v3
Add project function to camera and fix cocos ui bug
This commit is contained in:
commit
b973d08b6e
|
@ -240,10 +240,28 @@ bool Camera::initOrthographic(float zoomX, float zoomY, float nearPlane, float f
|
|||
return true;
|
||||
}
|
||||
|
||||
void Camera::unproject(const Size& viewport, Vec3* src, Vec3* dst) const
|
||||
Vec2 Camera::project(const Vec3& src) const
|
||||
{
|
||||
assert(dst);
|
||||
Vec4 screen(src->x / viewport.width, ((viewport.height - src->y)) / viewport.height, src->z, 1.0f);
|
||||
Vec2 screenPos;
|
||||
|
||||
auto viewport = Director::getInstance()->getWinSize();
|
||||
Vec4 clipPos;
|
||||
getViewProjectionMatrix().transformVector(Vec4(src.x, src.y, src.z, 1.0f), &clipPos);
|
||||
|
||||
CCASSERT(clipPos.w != 0.0f, "");
|
||||
float ndcX = clipPos.x / clipPos.w;
|
||||
float ndcY = clipPos.y / clipPos.w;
|
||||
|
||||
screenPos.x = (ndcX + 1.0f) * 0.5f * viewport.width;
|
||||
screenPos.y = (1.0f - (ndcY + 1.0f) * 0.5f) * viewport.height;
|
||||
return screenPos;
|
||||
}
|
||||
|
||||
Vec3 Camera::unproject(const Vec3& src) const
|
||||
{
|
||||
auto viewport = Director::getInstance()->getWinSize();
|
||||
|
||||
Vec4 screen(src.x / viewport.width, ((viewport.height - src.y)) / viewport.height, src.z, 1.0f);
|
||||
screen.x = screen.x * 2.0f - 1.0f;
|
||||
screen.y = screen.y * 2.0f - 1.0f;
|
||||
screen.z = screen.z * 2.0f - 1.0f;
|
||||
|
@ -256,7 +274,13 @@ void Camera::unproject(const Size& viewport, Vec3* src, Vec3* dst) const
|
|||
screen.z /= screen.w;
|
||||
}
|
||||
|
||||
dst->set(screen.x, screen.y, screen.z);
|
||||
return Vec3(screen.x, screen.y, screen.z);
|
||||
}
|
||||
|
||||
void Camera::unproject(const Size& viewport, const Vec3* src, Vec3* dst) const
|
||||
{
|
||||
CCASSERT(src && dst, "vec3 can not be null");
|
||||
*dst = unproject(*src);
|
||||
}
|
||||
|
||||
bool Camera::isVisibleInFrustum(const AABB* aabb) const
|
||||
|
|
|
@ -126,11 +126,30 @@ public:
|
|||
|
||||
/**get view projection matrix*/
|
||||
const Mat4& getViewProjectionMatrix() const;
|
||||
|
||||
/* convert the specified point of viewport from world-space coordinates into the screen-space coordinates.
|
||||
*
|
||||
* @param src The world-space position.
|
||||
* @return The screen-space position.
|
||||
*/
|
||||
Vec2 project(const Vec3& src) const;
|
||||
|
||||
/**
|
||||
* Convert the specified point of viewport from screen-space coordinate into the world-space coordinate.
|
||||
*
|
||||
* @param src The screen-space position.
|
||||
* @return The world-space position.
|
||||
*/
|
||||
Vec3 unproject(const Vec3& src) const;
|
||||
|
||||
/**
|
||||
* Convert the specified point of viewport from screenspace coordinate into the worldspace coordinate.
|
||||
*/
|
||||
void unproject(const Size& viewport, Vec3* src, Vec3* dst) const;
|
||||
* Convert the specified point of viewport from screen-space coordinate into the world-space coordinate.
|
||||
*
|
||||
* @param viewport The viewport size to use.
|
||||
* @param src The screen-space position.
|
||||
* @param dst The world-space position.
|
||||
*/
|
||||
void unproject(const Size& viewport, const Vec3* src, Vec3* dst) const;
|
||||
|
||||
/**
|
||||
* Is this aabb visible in frustum
|
||||
|
|
|
@ -1575,7 +1575,7 @@ public:
|
|||
|
||||
/** get & set camera mask, the node is visible by the camera whose camera flag & node's camera mask is true */
|
||||
unsigned short getCameraMask() const { return _cameraMask; }
|
||||
void setCameraMask(unsigned short mask, bool applyChildren = true);
|
||||
virtual void setCameraMask(unsigned short mask, bool applyChildren = true);
|
||||
|
||||
CC_CONSTRUCTOR_ACCESS:
|
||||
// Nodes should be created using create();
|
||||
|
|
|
@ -431,4 +431,17 @@ void ProtectedNode::disableCascadeOpacity()
|
|||
}
|
||||
}
|
||||
|
||||
void ProtectedNode::setCameraMask(unsigned short mask, bool applyChildren)
|
||||
{
|
||||
Node::setCameraMask(mask, applyChildren);
|
||||
if (applyChildren)
|
||||
{
|
||||
for (auto& iter: _protectedChildren)
|
||||
{
|
||||
iter->setCameraMask(mask);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -163,6 +163,7 @@ public:
|
|||
virtual void updateDisplayedColor(const Color3B& parentColor) override;
|
||||
virtual void disableCascadeColor() override;
|
||||
virtual void disableCascadeOpacity()override;
|
||||
virtual void setCameraMask(unsigned short mask, bool applyChildren = true) override;
|
||||
CC_CONSTRUCTOR_ACCESS:
|
||||
ProtectedNode();
|
||||
virtual ~ProtectedNode();
|
||||
|
|
|
@ -7266,6 +7266,20 @@ int lua_cocos2dx_Camera_unproject(lua_State* tolua_S)
|
|||
#endif
|
||||
|
||||
argc = lua_gettop(tolua_S)-1;
|
||||
if (argc == 1)
|
||||
{
|
||||
cocos2d::Vec3 arg0;
|
||||
|
||||
ok &= luaval_to_vec3(tolua_S, 2, &arg0, "cc.Camera:project");
|
||||
if(!ok)
|
||||
{
|
||||
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_Camera_project'", nullptr);
|
||||
return 0;
|
||||
}
|
||||
auto ret = cobj->unproject(arg0);
|
||||
vec3_to_luaval(tolua_S, ret);
|
||||
return 1;
|
||||
}
|
||||
if (argc == 3)
|
||||
{
|
||||
cocos2d::Size arg0;
|
||||
|
|
|
@ -1335,4 +1335,17 @@ namespace ui {
|
|||
return this->getScaleX();
|
||||
}
|
||||
|
||||
void Scale9Sprite::setCameraMask(unsigned short mask, bool applyChildren)
|
||||
{
|
||||
Node::setCameraMask(mask, applyChildren);
|
||||
|
||||
if(_scale9Image)
|
||||
_scale9Image->setCameraMask(mask,applyChildren);
|
||||
|
||||
for(auto& iter: _protectedChildren)
|
||||
{
|
||||
iter->setCameraMask(mask);
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
|
|
|
@ -376,6 +376,7 @@ namespace ui {
|
|||
virtual float getScaleY() const override;
|
||||
virtual float getScale() const override;
|
||||
using Node::getScaleZ;
|
||||
virtual void setCameraMask(unsigned short mask, bool applyChildren = true) override;
|
||||
protected:
|
||||
void updateCapInset();
|
||||
void updatePositions();
|
||||
|
|
|
@ -563,8 +563,8 @@ void Camera3DTestDemo::onTouchesEnded(const std::vector<Touch*>& touches, cocos2
|
|||
Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
|
||||
|
||||
auto size = Director::getInstance()->getWinSize();
|
||||
_camera->unproject(size, &nearP, &nearP);
|
||||
_camera->unproject(size, &farP, &farP);
|
||||
nearP = _camera->unproject(nearP);
|
||||
farP = _camera->unproject(farP);
|
||||
Vec3 dir(farP - nearP);
|
||||
float dist=0.0f;
|
||||
float ndd = Vec3::dot(Vec3(0,1,0),dir);
|
||||
|
@ -1000,30 +1000,30 @@ void CameraCullingDemo::drawCameraFrustum()
|
|||
// top-left
|
||||
Vec3 tl_0,tl_1;
|
||||
Vec3 src(0,0,0);
|
||||
_cameraFirst->unproject(size, &src, &tl_0);
|
||||
tl_0 = _cameraFirst->unproject(src);
|
||||
src = Vec3(0,0,1);
|
||||
_cameraFirst->unproject(size, &src, &tl_1);
|
||||
tl_1 = _cameraFirst->unproject(src);
|
||||
|
||||
// top-right
|
||||
Vec3 tr_0,tr_1;
|
||||
src = Vec3(size.width,0,0);
|
||||
_cameraFirst->unproject(size, &src, &tr_0);
|
||||
tr_0 = _cameraFirst->unproject(src);
|
||||
src = Vec3(size.width,0,1);
|
||||
_cameraFirst->unproject(size, &src, &tr_1);
|
||||
tr_1 = _cameraFirst->unproject(src);
|
||||
|
||||
// bottom-left
|
||||
Vec3 bl_0,bl_1;
|
||||
src = Vec3(0,size.height,0);
|
||||
_cameraFirst->unproject(size, &src, &bl_0);
|
||||
bl_0 = _cameraFirst->unproject(src);
|
||||
src = Vec3(0,size.height,1);
|
||||
_cameraFirst->unproject(size, &src, &bl_1);
|
||||
bl_1 = _cameraFirst->unproject(src);
|
||||
|
||||
// bottom-right
|
||||
Vec3 br_0,br_1;
|
||||
src = Vec3(size.width,size.height,0);
|
||||
_cameraFirst->unproject(size, &src, &br_0);
|
||||
br_0 = _cameraFirst->unproject(src);
|
||||
src = Vec3(size.width,size.height,1);
|
||||
_cameraFirst->unproject(size, &src, &br_1);
|
||||
br_1 = _cameraFirst->unproject(src);
|
||||
|
||||
_drawFrustum->drawLine(tl_0, tl_1, color);
|
||||
_drawFrustum->drawLine(tr_0, tr_1, color);
|
||||
|
|
|
@ -650,8 +650,8 @@ void Sprite3DFakeShadowTest::onTouchesEnded(const std::vector<Touch*>& touches,
|
|||
Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
|
||||
|
||||
auto size = Director::getInstance()->getWinSize();
|
||||
_camera->unproject(size, &nearP, &nearP);
|
||||
_camera->unproject(size, &farP, &farP);
|
||||
nearP = _camera->unproject(nearP);
|
||||
farP = _camera->unproject(farP);
|
||||
Vec3 dir(farP - nearP);
|
||||
float dist=0.0f;
|
||||
float ndd = Vec3::dot(Vec3(0,1,0),dir);
|
||||
|
|
|
@ -223,10 +223,9 @@ function Camera3DTestDemo:onEnter()
|
|||
if self._camera ~= nil and self._sprite3D ~= nil and self._cameraType == CameraType.ThirdCamera then
|
||||
local nearP = cc.vec3(location.x, location.y, -1.0)
|
||||
local farP = cc.vec3(location.x, location.y, 1.0)
|
||||
|
||||
local size = cc.Director:getInstance():getWinSize()
|
||||
nearP = self._camera:unproject(size, nearP, nearP)
|
||||
farP = self._camera:unproject(size, farP, farP)
|
||||
|
||||
nearP = self._camera:unproject(nearP)
|
||||
farP = self._camera:unproject(farP)
|
||||
local dir = cc.vec3(farP.x - nearP.x, farP.y - nearP.y, farP.z - nearP.z)
|
||||
local dist=0.0
|
||||
local ndd = dir.x * 0 + dir.y * 1 + dir.z * 0
|
||||
|
|
Loading…
Reference in New Issue