Unity3d game engine game Object Access draw line and draw plane details (17)

Source: Internet
Author: User
Unity3d game engine game Object Access draw line and draw plane details


Original by Yu Song MomoArticleIf you reprint it, please note: Reprinted to my independent domain name blogYusong Momo program Research Institute , Original address: http://www.xuanyusong.com/archives/561



After learning unity3d in a blink of an eye for a while, I have basically won this game engine. I think about the RPG Game Engine I wrote earlier and found more and more junk. People must keep learning and improving, study well, and make progress every day. Wow ~ Come on !! If you need to draw lines and surfaces for a recent project, you need to sort out the study notes for the past two days. One is to leave your notes for preparation, and the other is to facilitate beginners to learn. Any irregular curve is composed of several line segments. in a timely manner, it is a circle. It is also composed of several line segments. That is to say, splicing several line segments is the irregular curve we need ~ In the 3D world, we need to know three points x y z to determine a 3D line segment. First, add a line using the unity editor ~

Unity-> gameobject-> Create empty creates an empty object. I name it line. Click component-> miscellaneous-> line Renderer to add a line Renderer attribute to line. Line Renderer is a very medium attribute. I will describe it in detail below.



Create-> material: Create a material and map the line segment. Let's take a look at some important parameters of line Renderer. Cast shadows: whether to cast a shadow. Receive shadows: whether to receive shadow.
MATERIALS: Set the material. You can set multiple materials here. Line is the material we created above. Here I have painted the line material in red. Positions: This attribute is very important. It is used to set the coordinates of points of a line segment in the 3D world. If the size value is set to 3, there will be three points, element 0.Element 1Element 2 the three points determine that this curve is divided into two segments, the first segment is (0, 0, 1) to (0, 0, 2), the second segment is (0, 0, 2) to (0, 0, 0, 4 ).

Paramerters Startwidth: Set the width of the start point. Endwidth: set the end width,These two values are 1 by default, but the reality is very wide, so they are generally set to 0. A few ~ Start color: Set the start color. Start color: set the end color.
Use world space use the world coordinate system
Let's take a look at the results. Clearly, we can see that the curve is divided into two parts: the first part is shorter and the second part is longer.

How is it? Isn't it hard to learn how to draw a line? We can use the above method to set the line segment when the line segment position is known, but if the line segment position is generated dynamically during the gameCode.

Create the script main. CS and bind it to the camera. If you run the game to draw a line, you need to get the instance of the line Object in main. This is an important knowledge point.
Some important methods, some edited items in the editor, can also be done in the code.
Linerenderer. setwidth (0.1, 0.1); set the width of the start and end points of a line segment (parameter 1 is the start point parameter 2 is the end point) Linerenderer. setcolor (color. Black, color. White );Set the color of the starting and ending points of a line segment (parameter 1 is the starting point color parameter 2 is the end point color)
Linerenderer. setvertexcount (5); set the number of line segments.
Linerenderer. useworldspace = true; whether to use the World coordinate system, which corresponds to the editor above.

Using unityengine; using system. collections; using system. threading; public class main: monobehaviour {// game object, which is a line segment object private gameobject linerendergameobject; // line segment Renderer private linerenderer; // you can specify the number of line segments, indicates that a curve consists of several line segments. Private int linelength = 4; // records 4 points, respectively, use the points in the four 3D worlds to connect a line segment private vector3 V0 = new vector3 (1.0f, 0.0f, 0.0f); Private vector3 V1 = new vector3 (0.0f, 1.0f, 0.0f ); private vector3 v2 = new VEC Tor3 (0.0f, 0.0f, 1.0f); Private vector3 V3 = new vector3 (1.0f, 0.0f, 0.0f); void start () {// pass the name of the previously created object, you can get this object in other classes, // here in main. in CS, the line Object linerendergameobject = gameobject is obtained. find ("line"); // through the game object, the getcomponent method is used to pass in the linerenderer // is the Renderer attribute added to the Line Game object. // This object can be used to render the linerenderer = (linerenderer) linerendergameobject for the game world. getcomponent ("linerenderer"); // sets the length of a line segment. This value must be equal to the number of 3D points on the drawn line. // otherwise, an exception is thrown ~~ Linerenderer. setvertexcount (linelength);} void Update () {// set the vertex in the game update. // link the vertex to the Curve Based on the vertex. // The first parameter is the vertex ID. // The second parameter is the 3D coordinate of the vertex. // ID in the same way, it indicates a line segment // so you need to pay attention to it! Linerenderer. setposition (0, V0); linerenderer. setposition (1, V1); linerenderer. setposition (2, V2); linerenderer. setposition (3, V3 );}}


Through the settings of the above Code, we run the game and find that a brand new triangle curve suddenly shines into our eyes, with the above method, we can combine to draw a variety of 3D Game curves. Here Momo uses color, and you can also add a texture ~







If you draw a plane ~, In the 3D world, all the game surfaces are combined with triangles (for efficiency reasons), so all the faces we see are actually spliced with triangles. Next we will look at how to draw a Triangle Plane in unity3d. First, create a gameobject object. I name it face. Then, add two mesh attributes to the object, and add the meshfilter and mesh Renderer attributes, in this way, you can draw a grid surface.





In order to map the mesh surface, I add a material to the mesh Renderer and paste an image ~ In the 3D world, two points can determine a line, so three points can certainly determine a plane. In the 3D world, a plane must be composed of triangles, the number of coordinate points required for any 3D surface must be a multiple of 3. Like the draw line, Momo still creates a script to bind it to the camera, and then accesses the face game object created above to render the Triangle Surface ~

 using unityengine; using system. collections; public class main: monobehaviour {// update is called once per framevoid Update () {// get the mesh Renderer object meshfilter = (meshfilter) gameobject through the object name face. find ("face "). getcomponent (typeof (meshfilter); // obtain the mesh object mesh = meshfilter through the Renderer object. mesh; // what is written in the API is not clear. I will explain it in detail. // set the vertex. This attribute is very important. // determine a plane at three points, therefore, the number of vector3 arrays must be three multiples. // follow the three clockwise steps to determine one side. // The number here is 6, that is, I created two triangles. // enter 3D coordinates in sequence. mesh. vertices = new vector3 [] {New vector3 (5, 0, 0), new vector3 (0, 5, 0), new vector3 (0, 0, 5 ), new vector3 (-5, 0, 0), new vector3 (0,-5, 0), new vector3 (0, 0,-5)}; // set the texture point, after the surface is determined, it is 2D. // Therefore, the number of Paster maps is vector2. // The first triangle is set to 5 textures. // The second triangle is set to a Paster. // The number of numeric values is still equal the number of vertices is the same as that of mesh. UV = new vector2 [] {New vector2 (0, 0), new vector2 (0, 5), new vector2 (5, 5), new vector2 (0, 0 ), new vector2 (0, 1), new vector2 (1, 1)}; // you can specify a triangle index, this index is based on the index of the above vertex coordinate array // corresponds to each item in the fixed-point array vector3 // The two triangles are finally drawn in the plane // The number of values is still equal to the number of vertices same mesh. triangles = new int [] {0, 1, 2, 3, 4, 5 };}


After running build and run, you can clearly see two triangles. Momo reminds everyone to use the Triangle Plane to draw a variety of game faces ~ Wow ~




Finally, you are welcome to discuss unity3d game development with Momo. In general, this chapter is relatively simple, but it is also a very important one. I will not upload the code. Wow ~ Momo is willing to study with you and make progress together ~!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.