Unity彩色正弦波


前言

本文基于 Unity3D 和 C# 描绘了一个动态的彩色正弦波


以下是本篇文章正文内容

一、创建一排立方体

1.绘制 y = x y=x y=x

在这里插入图片描述

2.绘制 y = x 2 y=x^{2} y=x2

在这里插入图片描述

二、给视图上色

1.彩色的 y = x 2 y=x^{2} y=x2

在这里插入图片描述

2.彩色的 y = x 2 y=x^{2} y=x2

在这里插入图片描述

3.动态化

时刻1
在这里插入图片描述
时刻2
在这里插入图片描述

完整 c# 代码:

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

public class Graph : MonoBehaviour //
{
    public Transform pointPrefab;

    [Range(10,100)]
    public int resolution = 10;

    Transform[] points;
    
    void Awake()
    {
        points = new Transform[resolution]; // 数组

        float step = 2f / resolution;

        Vector3 scale = Vector3.one *step;
        Vector3 position;
        position.y = 0f;
        position.z = 0f;

        for (int i = 0; i < points.Length; i++) 
        {
            Transform point = Instantiate(pointPrefab);

            position.x = (i + 0.5f) *step - 1f;
            point.localPosition = position;
            point.localScale = scale;
            point.SetParent(transform,false);
            points[i] = point;
        }
    }

    void Update()
    {
        for (int i = 0; i < points.Length; i++)
        {
            Transform point = points[i];
            Vector3 position = point.localPosition;
            //    position.y = position.x * position.x * position.x;
            position.y = Mathf.Sin(Mathf.PI * (position.x+Time.time));
            point.localPosition = position;
        }
    }
}

shader文件代码:

Shader "Custom/NewSurfaceShader"
{
    Properties
    {
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200
        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows
		#pragma target 3.0

        struct Input
        {
		    float3 worldPos; 
        };
       
        half _Glossiness;
        half _Metallic;

        UNITY_INSTANCING_BUFFER_START(Props)
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            o.Albedo.rg=IN.worldPos.xy*0.5+0.5; //
			o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = 1;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

总结

参考教程

以上就是今天要讲的内容,简单介绍了Unity彩色正弦波的实现过程,也是记录下自己的学习和成长啦qwq Unity系列随缘更新qwq
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值