Unity 获得顶点数据


获得顶点数据,并将数据反作用于模型:

using UnityEngine;
using System.Collections;
 
[RequireComponent(typeof(MeshFilter))]
 public class example : MonoBehaviour {

	 void Update() {
	      Mesh mesh = this.transform.GetComponent<MeshFilter>().mesh;
		  Vector3 [] vertices = mesh.vertices;
		  int p = 0;
		  int flag = -1;
		  float maxheight = 0.0F;
	      while (p < vertices.Length) {
		     if(vertices[p].z > maxheight) {
			     maxheight = vertices[p].z;
			     flag = p;    
			 }
		        p++;
		   }
	     //vertices[flag] += new Vector3(0, 0, maxheight);
		         
		 mesh.vertices = vertices;
	     mesh.RecalculateNormals();
	    }
	void OnGUI(){
		if (GUI.Button (new Rect (0, 0, 100, 30), "Mesh")) {
			Mesh mesh = this.transform.GetComponent<MeshFilter>().mesh;
			Vector3 [] vertices = mesh.vertices;
			for(int i=0;i<vertices.Length;i++){
				Debug.Log("vertices["+i+"].x  " + vertices[i].x + "   vertices["+i+"].y  " + vertices[i].y + "   vertices["+i+"].z  " + vertices[i].z);
				vertices[i].x *= 2;
				vertices[i].y *= 2;
				vertices[i].z *= 2;

			}
			mesh.vertices = vertices;


		}
	}
  }


获取模型的顶点、三角形、法线数据

using UnityEngine;  
using System.Collections;  
using System.Collections.Generic;  

public class MeshPrinter : MonoBehaviour {  
	
	private string text_name;  
	private string text_vertices;  
	private string text_normals;  
	private string text_triangles;  
	private string text_uv;  
	private string text_tangents;  
	
	public MeshFilter mCurrentFilter;  
	
	public List<MeshFilter> targets;  
	
	void Start () {  
		
		targets = new List<MeshFilter>();  
		AddAllTargets();  
		
		mCurrentFilter = null;  
		
	}  
	
	void Update () {  
		
		if(Input.GetKeyUp(KeyCode.Tab)){  
			TargetMesh();  
			FillText();  
		}  
	}  
	
	void OnGUI(){  
		
		
		float _x = 10;  
		Vector2 textSize = GUI.skin.label.CalcSize (new GUIContent(text_name));  
		GUI.Label(new Rect(_x, 10 ,textSize.x, textSize.y), text_name);  
		
		textSize = GUI.skin.label.CalcSize (new GUIContent(text_triangles));  
		GUI.Label(new Rect(_x, 30 ,textSize.x, textSize.y), text_triangles);  
		
		_x += textSize.x +20;  
		textSize = GUI.skin.label.CalcSize (new GUIContent(text_vertices));  
		GUI.Label(new Rect(_x, 30 ,textSize.x, textSize.y), text_vertices);  
		
		_x += textSize.x +20;  
		textSize = GUI.skin.label.CalcSize (new GUIContent(text_normals));  
		GUI.Label(new Rect(_x, 30 ,textSize.x, textSize.y), text_normals);  
		
		_x += textSize.x +20;  
		textSize = GUI.skin.label.CalcSize (new GUIContent(text_tangents));  
		GUI.Label(new Rect(_x, 30 ,textSize.x, textSize.y), text_tangents);  
		
		_x += textSize.x +20;  
		textSize = GUI.skin.label.CalcSize (new GUIContent(text_uv));  
		GUI.Label(new Rect(_x, 30 ,textSize.x, textSize.y), text_uv);  
		
	}  
	
	public void AddAllTargets(){  
		
		GameObject[] gos = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];  
		foreach(GameObject go in gos)  
			if(go.GetComponent<MeshFilter>() != null)  
				AddTarget( go.GetComponent<MeshFilter>());  
	}  
	
	public void AddTarget(MeshFilter target){  
		
		targets.Add(target);  
	}  
	
	private void TargetMesh(){  
		
		if(mCurrentFilter == null){  
			mCurrentFilter = targets[0];  
		}else{  
			int index = targets.IndexOf(mCurrentFilter);  
			if(index < targets.Count-1){  
				index ++;  
			}else{  
				index = 0;  
			}  
			mCurrentFilter = targets[index];  
		}  
	}  
	
	private void FillText(){  
		
		text_name = "Name: " + mCurrentFilter.gameObject.name;  
		
		Mesh mesh = mCurrentFilter.mesh;  
		
		int size = mesh.vertexCount;  
		text_vertices = "vertices: "+ size + "\n";  
		for(int i = 0; i<size; i++){  
			text_vertices +=  i + ": " + mesh.vertices[i][0]+","+mesh.vertices[i][1]+","+mesh.vertices[i][2]+";\n";   
		}  
		
		size = mesh.normals.Length;  
		text_normals = "normals: " + size + "\n";  
		for(int i = 0; i<size; i++){  
			text_normals += mesh.normals[i].x +","+ mesh.normals[i].y +","+mesh.normals[i].z +";\n";  
		}  
		
		size = mesh.triangles.Length ;  
		text_triangles = "triangles: " + size + "\n";  
		for(int i = 0; i<size/3; i++){  
			text_triangles += mesh.triangles [3*i] +","+ mesh.triangles [3*i+1] +","+mesh.triangles [3*i+2] +";\n";  
		}  
		
		size = mesh.uv.Length ;  
		text_uv = "uv: " + size + "\n";  
		for(int i = 0; i<size; i++){  
			text_uv += mesh.uv [i][0] +","+ mesh.uv [i][1] +";\n";  
			
		}  
		size = mesh.tangents.Length ;text_tangents = "tangents: " + size + "\n";  
		for(int i = 0; i<size; i++){  
			text_tangents += mesh.tangents[i][0] + ", "+ mesh.tangents[i][1] + ", "+mesh.tangents[i][2] + ", "+mesh.tangents[i][3] +";\n";  
		}  
	}  
}  


  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值