how to draw a 3d circle hole

Sometimes, you need to describe lines, circles or curves in your Unity games. In these cases, you tin can utilize Unity'southward LineRenderer class. In this tutorial, we volition see how we can draw lines, polygons, circles, wave functions, Bézier Curves. And also we will see how we can do a gratuitous drawing using Line Renderer in Unity3D. In guild to see our other Unity tutorials, click hither.
Line Renderer Component
To describe a line we accept to add a LineRenderer component to a game object. Even if this component can be fastened to whatever game object, I suggest you create an empty game object and add together the LineRenderer to this object.
We need a material which volition be assigned to LineRenderer. To practise this create a material for the line in Project Tab. Unlit/Color shader is suitable for this material.

Assign LineMat textile to the Line Renderer component.

Line Renderer draws lines betwixt determined positions. In other words, we tell the Line Renderer the points which will be continued and Line Renderer connects these points.

In the Positions section, yous can modify the number of points and positions of points. If you enter two unlike points, you lot will get a straight line. You tin can as well change the width of the line in the department below.

Likewise, 2 describe a triangle, you need 3 points and to depict a rectangle you demand 4 points. Let's draw a rectangle as an example.
To draw a rectangle, we need to set positions of 4 points. We too have to check the Loop toggle to obtain a closed shape.

Drawing Lines From C# Script
If we want to describe or control lines in real-time, we demand 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 tin connect the points.
Let'south draw a triangle using a script as an instance. First, create a script with the proper noun "DrawScript". And attach this script to a game object which already has a LineRenderer component.
public course DrawScript : MonoBehaviour { private LineRenderer lineRenderer; void Start( ) { lineRenderer = GetComponent<LineRenderer> ( ) ; Vector3[ ] positions = new Vector3[ 3 ] { new Vector3( 0 , 0 , 0 ) , new Vector3( - 1 , 1 , 0 ) , new Vector3( i , 1 , 0 ) } ; DrawTriangle(positions) ; } void DrawTriangle(Vector3[ ] vertexPositions) { lineRenderer.positionCount = 3 ; lineRenderer.SetPositions(vertexPositions) ; } }
This script will depict a triangle. Note that nosotros already gear up the line width to 0.1 and checked the loop toggle, before. Therefore the same setting is also valid here.

We can also modify the line width from the script using startWidth and endWidth. In addition to this, if you would similar to modify line width by position, y'all tin can set different values to them. In this instance, Line Renderer will interpolate the line width according to position.
public grade DrawScript : MonoBehaviour { individual LineRenderer lineRenderer; void Start( ) { lineRenderer = GetComponent<LineRenderer> ( ) ; Vector3[ ] positions = new Vector3[ 3 ] { new Vector3( 0 , 0 , 0 ) , new Vector3( - one , 1 , 0 ) , new Vector3( i , 1 , 0 ) } ; DrawTriangle(positions, 0.02 f , 0.02 f ) ; } void DrawTriangle(Vector3[ ] vertexPositions, float startWidth, bladder endWidth) { lineRenderer.startWidth = startWidth; lineRenderer.endWidth = endWidth; lineRenderer.loop = true ; lineRenderer.positionCount = 3 ; lineRenderer.SetPositions(vertexPositions) ; } }
Cartoon Regular Polygons and Circles
In this section, we are going to run into how nosotros can write a method that draws regular polygons. Since circles are n-gons which has big northward, our role will be useful for circles also. Only starting time, permit me explain the mathematics behind it.
Vertices of regular polygons are on a circumvolve. Likewise, the centre of the circle and the center of the polygon are summit of each other. The most reliable method to depict a polygon is to observe 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, information technology is 45 degrees. To find this bending, we can dissever 360 degrees(or 2xPI radians) with the number of vertices.

Then we need to notice the positions of the vertices. To do this we assign an initial point for the starting time vertex and rotate this vertex for each vertex using a rotation matrix.
As yous probably know, in order to rotate a point effectually an axis, nosotros multiply the position vector of the betoken with the rotation matrix. Rotation matrices for rotations effectually 10, y and z axes are given on the right.
For example, when we want to rotate a point by 90 degrees around the z-axis, which has a coordinate (1,0,0), we multiply the position vector by a rotation matrix.

We need to construct a rotation matrix to rotate each vertex around the z-axis. Let's me write our DrawPolygon method get-go and explain it.
void DrawPolygon( int vertexNumber, float radius, Vector3 centerPos, float startWidth, float endWidth) { lineRenderer.startWidth = startWidth; lineRenderer.endWidth = endWidth; lineRenderer.loop = true ; bladder bending = 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( - one * Mathf.Sin(angle * i) , Mathf.Cos(angle * i) , 0 , 0 ) , new Vector4( 0 , 0 , ane , 0 ) , new Vector4( 0 , 0 , 0 , ane ) ) ; Vector3 initialRelativePosition = new Vector3( 0 , radius, 0 ) ; lineRenderer.SetPosition(i, centerPos + rotationMatrix.MultiplyPoint(initialRelativePosition) ) ; } }
You may wonder why the constructed rotation matrix is iv×4. In figurer graphics, the 3-dimensional world is represented every bit 4-dimensional just this topic is not related to our business here. We merely utilize information technology as if information technology is iii-dimensional.
Nosotros set the position of initial vertex and rotate it using rotationMatrix each time and add the heart position to information technology.
The following image 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 circumvolve.

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

where

