Skip to content

Commit

Permalink
add frustum culling functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed May 27, 2024
1 parent d36e187 commit d546f93
Show file tree
Hide file tree
Showing 41 changed files with 583 additions and 194 deletions.
3 changes: 3 additions & 0 deletions Assets/Script/Lib/Dora/en/Director.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ local record Director
-- The current active camera in Director's camera stack.
const currentCamera: Camera

-- The flag to enable or disable frustum culling.
frustumCulling: boolean

-- Adds a new camera to Director's camera stack and sets it to the current camera.
-- @param camera (Camera) The camera to add.
pushCamera: function(self: Director, camera: Camera)
Expand Down
5 changes: 5 additions & 0 deletions Assets/Script/Lib/Dora/en/Dora.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3625,6 +3625,11 @@ class Director {
*/
readonly currentCamera: Camera;

/**
* Whether to enable frustum culling.
*/
frustumCulling: boolean;

/**
* Adds a new camera to Director's camera stack and sets it to the current camera.
* @param camera The camera to add.
Expand Down
3 changes: 3 additions & 0 deletions Assets/Script/Lib/Dora/zh-Hans/Director.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ local record Director
-- 当前在应用的相机。
const currentCamera: Camera

-- 是否启用视锥体裁剪。
frustumCulling: boolean

-- 添加新相机到相机堆栈中,并设置为当前应用的相机。
-- @param camera (Camera) 要添加的相机。
pushCamera: function(self: Director, camera: Camera)
Expand Down
5 changes: 5 additions & 0 deletions Assets/Script/Lib/Dora/zh-Hans/Dora.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3629,6 +3629,11 @@ class Director {
*/
readonly currentCamera: Camera;

/**
* 是否启用视锥体裁剪。
*/
frustumCulling: boolean;

/**
* 向Director的摄像机堆栈添加新摄像机,并将其设置为当前摄像机。
* @param camera 要添加的摄像机。
Expand Down
13 changes: 13 additions & 0 deletions Docs/docs/api/Class/Director.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ const postScheduler: Scheduler
const currentCamera: Camera
```

## frustumCulling

**Type:** Field.

**Description:**

  The flag to enable or disable frustum culling.

**Signature:**
```tl
frustumCulling: boolean
```

## pushCamera

**Type:** Function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ const postScheduler: Scheduler
const currentCamera: Camera
```

## frustumCulling

**类型:** 成员变量。

**描述:**

  是否启用视锥体裁剪。

**签名:**
```tl
frustumCulling: boolean
```

## pushCamera

**类型:** 函数。
Expand Down
4 changes: 2 additions & 2 deletions Projects/Android/Dora/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "org.ippclub.dorassr"
minSdkVersion 28
targetSdkVersion 34
versionCode 59
versionName "1.4.2"
versionCode 60
versionName "1.4.3"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
externalNativeBuild {
cmake {
Expand Down
8 changes: 4 additions & 4 deletions Projects/Windows/Dora/Resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ IDI_ICON1 ICON "Dora SSR.ico"
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,4,2,0
PRODUCTVERSION 1,4,2,0
FILEVERSION 1,4,3,0
PRODUCTVERSION 1,4,3,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -79,12 +79,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "IppClub"
VALUE "FileDescription", "A game engine for rapid game development."
VALUE "FileVersion", "1.4.2.0"
VALUE "FileVersion", "1.4.3.0"
VALUE "InternalName", "Dora.exe"
VALUE "LegalCopyright", "Copyright (C) 2024"
VALUE "OriginalFilename", "Dora.exe"
VALUE "ProductName", "Dora SSR"
VALUE "ProductVersion", "1.4.2.0"
VALUE "ProductVersion", "1.4.3.0"
END
END
BLOCK "VarFileInfo"
Expand Down
4 changes: 2 additions & 2 deletions Projects/iOS/Dora/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.4.2</string>
<string>1.4.3</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>59</string>
<string>60</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down
4 changes: 2 additions & 2 deletions Projects/macOS/Dora/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.4.2</string>
<string>1.4.3</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>59</string>
<string>60</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.developer-tools</string>
<key>LSMinimumSystemVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion Source/Basic/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <ctime>
#include <thread>

#define DORA_VERSION "1.4.2"_slice
#define DORA_VERSION "1.4.3"_slice

#if BX_PLATFORM_ANDROID
#include <jni.h>
Expand Down
21 changes: 19 additions & 2 deletions Source/Basic/Director.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Director::Director()
, _nvgDirty(false)
, _paused(false)
, _stoped(false)
, _frustumCulling(true)
, _nvgContext(nullptr) { }

Director::~Director() {
Expand Down Expand Up @@ -167,7 +168,7 @@ Camera* Director::getCurrentCamera() {
}

const Matrix& Director::getViewProjection() const {
return *_viewProjs.top();
return _viewProjs.top()->matrix;
}

static void registerTouchHandler(Node* target) {
Expand Down Expand Up @@ -425,7 +426,10 @@ void Director::doRender() {
}

void Director::pushViewProjection(const Matrix& viewProj) {
_viewProjs.push(New<Matrix>(viewProj));
auto viewProject = New<ViewProject>();
viewProject->matrix = viewProj;
Matrix::toFrustum(viewProject->frustum, viewProj);
_viewProjs.push(std::move(viewProject));
}

void Director::popViewProjection() {
Expand Down Expand Up @@ -498,6 +502,19 @@ void Director::removeFromWaitingList(Node* node) {
_waitingList.end());
}

bool Director::isInFrustum(const AABB& aabb) const {
if (_viewProjs.empty()) return false;
return _viewProjs.top()->frustum.intersect(aabb);
}

bool Director::isFrustumCulling() const {
return _frustumCulling;
}

void Director::setFrustumCulling(bool var) {
_frustumCulling = var;
}

void Director::markDirty() {
if (_ui) _ui->markDirty();
if (_entry) {
Expand Down
10 changes: 9 additions & 1 deletion Source/Basic/Director.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Director {
PROPERTY_READONLY(Scheduler*, SystemScheduler);
PROPERTY_READONLY(Scheduler*, PostScheduler);
PROPERTY_READONLY(const Matrix&, ViewProjection);
PROPERTY_BOOL(FrustumCulling);
bool init();
void doLogic();
void doRender();
Expand All @@ -61,6 +62,8 @@ class Director {
popViewProjection();
}

bool isInFrustum(const AABB& aabb) const;

void addUnManagedNode(Node* node);

void addToWaitingList(Node* node);
Expand All @@ -75,6 +78,7 @@ class Director {
bool _nvgDirty;
bool _paused;
bool _stoped;
bool _frustumCulling;
Color _clearColor;
Ref<Node> _ui;
Ref<Node> _ui3D;
Expand All @@ -90,7 +94,11 @@ class Director {
RefVector<Node> _unManagedNodes;
std::vector<WRef<Node>> _waitingList;
Own<UITouchHandler> _uiTouchHandler;
std::stack<Own<Matrix>> _viewProjs;
struct ViewProject {
Matrix matrix;
Frustum frustum;
};
std::stack<Own<ViewProject>> _viewProjs;
Matrix _defaultViewProj;
NVGcontext* _nvgContext;
SINGLETON_REF(Director, EffekManager, FontManager, LuaEngine, BGFXDora, Application);
Expand Down
2 changes: 1 addition & 1 deletion Source/Basic/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include "Basic/Application.h"
#include "Basic/Director.h"
#include "Effect/Effect.h"
#include "Node/Node.h"
#include "Event/Event.h"
#include "Node/EffekNode.h"

NS_DORA_BEGIN
Expand Down
6 changes: 3 additions & 3 deletions Source/Common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ inline std::size_t constexpr operator"" _hash(const char* s, size_t) {
/** @brief Helpers for number comparision */
namespace Math {
template <class T>
inline T clamp(T value, T minVal, T maxVal) {
auto pair = std::minmax(minVal, maxVal);
return std::max(std::min(value, pair.second), pair.first);
inline T clamp(T value, T boundA, T boundB) {
auto [minVal, maxVal] = std::minmax(boundA, boundB);
return std::max(std::min(value, maxVal), minVal);
}

float rand0to1();
Expand Down
36 changes: 31 additions & 5 deletions Source/Node/DragonBone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,47 @@ DBSlotNode::DBSlotNode()
, _transform(AffineTransform::Indentity) { }

void DBSlotNode::render() {
if (!_texture || !_effect || _vertices.empty()) return;
if (!_texture || !_effect || _vertices.empty()) {
Node::render();
return;
}

if (SharedDirector.isFrustumCulling()) {
auto [minX, maxX] = std::minmax_element(_points.begin(), _points.end(), [](const auto& a, const auto& b) {
return a.x < b.x;
});
auto [minY, maxY] = std::minmax_element(_points.begin(), _points.end(), [](const auto& a, const auto& b) {
return a.y < b.y;
});
AABB aabb;
Matrix::mulAABB(aabb, _matrix, {
{minX->x, minY->y, 0},
{maxX->x, maxY->y, 0},
});
if (!SharedDirector.isInFrustum(aabb)) {
return;
}
}

Matrix transform;
Matrix::mulMtx(transform, SharedDirector.getViewProjection(), getWorld());
Matrix::mulMtx(transform, SharedDirector.getViewProjection(), _matrix);
for (size_t i = 0; i < _points.size(); i++) {
Matrix::mulVec4(&_vertices[i].x, transform, &_points[i].x);
}

auto parent = s_cast<DragonBone*>(getParent());
uint64_t renderState = (BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_MSAA | _blendFunc.toValue());
if (parent->isDepthWrite()) {
renderState |= (BGFX_STATE_WRITE_Z | BGFX_STATE_DEPTH_TEST_LESS);
}
for (size_t i = 0; i < _points.size(); i++) {
Matrix::mulVec4(&_vertices[i].x, transform, &_points[i].x);
}

SharedRendererManager.setCurrent(SharedSpriteRenderer.getTarget());
SharedSpriteRenderer.push(
_vertices.data(), _vertices.size(),
_indices.data(), _indices.size(),
_effect, _texture, renderState);

Node::render();
}

const Matrix& DBSlotNode::getWorld() {
Expand Down Expand Up @@ -560,6 +585,7 @@ void DragonBone::render() {
}
}
}
Node::render();
}

void DragonBone::cleanup() {
Expand Down
Loading

0 comments on commit d546f93

Please sign in to comment.