| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845 |
- #include "BsGizmoManager.h"
- #include "BsMesh.h"
- #include "BsAABox.h"
- #include "BsSphere.h"
- #include "BsVertexDataDesc.h"
- #include "BsShapeMeshes3D.h"
- #include "BsMeshHeap.h"
- #include "BsCamera.h"
- #include "BsSpriteTexture.h"
- #include "BsCoreThread.h"
- #include "BsBuiltinEditorResources.h"
- #include "BsMaterial.h"
- #include "BsGpuParams.h"
- #include "BsRenderAPI.h"
- #include "BsCoreRenderer.h"
- #include "BsTransientMesh.h"
- #include "BsRendererManager.h"
- #include "BsDrawHelper.h"
- using namespace std::placeholders;
- namespace BansheeEngine
- {
- const UINT32 GizmoManager::VERTEX_BUFFER_GROWTH = 4096;
- const UINT32 GizmoManager::INDEX_BUFFER_GROWTH = 4096 * 2;
- 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), mDrawHelper(nullptr), mPickingDrawHelper(nullptr), mCore(nullptr), mCurrentIdx(0)
- {
- mTransform = Matrix4::IDENTITY;
- mDrawHelper = bs_new<DrawHelper>();
- mPickingDrawHelper = bs_new<DrawHelper>();
- mIconVertexDesc = bs_shared_ptr<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);
- mIconMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mIconVertexDesc);
- HMaterial solidMaterial = BuiltinEditorResources::instance().createSolidGizmoMat();
- HMaterial wireMaterial = BuiltinEditorResources::instance().createWireGizmoMat();
- HMaterial iconMaterial = BuiltinEditorResources::instance().createIconGizmoMat();
- HMaterial pickingMaterial = BuiltinEditorResources::instance().createGizmoPickingMat();
- HMaterial alphaPickingMaterial = BuiltinEditorResources::instance().createAlphaGizmoPickingMat();
- CoreInitData initData;
- initData.solidMat = solidMaterial->getCore();
- initData.wireMat = wireMaterial->getCore();
- initData.iconMat = iconMaterial->getCore();
- initData.pickingMat = pickingMaterial->getCore();
- initData.alphaPickingMat = alphaPickingMaterial->getCore();
- mCore = bs_new<GizmoManagerCore>(GizmoManagerCore::PrivatelyConstuct());
- gCoreAccessor().queueCommand(std::bind(&GizmoManager::initializeCore, this, initData));
- }
- GizmoManager::~GizmoManager()
- {
- if (mIconMesh != nullptr)
- mIconMeshHeap->dealloc(mIconMesh);
- bs_delete(mDrawHelper);
- bs_delete(mPickingDrawHelper);
- gCoreAccessor().queueCommand(std::bind(&GizmoManager::destroyCore, this, mCore));
- }
- void GizmoManager::initializeCore(const CoreInitData& initData)
- {
- mCore->initialize(initData);
- }
- void GizmoManager::destroyCore(GizmoManagerCore* core)
- {
- bs_delete(core);
- }
- void GizmoManager::startGizmo(const HSceneObject& gizmoParent)
- {
- mActiveSO = gizmoParent;
- }
- void GizmoManager::endGizmo()
- {
- mActiveSO = nullptr;
- }
- void GizmoManager::setColor(const Color& color)
- {
- mDrawHelper->setColor(color);
- mColor = color;
- }
- void GizmoManager::setTransform(const Matrix4& transform)
- {
- mDrawHelper->setTransform(transform);
- mTransform = transform;
- }
- 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::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::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::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::update(const CameraHandlerPtr& camera)
- {
- mDrawHelper->clearMeshes();
- if (mIconMesh != nullptr)
- mIconMeshHeap->dealloc(mIconMesh);
- RenderTargetPtr rt = camera->getViewport()->getTarget();
- IconRenderDataVecPtr iconRenderData;
- mDrawHelper->buildMeshes();
- const Vector<DrawHelper::ShapeMeshData>& meshes = mDrawHelper->getMeshes();
- SPtr<MeshCoreBase> solidMesh = nullptr;
- SPtr<MeshCoreBase> wireMesh = nullptr;
- for (auto& meshData : meshes)
- {
- if (meshData.type == DrawHelper::MeshType::Solid)
- {
- if (solidMesh == nullptr)
- solidMesh = meshData.mesh->getCore();
- }
- else // Wire
- {
- if (wireMesh == nullptr)
- wireMesh = meshData.mesh->getCore();
- }
- }
- // Since there is no sorting used with draw helper meshes we only expect up to two of them,
- // one for solids, one for wireframe
- assert(meshes.size() <= 2);
- mIconMesh = buildIconMesh(camera, mIconData, false, iconRenderData);
- SPtr<MeshCoreBase> iconMesh = mIconMesh->getCore();
- gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, mCore, rt->getCore(),
- solidMesh, wireMesh, iconMesh, iconRenderData));
- }
- void GizmoManager::renderForPicking(const CameraHandlerPtr& 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& lineDataEntry : mLineData)
- {
- if (!lineDataEntry.pickable)
- continue;
- mPickingDrawHelper->setColor(idxToColorCallback(lineDataEntry.idx));
- mPickingDrawHelper->setTransform(lineDataEntry.transform);
- mPickingDrawHelper->line(lineDataEntry.start, lineDataEntry.end);
- }
- 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& iconDataEntry : mIconData)
- {
- if (!iconDataEntry.pickable)
- continue;
- iconData.push_back(iconDataEntry);
- iconData.back().color = idxToColorCallback(iconDataEntry.idx);
- }
- mPickingDrawHelper->buildMeshes();
- const Vector<DrawHelper::ShapeMeshData>& meshes = mPickingDrawHelper->getMeshes();
- TransientMeshPtr iconMesh = buildIconMesh(camera, iconData, true, iconRenderData);
- // Note: This must be rendered while Scene view is being rendered
- Matrix4 viewMat = camera->getViewMatrix();
- Matrix4 projMat = camera->getProjectionMatrixRS();
- ViewportPtr viewport = camera->getViewport();
- for (auto& meshData : meshes)
- {
- if (meshData.type == DrawHelper::MeshType::Solid)
- {
- gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos,
- mCore, viewMat, projMat, meshData.mesh->getCore(), GizmoMaterial::Picking));
- }
- else // Wire
- {
- gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos,
- mCore, viewMat, projMat, meshData.mesh->getCore(), GizmoMaterial::Picking));
- }
- }
- Rect2I screenArea = camera->getViewport()->getArea();
- gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderIconGizmos,
- mCore, screenArea, iconMesh->getCore(), iconRenderData, true));
- mPickingDrawHelper->clearMeshes();
- mIconMeshHeap->dealloc(iconMesh);
- }
- void GizmoManager::clearGizmos()
- {
- mSolidCubeData.clear();
- mWireCubeData.clear();
- mSolidSphereData.clear();
- mWireSphereData.clear();
- mLineData.clear();
- mFrustumData.clear();
- mIconData.clear();
- mIdxToSceneObjectMap.clear();
- mDrawHelper->clear();
- mCurrentIdx = 0;
- }
- void GizmoManager::clearRenderData()
- {
- mDrawHelper->clearMeshes();
- if (mIconMesh != nullptr)
- mIconMeshHeap->dealloc(mIconMesh);
- mIconMesh = nullptr;
- IconRenderDataVecPtr iconRenderData = bs_shared_ptr<IconRenderDataVec>();
- gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, mCore, nullptr, nullptr, nullptr, nullptr, iconRenderData));
- }
- TransientMeshPtr GizmoManager::buildIconMesh(const CameraHandlerPtr& 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) // 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;
- });
- MeshDataPtr meshData = bs_shared_ptr<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()->getHeight() / camera->getOrthoWindowHeight();
- else
- {
- Radian vertFOV(Math::tan(camera->getHorzFOV() * 0.5f));
- cameraScale = (camera->getViewport()->getHeight() * 0.5f) / vertFOV.valueRadians();
- }
- iconRenderData = bs_shared_ptr<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;
- }
- return mIconMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
- }
- 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 CameraHandlerPtr& camera,
- UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
- {
- normalColor = tint;
- if (!fixedScale)
- {
- float iconToScreenRatio = iconHeight / (float)camera->getViewport()->getHeight();
- if (iconToScreenRatio > 0.3f)
- {
- float alpha = 1.0f - Math::lerp01(iconToScreenRatio, 0.3f, 1.0f);
- normalColor.a *= alpha;
- }
- else if (iconToScreenRatio < 0.1f)
- {
- float alpha = Math::lerp01(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();
- }
- const float GizmoManagerCore::PICKING_ALPHA_CUTOFF = 0.5f;
- GizmoManagerCore::GizmoManagerCore(const PrivatelyConstuct& dummy)
- {
- }
- void GizmoManagerCore::initialize(const GizmoManager::CoreInitData& initData)
- {
- THROW_IF_NOT_CORE_THREAD;
- mSolidMaterial.mat = initData.solidMat;
- mWireMaterial.mat = initData.wireMat;
- mIconMaterial.mat = initData.iconMat;
- mPickingMaterial.mat = initData.pickingMat;
- mAlphaPickingMaterial.mat = initData.alphaPickingMat;
- {
- SPtr<MaterialCore> mat = mWireMaterial.mat;
- SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
- vertParams->getParam("matViewProj", mWireMaterial.mViewProj);
- }
- {
- SPtr<MaterialCore> mat = mSolidMaterial.mat;
- SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
- vertParams->getParam("matViewProj", mSolidMaterial.mViewProj);
- }
- {
- SPtr<MaterialCore> mat = mIconMaterial.mat;
- SPtr<PassParametersCore> pass0Params = mat->getPassParameters(0);
- SPtr<PassParametersCore> pass1Params = mat->getPassParameters(1);
- SPtr<GpuParamsCore> vertParams0 = pass0Params->mVertParams;
- SPtr<GpuParamsCore> vertParams1 = pass1Params->mVertParams;
- vertParams0->getParam("matViewProj", mIconMaterial.mViewProj[0]);
- vertParams1->getParam("matViewProj", mIconMaterial.mViewProj[1]);
- mIconMaterial.mFragParams[0] = pass0Params->mFragParams;
- mIconMaterial.mFragParams[1] = pass1Params->mFragParams;
- mIconMaterial.mFragParams[0]->getTextureParam("mainTexture", mIconMaterial.mTexture[0]);
- mIconMaterial.mFragParams[1]->getTextureParam("mainTexture", mIconMaterial.mTexture[1]);
- }
- {
- SPtr<MaterialCore> mat = mPickingMaterial.mat;
- SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
- vertParams->getParam("matViewProj", mPickingMaterial.mViewProj);
- }
- {
- SPtr<MaterialCore> mat = mAlphaPickingMaterial.mat;
- SPtr<PassParametersCore> passParams = mat->getPassParameters(0);
- SPtr<GpuParamsCore> vertParams = passParams->mVertParams;
- vertParams->getParam("matViewProj", mAlphaPickingMaterial.mViewProj);
- mAlphaPickingMaterial.mFragParams = passParams->mFragParams;
- mAlphaPickingMaterial.mFragParams->getTextureParam("mainTexture", mAlphaPickingMaterial.mTexture);
- GpuParamFloatCore alphaCutoffParam;
- mAlphaPickingMaterial.mFragParams->getParam("alphaCutoff", alphaCutoffParam);
- alphaCutoffParam.set(PICKING_ALPHA_CUTOFF);
- }
- CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
- activeRenderer->onCorePostRenderViewport.connect(std::bind(&GizmoManagerCore::render, this, _1));
- }
- void GizmoManagerCore::updateData(const SPtr<RenderTargetCore>& rt, const SPtr<MeshCoreBase>& solidMesh, const SPtr<MeshCoreBase>& wireMesh,
- const SPtr<MeshCoreBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
- {
- mSceneRenderTarget = rt;
- mSolidMesh = solidMesh;
- mWireMesh = wireMesh;
- mIconMesh = iconMesh;
- mIconRenderData = iconRenderData;
- }
- void GizmoManagerCore::render(const CameraHandlerCore& camera)
- {
- if (mSceneRenderTarget == nullptr)
- return;
- if (camera.getViewport()->getTarget() != mSceneRenderTarget)
- return;
- float width = (float)mSceneRenderTarget->getProperties().getWidth();
- float height = (float)mSceneRenderTarget->getProperties().getHeight();
- Rect2 normArea = camera.getViewport()->getNormArea();
- Rect2I screenArea;
- screenArea.x = (int)(normArea.x * width);
- screenArea.y = (int)(normArea.y * height);
- screenArea.width = (int)(normArea.width * width);
- screenArea.height = (int)(normArea.height * height);
- if (mSolidMesh != nullptr)
- renderGizmos(camera.getViewMatrix(), camera.getProjectionMatrixRS(), mSolidMesh, GizmoManager::GizmoMaterial::Solid);
- if (mWireMesh != nullptr)
- renderGizmos(camera.getViewMatrix(), camera.getProjectionMatrixRS(), mWireMesh, GizmoManager::GizmoMaterial::Wire);
- if (mIconMesh != nullptr)
- renderIconGizmos(screenArea, mIconMesh, mIconRenderData, false);
- }
- void GizmoManagerCore::renderGizmos(Matrix4 viewMatrix, Matrix4 projMatrix, SPtr<MeshCoreBase> mesh, GizmoManager::GizmoMaterial material)
- {
- THROW_IF_NOT_CORE_THREAD;
- Matrix4 viewProjMat = projMatrix * viewMatrix;
- switch (material)
- {
- case GizmoManager::GizmoMaterial::Solid:
- mSolidMaterial.mViewProj.set(viewProjMat);
- CoreRenderer::setPass(mSolidMaterial.mat, 0);
- break;
- case GizmoManager::GizmoMaterial::Wire:
- mWireMaterial.mViewProj.set(viewProjMat);
- CoreRenderer::setPass(mWireMaterial.mat, 0);
- break;
- case GizmoManager::GizmoMaterial::Picking:
- mPickingMaterial.mViewProj.set(viewProjMat);
- CoreRenderer::setPass(mPickingMaterial.mat, 0);
- break;
- }
-
- CoreRenderer::draw(mesh, mesh->getProperties().getSubMesh(0));
- }
- void GizmoManagerCore::renderIconGizmos(Rect2I screenArea, SPtr<MeshCoreBase> mesh, GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial)
- {
- RenderAPICore& rs = RenderAPICore::instance();
- const MeshProperties& meshProps = mesh->getProperties();
- std::shared_ptr<VertexData> vertexData = mesh->getVertexData();
- rs.setVertexDeclaration(vertexData->vertexDeclaration);
- auto vertexBuffers = vertexData->getBuffers();
- SPtr<VertexBufferCore> vertBuffers[1] = { vertexBuffers.begin()->second };
- rs.setVertexBuffers(0, vertBuffers, 1);
- SPtr<IndexBufferCore> indexBuffer = mesh->getIndexBuffer();
- rs.setIndexBuffer(indexBuffer);
- rs.setDrawOperation(DOT_TRIANGLE_LIST);
- // Set up ortho matrix
- Matrix4 projMat;
- float left = screenArea.x + rs.getHorizontalTexelOffset();
- float right = screenArea.x + screenArea.width + rs.getHorizontalTexelOffset();
- float top = screenArea.y + rs.getVerticalTexelOffset();
- float bottom = screenArea.y + screenArea.height + rs.getVerticalTexelOffset();
- float near = rs.getMinimumDepthInputValue();
- float far = rs.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)
- {
- mIconMaterial.mViewProj[0].set(projMat);
- mIconMaterial.mViewProj[1].set(projMat);
- for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
- {
- CoreRenderer::setPass(mIconMaterial.mat, passIdx);
- UINT32 curIndexOffset = mesh->getIndexOffset();
- for (auto curRenderData : *renderData)
- {
- mIconMaterial.mTexture[passIdx].set(curRenderData.texture);
- rs.bindGpuParams(GPT_FRAGMENT_PROGRAM, mIconMaterial.mFragParams[passIdx]);
- rs.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
- curIndexOffset += curRenderData.count * 6;
- }
- }
- }
- else
- {
- mAlphaPickingMaterial.mViewProj.set(projMat);
- CoreRenderer::setPass(mAlphaPickingMaterial.mat, 0);
- UINT32 curIndexOffset = 0;
- for (auto curRenderData : *renderData)
- {
- mAlphaPickingMaterial.mTexture.set(curRenderData.texture);
- rs.bindGpuParams(GPT_FRAGMENT_PROGRAM, mAlphaPickingMaterial.mFragParams);
- rs.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
- curIndexOffset += curRenderData.count * 6;
- }
- }
- mesh->_notifyUsedOnGPU();
- }
- }
|