UE realizes the mouse click model

wedge

In a twinned scene, clicking on a 3D object is a common operation. For example, click on the model to display related attributes and pictures, click on the camera model to play the video, click on the building to expand the floor, and so on.

Therefore, the point-and-click model is the most necessary basic capability for digital twins.

Prepare knowledge

UE blueprint introduction

This article will involve some knowledge of blueprints. If you don't know blueprints, you need to understand UE's blueprints first. Blueprints  are Unreal Engine 4's approach to visual scripting. That is, tasks that would normally be accomplished by writing scripts can now be created through a graph of nodes and connections without having to output any actual code.

For basic knowledge about blueprints, you can refer to official documents.
https://docs.unrealengine.com/4.27/zh-CN/ProgrammingAndScripting/Blueprints/
https://docs.unrealengine.com/4.27/zh-CN/Resources/Showcases/BlueprintExamples/
Follow-up will also write related articles Blueprint knowledge.

mathematical theory

The technical theory of the UE mouse click model is roughly like this:

  1. Obtain the position and direction of the mouse click (involving the conversion of the mouse position to three-dimensional space coordinates, which can be obtained by setting the built-in method)

  2. Fires a ray with position and direction.

  3. Determine which objects the ray intersects (you can use the built-in method to obtain it), and take the closest intersecting model, which is the 3D object obtained by clicking the mouse.

If you are familiar with threejs, you know that this is somewhat similar to Threejs' raycaster.

Overload Pawn

Our Blueprint is implemented in an overloaded Pawn class, which  is the base class for all Actors that can be controlled by the player or AI. A Pawn is the embodiment of a player or AI entity in a game scene. This means that the Pawn not only determines how the player or AI entities look, but also how they collide with the scene and interact with other physics. Some games may not have a player model or avatar visible in the game, so this can be confusing in some cases. Regardless, however, the Pawn still represents the player's or entity's physical orientation, rotation, etc. in the game. A Character  is a special kind of Pawn that can walk.

For more on Pawn, Pawn

Right-click in the content browser and create a new blueprint -> blueprint class:

image.png

Choose a Pawn:

image.png

Then enter the name in the browser:

image.png

Double-click the newly created blueprint class to enter the blueprint editing page.

image.png

Basic operations related to blueprints, such as adding nodes, moving nodes, connecting lines, etc., are not described in detail here.

monitor mouse

Listen to the mouse event in the blueprint (this article is the right button) as follows:

image.png

Pressed means pressed, Released means released.

Get the mouse position and orientation

Blueprint node "Transform Mouse Position to Scene Space" that gets the mouse position

 

image.png

The target is the player controller, and the player controller is obtained through the following node:

image.png

construction ray

The node "convert mouse position to scene space" can get the world coordinates where the mouse is located and the forward direction, which are set as origin and direction respectively. Where origin is the origin of the ray, and the end point of the ray can be obtained through vector calculation:

end = origin + directon * length

Among them, length is a constant, we can specify it, so the blueprint for calculating end is as follows:

image.png

This involves a Blueprint node that multiplies a vector by a constant, and a node that adds two vectors. First, the world direction is multiplied by a constant 10000, and the calculation result is added to the world location to get the end point.

The start point and end point will eventually be used as input values ​​for the next calculation.

Obtain inspection results by ray

The node that gets the click result by ray is "Detect lines by channel"

image.png

in:

  • start indicates the starting point of the ray

  • end indicates the focus of the ray

  • Out Hit indicates detected objects

  • Return Value is a bool, true means there is an object hit, false means no object hit.

interrupt hit result

The meaning of the so-called interrupt hit result can be understood to split the hit packaging result item by item.
Firstly, it is judged by the return value of the detection result, the hit result is interrupted, and the conditional judgment is carried out through the branch node:

image.png

  • Condition represents the input condition,

  • True means execution when the condition is true

  • False means execution when the condition is false

In this example, when the condition is true, the interrupt result is executed:

image.png

Get result information

Among the hit results above:

  • Hit Actor represents the actor that was hit

  • Hit Component The component in the actor that was hit, if there is a subcomponent mesh that can be detected by the ray

After obtaining the relevant information, you can perform relevant operations. The relevant information is printed here as follows:

image.png

show mouse cursor

After running the program by default, the mouse cursor is not displayed. In order to see the click point clearly, the mouse cursor needs to be displayed. For example, press the tap key to display the cursor, as follows:

image.png

Set up Pawns

After rewriting the Pawn class, in the program settings, you need to change the Pawn of the model to our rewritten Pawn class to take effect, as shown in the figure below:

image.png

epilogue

This article illustrates the functionality of the ray-by-ray method for detecting mouse click models. The final effect is shown in the figure below:

image.png

When clicked, print the name of the corresponding component.

Welcome to pay attention to ITman Biaoshu, a visualization service expert, providing 3D visualization, data center, smart park, smart building, industrial configuration, 2D topology, large-screen presentation and other visualization professional services.

Guess you like

Origin blog.csdn.net/netcy/article/details/126591030