IT圈老男孩1 发表于 2022-2-22 08:52

20行代码,Unity中的“神笔马良”

0.效果



1.代码

using UnityEngine;

public class GameObjectBrush : MonoBehaviour
{
    private float width = 0.1f;
    private Color color = Color.grey;

    private LineRenderer currentLR;
    private Vector2 previousPoint;

    private void Update()
    {
      if (Input.GetMouseButtonDown(0))
      {
            //线条渲染
            currentLR = new GameObject("LineRenderer").AddComponent<LineRenderer>();
            currentLR.material = new Material(Shader.Find("Sprites/Default")) { color = color };
            currentLR.widthMultiplier = width;
            currentLR.useWorldSpace = false;
            currentLR.positionCount = 1;
            currentLR.SetPosition(0, (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition));

            //更新数据
            previousPoint = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
      }

      else if (Input.GetMouseButton(0))
      {
            if (previousPoint != (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition))
            {
                //线条渲染
                currentLR.positionCount++;
                currentLR.SetPosition(currentLR.positionCount - 1, (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition));

                //碰撞器
                BoxCollider2D collider = new GameObject("BoxCollider2D").AddComponent<BoxCollider2D>();
                collider.transform.parent = currentLR.transform;
                Vector2 latestPoint = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
                collider.transform.position = (previousPoint + latestPoint) * 0.5f;
                float angle = Mathf.Atan2((latestPoint - previousPoint).y, (latestPoint - previousPoint).x) * Mathf.Rad2Deg;
                collider.transform.eulerAngles = new Vector3(0, 0, angle);
                collider.size = new Vector2(Vector2.Distance(latestPoint, previousPoint), width);

                //更新数据
                previousPoint = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
            }
      }

      else if (Input.GetMouseButtonUp(0))
      {
            if (currentLR.transform.childCount > 0)
            {
                currentLR.gameObject.AddComponent<Rigidbody2D>().useAutoMass = true;
            }
      }
    }
}
2.一些要点

1.为什么不选择EdgeCollider2D,它似乎更加方便?

对于LineRenderer添加碰撞来说,EdgeCollider2D确实比BoxCollider2D看起来更加方便,因为只要同步两个组件的Points数组,再加厚EdgeCollider2D的边缘半径,就立刻可以实现线条的碰撞效果,而不用再自己去用Atan和开平方根计算每个线段碰撞盒的角度与尺寸。EdgeCollider2D完全可以实现碰撞的需求,但是它却不能实现刚体的重心(Center Of Mass)需求。EdgeCollider2D本质上还是一个线条碰撞器,没有形状,所以不参与刚体的重心计算,当EdgeCollider2D附加到刚体时,刚体的重心永远是,这样的结果就造成了物体错误的重心位置,物理模拟失真。而附加了BoxCollider2D的刚体,刚体会依据BoxCollider2D的布局计算一个最符合的重心位置,实现一个较为拟真的物理物体。
2.为什么刚体要使用自动质量?

首先密度的计算公式为: https://www.zhihu.com/equation?tex=%5Crho%3D%5Cfrac%7Bm%7D%7BV%7D ,所以刚体的质量就是 https://www.zhihu.com/equation?tex=m%3D%5Crho+V ,也就是说如果BoxCollider2D的面积尺寸为2并且密度为2,那么这个附加了BoxCollider2D的刚体质量就是4,依此类推。基于此,我们就可以使用自动质量,实现一个较为拟真的物理结构,也就是说我们用画笔着墨越多的地方,BoxCollider2D的面积就越大,它的质量也越大,着墨越少的地方,BoxCollider2D的面积越小,它的质量也越小。这样就让下方这种结构的物体的物理模拟成为可能:

XGundam05 发表于 2022-2-22 08:53

麦克斯与魔法画笔!

jquave 发表于 2022-2-22 08:57

效果同小时候玩的单机游戏《蜡笔物理学》,至今印象深刻

stonstad 发表于 2022-2-22 09:05

还要设置什么吗,脚本直接用,虽然生成了物体,但是没有显示出来

ChuanXin 发表于 2022-2-22 09:12

找到了一个小问题,不能直接用Input.mousePosition,要不然第二个人循环进不去,所以画不出来。

Doris232 发表于 2022-2-22 09:21

经典收藏比赞多

kyuskoj 发表于 2022-2-22 09:22

好文章,提供了思路[赞同]

DomDomm 发表于 2022-2-22 09:27

没问题,是因为他用的正交摄像机
页: [1]
查看完整版本: 20行代码,Unity中的“神笔马良”