Unity VR实时写字(画笔挂载脚本)

这个脚本是挂载在你的画笔物体上的,废话不多说,上代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MarkPen : MonoBehaviour
{

    private Texture tex;
    public RenderTexture cacheTex;
    RenderTexture currentTex;
    private float brushMaxSize;

    public float brushSize=0.01f;
    public Color brushCol=Color.red;

    private Material effectMat;

    private Material renderMat;


    public Transform penHead;
    public Transform board;

    private Vector2 lastuv;
    private bool isDown;
   
    void Start()
    {
        Initialized();
    }
    void Update()
    {

        Ray ray = new Ray(penHead.position, transform.forward);

        if (Physics.Raycast(ray, out RaycastHit raycastHit, 1, LayerMask.GetMask("Board")))
        {
            if (raycastHit.distance < 0.05f)
            {
                if (!isDown)
                {
                    isDown = true;
                    lastuv = raycastHit.textureCoord2;
                }
                brushSize = Mathf.Clamp((0.05f / raycastHit.distance) * 0.001f,0, brushMaxSize);
                RenderBrushToBoard(raycastHit);
                lastuv = raycastHit.textureCoord2;
            }
            else {
                isDown = false;
            }
        }
        else {
            isDown = false;
        }
    }

    private void RenderBrushToBoard(RaycastHit hit) {

        Vector2 dir = hit.textureCoord2 - lastuv;

        if (Vector3.SqrMagnitude(dir)>brushSize*brushSize) {
            int length = Mathf.CeilToInt(dir.magnitude / brushSize);

            for (int i = 0; i < length; i++)
            {
                RenderToMatTex(lastuv+dir.normalized*i*brushSize);
            }
        }
        RenderToMatTex(hit.textureCoord2);
    }

    private void RenderToMatTex(Vector2 uv) {
        effectMat.SetVector("_BrushPos", new Vector4(uv.x, uv.y,lastuv.x,lastuv.y));
        effectMat.SetColor("_BrushColor", brushCol);
        effectMat.SetFloat("_BrushSize", brushSize);
        Graphics.Blit(cacheTex, currentTex, effectMat);
        renderMat.SetTexture("_MainText", currentTex);
        Graphics.Blit(currentTex,cacheTex);
    }

    private void Initialized()
    {
        brushMaxSize = brushSize;
        effectMat =new Material(Shader.Find("Brush/MarkPenEffect"));
        Material boardMat = board.GetComponent<MeshRenderer>().material;
        tex = boardMat.mainTexture;

        renderMat = boardMat;

        cacheTex = new RenderTexture(tex.width, tex.height, 0, RenderTextureFormat.ARGB32);
        Graphics.Blit(tex,cacheTex);
        renderMat.SetTexture("_MainTex",cacheTex);

        currentTex = new RenderTexture(tex.width, tex.height, 0, RenderTextureFormat.ARGB32);
    } 
}

  • 27
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南無忘码至尊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值