Hither, g is wave number, f is frequency, ω is the angular frequency, λ is wavelength, v is the linear speed, t is the time and φ is the phase bending. We will non worry virtually 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 moving ridge right to left.
In order to draw a stable sinusoidal wave, nosotros can drop the time part. The following method volition draw a sine wave.
void DrawSineWave(Vector3 startPoint, bladder amplitude, float wavelength) { bladder ten = 0f; float y; float chiliad = 2 * Mathf.PI / wavelength; lineRenderer.positionCount = 200 ; for ( int i = 0 ; i < lineRenderer.positionCount; i+ + ) { ten + = i * 0.001 f ; y = amplitude * Mathf.Sin(k * ten) ; lineRenderer.SetPosition(i, new Vector3(ten, y, 0 ) + startPoint) ; } }
Our DrawSineWave method takes 3 parameters. They are startPoint which is for setting the start position in earth infinite, 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 respective mathematical part, first, we make up one's mind the positions on the x-axis. For each x, we have to calculate the y-position.
To breathing this moving ridge, we have to implement fourth dimension to our office as follows:
void DrawTravellingSineWave(Vector3 startPoint, bladder amplitude, float wavelength, float waveSpeed) { float x = 0f; float y; float 1000 = ii * Mathf.PI / wavelength; float w = thousand * waveSpeed; lineRenderer.positionCount = 200 ; for ( int i = 0 ; i < lineRenderer.positionCount; i+ + ) { x + = i * 0.001 f ; y = aamplitude * Mathf.Sin(k * ten + due west * Time.time) ; lineRenderer.SetPosition(i, new Vector3(x, y, 0 ) + startPoint) ; } }


This fourth dimension we have 4 parameters. The fourth parameter is to gear up the wave speed. This wave travels to the left since we used plus sign in the function. If we would like to create a wave that travels to the right, we accept to apply the minus sign. You should keep in heed that we have to write this method in Update().
To create a standing wave, we accept to add two waves which travel to the right and which travel to left.
void DrawStandingSineWave(Vector3 startPoint, float amplitude, float wavelength, bladder waveSpeed) { float ten = 0f; bladder y; float k = ii * Mathf.PI / wavelength; bladder w = k * waveSpeed; lineRenderer.positionCount = 200 ; for ( int i = 0 ; i < lineRenderer.positionCount; i+ + ) { 10 + = i * 0.001 f ; y = amplitude * (Mathf.Sin(k * x + w * Fourth dimension.fourth dimension) + Mathf.Sin(1000 * x - w * Time.time) ) ; 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 encounter how we tin depict Bézier curves.
When a Bézier bend is controlled past iii points, then information technology is called Quadratic Bézier Curve(the kickoff equation below) and when it is controlled by 4 points, it is called Cubic Bézier Curve.


The post-obit script will describe a quadratic Bézier bend using positions p0, p1, and p2. You should create three game objects and assign these objects to corresponding variables in the script to change the shape of the curve in existent-time.
using System.Collections; using Organisation.Collections.Generic; using UnityEngine; public class 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 ; float 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 + = ( one / ( bladder )lineRenderer.positionCount) ; } } }

Likewise, the following method draws a cubic Bézier curve. This time nosotros 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) * ( i - t) * ( 1 - t) * point0 + 3 * ( one - t) * ( 1 - t) * t * point1 + 3 * ( 1 - t) * t * t * point2 + t * t * t * point3; lineRenderer.SetPosition(i, B) ; t + = ( 1 / ( float )lineRenderer.positionCount) ; } }

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

Starting time of all, nosotros demand a prefab to create a new game object when nosotros printing the left mouse push. This is an empty game object with a line renderer component attached. In add-on to this, do 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 following script DrawManager.
using Arrangement.Collections; using System.Collections.Generic; using UnityEngine; public course DrawManager: MonoBehaviour { individual LineRenderer lineRenderer; public GameObject drawingPrefab; void Update( ) { if (Input.GetMouseButtonDown( 0 ) ) { GameObject drawing = Instantiate(drawingPrefab) ; lineRenderer = cartoon.GetComponent<LineRenderer> ( ) ; } if (Input.GetMouseButton( 0 ) ) { FreeDraw( ) ; } } void FreeDraw( ) { lineRenderer.startWidth = 0.1 f ; lineRenderer.endWidth = 0.1 f ; Vector3 mousePos = new Vector3(Input.mousePosition.10, Input.mousePosition.y, 10f) ; lineRenderer.positionCount+ + ; lineRenderer.SetPosition(lineRenderer.positionCount - 1 , Photographic camera.primary.ScreenToWorldPoint(mousePos) ) ; } }
When you press the left mouse push, a new game object is instantiated from the prefab which we created earlier. Nosotros become the line renderer component from this game object. And then while we are pressing the left mouse push button, nosotros call FreeDraw() method.
In the FreeDraw method, we take x and y components of the mouse position and prepare the z-position as 10. Here, the mouse position is in the screen space coordinates. But we use world space coordinates in line renderer. Therefore nosotros need to convert mouse position to world space coordinates. In each frame, nosotros also need to increase the number of points. Since nosotros do not know how many points we need, we cannot set position count before.
References
one-https://docs.unity3d.com/ScriptReference/LineRenderer.html
ii-http://www.theappguruz.com/blog/bezier-curve-in-games
3-https://en.wikipedia.org/wiki/Bézier_curve
four-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 circle hole"