how to draw a 3d rug

Sometimes, yous need to describe lines, circles or curves in your Unity games. In these cases, yous can utilize Unity'south LineRenderer class. In this tutorial, we volition see how nosotros tin depict lines, polygons, circles, moving ridge functions, Bézier Curves. And also nosotros will encounter how we tin do a free drawing using Line Renderer in Unity3D. In gild to see our other Unity tutorials, click here.
Line Renderer Component
To draw a line we have to add a LineRenderer component to a game object. Even if this component can be attached to whatever game object, I propose you create an empty game object and add the LineRenderer to this object.
We demand a material which will be assigned to LineRenderer. To do this create a material for the line in Project Tab. Unlit/Colour shader is suitable for this material.

Assign LineMat material to the Line Renderer component.

Line Renderer draws lines betwixt determined positions. In other words, nosotros tell the Line Renderer the points which volition exist connected and Line Renderer connects these points.

In the Positions department, you can change the number of points and positions of points. If y'all enter two dissimilar points, you lot will become a direct line. You can as well change the width of the line in the section below.

Also, ii depict a triangle, yous need 3 points and to depict a rectangle you need 4 points. Let's draw a rectangle as an instance.
To draw a rectangle, nosotros need to set positions of 4 points. Nosotros also have to cheque the Loop toggle to obtain a closed shape.

Drawing Lines From C# Script
If we want to draw or control lines in real-time, we need to create a C# script. To draw lines from a script, we determine the size of position assortment and coordinates of positions in C# script. Therefore, LineRenderer can connect the points.
Let's draw a triangle using a script as an example. First, create a script with the name "DrawScript". And attach this script to a game object which already has a LineRenderer component.
public form DrawScript : MonoBehaviour { individual LineRenderer lineRenderer; void Start( ) { lineRenderer = GetComponent<LineRenderer> ( ) ; Vector3[ ] positions = new Vector3[ 3 ] { new Vector3( 0 , 0 , 0 ) , new Vector3( - i , i , 0 ) , new Vector3( one , ane , 0 ) } ; DrawTriangle(positions) ; } void DrawTriangle(Vector3[ ] vertexPositions) { lineRenderer.positionCount = 3 ; lineRenderer.SetPositions(vertexPositions) ; } }
This script volition draw a triangle. Note that nosotros already ready the line width to 0.1 and checked the loop toggle, before. Therefore the aforementioned setting is as well valid hither.

We can also change the line width from the script using startWidth and endWidth. In improver to this, if y'all would like to change line width by position, you lot can gear up different values to them. In this example, Line Renderer volition interpolate the line width co-ordinate to position.
public class DrawScript : MonoBehaviour { individual LineRenderer lineRenderer; void Commencement( ) { lineRenderer = GetComponent<LineRenderer> ( ) ; Vector3[ ] positions = new Vector3[ 3 ] { new Vector3( 0 , 0 , 0 ) , new Vector3( - 1 , 1 , 0 ) , new Vector3( 1 , 1 , 0 ) } ; DrawTriangle(positions, 0.02 f , 0.02 f ) ; } void DrawTriangle(Vector3[ ] vertexPositions, float startWidth, float endWidth) { lineRenderer.startWidth = startWidth; lineRenderer.endWidth = endWidth; lineRenderer.loop = true ; lineRenderer.positionCount = 3 ; lineRenderer.SetPositions(vertexPositions) ; } }
Cartoon Regular Polygons and Circles
In this section, nosotros are going to see how nosotros can write a method that draws regular polygons. Since circles are n-gons which has large due north, our function will be useful for circles also. Simply first, let me explain the mathematics backside it.
Vertices of regular polygons are on a circle. As well, the center of the circle and the middle of the polygon are top of each other. The most reliable method to draw a polygon is to discover the angle between successive vertices and locate the vertices on the circle. For instance, angle of the arc between successive vertices of a pentagon is 72 degrees or for an octagon, it is 45 degrees. To find this angle, we can split 360 degrees(or 2xPI radians) with the number of vertices.

Then we need to discover the positions of the vertices. To do this nosotros assign an initial indicate for the first vertex and rotate this vertex for each vertex using a rotation matrix.
As you probably know, in guild to rotate a point effectually an axis, we multiply the position vector of the bespeak with the rotation matrix. Rotation matrices for rotations effectually x, y and z axes are given on the right.
For instance, when we desire to rotate a bespeak by 90 degrees effectually the z-axis, which has a coordinate (1,0,0), we multiply the position vector past a rotation matrix.

