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

unity3d 通过代码建立曲面

时间:2015-10-22 00:06:58      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

using UnityEngine;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.IO;


public class NewBehaviourScript : MonoBehaviour {

// Use this for initialization
void Start () {

var stream = new FileStream("C:\\shuju2.data", FileMode.Open);
var reader = new StreamReader(stream);
var list1 = new List<String>();
var list2 = new List<String>();
while (!reader.EndOfStream)
{
var content = reader.ReadLine();
var result = content.Split(new Char[] {‘ ‘}, StringSplitOptions.RemoveEmptyEntries);
list1.Add(result[0]);
list2.Add(result[1]);
}
int length = list1.Count;
double[] x = new double[length];
double[] y = new double[length];
double[] z = new double[length];
for (int i = 0; i < length; ++i)
{
x[i] = double.Parse(list1[i]);
y[i] = double.Parse(list2[i]);
z[i] = double.Parse(list2[i]);
}
reader.Close();
stream.Close();

 

double Pi = 3.1415926f;
double[] th = new double[100];
for (int i=0; i<100; ++i)
th[i] = i * 2 * Pi / 100;
double [,] R = new double[100, length];
double [,] TH = new double[100, length];
double [,] X = new double[100, length];
double [,] Y = new double[100, length];
double [,] Z = new double[100, length];
for(int i=0;i<100;++i)
for(int j=0;j<length;++j)
R[i,j]=x[j];
for(int i=0;i<100;++i)
for(int j=0;j<length;++j)
TH[i,j]=th[i];
for(int i=0;i<100;++i)
{
for(int j=0;j<length;++j)
{
X[i,j]=R[i,j]*System.Math.Cos(TH[i,j]);
Y[i,j]=R[i,j]*System.Math.Sin(TH[i,j]);
}
}
for(int i=0;i<100;++i)
for(int j=0;j<length;++j)
Z[i,j]=z[j];
double max = 0;
for(int i=0;i<length;++i)
{
for(int j=0;j<100;++j)
{
if(Z[j,i]>max)
max=Z[j,i];
}
}
Vector3[] vertices = new Vector3[length*100];
for (int i=0; i<100; ++i)
{
for(int j=0;j<length;++j)
{
vertices[i*length+j].Set((float)X[i,j],(float)Y[i,j],(float)Z[i,j]);
}
}
int [] triangles = new int[101*length*2*6];

int triangles_count =0;
for (int i=0; i<100; ++i)
{
for (int j=0; j<length-1; ++j)
{
if(i==99)
{
triangles[triangles_count++]=i*length+j;
triangles[triangles_count++]=i*length+j+1;
triangles[triangles_count++]=j+1;
triangles[triangles_count++]=i*length+j;
triangles[triangles_count++]=j+1;
triangles[triangles_count++]=j;
}
else
{
triangles[triangles_count++]=i*length+j;
triangles[triangles_count++]=i*length+j+1;
triangles[triangles_count++]=i*length+j+1+length;
triangles[triangles_count++]=i*length+j;
triangles[triangles_count++]=i*length+j+length+1;
triangles[triangles_count++]=i*length+j+length;
}

}
}

 

gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();


Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.vertices = vertices;
mesh.triangles = triangles;
gameObject.renderer.material.color = new Color(1.0f,0.0f,0.0f,0.5f);
GetComponent<MeshRenderer>().material.shader = Shader.Find("Transparent/Diffuse");

 


//CreateCube ();
}

// Update is called once per frame
void Update () {

}
void CreateCube()
{
gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
Vector3[] vertices = new Vector3[3];
int [] triangles = new int[3];
vertices [0].Set (0, 0, 0);
vertices [1].Set (0, 1, 0);
vertices [2].Set (0, 0, 1);
triangles [0] = 0;
triangles [1] = 1;
triangles [2] = 2;
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.vertices = vertices;
mesh.triangles = triangles;
}
}

 

测试数据 shuju2.data

