unity下使用mesh网格实现翻书效果

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class CreatBook : MonoBehaviour
{
    [Range(-90,90)]
    public float ang = 0;
    public Material[] materialist;
    public int matid = 0;
    Material[] materials = new Material[2];
    public float h = 0.4f;
    // Start is called before the first frame update
    Vector3 screenPoint;
    void Start()
    {
        screenPoint = Camera.main.WorldToScreenPoint(transform.position);
        materials[0] = materialist[matid];
        materials[1] = materialist[matid + 1];
        Book();
    }

    public void Book()
    {
        //计算活动页位置
        float x = Mathf.Sin(ang * Mathf.Deg2Rad);
        float y = Mathf.Cos(ang * Mathf.Deg2Rad);

        List<CombineInstance> combines = new List<CombineInstance>();

        CombineInstance combine1 = new CombineInstance();
        VertexHelper vh1 = new VertexHelper();
        //左半书底页
        for (int i = 0; i < 10; i++)
        {
            Vector2 pos1 = Vector2.Lerp(new Vector2(-1, 0), new Vector2(0, h), (float)i / 10);
            Vector2 pos2 = Vector2.Lerp(new Vector2(0, h), new Vector2(0, 0), (float)i / 10);
            Vector2 pos = Vector2.Lerp(pos1, pos2, (float)i / 10);
            vh1.AddVert(new Vector3(pos.x, pos.y, -1), Color.white, new Vector2((float)i / 20, 0));
            vh1.AddVert(new Vector3(pos.x, pos.y, 1), Color.white, new Vector2((float)i / 20, 1));
        }
        //左半书活动页
        for (int i = 0; i < 10; i++)
        {
            Vector2 pos1 = Vector2.Lerp(new Vector2(0, 0), new Vector2(0, h), (float)i / 10);
            Vector2 pos2 = Vector2.Lerp(new Vector2(0, h), new Vector2(x, y), (float)i / 10);
            Vector2 pos = Vector2.Lerp(pos1, pos2, (float)i / 10);
            vh1.AddVert(new Vector3(pos.x, pos.y, -1), Color.white, new Vector2(0.5f + (float)i / 20, 0));
            vh1.AddVert(new Vector3(pos.x, pos.y, 1), Color.white, new Vector2(0.5f + (float)i / 20, 1));
        }
        vh1.AddVert(new Vector3(x, y, -1), Color.white, new Vector2(1, 0));
        vh1.AddVert(new Vector3(x, y, 1), Color.white, new Vector2(1, 1));
        for (int i = 0; i < 20; i++)
        {
            vh1.AddTriangle(i * 2, i * 2 + 1, (i + 1) * 2 + 1);
            vh1.AddTriangle(i * 2, (i + 1) * 2 + 1, (i + 1) * 2);
        }
        combine1.mesh = new Mesh();
        vh1.FillMesh(combine1.mesh);
        combines.Add(combine1);

        CombineInstance combine2 = new CombineInstance();
        VertexHelper vh2 = new VertexHelper();
        //右半书活动页
        for (int i = 0; i < 10; i++)
        {
            Vector2 pos1 = Vector2.Lerp(new Vector2(x, y), new Vector2(0, h), (float)i / 10);
            Vector2 pos2 = Vector2.Lerp(new Vector2(0, h), new Vector2(0, 0), (float)i / 10);
            Vector2 pos = Vector2.Lerp(pos1, pos2, (float)i / 10);
            vh2.AddVert(new Vector3(pos.x, pos.y, -1), Color.white, new Vector2((float)i / 20, 0));
            vh2.AddVert(new Vector3(pos.x, pos.y, 1), Color.white, new Vector2((float)i / 20, 1));
        }
        //右半书底页
        for (int i = 0; i < 10; i++)
        {
            Vector2 pos1 = Vector2.Lerp(new Vector2(0, 0), new Vector2(0, h), (float)i / 10);
            Vector2 pos2 = Vector2.Lerp(new Vector2(0, h), new Vector2(1, 0), (float)i / 10);
            Vector2 pos = Vector2.Lerp(pos1, pos2, (float)i / 10);
            vh2.AddVert(new Vector3(pos.x, pos.y, -1), Color.white, new Vector2(0.5f + (float)i / 20, 0));
            vh2.AddVert(new Vector3(pos.x, pos.y, 1), Color.white, new Vector2(0.5f + (float)i / 20, 1));
        }
        vh2.AddVert(new Vector3(1, 0, -1), Color.white, new Vector2(1, 0));
        vh2.AddVert(new Vector3(1, 0, 1), Color.white, new Vector2(1, 1));
        for (int i = 0; i < 20; i++)
        {
            vh2.AddTriangle(i * 2, i * 2 + 1, (i + 1) * 2 + 1);
            vh2.AddTriangle(i * 2, (i + 1) * 2 + 1, (i + 1) * 2);
        }
        combine2.mesh = new Mesh();
        vh2.FillMesh(combine2.mesh);
        combines.Add(combine2);

        Mesh mesh = new Mesh();
        //将左右两半书合并成一本
        mesh.CombineMeshes(combines.ToArray(), false, false);
        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshCollider>().sharedMesh = mesh;
        GetComponent<MeshRenderer>().materials = materials;
    }

    // Update is called once per frame
    void Update()
    {

    }
    Vector3 mousePosition;
    private void OnMouseDown()
    {
        mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    }
    //拖动翻书
    private void OnMouseDrag()
    {
        Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
        ang += Mathf.Asin(pos.x - mousePosition.x) * Mathf.Rad2Deg;
        if (Mathf.Asin(pos.x - mousePosition.x) * Mathf.Rad2Deg < 0 && ang <= -90)
        {
            ang = 90;
            matid++;
            materials[0] = materialist[matid % materialist.Length];
            materials[1] = materialist[(matid + 1) % materialist.Length];
        }
        if (Mathf.Asin(pos.x - mousePosition.x) * Mathf.Rad2Deg > 0 && ang >= 90)
        {
            ang = -90;
            matid--;
            if (matid < 0)
            {
                matid = materialist.Length - 1;
            }
            materials[0] = materialist[matid % materialist.Length];
            materials[1] = materialist[(matid + 1) % materialist.Length];
        }
        mousePosition = pos;
        Book();
    }
    //停止翻动书页自动落下
    private void OnMouseUp()
    {
        Debug.Log(ang);
        if (ang < 0)
        {
            float time = Mathf.Abs(-90 - ang) / 10;
            DOTween.To((a) =>
            {
                ang = a;
                Book();
            }, ang, -90, time);
        }
        if (ang > 0)
        {
            float time = Mathf.Abs(89.999f - ang) / 10;
            DOTween.To((a) =>
            {
                ang = a;
                Book();
            }, ang, 89.999f, time);
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值