We need to construct a rotation matrix to rotate each vertex around the z-centrality. Let's me write our DrawPolygon method showtime and explain it.
void DrawPolygon( int vertexNumber, float radius, Vector3 centerPos, float startWidth, bladder endWidth) { lineRenderer.startWidth = startWidth; lineRenderer.endWidth = endWidth; lineRenderer.loop = truthful ; float angle = 2 * Mathf.PI / vertexNumber; lineRenderer.positionCount = vertexNumber; for ( int i = 0 ; i < vertexNumber; i+ + ) { Matrix4x4 rotationMatrix = new Matrix4x4( new Vector4(Mathf.Cos(bending * i) , Mathf.Sin(angle * i) , 0 , 0 ) , new Vector4( - i * Mathf.Sin(bending * i) , Mathf.Cos(angle * i) , 0 , 0 ) , new Vector4( 0 , 0 , 1 , 0 ) , new Vector4( 0 , 0 , 0 , 1 ) ) ; Vector3 initialRelativePosition = new Vector3( 0 , radius, 0 ) ; lineRenderer.SetPosition(i, centerPos + rotationMatrix.MultiplyPoint(initialRelativePosition) ) ; } }
You may wonder why the constructed rotation matrix is 4×iv. In computer graphics, the 3-dimensional earth is represented as 4-dimensional simply this topic is non related to our concern hither. We just employ it every bit if it is 3-dimensional.
Nosotros ready the position of initial vertex and rotate it using rotationMatrix each time and add the center position to it.
The post-obit prototype is an example of a hexagon which is drawn by this method.

If you increase the number of vertex points, this polygon turns to a circle.

Drawing Waves
In this section, we are going to draw a sinusoidal wave, a traveling wave and a standing wave using sine function.
The mathematical function of the sine wave is given by the following:

where

Here, k is wave number, f is frequency, ω is the athwart frequency, λ is wavelength, v is the linear speed, t is the time and φ is the stage angle. We will non worry about the phase angle in our discussions.
Sine wave equation with a minus sign represents traveling wave from left to right and the equation with plus sign represents a traveling line wave right to left.
In order to draw a stable sinusoidal moving ridge, we can drop the time part. The following method will draw a sine wave.
void DrawSineWave(Vector3 startPoint, float amplitude, float wavelength) { bladder 10 = 0f; float y; float g = 2 * Mathf.PI / wavelength; lineRenderer.positionCount = 200 ; for ( int i = 0 ; i < lineRenderer.positionCount; i+ + ) { x + = i * 0.001 f ; y = aamplitude * Mathf.Sin(one thousand * ten) ; lineRenderer.SetPosition(i, new Vector3(x, y, 0 ) + startPoint) ; } }
Our DrawSineWave method takes three parameters. They are startPoint which is for setting the start position in world space, amplitude which is for setting the amplitude of the wave and wavelength which is for setting the wavelength of the sine wave.

To obtain the positions of the corresponding mathematical function, first, nosotros determine the positions on the x-axis. For each x, we have to calculate the y-position.
To breathing this wave, nosotros take to implement fourth dimension to our function every bit follows:
void DrawTravellingSineWave(Vector3 startPoint, bladder amplitude, bladder wavelength, float waveSpeed) { float x = 0f; float y; float k = 2 * Mathf.PI / wavelength; float due west = grand * waveSpeed; lineRenderer.positionCount = 200 ; for ( int i = 0 ; i < lineRenderer.positionCount; i+ + ) { 10 + = i * 0.001 f ; y = amplitude * Mathf.Sin(k * ten + w * Time.time) ; lineRenderer.SetPosition(i, new Vector3(x, y, 0 ) + startPoint) ; } }


This time nosotros have four parameters. The fourth parameter is to set the wave speed. This wave travels to the left since nosotros used plus sign in the part. If nosotros would like to create a wave that travels to the right, we take to use the minus sign. You should keep in mind that we have to write this method in Update().
To create a standing moving ridge, we have to add 2 waves which travel to the right and which travel to left.
void DrawStandingSineWave(Vector3 startPoint, bladder amplitude, float wavelength, float waveSpeed) { bladder x = 0f; float y; bladder g = two * Mathf.PI / wavelength; float west = k * waveSpeed; lineRenderer.positionCount = 200 ; for ( int i = 0 ; i < lineRenderer.positionCount; i+ + ) { x + = i * 0.001 f ; y = aamplitude * (Mathf.Sin(chiliad * ten + westward * Time.time) + Mathf.Sin(thou * x - w * Fourth dimension.fourth dimension) ) ; lineRenderer.SetPosition(i, new Vector3(x, y, 0 ) + startPoint) ; } }
Drawing Bézier Curves
Bézier curves are parametric curves that are used to create shine curved shapes. They are widely used in computer graphics. In this section, we are going to see how we can draw Bézier curves.
When a Bézier curve is controlled by 3 points, so it is called Quadratic Bézier Curve(the first equation below) and when information technology is controlled by iv points, information technology is chosen Cubic Bézier Curve.