1.62529962517393e-001 5.03736912948521e-002
3.32069364646237e-001 6.05343235585258e-002
5.17768012863151e-001 8.10546107796976e-002
6.79262708210035e-001 1.11986927351479e-001
8.56916649644989e-001 1.53278898880759e-001
1.08296453031517e+000 1.63366206994076e-001
1.30096770420339e+000 1.94225199415502e-001
1.47864957864801e+000 2.56278380374267e-001
1.71281199862430e+000 2.97497027753191e-001
1.90666708566181e+000 3.80290468384198e-001
2.04397179464350e+000 4.32015419021332e-001
2.22971234237490e+000 4.83677520386733e-001
2.41546685661113e+000 5.45720226466876e-001
2.61736665043061e+000 6.07741982789774e-001
2.79504852487522e+000 6.69795163748540e-001
2.94043984015335e+000 7.31890244221795e-001
3.15046813978376e+000 8.35423944525043e-001
3.32013324045608e+000 9.39010019221402e-001
3.50590172119714e+000 1.01143333001629e+000
3.66745228256334e+000 1.08388806544704e+000
3.81284359784148e+000 1.14598314592030e+000
3.97436622619802e+000 1.19767667192156e+000
4.09552565559647e+000 1.24942257231594e+000
4.21674095101423e+000 1.34269089156929e+000
4.33792831342234e+000 1.41519800139316e+000
4.45109890205814e+000 1.52923800495462e+000
4.57231419747590e+000 1.62250632420797e+000
4.70971667199140e+000 1.74689550784830e+000
4.78248216215461e+000 1.82984607165864e+000
4.86336219162394e+000 1.94392797473459e+000
4.97658864627906e+000 2.09949039715502e+000
5.04133736266998e+000 2.22397385470296e+000
5.10605814605123e+000 2.32769610282141e+000
5.18693817552056e+000 2.44177800589736e+000
5.21948013177397e+000 2.62858699124824e+000
5.30037412774813e+000 2.75304949903893e+000
5.34904739707995e+000 2.92945692991782e+000
5.39777653243110e+000 3.14738677965569e+000
5.41422907512059e+000 3.37573913362279e+000
5.41446650570270e+000 3.55220941377342e+000
5.39860055621605e+000 3.75984245782552e+000
5.39072344749205e+000 3.90518139871054e+000
5.32630992721705e+000 4.02983245431644e+000
5.15693812314616e+000 4.14423907862968e+000
4.97947971277883e+000 4.24827557310680e+000
4.81006600919345e+000 4.33154038327581e+000
4.57604325426546e+000 4.39412778304431e+000
4.36616858618817e+000 4.40478073460324e+000
4.12400335894441e+000 4.41547558567665e+000
3.93830471072750e+000 4.39495529845548e+000
3.73641888341286e+000 4.34331414684732e+000
3.52648834931625e+000 4.31244467954728e+000
3.35687911466326e+000 4.25038102370989e+000
3.17111063392220e+000 4.17795771291500e+000
2.99341479297275e+000 4.10552392724149e+000
2.84803744419945e+000 4.05380945148298e+000
2.70257629639717e+000 3.93981134743601e+000
2.56524365440582e+000 3.86732518736939e+000
2.42791101241448e+000 3.79483902730277e+000
2.31475439028350e+000 3.69117962845606e+000
2.20165363417185e+000 3.62904264846831e+000
2.12084343722667e+000 3.56686376896608e+000
2.04000530727184e+000 3.48392368003436e+000
1.95919511032666e+000 3.42174480053212e+000
1.86225360030307e+000 3.36996747550188e+000
1.79757471643631e+000 3.29738704152766e+000
1.68448792682948e+000 3.24563066625465e+000
1.61976714344823e+000 3.14190841813621e+000
1.55508825958146e+000 3.06932798416198e+000
1.48232276941824e+000 2.98637742035164e+000
1.39338406666213e+000 2.88268659686906e+000
1.28025537754081e+000 2.79978840745183e+000
1.21553459415956e+000 2.69606615933338e+000
1.13465456469023e+000 2.58198425625743e+000
1.06993378130898e+000 2.47826200813898e+000
1.00524093093738e+000 2.39530096945002e+000
9.24360901468054e-001 2.28121906637407e+000
8.67656891859099e-001 2.13596392451802e+000
7.70645549311364e-001 2.03228357591406e+000
6.81637014031100e-001 1.87668972885777e+000
6.16902264145014e-001 1.76258687602457e+000
5.36022234675689e-001 1.64850497294863e+000
4.46943866871276e-001 1.44100810231861e+000
3.17488333603934e-001 1.22318300136697e+000
1.47488036815707e-001 8.70462413516783e-001

 

unity3d 通过代码建立曲面

标签:

原文地址:http://www.cnblogs.com/xd-jinjian/p/4899333.html

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