Unity 3D : Catmull-Rom 曲線調整小工具

前言 :

本範例使用的 CatmullRom.cs 請參考這篇 ( 也建議先讀讀這篇原理,再來看這篇文章 ) :

https://blog.csdn.net/weixin_38884324/article/details/81328803

執行結果 :

这里写图片描述

C # :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MathTool.Curve;

public class Test6 : MonoBehaviour
{

    public RawImage imgCurve;
    public Slider s1, s2, s3, s4;
    public int width = 512, height = 512;
    public Color bgColor = new Color(1, 0, 0, 1);
    public Color lineColor = new Color(1, 1, 1, 1);

    Texture2D tCurveBG_Template = null;

    void Start()
    {
        tCurveBG_Template = new Texture2D(width, height);
        Color[] colors = new Color[width * height];
        for (int i = 0; i < colors.Length; i++)
        {
            colors[i] = bgColor;
        }
        tCurveBG_Template.SetPixels(colors);
        tCurveBG_Template.Apply();

        s1.value = 0;
        s2.value = 0.333f;
        s3.value = 0.666f;
        s4.value = 1;
    }

    Texture2D tCurve = null;

    public void SliderValueChange()
    {
        float y1 = s1.value * height;
        float y2 = s2.value * height;
        float y3 = s3.value * height;
        float y4 = s4.value * height - 1;

        Vector2[] pvs = {
            new Vector2(0,y1),
            new Vector2(0,y1),
            new Vector2(width/3f*1f,y2),
            new Vector2(width/3f*2f,y3),
            new Vector2(width,y4),
            new Vector2(width,y4)
        };

        for (int i = 0; i < width; i++)
        {
            Vector2 v = CatmullRom.Interp2D(pvs, i / (float)width);
            if (v.y < 0 || v.y >= width)
                return;
        }

        if (tCurve != null)
        {
            Destroy(tCurve);
        }

        tCurve = new Texture2D(width, height);

        tCurve.SetPixels(tCurveBG_Template.GetPixels());

        for (int i = 0; i < width; i++)
        {
            Vector2 v = CatmullRom.Interp2D(pvs, i / (float)width);
            tCurve.SetPixel(i, (int)v.y, lineColor);
            tCurve.SetPixel(i, (int)v.y + 1, lineColor);
            tCurve.SetPixel(i, (int)v.y - 1, lineColor);
        }

        tCurve.Apply();

        imgCurve.texture = tCurve;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值