zoukankan      html  css  js  c++  java
  • 【自走棋】曲线箭头

    <1>效果:

    <2>思路:

    使用贝塞尔曲线和LineRender

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO.Compression;
    
    public class CurveTest : MonoBehaviour
    {
    
        public float height = 4;
        [SerializeField]
        private Vector3 P0;
        [SerializeField]
        private Vector3 P1;
        [SerializeField]
        private Vector3 P2 = Vector3.zero;
        [SerializeField]
        private LineRenderer lineRenderer;
    
        private float useTime = 0;
        private float interval = 0;
        private int pointNum = 0;
        private List<Vector3> lst = new List<Vector3>();
    
        private GameObject start;
        private GameObject end;
    
        private void reset()
        {
            useTime = 0;
            float val = (P2 - P0).magnitude;
            pointNum = Mathf.FloorToInt(val)+1;
            interval = 1.0f / pointNum;
            lst.Clear();
            Vector3 p = (P0 + P2) * 0.5f;
            P1 = p + Vector3.up* pointNum/2;
        }
    
        private void showLine()
        {
            //从0开始 num+1
            Vector3 pos0 = getPosByTime(useTime);
            lst.Add(pos0);
            for (int i = 1; i <= pointNum; i++)
            {
                useTime += interval;
                Vector3 pos = getPosByTime(useTime);
                lst.Add(pos);
            }
            //策略:根据P0-P2的距离 计算应该有多少个间隔
            lineRenderer.SetVertexCount(lst.Count);
            for (int i = 0; i < lst.Count; i++)
            {
                lineRenderer.SetPosition(i, lst[i]);
            }
            lineRenderer.material.SetTextureScale("_MainTex", new Vector2(  (int)(lst.Count * 0.9), 1));
            setStartEnd();
        }
    
        private void setStartEnd()
        {
            if (start == null) start = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            start.transform.localScale = Vector3.one * 0.3f;
            if (end == null) end = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            end.transform.localScale = Vector3.one * 0.3f;
            start.transform.position = P0;
            end.transform.position = P2;
        }
    
        /// <summary>
        /// 通过时间点获取时间
        /// </summary>
        /// <param name="useTime"></param>
        /// <returns></returns>
        private Vector3 getPosByTime(float useTime)
        {
            return (1 - useTime) * (1 - useTime) * P0 + 2 * useTime * (1 - useTime) * P1 + useTime * useTime * P2;
        }
    
        void OnGUI()
        {
            if (GUILayout.Button("重置", GUILayout.Width(100), GUILayout.Height(100)))
            {
                reset();
                showLine();
            }
        }
    
    }
    

      

  • 相关阅读:
    ntohs, ntohl, htons,htonl的比较和详解
    转 linux socket的select函数例子
    转 结构体中字节对齐问题(转载)
    C语言中volatile关键字的作用
    转 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法
    转 常见hash算法的原理
    转 从头到尾彻底解析Hash表算法
    [CEOI2020 D2T1 / CF1403A] 权力药水The Potion of Great Power 题解
    [CEOI2020 D1T3 / CF1402C] 星际迷航Star Trek 题解
    [CEOI2020 D1T2 / CF1402B] 道路Roads 题解
  • 原文地址:https://www.cnblogs.com/cocotang/p/10825417.html
Copyright © 2011-2022 走看看