| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "Scene/BsGizmoManager.h"
- #include "Mesh/BsMesh.h"
- #include "Math/BsAABox.h"
- #include "Math/BsSphere.h"
- #include "RenderAPI/BsVertexDataDesc.h"
- #include "Utility/BsShapeMeshes3D.h"
- #include "Components/BsCCamera.h"
- #include "Image/BsSpriteTexture.h"
- #include "CoreThread/BsCoreThread.h"
- #include "Utility/BsBuiltinEditorResources.h"
- #include "Material/BsMaterial.h"
- #include "RenderAPI/BsGpuParams.h"
- #include "Material/BsGpuParamsSet.h"
- #include "RenderAPI/BsRenderAPI.h"
- #include "Renderer/BsRenderer.h"
- #include "Renderer/BsRendererUtility.h"
- #include "Renderer/BsRendererManager.h"
- #include "Utility/BsDrawHelper.h"
- using namespace std::placeholders;
- namespace bs
- {
- const UINT32 GizmoManager::SPHERE_QUALITY = 1;
- const UINT32 GizmoManager::WIRE_SPHERE_QUALITY = 10;
- const float GizmoManager::MAX_ICON_RANGE = 500.0f;
- const UINT32 GizmoManager::OPTIMAL_ICON_SIZE = 64;
- const float GizmoManager::ICON_TEXEL_WORLD_SIZE = 0.05f;
- GizmoManager::GizmoManager()
- : mPickable(false), mCurrentIdx(0), mTransformDirty(false), mColorDirty(false), mDrawHelper(nullptr)
- , mPickingDrawHelper(nullptr)
-
- {
- mTransform = Matrix4::IDENTITY;
- mDrawHelper = bs_new<DrawHelper>();
- mPickingDrawHelper = bs_new<DrawHelper>();
- mIconVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
- mIconVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
- mIconVertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD);
- mIconVertexDesc->addVertElem(VET_COLOR, VES_COLOR, 0);
- mIconVertexDesc->addVertElem(VET_COLOR, VES_COLOR, 1);
- HMaterial solidMaterial = BuiltinEditorResources::instance().createSolidGizmoMat();
- HMaterial wireMaterial = BuiltinEditorResources::instance().createWireGizmoMat();
- HMaterial lineMaterial = BuiltinEditorResources::instance().createLineGizmoMat();
- HMaterial iconMaterial = BuiltinEditorResources::instance().createIconGizmoMat();
- HMaterial textMaterial = BuiltinEditorResources::instance().createTextGizmoMat();
- HMaterial pickingMaterial = BuiltinEditorResources::instance().createGizmoPickingMat();
- HMaterial alphaPickingMaterial = BuiltinEditorResources::instance().createAlphaGizmoPickingMat();
- CoreInitData initData;
- initData.solidMat = solidMaterial->getCore();
- initData.wireMat = wireMaterial->getCore();
- initData.lineMat = lineMaterial->getCore();
- initData.iconMat = iconMaterial->getCore();
- initData.textMat = textMaterial->getCore();
- initData.pickingMat = pickingMaterial->getCore();
- initData.alphaPickingMat = alphaPickingMaterial->getCore();
- mGizmoRenderer = RendererExtension::create<ct::GizmoRenderer>(initData);
- }
- GizmoManager::~GizmoManager()
- {
- mActiveMeshes.clear();
- bs_delete(mDrawHelper);
- bs_delete(mPickingDrawHelper);
- }
- void GizmoManager::startGizmo(const HSceneObject& gizmoParent)
- {
- mActiveSO = gizmoParent;
- if(mTransformDirty)
- {
- mTransform = Matrix4::IDENTITY;
- mDrawHelper->setTransform(Matrix4::IDENTITY);
- mTransformDirty = false;
- }
- if(mColorDirty)
- {
- mColor = Color();
- mColorDirty = false;
- }
- }
- void GizmoManager::endGizmo()
- {
- mActiveSO = nullptr;
- }
- void GizmoManager::setColor(const Color& color)
- {
- mDrawHelper->setColor(color);
- mColor = color;
- mColorDirty = true;
- }
- void GizmoManager::setTransform(const Matrix4& transform)
- {
- mDrawHelper->setTransform(transform);
- mTransform = transform;
- mTransformDirty = true;
- }
- void GizmoManager::drawCube(const Vector3& position, const Vector3& extents)
- {
- mSolidCubeData.push_back(CubeData());
- CubeData& cubeData = mSolidCubeData.back();
- cubeData.idx = mCurrentIdx++;
- cubeData.position = position;
- cubeData.extents = extents;
- cubeData.color = mColor;
- cubeData.transform = mTransform;
- cubeData.sceneObject = mActiveSO;
- cubeData.pickable = mPickable;
- mDrawHelper->cube(position, extents);
- mIdxToSceneObjectMap[cubeData.idx] = mActiveSO;
- }
- void GizmoManager::drawSphere(const Vector3& position, float radius)
- {
- mSolidSphereData.push_back(SphereData());
- SphereData& sphereData = mSolidSphereData.back();
- sphereData.idx = mCurrentIdx++;
- sphereData.position = position;
- sphereData.radius = radius;
- sphereData.color = mColor;
- sphereData.transform = mTransform;
- sphereData.sceneObject = mActiveSO;
- sphereData.pickable = mPickable;
- mDrawHelper->sphere(position, radius);
- mIdxToSceneObjectMap[sphereData.idx] = mActiveSO;
- }
- void GizmoManager::drawCone(const Vector3& base, const Vector3& normal, float height, float radius, const Vector2& scale)
- {
- mSolidConeData.push_back(ConeData());
- ConeData& coneData = mSolidConeData.back();
- coneData.idx = mCurrentIdx++;
- coneData.base = base;
- coneData.radius = radius;
- coneData.color = mColor;
- coneData.transform = mTransform;
- coneData.sceneObject = mActiveSO;
- coneData.pickable = mPickable;
- coneData.scale = scale;
- mDrawHelper->cone(base, normal, height, radius, scale);
- mIdxToSceneObjectMap[coneData.idx] = mActiveSO;
- }
- void GizmoManager::drawDisc(const Vector3& position, const Vector3& normal, float radius)
- {
- mSolidDiscData.push_back(DiscData());
- DiscData& discData = mSolidDiscData.back();
- discData.idx = mCurrentIdx++;
- discData.position = position;
- discData.normal = normal;
- discData.radius = radius;
- discData.color = mColor;
- discData.transform = mTransform;
- discData.sceneObject = mActiveSO;
- discData.pickable = mPickable;
- mDrawHelper->disc(position, normal, radius);
- mIdxToSceneObjectMap[discData.idx] = mActiveSO;
- }
- void GizmoManager::drawWireCube(const Vector3& position, const Vector3& extents)
- {
- mWireCubeData.push_back(CubeData());
- CubeData& cubeData = mWireCubeData.back();
- cubeData.idx = mCurrentIdx++;
- cubeData.position = position;
- cubeData.extents = extents;
- cubeData.color = mColor;
- cubeData.transform = mTransform;
- cubeData.sceneObject = mActiveSO;
- cubeData.pickable = mPickable;
- mDrawHelper->wireCube(position, extents);
- mIdxToSceneObjectMap[cubeData.idx] = mActiveSO;
- }
- void GizmoManager::drawWireSphere(const Vector3& position, float radius)
- {
- mWireSphereData.push_back(SphereData());
- SphereData& sphereData = mWireSphereData.back();
- sphereData.idx = mCurrentIdx++;
- sphereData.position = position;
- sphereData.radius = radius;
- sphereData.color = mColor;
- sphereData.transform = mTransform;
- sphereData.sceneObject = mActiveSO;
- sphereData.pickable = mPickable;
- mDrawHelper->wireSphere(position, radius);
- mIdxToSceneObjectMap[sphereData.idx] = mActiveSO;
- }
- void GizmoManager::drawWireCapsule(const Vector3& position, float height, float radius)
- {
- float halfHeight = height * 0.5f;
- // Draw capsule sides
- Vector3 sideNegXBottom = position + Vector3(-radius, -halfHeight, 0.0f);
- Vector3 sideNegXTop = position + Vector3(-radius, halfHeight, 0.0f);
- Vector3 sidePosXBottom = position + Vector3(radius, -halfHeight, 0.0f);
- Vector3 sidePosXTop = position + Vector3(radius, halfHeight, 0.0f);
- Vector3 sideNegZBottom = position + Vector3(0.0f, -halfHeight, -radius);
- Vector3 sideNegZTop = position + Vector3(0.0f, halfHeight, -radius);
- Vector3 sidePosZBottom = position + Vector3(0.0f, -halfHeight, radius);
- Vector3 sidePosZTop = position + Vector3(0.0f, halfHeight, radius);
- drawLine(sideNegXBottom, sideNegXTop);
- drawLine(sidePosXBottom, sidePosXTop);
- drawLine(sideNegZBottom, sideNegZTop);
- drawLine(sidePosZBottom, sidePosZTop);
- // Draw capsule caps
- Vector3 topHemisphere = position + Vector3(0.0f, halfHeight, 0.0f);
- Vector3 botHemisphere = position + Vector3(0.0f, -halfHeight, 0.0f);
- drawWireArc(topHemisphere, Vector3::UNIT_X, radius, Degree(270.0f), -Degree(180.0f));
- drawWireArc(topHemisphere, Vector3::UNIT_Z, radius, Degree(0.0f), -Degree(180.0f));
- drawWireArc(botHemisphere, Vector3::UNIT_X, radius, Degree(90.0f), -Degree(180.0f));
- drawWireArc(botHemisphere, Vector3::UNIT_Z, radius, Degree(180.0f), -Degree(180.0f));
- drawWireDisc(topHemisphere, Vector3::UNIT_Y, radius);
- drawWireDisc(botHemisphere, Vector3::UNIT_Y, radius);
- }
- void GizmoManager::drawWireCone(const Vector3& base, const Vector3& normal, float height, float radius, const Vector2& scale)
- {
- mWireConeData.push_back(ConeData());
- ConeData& coneData = mWireConeData.back();
- coneData.idx = mCurrentIdx++;
- coneData.base = base;
- coneData.radius = radius;
- coneData.color = mColor;
- coneData.transform = mTransform;
- coneData.sceneObject = mActiveSO;
- coneData.pickable = mPickable;
- coneData.scale = scale;
- mDrawHelper->wireCone(base, normal, height, radius, scale);
- mIdxToSceneObjectMap[coneData.idx] = mActiveSO;
- }
- void GizmoManager::drawLine(const Vector3& start, const Vector3& end)
- {
- mLineData.push_back(LineData());
- LineData& lineData = mLineData.back();
- lineData.idx = mCurrentIdx++;
- lineData.start = start;
- lineData.end = end;
- lineData.color = mColor;
- lineData.transform = mTransform;
- lineData.sceneObject = mActiveSO;
- lineData.pickable = mPickable;
- mDrawHelper->line(start, end);
- mIdxToSceneObjectMap[lineData.idx] = mActiveSO;
- }
- void GizmoManager::drawLineList(const Vector<Vector3>& linePoints)
- {
- mLineListData.push_back(LineListData());
- LineListData& lineListData = mLineListData.back();
- lineListData.idx = mCurrentIdx++;
- lineListData.linePoints = linePoints;
- lineListData.color = mColor;
- lineListData.transform = mTransform;
- lineListData.sceneObject = mActiveSO;
- lineListData.pickable = mPickable;
- mDrawHelper->lineList(linePoints);
- mIdxToSceneObjectMap[lineListData.idx] = mActiveSO;
- }
- void GizmoManager::drawWireDisc(const Vector3& position, const Vector3& normal, float radius)
- {
- mWireDiscData.push_back(DiscData());
- DiscData& wireDiscData = mWireDiscData.back();
- wireDiscData.idx = mCurrentIdx++;
- wireDiscData.position = position;
- wireDiscData.normal = normal;
- wireDiscData.radius = radius;
- wireDiscData.color = mColor;
- wireDiscData.transform = mTransform;
- wireDiscData.sceneObject = mActiveSO;
- wireDiscData.pickable = mPickable;
- mDrawHelper->wireDisc(position, normal, radius);
- mIdxToSceneObjectMap[wireDiscData.idx] = mActiveSO;
- }
- void GizmoManager::drawWireArc(const Vector3& position, const Vector3& normal, float radius,
- Degree startAngle, Degree amountAngle)
- {
- mWireArcData.push_back(WireArcData());
- WireArcData& wireArcData = mWireArcData.back();
- wireArcData.idx = mCurrentIdx++;
- wireArcData.position = position;
- wireArcData.normal = normal;
- wireArcData.radius = radius;
- wireArcData.startAngle = startAngle;
- wireArcData.amountAngle = amountAngle;
- wireArcData.color = mColor;
- wireArcData.transform = mTransform;
- wireArcData.sceneObject = mActiveSO;
- wireArcData.pickable = mPickable;
- mDrawHelper->wireArc(position, normal, radius, startAngle, amountAngle);
- mIdxToSceneObjectMap[wireArcData.idx] = mActiveSO;
- }
- void GizmoManager::drawWireMesh(const SPtr<MeshData>& meshData)
- {
- mWireMeshData.push_back(WireMeshData());
- WireMeshData& wireMeshData = mWireMeshData.back();
- wireMeshData.idx = mCurrentIdx++;
- wireMeshData.meshData = meshData;
- wireMeshData.color = mColor;
- wireMeshData.transform = mTransform;
- wireMeshData.sceneObject = mActiveSO;
- wireMeshData.pickable = mPickable;
- mDrawHelper->wireMesh(meshData);
- mIdxToSceneObjectMap[wireMeshData.idx] = mActiveSO;
- }
- void GizmoManager::drawFrustum(const Vector3& position, float aspect, Degree FOV, float near, float far)
- {
- mFrustumData.push_back(FrustumData());
- FrustumData& frustumData = mFrustumData.back();
- frustumData.idx = mCurrentIdx++;
- frustumData.position = position;
- frustumData.aspect = aspect;
- frustumData.FOV = FOV;
- frustumData.near = near;
- frustumData.far = far;
- frustumData.color = mColor;
- frustumData.transform = mTransform;
- frustumData.sceneObject = mActiveSO;
- frustumData.pickable = mPickable;
- mDrawHelper->frustum(position, aspect, FOV, near, far);
- mIdxToSceneObjectMap[frustumData.idx] = mActiveSO;
- }
- void GizmoManager::drawIcon(Vector3 position, HSpriteTexture image, bool fixedScale)
- {
- mIconData.push_back(IconData());
- IconData& iconData = mIconData.back();
- iconData.idx = mCurrentIdx++;
- iconData.position = position;
- iconData.texture = image;
- iconData.fixedScale = fixedScale;
- iconData.color = mColor;
- iconData.transform = mTransform;
- iconData.sceneObject = mActiveSO;
- iconData.pickable = mPickable;
- mIdxToSceneObjectMap[iconData.idx] = mActiveSO;
- }
- void GizmoManager::drawText(const Vector3& position, const String& text, const HFont& font, UINT32 fontSize)
- {
- HFont myFont = font;
- if (myFont == nullptr)
- myFont = BuiltinEditorResources::instance().getDefaultAAFont();
- mTextData.push_back(TextData());
- TextData& textData = mTextData.back();
- textData.idx = mCurrentIdx++;
- textData.position = position;
- textData.text = text;
- textData.font = myFont;
- textData.fontSize = fontSize;
- textData.color = mColor;
- textData.transform = mTransform;
- textData.sceneObject = mActiveSO;
- textData.pickable = mPickable;
- mDrawHelper->text(position, text, myFont, fontSize);
- mIdxToSceneObjectMap[textData.idx] = mActiveSO;
- }
- Vector<GizmoManager::MeshRenderData> GizmoManager::createMeshProxyData(const Vector<DrawHelper::ShapeMeshData>& meshData)
- {
- Vector<MeshRenderData> proxyData;
- for (auto& entry : meshData)
- {
- SPtr<ct::Texture> tex;
- if (entry.texture.isLoaded())
- tex = entry.texture->getCore();
- if (entry.type == DrawHelper::MeshType::Solid)
- proxyData.push_back(MeshRenderData(entry.mesh->getCore(), entry.subMesh, tex, GizmoMeshType::Solid));
- else if (entry.type == DrawHelper::MeshType::Wire)
- proxyData.push_back(MeshRenderData(entry.mesh->getCore(), entry.subMesh, tex, GizmoMeshType::Wire));
- else if (entry.type == DrawHelper::MeshType::Line)
- proxyData.push_back(MeshRenderData(entry.mesh->getCore(), entry.subMesh, tex, GizmoMeshType::Line));
- else // Text
- proxyData.push_back(MeshRenderData(entry.mesh->getCore(), entry.subMesh, tex, GizmoMeshType::Text));
- }
- return proxyData;
- }
- void GizmoManager::update(const SPtr<Camera>& camera)
- {
- mActiveMeshes.clear();
- mActiveMeshes = mDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera.get());
- Vector<MeshRenderData> proxyData = createMeshProxyData(mActiveMeshes);
- IconRenderDataVecPtr iconRenderData;
- mIconMesh = buildIconMesh(camera, mIconData, false, iconRenderData);
- SPtr<ct::MeshBase> iconMesh;
- if(mIconMesh != nullptr)
- iconMesh = mIconMesh->getCore();
- ct::GizmoRenderer* renderer = mGizmoRenderer.get();
- gCoreThread().queueCommand(std::bind(&ct::GizmoRenderer::updateData, renderer, camera->getCore(),
- proxyData, iconMesh, iconRenderData));
- }
- void GizmoManager::renderForPicking(const SPtr<Camera>& camera, std::function<Color(UINT32)> idxToColorCallback)
- {
- Vector<IconData> iconData;
- IconRenderDataVecPtr iconRenderData;
- mPickingDrawHelper->clear();
- for (auto& cubeDataEntry : mSolidCubeData)
- {
- if (!cubeDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
- mPickingDrawHelper->setTransform(cubeDataEntry.transform);
- mPickingDrawHelper->cube(cubeDataEntry.position, cubeDataEntry.extents);
- }
- for (auto& cubeDataEntry : mWireCubeData)
- {
- if (!cubeDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
- mPickingDrawHelper->setTransform(cubeDataEntry.transform);
- mPickingDrawHelper->wireCube(cubeDataEntry.position, cubeDataEntry.extents);
- }
- for (auto& sphereDataEntry : mSolidSphereData)
- {
- if (!sphereDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
- mPickingDrawHelper->setTransform(sphereDataEntry.transform);
- mPickingDrawHelper->sphere(sphereDataEntry.position, sphereDataEntry.radius);
- }
- for (auto& sphereDataEntry : mWireSphereData)
- {
- if (!sphereDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
- mPickingDrawHelper->setTransform(sphereDataEntry.transform);
- mPickingDrawHelper->wireSphere(sphereDataEntry.position, sphereDataEntry.radius);
- }
- for (auto& coneDataEntry : mSolidConeData)
- {
- if (!coneDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(coneDataEntry.idx));
- mPickingDrawHelper->setTransform(coneDataEntry.transform);
- mPickingDrawHelper->cone(coneDataEntry.base, coneDataEntry.normal, coneDataEntry.radius, coneDataEntry.radius,
- coneDataEntry.scale);
- }
- for (auto& coneDataEntry : mWireConeData)
- {
- if (!coneDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(coneDataEntry.idx));
- mPickingDrawHelper->setTransform(coneDataEntry.transform);
- mPickingDrawHelper->wireCone(coneDataEntry.base, coneDataEntry.normal, coneDataEntry.radius, coneDataEntry.radius,
- coneDataEntry.scale);
- }
- for (auto& lineDataEntry : mLineData)
- {
- if (!lineDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(lineDataEntry.idx));
- mPickingDrawHelper->setTransform(lineDataEntry.transform);
- mPickingDrawHelper->line(lineDataEntry.start, lineDataEntry.end);
- }
- for (auto& lineListDataEntry : mLineListData)
- {
- if (!lineListDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(lineListDataEntry.idx));
- mPickingDrawHelper->setTransform(lineListDataEntry.transform);
- mPickingDrawHelper->lineList(lineListDataEntry.linePoints);
- }
- for (auto& discDataEntry : mSolidDiscData)
- {
- if (!discDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(discDataEntry.idx));
- mPickingDrawHelper->setTransform(discDataEntry.transform);
- mPickingDrawHelper->disc(discDataEntry.position, discDataEntry.normal, discDataEntry.radius);
- }
- for (auto& discDataEntry : mWireDiscData)
- {
- if (!discDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(discDataEntry.idx));
- mPickingDrawHelper->setTransform(discDataEntry.transform);
- mPickingDrawHelper->wireDisc(discDataEntry.position, discDataEntry.normal, discDataEntry.radius);
- }
- for (auto& wireArcDataEntry : mWireArcData)
- {
- if (!wireArcDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(wireArcDataEntry.idx));
- mPickingDrawHelper->setTransform(wireArcDataEntry.transform);
- mPickingDrawHelper->wireArc(wireArcDataEntry.position, wireArcDataEntry.normal, wireArcDataEntry.radius,
- wireArcDataEntry.startAngle, wireArcDataEntry.amountAngle);
- }
- for (auto& wireMeshData : mWireMeshData)
- {
- if (!wireMeshData.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(wireMeshData.idx));
- mPickingDrawHelper->setTransform(wireMeshData.transform);
- mPickingDrawHelper->wireMesh(wireMeshData.meshData);
- }
- for (auto& frustumDataEntry : mFrustumData)
- {
- if (!frustumDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(frustumDataEntry.idx));
- mPickingDrawHelper->setTransform(frustumDataEntry.transform);
- mPickingDrawHelper->frustum(frustumDataEntry.position, frustumDataEntry.aspect, frustumDataEntry.FOV,
- frustumDataEntry.near, frustumDataEntry.far);
- }
- for (auto& textDataEntry : mTextData)
- {
- if (!textDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(textDataEntry.idx));
- mPickingDrawHelper->setTransform(textDataEntry.transform);
- mPickingDrawHelper->text(textDataEntry.position, textDataEntry.text, textDataEntry.font,
- textDataEntry.fontSize);
- }
- for (auto& iconDataEntry : mIconData)
- {
- if (!iconDataEntry.pickable)
- continue;
- iconData.push_back(iconDataEntry);
- iconData.back().color = idxToColorCallback(iconDataEntry.idx);
- }
- const Vector<DrawHelper::ShapeMeshData>& meshes =
- mPickingDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera.get());
- SPtr<Mesh> iconMesh = buildIconMesh(camera, iconData, true, iconRenderData);
-
- SPtr<ct::Mesh> iconMeshCore;
- if (iconMesh != nullptr)
- iconMeshCore = iconMesh->getCore();
- // Note: This must be rendered while Scene view is being rendered
- ct::GizmoRenderer* renderer = mGizmoRenderer.get();
- Vector<MeshRenderData> proxyData = createMeshProxyData(meshes);
- gCoreThread().queueCommand(std::bind(&ct::GizmoRenderer::renderData, renderer, camera->getCore(),
- proxyData, iconMeshCore, iconRenderData, true));
- }
- void GizmoManager::clearGizmos()
- {
- mSolidCubeData.clear();
- mWireCubeData.clear();
- mSolidSphereData.clear();
- mWireSphereData.clear();
- mSolidConeData.clear();
- mWireConeData.clear();
- mLineData.clear();
- mLineListData.clear();
- mSolidDiscData.clear();
- mWireDiscData.clear();
- mWireArcData.clear();
- mWireMeshData.clear();
- mFrustumData.clear();
- mTextData.clear();
- mIconData.clear();
- mIdxToSceneObjectMap.clear();
- mDrawHelper->clear();
- mCurrentIdx = 0;
- }
- void GizmoManager::clearRenderData()
- {
- mActiveMeshes.clear();
- mIconMesh = nullptr;
- ct::GizmoRenderer* renderer = mGizmoRenderer.get();
- IconRenderDataVecPtr iconRenderData = bs_shared_ptr_new<IconRenderDataVec>();
-
- gCoreThread().queueCommand(std::bind(&ct::GizmoRenderer::updateData, renderer,
- nullptr, Vector<MeshRenderData>(), nullptr, iconRenderData));
- }
- SPtr<Mesh> GizmoManager::buildIconMesh(const SPtr<Camera>& camera, const Vector<IconData>& iconData,
- bool forPicking, GizmoManager::IconRenderDataVecPtr& iconRenderData)
- {
- mSortedIconData.clear();
-
- if (iconData.size() > mSortedIconData.size())
- mSortedIconData.resize(iconData.size());
- UINT32 i = 0;
- for (auto& iconEntry : iconData)
- {
- Vector3 viewPoint = camera->worldToViewPoint(iconEntry.position);
- float distance = -viewPoint.z;
- if (distance < camera->getNearClipDistance()) // Ignore behind clip plane
- continue;
- if (distance > MAX_ICON_RANGE) // Ignore too far away
- continue;
- if (!iconEntry.texture.isLoaded()) // Ignore missing texture
- continue;
- if (forPicking && !iconEntry.pickable)
- continue;
- SortedIconData& sortedIconData = mSortedIconData[i];
- sortedIconData.iconIdx = i;
- sortedIconData.distance = distance;
- sortedIconData.screenPosition = camera->viewToScreenPoint(viewPoint);
- i++;
- }
- UINT32 actualNumIcons = i;
- // Sort back to front first, then by texture
- std::sort(mSortedIconData.begin(), mSortedIconData.begin() + actualNumIcons,
- [&](const SortedIconData& a, const SortedIconData& b)
- {
- if (a.distance == b.distance)
- {
- HTexture texA = iconData[a.iconIdx].texture->getTexture();
- HTexture texB = iconData[b.iconIdx].texture->getTexture();
- if (texA == texB)
- return a.iconIdx < b.iconIdx;
- return texA->getInternalID() < texB->getInternalID();
- }
- else
- return a.distance > b.distance;
- });
- SPtr<MeshData> meshData = bs_shared_ptr_new<MeshData>(actualNumIcons * 4, actualNumIcons * 6, mIconVertexDesc);
- auto positionIter = meshData->getVec3DataIter(VES_POSITION);
- auto texcoordIter = meshData->getVec2DataIter(VES_TEXCOORD);
- auto normalColorIter = meshData->getDWORDDataIter(VES_COLOR, 0);
- auto fadedColorIter = meshData->getDWORDDataIter(VES_COLOR, 1);
- UINT32* indices = meshData->getIndices32();
- float cameraScale = 1.0f;
- if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
- cameraScale = camera->getViewport()->getPixelArea().height / camera->getOrthoWindowHeight();
- else
- {
- Radian vertFOV(Math::tan(camera->getHorzFOV() * 0.5f));
- cameraScale = (camera->getViewport()->getPixelArea().height * 0.5f) / vertFOV.valueRadians();
- }
- iconRenderData = bs_shared_ptr_new<IconRenderDataVec>();
- UINT32 lastTextureIdx = std::numeric_limits<UINT32>::max();
- HTexture curTexture;
- // Note: This assumes the meshes will be rendered using the same camera
- // properties as when they are created
- for (i = 0; i < actualNumIcons; i++)
- {
- SortedIconData& sortedIconData = mSortedIconData[i];
- const IconData& curIconData = iconData[sortedIconData.iconIdx];
- HTexture atlasTexture = curIconData.texture->getTexture();
- if (curTexture != atlasTexture)
- {
- UINT32 numIconsPerTexture = i - lastTextureIdx;
- if (numIconsPerTexture > 0)
- {
- iconRenderData->push_back(IconRenderData());
- IconRenderData& renderData = iconRenderData->back();
- renderData.count = numIconsPerTexture;
- renderData.texture = atlasTexture->getCore();
- }
- lastTextureIdx = i;
- curTexture = atlasTexture;
- }
- UINT32 iconWidth = curIconData.texture->getWidth();
- UINT32 iconHeight = curIconData.texture->getHeight();
- limitIconSize(iconWidth, iconHeight);
- Vector3 position((float)sortedIconData.screenPosition.x, (float)sortedIconData.screenPosition.y, -sortedIconData.distance);
- Vector3 projPosition = camera->projectPoint(position);
- position.z = projPosition.z;
- float halfWidth = iconWidth * 0.5f;
- float halfHeight = iconHeight * 0.5f;
- if (!curIconData.fixedScale)
- {
- float iconScale = 1.0f;
- if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
- iconScale = cameraScale * ICON_TEXEL_WORLD_SIZE;
- else
- iconScale = (cameraScale * ICON_TEXEL_WORLD_SIZE) / sortedIconData.distance;
- halfWidth *= iconScale;
- halfHeight *= iconScale;
- }
- Color normalColor, fadedColor;
- calculateIconColors(curIconData.color, camera, (UINT32)(halfHeight * 2.0f), curIconData.fixedScale, normalColor, fadedColor);
- if (forPicking)
- {
- normalColor = curIconData.color;
- fadedColor = curIconData.color;
- }
- Vector3 positions[4];
- positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
- positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
- positions[2] = position + Vector3(halfWidth, halfHeight, 0.0f);
- positions[3] = position + Vector3(-halfWidth, halfHeight, 0.0f);
- Vector2 uvs[4];
- uvs[0] = curIconData.texture->transformUV(Vector2(0.0f, 0.0f));
- uvs[1] = curIconData.texture->transformUV(Vector2(1.0f, 0.0f));
- uvs[2] = curIconData.texture->transformUV(Vector2(1.0f, 1.0f));
- uvs[3] = curIconData.texture->transformUV(Vector2(0.0f, 1.0f));
- for (UINT32 j = 0; j < 4; j++)
- {
- positionIter.addValue(positions[j]);
- texcoordIter.addValue(uvs[j]);
- normalColorIter.addValue(normalColor.getAsRGBA());
- fadedColorIter.addValue(fadedColor.getAsRGBA());
- }
- UINT32 vertOffset = i * 4;
- indices[0] = vertOffset + 0;
- indices[1] = vertOffset + 1;
- indices[2] = vertOffset + 2;
- indices[3] = vertOffset + 0;
- indices[4] = vertOffset + 2;
- indices[5] = vertOffset + 3;
- indices += 6;
- }
- if(actualNumIcons > 0)
- return Mesh::_createPtr(meshData);
- return nullptr;
- }
- void GizmoManager::limitIconSize(UINT32& width, UINT32& height)
- {
- if (width <= OPTIMAL_ICON_SIZE && height <= OPTIMAL_ICON_SIZE)
- return;
- float relWidth = OPTIMAL_ICON_SIZE / (float)width;
- float relHeight = OPTIMAL_ICON_SIZE / (float)height;
- float scale = std::min(relWidth, relHeight);
- width = Math::roundToInt(width * scale);
- height = Math::roundToInt(height * scale);
- }
- void GizmoManager::calculateIconColors(const Color& tint, const SPtr<Camera>& camera,
- UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
- {
- normalColor = tint;
- if (!fixedScale)
- {
- float iconToScreenRatio = iconHeight / (float)camera->getViewport()->getPixelArea().height;
- if (iconToScreenRatio > 0.3f)
- {
- float alpha = 1.0f - Math::invLerp(iconToScreenRatio, 0.3f, 1.0f);
- normalColor.a *= alpha;
- }
- else if (iconToScreenRatio < 0.1f)
- {
- float alpha = Math::invLerp(iconToScreenRatio, 0.0f, 0.1f);
- normalColor.a *= alpha;
- }
- }
- fadedColor = normalColor;
- fadedColor.a *= 0.2f;
- }
- HSceneObject GizmoManager::getSceneObject(UINT32 gizmoIdx)
- {
- auto iterFind = mIdxToSceneObjectMap.find(gizmoIdx);
- if (iterFind != mIdxToSceneObjectMap.end())
- return iterFind->second;
- return HSceneObject();
- }
- namespace ct
- {
- GizmoParamBlockDef gGizmoParamBlockDef;
- GizmoPickingParamBlockDef gGizmoPickingParamBlockDef;
- const float GizmoRenderer::PICKING_ALPHA_CUTOFF = 0.5f;
- GizmoRenderer::GizmoRenderer()
- :RendererExtension(RenderLocation::PostLightPass, 0)
- {
- }
- void GizmoRenderer::initialize(const Any& data)
- {
- THROW_IF_NOT_CORE_THREAD;
- const GizmoManager::CoreInitData& initData = any_cast_ref<GizmoManager::CoreInitData>(data);
- mMeshMaterials[(UINT32)GizmoMeshType::Solid] = initData.solidMat;
- mMeshMaterials[(UINT32)GizmoMeshType::Wire] = initData.wireMat;
- mMeshMaterials[(UINT32)GizmoMeshType::Line] = initData.lineMat;
- mMeshMaterials[(UINT32)GizmoMeshType::Text] = initData.textMat;
- mIconMaterial = initData.iconMat;
- mPickingMaterials[0] = initData.pickingMat;
- mPickingMaterials[1] = initData.alphaPickingMat;
- mMeshGizmoBuffer = gGizmoParamBlockDef.createBuffer();
- mIconGizmoBuffer = gGizmoParamBlockDef.createBuffer();
- mMeshPickingParamBuffer = gGizmoPickingParamBlockDef.createBuffer();
- mIconPickingParamBuffer = gGizmoPickingParamBlockDef.createBuffer();
- }
- void GizmoRenderer::updateData(const SPtr<Camera>& camera, const Vector<GizmoManager::MeshRenderData>& meshes,
- const SPtr<MeshBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
- {
- mCamera = camera;
- mMeshes = meshes;
- mIconMesh = iconMesh;
- mIconRenderData = iconRenderData;
- // Allocate and assign GPU program parameter objects
- UINT32 meshCounters[(UINT32)GizmoMeshType::Count];
- bs_zero_out(meshCounters);
- for (auto& meshData : mMeshes)
- {
- UINT32 typeIdx = (UINT32)meshData.type;
- UINT32 paramsIdx = meshCounters[typeIdx];
- meshData.paramsIdx = paramsIdx;
- SPtr<GpuParamsSet> paramsSet;
- if (paramsIdx >= mMeshParamSets[typeIdx].size())
- {
- paramsSet = mMeshMaterials[typeIdx]->createParamsSet();
- mMeshMaterials[typeIdx]->updateParamsSet(paramsSet, true);
- paramsSet->setParamBlockBuffer("Uniforms", mMeshGizmoBuffer, true);
- mMeshParamSets[typeIdx].push_back(paramsSet);
- }
- else
- paramsSet = mMeshParamSets[typeIdx][paramsIdx];
- if(meshData.type == GizmoMeshType::Text)
- {
- SPtr<GpuParams> params = paramsSet->getGpuParams();
- GpuParamTexture textureParam;
- params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", textureParam);
- textureParam.set(meshData.texture);
- }
- meshCounters[typeIdx]++;
- }
- UINT32 iconMeshIdx = 0;
- for(auto& iconData : *mIconRenderData)
- {
- iconData.paramsIdx = iconMeshIdx;
- SPtr<GpuParamsSet> paramsSet;
- if (iconMeshIdx >= mIconParamSets.size())
- {
- mIconMaterial->createParamsSet();
- paramsSet->setParamBlockBuffer("Uniforms", mIconGizmoBuffer, true);
- mIconParamSets.push_back(paramsSet);
- }
- else
- paramsSet = mIconParamSets[iconMeshIdx];
- SPtr<GpuParams> params0 = paramsSet->getGpuParams(0);
- SPtr<GpuParams> params1 = paramsSet->getGpuParams(1);
- GpuParamTexture textureParam0;
- GpuParamTexture textureParam1;
- params0->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", textureParam0);
- params1->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", textureParam1);
- textureParam0.set(iconData.texture);
- textureParam1.set(iconData.texture);
- iconMeshIdx++;
- }
- }
- bool GizmoRenderer::check(const Camera& camera)
- {
- return &camera == mCamera.get();
- }
- void GizmoRenderer::render(const Camera& camera)
- {
- renderData(mCamera, mMeshes, mIconMesh, mIconRenderData, false);
- }
- void GizmoRenderer::renderData(const SPtr<Camera>& camera, Vector<GizmoManager::MeshRenderData>& meshes,
- const SPtr<MeshBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData, bool usePickingMaterial)
- {
- if (camera == nullptr)
- return;
- SPtr<RenderTarget> renderTarget = camera->getViewport()->getTarget();
- if (renderTarget == nullptr)
- return;
- Rect2I screenArea = camera->getViewport()->getPixelArea();
- Matrix4 viewMatrix = camera->getViewMatrix();
- Matrix4 projMatrix = camera->getProjectionMatrixRS();
- Matrix4 viewProjMat = projMatrix * viewMatrix;
- float invViewportWidth = 1.0f / (camera->getViewport()->getPixelArea().width * 0.5f);
- float invViewportHeight = 1.0f / (camera->getViewport()->getPixelArea().height * 0.5f);
- float viewportYFlip = bs::RenderAPI::getAPIInfo().isFlagSet(RenderAPIFeatureFlag::NDCYAxisDown) ? -1.0f : 1.0f;
- if (!usePickingMaterial)
- {
- gGizmoParamBlockDef.gMatViewProj.set(mMeshGizmoBuffer, viewProjMat);
- gGizmoParamBlockDef.gViewDir.set(mMeshGizmoBuffer, (Vector4)camera->getTransform().getForward());
- gGizmoParamBlockDef.gInvViewportWidth.set(mMeshGizmoBuffer, invViewportWidth);
- gGizmoParamBlockDef.gInvViewportHeight.set(mMeshGizmoBuffer, invViewportHeight);
- gGizmoParamBlockDef.gViewportYFlip.set(mMeshGizmoBuffer, viewportYFlip);
- for (auto& entry : meshes)
- {
- UINT32 typeIdx = (UINT32)entry.type;
- gRendererUtility().setPass(mMeshMaterials[typeIdx]);
- gRendererUtility().setPassParams(mMeshParamSets[typeIdx][entry.paramsIdx]);
- gRendererUtility().draw(entry.mesh, entry.subMesh);
- }
- }
- else
- {
- // Allocate and assign GPU program parameter objects
- UINT32 pickingCounters[2];
- bs_zero_out(pickingCounters);
- for (auto& entry : meshes)
- {
- UINT32 typeIdx = entry.type == GizmoMeshType::Text ? 1 : 0;
- UINT32 paramsIdx = pickingCounters[typeIdx];
- entry.paramsIdx = paramsIdx;
- if (paramsIdx >= mPickingParamSets[typeIdx].size())
- {
- SPtr<GpuParamsSet> paramsSet = mPickingMaterials[typeIdx]->createParamsSet();
- paramsSet->setParamBlockBuffer("Uniforms", mMeshPickingParamBuffer, true);
- mPickingParamSets[typeIdx].push_back(paramsSet);
- }
- pickingCounters[typeIdx]++;
- }
- for (auto& iconData : *iconRenderData)
- {
- iconData.paramsIdx = pickingCounters[1];
- if (iconData.paramsIdx >= mPickingParamSets[1].size())
- {
- SPtr<GpuParamsSet> paramsSet = mPickingMaterials[1]->createParamsSet();
- paramsSet->setParamBlockBuffer("Uniforms", mIconPickingParamBuffer, true);
- mPickingParamSets[1].push_back(paramsSet);
- }
- pickingCounters[1]++;
- }
- gGizmoPickingParamBlockDef.gMatViewProj.set(mMeshPickingParamBuffer, viewProjMat);
- gGizmoPickingParamBlockDef.gAlphaCutoff.set(mMeshPickingParamBuffer, PICKING_ALPHA_CUTOFF);
- for (auto& entry : meshes)
- {
- UINT32 typeIdx = entry.type == GizmoMeshType::Text ? 1 : 0;
- gRendererUtility().setPass(mPickingMaterials[typeIdx]);
- gRendererUtility().setPassParams(mPickingParamSets[typeIdx][entry.paramsIdx]);
- gRendererUtility().draw(entry.mesh, entry.subMesh);
- }
- }
- if (iconMesh != nullptr)
- renderIconGizmos(screenArea, iconMesh, iconRenderData, usePickingMaterial);
- }
- void GizmoRenderer::renderIconGizmos(Rect2I screenArea, SPtr<MeshBase> mesh,
- GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial)
- {
- RenderAPI& rapi = RenderAPI::instance();
- SPtr<VertexData> vertexData = mesh->getVertexData();
- rapi.setVertexDeclaration(vertexData->vertexDeclaration);
- auto vertexBuffers = vertexData->getBuffers();
- SPtr<VertexBuffer> vertBuffers[1] = { vertexBuffers.begin()->second };
- rapi.setVertexBuffers(0, vertBuffers, 1);
- SPtr<IndexBuffer> indexBuffer = mesh->getIndexBuffer();
- rapi.setIndexBuffer(indexBuffer);
- rapi.setDrawOperation(DOT_TRIANGLE_LIST);
- // Set up ortho matrix
- Matrix4 projMat;
- const RenderAPIInfo& rapiInfo = rapi.getAPIInfo();
- float left = screenArea.x + rapiInfo.getHorizontalTexelOffset();
- float right = screenArea.x + screenArea.width + rapiInfo.getHorizontalTexelOffset();
- float top = screenArea.y + rapiInfo.getVerticalTexelOffset();
- float bottom = screenArea.y + screenArea.height + rapiInfo.getVerticalTexelOffset();
- float near = rapiInfo.getMinimumDepthInputValue();
- float far = rapiInfo.getMaximumDepthInputValue();
- // Top/bottom have been swapped because we're moving from window coordinates (origin top left)
- // to normalized device coordinates (origin bottom left)
- // Negative near/far because Z is flipped for normalized device coordinates
- // (positive Z goes into screen as opposed to view space here we're looking along negative Z)
- projMat.makeProjectionOrtho(left, right, top, bottom, -near, -far);
- if (!usePickingMaterial)
- {
- gGizmoParamBlockDef.gMatViewProj.set(mIconGizmoBuffer, projMat);
- gGizmoParamBlockDef.gViewDir.set(mIconGizmoBuffer, Vector4::ZERO);
- for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
- {
- gRendererUtility().setPass(mIconMaterial, passIdx);
-
- UINT32 curIndexOffset = mesh->getIndexOffset();
- for (auto curRenderData : *renderData)
- {
- gRendererUtility().setPassParams(mIconParamSets[curRenderData.paramsIdx], passIdx);
- rapi.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
- curIndexOffset += curRenderData.count * 6;
- }
- }
- }
- else
- {
- gGizmoPickingParamBlockDef.gMatViewProj.set(mIconPickingParamBuffer, projMat);
- gGizmoPickingParamBlockDef.gAlphaCutoff.set(mIconPickingParamBuffer, PICKING_ALPHA_CUTOFF);
- for (auto& iconData : *renderData)
- {
- SPtr<GpuParamsSet> paramsSet = mPickingParamSets[1][iconData.paramsIdx];
- SPtr<GpuParams> params = paramsSet->getGpuParams();
- GpuParamTexture textureParam;
- params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", textureParam);
- textureParam.set(iconData.texture);
- }
- gRendererUtility().setPass(mPickingMaterials[1]);
- UINT32 curIndexOffset = mesh->getIndexOffset();
- for (auto curRenderData : *renderData)
- {
- gRendererUtility().setPassParams(mPickingParamSets[1][curRenderData.paramsIdx]);
- rapi.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
- curIndexOffset += curRenderData.count * 6;
- }
- }
- mesh->_notifyUsedOnGPU();
- }
- }
- }
|