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);
}
}
}
unity下使用mesh网格实现翻书效果
于 2024-05-11 16:10:21 首次发布