Unity 如何在任意物体上自由绘制

本文介绍了如何在Unity游戏中实现物体表面的自由绘制功能。通过挂载特定的Shader和利用鼠标或粒子系统,可以实现在任意物体上进行交互式绘制。详细探讨了Shader的应用和绘制方法,为游戏开发和互动体验提供新思路。
摘要由CSDN通过智能技术生成

视频链接

Unity 如何在任意物体上自由绘制

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

public class PaintManager : Singleton<PaintManager>
{
    public Shader texturePaint;
    private Material paintMaterial;

    int debugUVID = Shader.PropertyToID("_DebugUV");
    int positionID = Shader.PropertyToID("_Position");
    int hardnessID = Shader.PropertyToID("_Hardness");
    int strengthID = Shader.PropertyToID("_Strength");
    int radiusID = Shader.PropertyToID("_Radius");
    int colorID = Shader.PropertyToID("_Color");
    int textureID = Shader.PropertyToID("_MainTex");
    
    CommandBuffer command;

    private void Awake()
    {
        paintMaterial = new Material(texturePaint);

        command = new CommandBuffer();
        command.name = "CommmandBuffer - " + gameObject.name;
    }

    public void InitUVMask(Paintable paintable) 
    {
        RenderTexture mask = paintable.GetMask();
        RenderTexture copy = paintable.GetCopy();
        Renderer rend = paintable.GetRenderer();

        paintMaterial.SetInt(debugUVID, 1);

        command.SetRenderTarget(mask);
        command.SetRenderTarget(copy);
        command.DrawRenderer(rend, paintMaterial, 0);

        Graphics.ExecuteCommandBuffer(command);
        command.Clear();
    }

    public void Paint(Paintable paintable, Vector3 pos, float radius = 1f, float hardness = .5f, float strength = .5f, Color? color = null)
    {
        RenderTexture mask = paintable.GetMask();
        RenderTexture copy = paintable.GetCopy();
        Renderer rend = paintable.GetRenderer();
        
        paintMaterial.SetInt(debugUVID, 0);
        paintMaterial.SetVector(positionID, pos);
        paintMaterial.SetColor(colorID, color ?? Color.red);
        paintMaterial.SetFloat(hardnessID, hardness);
        paintMaterial.S
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值