The post-obit script will describe a quadratic Bézier curve using positions p0, p1, and p2. Y'all should create iii game objects and assign these objects to respective variables in the script to alter the shape of the curve in real-time.
using Arrangement.Collections; using System.Collections.Generic; using UnityEngine; public course BezierScript : MonoBehaviour { private LineRenderer lineRenderer; public Transform p0; public Transform p1; public Transform p2; void Start( ) { lineRenderer = GetComponent<LineRenderer> ( ) ; } void Update( ) { DrawQuadraticBezierCurve(p0.position, p1.position, p2.position) ; } void DrawQuadraticBezierCurve(Vector3 point0, Vector3 point1, Vector3 point2) { lineRenderer.positionCount = 200 ; bladder t = 0f; Vector3 B = new Vector3( 0 , 0 , 0 ) ; for ( int i = 0 ; i < lineRenderer.positionCount; i+ + ) { B = ( i - t) * ( 1 - t) * point0 + 2 * ( 1 - t) * t * point1 + t * t * point2; lineRenderer.SetPosition(i, B) ; t + = ( i / ( float )lineRenderer.positionCount) ; } } }

Likewise, the following method draws a cubic Bézier bend. This fourth dimension we need 4 points.
void DrawCubicBezierCurve(Vector3 point0, Vector3 point1, Vector3 point2, Vector3 point3) { lineRenderer.positionCount = 200 ; float t = 0f; Vector3 B = new Vector3( 0 , 0 , 0 ) ; for ( int i = 0 ; i < lineRenderer.positionCount; i+ + ) { B = ( 1 - t) * ( one - t) * ( one - t) * point0 + 3 * ( ane - t) * ( 1 - t) * t * point1 + 3 * ( 1 - t) * t * t * point2 + t * t * t * point3; lineRenderer.SetPosition(i, B) ; t + = ( ane / ( bladder )lineRenderer.positionCount) ; } }

Free Drawing using Line Renderer
In this section, we are going to run into how we can draw freely using the mouse position. Nosotros tin can practise this by creating a new game object with a line renderer attached. When we printing the left mouse button, a new game object is created and each frame the position of the mouse added to the line renderer.

First of all, nosotros need a prefab to create a new game object when nosotros press the left mouse button. This is an empty game object with a line renderer component attached. In addition to this, practice not forget to assign a material to the line renderer component. Create a prefab from this game object.
Second, create an empty game object and attach the post-obit script DrawManager.
using Organisation.Collections; using Arrangement.Collections.Generic; using UnityEngine; public class DrawManager: MonoBehaviour { private LineRenderer lineRenderer; public GameObject drawingPrefab; void Update( ) { if (Input.GetMouseButtonDown( 0 ) ) { GameObject drawing = Instantiate(drawingPrefab) ; lineRenderer = drawing.GetComponent<LineRenderer> ( ) ; } if (Input.GetMouseButton( 0 ) ) { FreeDraw( ) ; } } void FreeDraw( ) { lineRenderer.startWidth = 0.1 f ; lineRenderer.endWidth = 0.i f ; Vector3 mousePos = new Vector3(Input.mousePosition.10, Input.mousePosition.y, 10f) ; lineRenderer.positionCount+ + ; lineRenderer.SetPosition(lineRenderer.positionCount - 1 , Photographic camera.chief.ScreenToWorldPoint(mousePos) ) ; } }
When yous press the left mouse button, a new game object is instantiated from the prefab which we created before. We go the line renderer component from this game object. Then while we are pressing the left mouse button, we call FreeDraw() method.
In the FreeDraw method, nosotros take x and y components of the mouse position and set the z-position as 10. Hither, the mouse position is in the screen infinite coordinates. But we use earth space coordinates in line renderer. Therefore we demand to catechumen mouse position to earth space coordinates. In each frame, we also need to increase the number of points. Since we do not know how many points nosotros need, we cannot set position count earlier.
References
i-https://docs.unity3d.com/ScriptReference/LineRenderer.html
2-http://world wide web.theappguruz.com/web log/bezier-curve-in-games
iii-https://en.wikipedia.org/wiki/Bézier_curve
4-https://en.wikipedia.org/wiki/Sine_wave
Source: https://www.codinblack.com/how-to-draw-lines-circles-or-anything-else-using-linerenderer/
Post a Comment for "how to draw a 3d rug"