LineRenderer——line drawing function

1、LineRenderer

LineRenderer is a component provided by Unity for drawing lines. It can be used to draw line segments in the scene. It can generally be used for

  • Draw attack range
  • weapon infrared
  • Accessibility
  • Other line drawing functions

An object can only be bound to one LineRenderer component

2. LineRenderer parameters related

Loop Whether the starting point and the starting point are always connected
Positions

The point coordinates of the line segment (usually the world coordinate system, adjusted through Use World Space), you can draw a circle by increasing the number of points and controlling the code

Line segment width adjustment (not point-to-point width adjustment, but from start to end)

Color Color change, you can change the gradient effect of color

Corner Vertices

(corner vertex, fillet)

How many extra points are used when drawing corners in a line (that is, when the line segment turns). Increasing this value can make the line corners look smoother.

End Cap Vertices

(terminal vertex, fillet)

Adjust the end point fillet

Alignment
                                

Alignment View: viewpoint, line segment facing the camera
Transform Z: The line segment faces its Z axis

Texture Mode
                
        

Texture mode, used according to the material Stretch: Stretch, map the texture once along the entire line
Tile: tiling, repeated texture
Distribute Per Segment: Distribute execution
Repeat Per Segment:Repeat display
Shadow Bias shadow offset
Generate Lighting Data Generate light source data (if the material will be affected by the light source, it needs to be turned on to display normally)
Use World Space Whether to use the world coordinate system
Materials The shader used for the line
Lighting

Care for the impact

Cast Shadows: Whether to turn on shadows

Receive Shadows: Receive
Probes light probe

Light Probes: Light detector mode

  • off: Do not use light probe
  • Blend Probes: Using interpolated light probes
  • Use Proxy Volume: Use a 3D grid to interpolate light probes
  • Custom Provided: Customization is determined by the material

Reflection Probes: reflection probe mode

Almost the same as Use Probes, except for two things:

  • Blend Probes And Skybox: Use blended reflection probes and blend them with the skybox
  • Simple: Enable normal probes, overlapped probes are not mixed

Additional Settings

Additional settings

Motion Vectors: motion vectors

  • Camera Motion Only: Use camera motion to track motion
  • Per Object Motion: Track motion for a specific object
  • Force No Motion: Do not track

Dynamic Occlude: dynamic occlusion culling

Sorting Layer: Sorting albums
Order in Layer: The order of line segments in the sorting layer

3. LineRenderer code related

// 动态添加一个线段
GameObject line = new GameObject();
line.name = "Line";
LineRenderer lineRenderer = line.AddComponent<LineRenderer>();

// 首位相连
lineRenderer.loop = true;

// 开始结束的宽
lineRenderer.startWidth = 0.03f;
lineRenderer.endWidth = 0.02f;

// 开始结束的颜色
lineRenderer.startColor = Color.white;
lineRenderer.endColor = Color.red;

// 设置材质
lineRenderer.material = Resources.Load<Material>("material");

// 设置点,一定要设置点的个数,然后设置点的位置
// 同时设定了点的数量后,如果少于已有点的数量,会将多余的点删掉
lineRenderer.positionCount = 4;
// 通过数组批量设置
lineRenderer.SetPositions(new Vector3[] {new Vector3(0, 0, 0),
                                         new Vector3(0, 0, 5),
                                         new Vector3(5, 0, 5)};
// 注意,如果设置的点的数量与插入点位置的数量不一致,那么最后会补齐一个坐标为(0,0,0)的点
// 通过索引设定第(index+1)个点的坐标
lineRenderer.SetPostion(3, new Vector3(5, 0, 0));

// 是否使用世界坐标系,决定了是否随对象而移动
lineRenderer.useWorldSpace = false;

// 让线段会受光的影响,进行着色器计算
lineRenderer.generateLightingData = true
  •  If you want to draw a circle:

public void DrawCircle(Vector3 centerPos, float r, int pointNum) {
    GameObject obj = new GameObject();
    obj.name = "circle";
    LineRenderer line = obj.AddCompoent<LineRenderer>();
    line.loop = true;
    line.positionCount = pointNum;

    float angle = 350f / pointNum;
    for(int i = 0; i < pointNum; ++i) {
        // 1、点+向量 => 平移点
        // 2、 四元数* 向量 => 旋转向量
        line.SetPosition(i, centerPos + Quaternion.AngleAxis(angle * i, Vector3.up) * Vector3.forward * r)
    }
}
  • Long press the mouse to get the trajectory of the mouse movement

  • private Vectore3 nowPos;
    
    private void Update() {
        if (Input.GetMouseButtonDown(0)) {
            GameObject obj = new GameObject();
            obj.name = "trace";
            LineRenderer line = obj.AddCompoent<LineRenderer>();
            line.positionCount = 0;
            line.loop = false;
            line.starWidth = 0.5f;
            line.endWidth = 0.5f;
        }
    
        if (Input.GetMousePosition(0)) {
            line.positionCount += 1;
    
            nowPos = Input.mousePosition;
            nowPos.z = 10;        // 设定屏幕的横截面,即画线的平面
    
            Camera.main.ScreenToWorldPoint(Input.nowPos);
    
            line.sSetPostion(line.positionCount - 1, 
        }
    }

Guess you like

Origin blog.csdn.net/Go_Accepted/article/details/127341204