Disclosure of Invention
The invention aims to provide a GIS digital twin system based on Unity3D, which uses virtual 3D data to map real life, ensures that the number is coordinated with the physical world, can carry out real-time monitoring, data analysis and various simulations based on a digital model, and solves the defects of the prior art.
The technical scheme includes that the GIS digital twin system based on Unity3D comprises a resource downloading module, a view angle control module, a view angle transition module, a model interaction module, a monitoring management module and an account management module, wherein the resource downloading module downloads resources from a server and decompresses the resources, the view angle control module is used for zooming in and out the view angle when the view angle is monitored in a full screen mode, the view angle transition module is used for zooming out the view angle when the view angle is monitored in a full screen mode, zooming in and out the view angle when the view angle is monitored in a full screen mode, the model interaction module is used for clicking building display information and highlighting, clicking the floor to highlight, entering the floor to click the room display information and highlighting, the monitoring management module is used for controlling and displaying or hiding the monitoring button in a button mode, clicking the monitoring display information and realizing account management, and the account management module is used for interfacing a user database.
The resource downloading module specifically comprises:
Step 1.1, storing a compressed resource file in a server, wherein the resource file comprises resources such as a 3D model, a video and the like;
Step 1.2, in the operation initialization stage of the digital twin system, judging whether the downloading of the resource file is completed or not, and checking the version number of the resource file; when the resources are not downloaded or the version numbers are inconsistent, the information of the resource files to be downloaded is sent to the server, the latest version of the resource files are downloaded from the server, and the latest version of the resource files are decompressed to the appointed catalogue after the downloading is completed;
and 1.3, downloading the resource file for use with hot update, wherein when the program changes UI display or modifies program logic, a developer does not repackage the program, and a user does not download the client again.
The visual angle control module specifically comprises:
Step 2.1, a user-defined creation view angle control script Click2Rotate.cs defines a rotating object target, a moving speed moveSpeed, a rotating speed rotateSpeed and a zooming speed zoomSpeed;
Step 2.2, a camera movement function cameraRotate () is customized in an Update () function of a view angle control script click2 rotation.cs, movement information of an X axis and a Y axis of a mouse is obtained through input.GetAxis () of a Unity editor, so that the position of a main camera is changed, and the calculation formula is as follows:
mainCamera.transform.Translate(Vector3.left*(mouse_x*moveSpeed)*
Time.deltaTime);
mainCamera.transform.Translate(Vector3.up*(mouse_y*moveSpeed)*
Time.deltaTime);
wherein MAINCAMERA is the main camera, transform () is a function of the Unity editor, vector3 represents a three-dimensional Vector, left is a left Vector of the main camera, up is an up Vector of the main camera, mouse_x is movement information of the mouse X axis, mouse_y is movement information of the mouse Y axis, moveSpeed is movement speed, and time.
When the right button of the mouse is pressed and moved, the position of the main camera is moved according to the moving speed, and the function of moving the visual angle is completed;
Step 2.3, the rotation view function cameraZoom () is customized in the Update () function of the view control script, and its calculation formula is as follows:
mainCamera.transform.RotateAround(transform.position,Vector3.up,mouse_x*rotateSpeed);
mainCamera.transform.RotateAround(transform.position,Vector3.right,mouse_y*rotateSpeed);
Wherein transform. Rotation around () is the surrounding rotation function of the Unity editor, transform. Position is the main camera position coordinates, right is the right vector of the main camera, rotateSpeed is the rotation speed;
when the left button of the mouse is pressed and moved, the visual angle of the camera is rotated according to the rotation speed;
step 2.4, customizing a view scaling function in an Update () function of the view control script, wherein the calculation formula is as follows:
When input.GetAxis ("Mouse ScrollWheel") >0, the formula is
mainCamera.transform.Translate(Vector3.forward*zoomSpeed);
When input. Getaxis ("Mouse ScrollWheel") <0, the formula is
mainCamera.transform.Translate(Vector3.forward*-zoomSpeed);
The input GetAxis ("Mouse ScrollWheel") is a Unity editor, the rolling information function of the mouse wheel is obtained, forward is the forward vector of the main camera, and zoomSpeed is the zoom speed;
when the mouse wheel rolls, the main camera is moved forward or backward towards the camera according to the zoom speed, so as to finish zooming the visual angle;
And 2.5, adding a view control script to the main camera in the Unity editor, setting values of a moving speed, a rotating speed and a zooming speed, and giving the main camera to the rotating object.
The visual angle transition module specifically comprises:
Step 3.1, creating a zoom-in view script CamaraFollow.cs by user definition, and defining a target object, a target object moving speed, a target object rotating speed, a minimum angle and a minimum distance;
Step 3.2, acquiring the real-time position and the main camera position of the target object in LateUpdate () function of the zoom-in view script camera focus, calculating the direction vector from the main camera to the target object through the real-time position and the main camera real-time position of the target object, calculating the angle between the main camera direction and the direction vector from the main camera to the target object, and calculating the distance between the real-time position and the main camera real-time position of the target object;
Step 3.3, when the main camera orientation and the angle between the main camera and the direction vector of the target object > minimum angle, rotating the main camera view angle by Lerp () interpolation function, the main camera view angle is denoted camRoatae, the calculation formula is as follows:
camRotate=Quaternion.Lerp(transform.rotation,dir,rotateSpeed*Time.deltaTime)
Wherein, quaternion. Lerp () is an interpolation function, transform. Rotation is a real-time view angle of the main camera, dir is a direction vector from the main camera to the target object, rotateSpeed is a target object rotation speed, and Time. DeltaTime is an increment time provided by the Unity editor;
When the distance between the real-time position of the target object and the real-time position of the main camera > minimum distance, the main camera position is moved to be close to the target object by Lerp () interpolation function, the main camera position is denoted as camPos, and the calculation formula is as follows:
camPos=Vector3.Lerp(camPos,tarPos,moveSpeed*Time.deltaTime)
wherein vector3.Lerp () is an interpolation function, transform. Position is a main camera real-time position, camPos is a main camera position, tarPos is a target object real-time position, and moveSpeed is a target object moving speed;
When the condition that the angle between the main camera orientation and the direction vector from the main camera to the target object is not satisfied is the minimum angle and the distance between the real-time position of the target object and the real-time position of the main camera is the minimum distance, closing or removing the self-defined visual angle zoom-in script to complete the visual angle zoom-in function;
Step 3.4, self-defining and creating a remote visual angle script CameraFar.cs, defining remote speed and maximum distance, obtaining a primary camera initial position in a Start () function, obtaining a primary camera real-time position in an Update () function, calculating the distance between the primary camera and the initial position through the primary camera initial position and the primary camera real-time position, moving the primary camera at the remote speed towards the opposite direction of the visual angle of the camera when the distance between the primary camera and the initial position is the maximum distance, closing or removing the self-defined remote visual angle script when the distance between the primary camera and the initial position is the maximum distance, and completing the visual angle remote function;
Step 3.5, a custom creation script control script controls the above zoom-in view script and the zoom-out view script, defines a private zoom-in view class object, and adds the zoom-in view script to the camera object in the Start () function, wherein the formula is as follows:
camFow=mainCamera.AddComponent<CameraFollow>(),
wherein camFow is a zoom-in view class object, MAINCAMERA is a camera object, addComponent is an add script function of a Unity editor, cameraFollow is a zoom-in view script;
and finally, assigning a value to the target object moving speed, the target object rotating speed, the minimum angle and the minimum distance in camFow.
The model interaction module specifically comprises:
Step 4.1, a ray click script ClkHiliCtrl.cs is custom created, a collision body detection function is completed, and whether a mouse ray collides with a model or not is judged to generate an interaction function;
a mouse ray is created, denoted as ray, whose formula is as follows:
ray=Camera.main.ScreenPointToRay(Input.mousePosition)
wherein camera main is the main camera, input. Mouseposition is the coordinate of mouse click, screenPointToRay () is the function provided by Unity editor;
and then performing collision detection, wherein whether collision occurs or not is expressed as isHit, and the calculation formula is as follows:
isHit=Physics.Raycast(ray,out hit,maxDistance,clickableLayer)
wherein hit contains object information detected by collision, maxDistance is the maximum length of rays, clickableLayer is a clickable object layer, and Physics.
When isHit is true, an object detected by collision is acquired, denoted obj, and the calculation formula is as follows:
obj=hit.collider.gameObject
wherein collider is the impactor attribute of the Unity editor, gameObject is the object attribute of the Unity editor;
When the mouse clicks, acquiring mouse click information, creating corresponding rays, and acquiring a collision object when the rays detect a collision body;
Step 4.2, obtaining the label of the collision object, obtaining the information of the collision object from the database according to the label, wherein the information comprises the object name, the object position and the object function, and updating the object information into a text content area for display;
And 4.3, loading HighlighterPlus a highlight plug-in, loading a collision body of a corresponding tag with a highlight attribute, setting the highlight attribute to be normally bright, activating the highlight attribute to complete the highlight display, using a global variable to solve the highlight conflict, detecting whether rays pressed by a mouse collide with a 3D model, canceling the highlight of the object in the global variable when the 3D model is collided and the object in the global variable is in the highlight state, and highlighting the collided building.
The maximum length of the ray in the step 4.1 is 1000.
The monitoring management module specifically comprises:
Step 5.1, a button control script is custom created, when the display or hiding monitoring is clicked, all monitoring camera icons are obtained, and all monitoring icons are displayed or hidden;
And 5.2, displaying a monitoring button by using a billboard technology, enabling the monitoring button to always face to a main camera visual angle, enabling the display size not to be influenced by scene enlargement or reduction, custom creating a billboard loader file, defining an upward vector of the button to be consistent with the upward vector of a camera, defining a forward vector as a normal vector pointing to the front surface of a camera plane, defining a rightward vector as a cross product of the upward vector and the forward vector, transforming all vertex coordinates of the button through the upward vector, the forward vector and the rightward vector to obtain the vertex coordinates of a final display button, and representing the vertex coordinates as localPos, wherein the calculation formula is as follows:
localPos=center+centerOffs*[rightDir,upDir,normalDir]
Wherein centerOffs is the coordinates of all the vertexes of the button, which are three-dimensional column vectors, the coordinates of x, y and z axes respectively, center is the origin of coordinates, rightDir is the right vector, upDir is the upward vector, normalDir is the forward vector, and the matrix is multiplied;
And then carrying out zoom control on the buttons, creating a billboard script by self definition, wherein the zoom factor is expressed as k, and the calculation formula is as follows:
k=(depth-nearPlaneDis)/(farPlaneDis-nearPlaneDis)
Wherein k is a zoom factor, depth is the distance from the button to the main camera, NEARPLANEDIS is the distance from the main camera near plane to the main camera, FARPLANEDIS is the distance from the main camera far plane to the main camera;
finally, scaling the vertex coordinates of the final display button according to the scaling factor to obtain the final billboard display effect;
Step 5.3, when the user checks the monitoring picture in a full screen mode, calling a self-defined visual angle zooming script to enable the main camera to rotate to an angle towards the target object stably, and then smoothly zooming in the distance between the main camera and the target object to finish the visual angle transition function;
And 5.4, when a closing button at the upper right corner of the full-screen monitoring picture is clicked, the transparency channel value of the video is gradually changed to 1, the display of the full-screen monitoring picture is finally canceled, and then a user-defined visual angle zooming script is called, so that the main camera is gradually zoomed out to the original position.
The set value in the step 5.3 is 100.
The account management module specifically comprises:
step 6.1, a server installs MySQL operation environment, and references MySql.Data.dll dynamic link library files in a Unity editor to operate a database;
step 6.2, referring to MySql.data.MySqlClient package for connecting database and operating database data, creating database access class MYSQLACCESS comprising 5 character string variables for storing database information, namely IP address, port number, user name, password and database name, and defining MYSQLACCESS functions comprising database function opening, table function creation, data insertion function, query function, update function, deletion function and connection function closing;
Step 6.3, operating the database, referring to MYSQLACCESS class in step 6.2, declaring the class object for connecting and operating the database, calling the database function connection database in step 6.2, calling the user-defined creation table function to create a user information table, wherein the user information table comprises user names, user passwords and user roles, different user roles correspond to different authorities, calling the user-defined insertion data function to insert data into the user information table, and calling the user-defined connection closing function to close the database connection.
The interpolation function is used for realizing smooth transition of the view angle of the main camera, when a user interacts with the system, the user enters a certain floor or monitors the full screen display to automatically trigger the zoom-in transition of the view angle, the view angle smoothly turns to a target object and zooms in, and the user exits the floor or cancels the monitoring of the full screen display to automatically trigger the zoom-out transition of the view angle.
The GIS digital twin system based on the Unity3D has the beneficial effects that the GIS digital twin system based on the Unity3D is provided. The Unity3D editor is an open source engine, is low in cost, powerful in platform function, excellent in visual effect, capable of being used for receiving a database, convenient to develop across platforms (PC, web, android), high in universality, capable of supporting various third-party plug-ins, high in self-definition degree, capable of completing more specific functions on the basis, high in controllability, safer in privacy, capable of achieving model interaction without a third-party platform by using ray clicking, capable of achieving highlight conflict by using global variables, capable of displaying monitoring buttons by using a billboard technology, free of influence of scene magnification and reduction, and capable of achieving smooth transition of a main camera view angle by using an interpolation algorithm. The method provides a reliable solution for subsequent GIS system developers, so that the GIS developers can pay more attention to specific requirements, and the development threshold of the digital twin system is reduced.