码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3d 制作动态Mesh且可以随地面凹凸起伏

时间:2015-11-03 19:17:57      阅读:893      评论:0      收藏:0      [点我收藏+]

标签:

适用情景:主角带着光环,光环用一张贴图,要贴在地面上,并且随地面凹凸起伏

 

技术分享

 

//代码

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class test : MonoBehaviour {
	private MeshFilter mFilter;
	private Mesh mMesh;

	public int QuadNum = 1;
	public float QuadLength = 1;

	// Use this for initialization
	void Start () {
		mFilter = gameObject.GetComponent<MeshFilter>();
		mMesh = new Mesh();
		mFilter.sharedMesh = mMesh;
		mMesh.MarkDynamic();

		var len = 2 * QuadNum * QuadLength;
		var h_len = QuadNum * QuadLength;

		var vertices = new Vector3[QuadNum * 2 * QuadNum * 2 * 4];
		var uv = new Vector2[QuadNum * 2 * QuadNum * 2 * 4];
		var triangle = new int[QuadNum * 2 * QuadNum * 2 * 6];

		var offset = new Vector3(-h_len, 0, -h_len);

		int idx = 0;
		for (int i = 0; i < QuadNum * 2; i++)
		{
			for (int j = 0; j < QuadNum * 2; j++)
			{
				var p0 = offset + new Vector3(j * QuadLength, 0, i * QuadLength);
				var p1 = offset + new Vector3(j * QuadLength, 0, (i + 1) * QuadLength);
				var p2 = offset + new Vector3((j + 1) * QuadLength, 0, (i + 1) * QuadLength);
				var p3 = offset + new Vector3((j + 1) * QuadLength, 0, i * QuadLength);

				int quad = (j + i * QuadNum * 2) * 4;
				vertices[quad] = p0;
				vertices[quad + 1] = p1;
				vertices[quad + 2] = p2;
				vertices[quad + 3] = p3;

				uv[quad] = new Vector2((p0.x + h_len) / len, (p0.z + h_len) / len);
				uv[quad + 1] = new Vector2((p1.x + h_len) / len, (p1.z + h_len) / len);
				uv[quad + 2] = new Vector2((p2.x + h_len) / len, (p2.z + h_len) / len);
				uv[quad + 3] = new Vector2((p3.x + h_len) / len, (p3.z + h_len) / len);

				triangle[idx++] = quad;
				triangle[idx++] = quad + 1;
				triangle[idx++] = quad + 2;

				triangle[idx++] = quad;
				triangle[idx++] = quad + 2;
				triangle[idx++] = quad + 3;
			}
		}

		mMesh.vertices = vertices;
		mMesh.uv = uv;
		mMesh.triangles = triangle;
 		}
	
	// Update is called once per frame
	protected virtual void Update () {

		var len = 2 * QuadNum * QuadLength;
		var h_len = QuadNum * QuadLength;

		var vertices = new Vector3[QuadNum * 2 * QuadNum * 2 * 4];

		var offset = new Vector3(-h_len, 0, -h_len);

		for (int i = 0; i < QuadNum * 2; i++)
		{
			for (int j = 0; j < QuadNum * 2; j++)
			{
				var p0 = offset + new Vector3(j * QuadLength, 0, i * QuadLength);
				var p1 = offset + new Vector3(j * QuadLength, 0, (i + 1) * QuadLength); 
				var p2 = offset + new Vector3((j + 1) * QuadLength, 0, (i + 1) * QuadLength);
				var p3 = offset + new Vector3((j + 1) * QuadLength, 0, i * QuadLength);

				int quad = (j + i * QuadNum * 2) * 4;
				vertices[quad] = p0;
				vertices[quad + 1] = p1;
				vertices[quad + 2] = p2;
				vertices[quad + 3] = p3;

			}
		}
		

		var ray = new Ray(Vector3.down, Vector3.down);
		var hit = new RaycastHit();

		for (int i = 0; i < QuadNum * 2; i++)
		{
			for (int j = 0; j < QuadNum * 2; j++)
			{
				var p0 = offset + new Vector3(j * QuadLength, 0, i * QuadLength);
				var p1 = offset + new Vector3(j * QuadLength, 0, (i + 1) * QuadLength);
				var p2 = offset + new Vector3((j + 1) * QuadLength, 0, (i + 1) * QuadLength);
				var p3 = offset + new Vector3((j + 1) * QuadLength, 0, i * QuadLength);
				
				

				ray.origin = transform.localToWorldMatrix.MultiplyPoint3x4(p0)+new Vector3(0,10,0);
				if(Physics.Raycast(ray,out hit, 100))
				{
					p0 = transform.worldToLocalMatrix.MultiplyPoint3x4(hit.point);
				}

				ray.origin = transform.localToWorldMatrix.MultiplyPoint3x4(p1) + new Vector3(0, 10, 0);
				if (Physics.Raycast(ray, out hit, 100))
				{
					p1 = transform.worldToLocalMatrix.MultiplyPoint3x4(hit.point);
				}

				ray.origin = transform.localToWorldMatrix.MultiplyPoint3x4(p2) + new Vector3(0, 10, 0);
				if (Physics.Raycast(ray, out hit, 100))
				{
					p2 = transform.worldToLocalMatrix.MultiplyPoint3x4(hit.point);
				}

				ray.origin = transform.localToWorldMatrix.MultiplyPoint3x4(p3) + new Vector3(0, 10, 0);
				if (Physics.Raycast(ray, out hit, 100))
				{
					p3 = transform.worldToLocalMatrix.MultiplyPoint3x4(hit.point);
				}

				int quad = (j + i * QuadNum * 2) * 4;
				vertices[quad] = p0;
				vertices[quad + 1] = p1;
				vertices[quad + 2] = p2;
				vertices[quad + 3] = p3;
			}
		}
		mMesh.vertices = vertices;

	}

}

  

Unity3d 制作动态Mesh且可以随地面凹凸起伏

标签:

原文地址:http://www.cnblogs.com/mrblue/p/4933945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!