纪念碑谷,示例图片:
需要注意的是:FBX 导入新工程 File Scale会缩小,需要放大 Scale Factor
需要使用特殊的MeshFilter材质。
下载地址:http://pan.baidu.com/s/1jIbnQfG
代码如下:
using UnityEngine;
using System.Collections;
using System;
public class TwistScript : MonoBehaviour {
public float twist = 1.0f;
public float inputSensitivity = 1.5f;
private Vector3[] baseVertices;
private Vector3[] baseNormals;
private Vector3 new_pos;
// Update is called once per frame
void Update () {
twist += Input.GetAxis("Horizontal") * inputSensitivity * Time.deltaTime;
//限制扭曲范围
if (twist < -2)
{
twist = -2;
}
if (twist > 2)
{
twist = 2;
}
Mesh mesh = GetComponent<MeshFilter>().mesh;
if (baseVertices == null)
{
baseVertices = mesh.vertices;
}
if (baseNormals == null)
{
baseNormals = mesh.normals;
}
Vector3[] vertices = new Vector3[baseVertices.Length];
Vector3[] normals = new Vector3[baseNormals.Length];
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = DoTwist(baseVertices[i], baseVertices[i].y * twist);
normals[i] = DoTwist(baseNormals[i], baseVertices[i].y * twist);
}
mesh.vertices = vertices;
mesh.normals = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
//产生扭曲
private Vector3 DoTwist(Vector3 pos, float t)
{
float st = Mathf.Sin(t);
float ct = Mathf.Cos(t);
new_pos = Vector3.zero;
new_pos.x = pos.x * ct - pos.z * st;
new_pos.z = pos.x * st + pos.z * ct;
new_pos.y = pos.y;
return new_pos;
}
}
效果如下图所示: