本文主要是介绍Unity3D 实现类似“纪念碑谷”扭曲物体的效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
纪念碑谷,示例图片:
需要注意的是: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 framevoid 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;}
}
效果如下图所示:
这篇关于Unity3D 实现类似“纪念碑谷”扭曲物体的效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!