1#include "PickingBeamComponent.h"
4#include "Input/InputManager.h"
5#include "Resources/MaterialManager.h"
7#include "Components/Geometry/MeshGeneratorComponent.h"
8#include "Components/Core/MeshRenderComponent.h"
10#include "Scene/RayPick.h"
11#include "EntityStore.h"
13#include "Systems/Core/CameraSystem.h"
14#include "Systems/Core/TransformSystem.h"
15#include "Systems/Core/SceneSystem.h"
17#include "Foundation/Logging/Logger.h"
18#include "Foundation/Reflection/Method.h"
20#include <glm/gtx/matrix_decompose.hpp>
34void Cogs::Core::PickingBeamComponent::registerType()
46 Method(
Name(
"initialize"), &PickingBeamComponent::initialize),
47 Method(
Name(
"update"), &PickingBeamComponent::update),
50 DynamicComponent::registerDerivedType<PickingBeamComponent>().setMethods(methods).setFields(fields);
55 this->context = context;
60 aimTransform = getComponentHandle<TransformComponent>();
67 MaterialHandle defaultMat = context->materialManager->getDefaultMaterial();
68 MaterialInstanceHandle materialInstance = context->materialInstanceManager->createMaterialInstance(defaultMat);
69 materialInstance->
setOption(
"CullMode",
"None");
70 materialInstance->
setOption(
"Transparency",
"On");
71 materialInstance->
setOption(
"DepthTestEnabled", enableDepthTest ?
"On" :
"Off");
74 beamMeshRender->
material = materialInstance;
79 beamRender->
drawOrder = transparencyLayer;
84 beamGeometryTrans->
position = glm::vec3(0, 0, -2500);
85 beamGeometryTrans->scale = glm::vec3(0.002f, 0.002f, 2500);
86 beamGeometryTrans->setChanged();
89 beamTransform->
position = beamOffset;
90 beamTransform->
rotation = beamRotation;
94 beamVisibility->
visible =
false;
114 destinationMarker = context->
store->
findEntity(
"destinationMarker", getContainer());
115 if (!destinationMarker) {
120 destGeometry = context->
store->
createChildEntity(
"BasicMeshGeneratorEntity", destinationMarker.get(),
"destinationSphere");
126 destMeshRender->
material = materialInstance;
131 destRender->
drawOrder = transparencyLayer;
135 destTransform->
scale = glm::vec3(0.25f);
143 destVis->visible =
false;
144 destVis->setChanged();
146 mainCamera = context->cameraSystem->getMainCamera()->getComponentHandle<
CameraComponent>();
149void Cogs::Core::PickingBeamComponent::update()
152 auto destVis = destinationScene.resolveComponent<
SceneComponent>();
158 materialInstance->setProperty(
"diffuseColor", &diffuseColor,
sizeof(glm::vec4));
159 materialInstance->setProperty(
"emissiveColor", &emissiveColor,
sizeof(glm::vec3));
162 context->transformSystem->
update(context);
163 context->cameraSystem->update(context);
167 beamVis->visible =
true;
168 beamVis->setChanged();
169 context->sceneSystem->updateState(*beamVis);
175 auto size = beamCam->viewportSize;
180 const glm::vec2 windowPosition(size.x / 2, size.y / 2);
181 std::vector<RayPicking::RayPickHit> hits;
186 lastPickedEntity = hits.front().entity;
187 lastPickedPos = context->transformSystem->
getOrigin() + glm::dvec3(hits.front().position);
189 glm::vec3 scale, trans, skew;
192 glm::decompose(context->transformSystem->getLocalToWorld(aimTrans), scale, orient, trans, skew, persp);
194 dist = glm::distance(hits.front().position, trans);
197 destTrans->coordinates = lastPickedPos;
198 destTrans->setChanged();
204 context->sceneSystem->updateState(*destVis);
208 lastPickedEntity = NoEntity;
209 lastPickedPos = glm::dvec3(0);
213 context->sceneSystem->updateState(*destVis);
218 beamTrans->scale = glm::vec3(0.002f, 0.002f, dist / 2);
219 beamTrans->position = glm::vec3(0, 0, -dist / 2);
220 beamTrans->setChanged();
227 beamVis->visible =
false;
228 beamVis->setChanged();
231 context->sceneSystem->updateState(*beamVis);
232 context->sceneSystem->updateState(*destVis);
void setChanged()
Sets the component to the ComponentFlags::Changed state with carry.
ComponentType * getComponent() const
ComponentHandle getComponentHandle() const
CameraFlags flags
Camera behavior flags.
float fieldOfView
Vertical field of view, given in radians.
bool enableClippingPlaneAdjustment
If automatic adjustment of the clipping planes should be performed to fit as much of the scene as pos...
glm::vec2 viewportSize
Size of the viewport covered by this instance, given in pixels.
ProjectionMode projectionMode
The projection mode to use for the camera.
float nearPlaneLimit
Smallest value allowed to adjust near plane to.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
class EntityStore * store
Entity store.
std::unique_ptr< class RayPicking > rayPicking
RayPicking service instance.
ComponentModel::Component * addComponent(ComponentModel::Entity *entity, Reflection::TypeId typeId)
Add a component of the given type to the entity.
EntityPtr createChildEntity(const StringView &type, ComponentModel::Entity *parent, const StringView &name=StringView())
Create a new Entity, parenting it to the given parent.
EntityPtr findEntity(const StringView &name, const ComponentModel::Entity *root=nullptr, EntityFind findOptions=EntityFind::Default) const
Finds an entity with the given name.
Renders the contents of a MeshComponent using the given materials.
MaterialInstanceHandle material
Material used to render the mesh.
glm::quat beamRotation
Rotation of the beam relative to the parent transform.
glm::dvec3 lastPickedPos
World coordinates of the last raypick hit.
bool active
whether the beam is active and should be visible and update the picked position each frame
glm::vec4 diffuseColor
diffuse color of the beam and marker
EntityId lastPickedEntity
Last entity picked.
glm::vec4 emissiveColor
emissive color of the beam and marker
glm::vec3 beamOffset
Offset of the beam starting point relative to the parent transform.
Base component for all rendering content.
int32_t drawOrder
Draw order within a render bucke.
constexpr void unsetRenderFlag(RenderFlags flag)
Unset the given flag.
Contains information on how the entity behaves in the scene.
bool visible
If the entity this component is a member of should be visible.
bool pickable
If the entity this component is a member of should be pickable.
Log implementation class.
Field definition describing a single data member of a data structure.
Simple method definition.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
@ Closest
Return just the closest hit.
@ CastShadows
Casts shadows.
@ EnablePicking
Supports picking.
@ None
No flags specified,.
@ Perspective
Perspective projection.
@ Cylinder
Generic cylinder shape.
@ Sphere
Generic sphere shape.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains reflection support.
void setChanged(Cogs::Core::Context *context, Cogs::ComponentModel::Component *component, Reflection::FieldId fieldId)
Must be Called after changing a Component field. Mark field changed. Request engine update.
void setOption(const StringView &key, const StringView &value)
Sets the option with the given key to a value parsed from the value string.
Generates mesh data and assigns the generated mesh to a MeshComponent on the same entity.
Represents an unique name.