private Task task_(string name, VoxelData[] datas, GeometryResult cb) { if (list_.Count == 0) { init (); } VoxelProduct product = new VoxelProduct (); begin_.setup (datas); TaskList tl = new TaskList (); tl.push(begin_.task (product)); for (int i = 0; i < list_.Count; ++i) { tl.push(list_[i].task (product)); } TaskManager.PushBack (tl, delegate { VoxelGeometry geometry = new VoxelGeometry (); geometry.draw (name, product, this.gameObject, this._material);; cb(geometry); }); return tl; }
public override void build(VoxelProduct product){ product.min = new Vector3(999, 999, 999); product.max = new Vector3(-999, -999, -999); product.voxels = new Dictionary<VectorInt3, VoxelHandler>(); for (int i=0; i<data_.Length; ++i) { VoxelData d = data_ [i]; product.min.x = Mathf.Min (product.min.x, d.pos.x); product.min.y = Mathf.Min (product.min.y, d.pos.y); product.min.z = Mathf.Min (product.min.z, d.pos.z); product.max.x = Mathf.Max (product.max.x, d.pos.x); product.max.y = Mathf.Max (product.max.y, d.pos.y); product.max.z = Mathf.Max (product.max.z, d.pos.z); } for (int i=0; i<data_.Length; ++i) { VoxelHandler handler = data2Handler(data_[i]); product.voxels.Add (handler.position, handler); } }
public void build (VoxelData[] datas) { if (empty) { this._product = new VoxelProduct(); VoxelData2Point vd2p = new VoxelData2Point(datas); vd2p.build(this._product); VoxelShadowBuild vsb = new VoxelShadowBuild (); vsb.build(this._product); VoxelMeshBuild vmb = new VoxelMeshBuild (); vmb.build(this._product); VoxelRemoveSameVertices rsv = new VoxelRemoveSameVertices (); rsv.build(this._product); //VoxelRemoveFace vrf = new VoxelRemoveFace (); //vrf.build(this._product); /* VoxelRemoveFace vrf = new VoxelRemoveFace (); vrf.build(this._product); */ _geometry = new VoxelGeometry(); _geometry.draw (this._product, this.gameObject, this._material); } }
static public Task Build(VoxelData2Point vd2p, VoxelProduct product) { Task task = new Task(); task.init = delegate { vd2p.build(product); }; return(task); }
static public Task Task(VoxelMerge vm, VoxelProduct product) { Task task = new Task(); TaskManager.PushFront(task, delegate { Build.Run(vm, product); }); return(task); }
static public Task Task(VoxelData2Point d2p, VoxelProduct product) { Task task = new Task(); TaskManager.PushFront(task, delegate { Build.Run(d2p, product); }); return(task); }
static public Task Task(VoxelSplitSmall vss, VoxelProduct product) { Task task = new Task(); TaskManager.PushFront(task, delegate { Build.Run(vss, product); }); return(task); }
static public Task Task(VoxelRemoveSameVertices rsv, VoxelProduct product) { Task task = new Task(); TaskManager.PushFront(task, delegate { rsv.build(product); }); return(task); }
public MeshFilter crateMeshFilter(VoxelProduct product, string name, Material material){ GameObject go = new GameObject(name); MeshFilter meshFilter = go.AddComponent<MeshFilter>(); meshFilter.mesh = createMesh(product); MeshRenderer renderer = go.AddComponent<MeshRenderer>(); renderer.material = material; return meshFilter; }
// Use this for initialization void Start() { //读入文件数据 Stream sw = new MemoryStream(_file.bytes); System.IO.BinaryReader br = new System.IO.BinaryReader(sw); //从文件中解析MagicaVoxel文件,并转化成通用格式 MagicaVoxel magica = MagicaVoxelFormater.ReadFromBinary(br); VoxelStruct vs = magica.vs; //拿出体素数据 VoxelData[] datas = vs.datas.ToArray(); //创建体素“产品” VoxelProduct product = new VoxelProduct(); //将数据写入产品 (new VoxelData2Point(datas)).build(product); //切成8x8x8立方体,为了等下减面的时间可控,如果不切割的话,对于大模型减面时间太长 (new VoxelSplitSmall(new VectorInt3(8, 8, 8))).build(product); //构造模型 (new VoxelMeshBuild()).build(product); //减去重复的顶点 (new VoxelRemoveSameVertices()).build(product); //减去重复的面 (new VoxelRemoveFace()).build(product); //减去重复的顶点(在减面过程中出现的重复顶点) (new VoxelRemoveSameVertices()).build(product); //得到模型数据 VoxelMeshData data = product.getMeshData(); //以上是通过体素生成模型数据的代码,如果有别的算法生成,可以直接走下面代码 //通过模型数据生成Mesh 和 MeshFilter。 Mesh mesh = VoxelBuilder.Data2Mesh(data); MeshFilter filter = VoxelBuilder.Mesh2Filter(mesh); //别忘了加上材质。 VoxelBuilder.FilterAddRenderer(filter, _material); //设置模型的坐标和名称等。 filter.transform.SetParent(this.transform); filter.transform.localEulerAngles = Vector3.zero; filter.transform.localPosition = data.offset; filter.name = "Voxel"; }
//public TextAsset _file = null; // Use this for initialization void Start() { //这里用程序创建5个voxel VoxelData[] datas = new VoxelData[5]; //分别设置颜色和坐标 datas [0] = new VoxelData(new VectorInt3(0, 0, 0), Color.white); datas [1] = new VoxelData(new VectorInt3(0, 1, 0), Color.green); datas [2] = new VoxelData(new VectorInt3(0, -1, 0), Color.yellow); datas [3] = new VoxelData(new VectorInt3(1, 0, 0), Color.red); datas [4] = new VoxelData(new VectorInt3(-1, 0, 0), Color.blue); //创建体素“产品” VoxelProduct product = new VoxelProduct(); //将数据写入产品 (new VoxelData2Point(datas)).build(product); //切成8x8x8立方体,为了等下减面的时间可控,如果不切割的话,对于大模型减面时间太长 (new VoxelSplitSmall(new VectorInt3(8, 8, 8))).build(product); //构造模型 (new VoxelMeshBuild()).build(product); //减去重复的顶点 (new VoxelRemoveSameVertices()).build(product); //减去重复的面 (new VoxelRemoveFace()).build(product); //减去重复的顶点(在减面过程中出现的重复顶点) (new VoxelRemoveSameVertices()).build(product); //得到模型数据 VoxelMeshData data = product.getMeshData(); //以上是通过体素生成模型数据的代码,如果有别的算法生成,可以直接走下面代码 //通过模型数据生成Mesh 和 MeshFilter。 Mesh mesh = VoxelBuilder.Data2Mesh(data); MeshFilter filter = VoxelBuilder.Mesh2Filter(mesh); //别忘了加上材质。 VoxelBuilder.FilterAddRenderer(filter, _material); //设置模型的坐标和名称等。 filter.transform.SetParent(this.transform); filter.transform.localEulerAngles = Vector3.zero; filter.transform.localPosition = data.offset; filter.name = "Voxel"; }
public void draw(string name, VoxelProduct product, GameObject gameObject, Material material) { this._mesh = this.crateMeshFilter (product, name, material); this._mesh.gameObject.transform.SetParent (gameObject.transform); this._mesh.gameObject.transform.localPosition = Vector3.zero; this._mesh.gameObject.transform.localScale = Vector3.one; this._mesh.gameObject.transform.localRotation = Quaternion.Euler (Vector3.zero); this._mesh.gameObject.SetActive (true); Renderer renderer = this._mesh.GetComponent<Renderer> (); renderer.material = material; refresh (product, gameObject); }
void create() { int tick = ConvertDateTimeInt(DateTime.Now); // this._director.clear();//.arrange (); product_ = new VoxelProduct(); VoxelData2Point vd2p = new VoxelData2Point(); vd2p.setup(this.vs2.datas.ToArray()); vd2p.build(product_); logIt("create", ConvertDateTimeInt(DateTime.Now) - tick); }
private Mesh createMesh(VoxelProduct product) { Mesh m = new Mesh(); m.name = "ScriptedMesh"; product.draw.refresh(); m.vertices = product.draw.postions; m.colors = product.draw.colors; m.uv = product.draw.uv1s; m.uv2 = product.draw.uv2s; m.triangles = product.draw.triangles.ToArray (); m.RecalculateNormals(); return m; }
public void refresh(VoxelProduct product, GameObject gameObject) { Vector3 offset = Vector3.zero; Vector3 size = new Vector3 (product.max.x - product.min.x, product.max.z - product.min.z, product.max.y - product.min.y); offset = size / -2.0f -new Vector3 ( product.min.x, product.min.z, product.min.y); this._mesh.transform.localPosition = offset; if (_collider == null) { _collider = gameObject.GetComponent <BoxCollider>(); } if (_collider == null) { _collider = gameObject.AddComponent <BoxCollider>(); } _collider.size = size + Vector3.one; }
public VoxelGeometry build(VoxelData[] datas, GameObject obj = null) { if (obj == null) { obj = this.gameObject; } VoxelProduct product = new VoxelProduct(); begin_.init (); begin_.setup (datas); begin_.build(product); for (int i = 0; i < list_.Count; ++i) { list_ [i].init (); list_[i].build (product); } VoxelGeometry geometry = new VoxelGeometry(); geometry.draw ("Mesh", product, obj, this._material); return geometry; }
public override void build(VoxelProduct product){ draw_ = product.draw; List<VoxelDrawData.Vertice> vertices = draw_.vertices; List<int> triangles = draw_.triangles; List<VoxelDrawData.Vertice> tVertices = new List<VoxelDrawData.Vertice>(); List<int> tTriangles = new List<int>(); Dictionary<int, int> ht = new Dictionary<int, int>(); for(int i=0; i<vertices.Count; ++i){ int index = tVertices.FindIndex(delegate(VoxelDrawData.Vertice v) { if(v.position == vertices[i].position && v.color == vertices[i].color && v.normal == vertices[i].normal ){ return true; } else{ return false; } }); int newIndex = -1; int oldIndex = i; if(index == -1){ newIndex = tVertices.Count; tVertices.Add(vertices[i]); }else{ newIndex = index; } ht[oldIndex] = newIndex; } for(int i = 0; i<triangles.Count; ++i){ int oldIndex = triangles[i]; int newIndex = ht[oldIndex]; tTriangles.Add(newIndex); } product.draw.triangles = tTriangles; product.draw.vertices = tVertices; }
static public Task Task(VoxelMeshBuild vmb, VoxelProduct product) { TaskPack tp = new TaskPack(delegate() { if (product.sub != null) { TaskList tl = new TaskList(); for (int i = 0; i < product.sub.Length; ++i) { tl.push(Build.Task(vmb, product.sub[i], product.main.voxels)); } return(tl); } else { return(Build.Task(vmb, product.main, product.main.voxels)); } }); return(tp); }
static public Task Task(VoxelRemoveFace vrf, VoxelProduct product) { TaskPack tp = new TaskPack(delegate() { if (product.sub != null) { TaskList tl = new TaskList(); for (int i = 0; i < product.sub.Length; ++i) { tl.push(Build.Task(vrf, product.sub[i])); } return(tl); } else { return(Build.Task(vrf, product.main)); } }); return(tp); }
private Task task_(VoxelProduct product) { TaskList tl = new TaskList (); tl.push (TaskLog.Logger(initVerticesTask(product.draw), "init_vertices")); tl.push (TaskLog.Logger(removeFacesTask(), "remove_face")); tl.push (TaskLog.Logger(updateVerticesAndTriangleOfProductTask(product), "update_vertices")); return tl; }
static public void Run(VoxelRemoveSameVertices rsv, VoxelProduct product) { rsv.build(product); }
static public void Run(VoxelData2Point d2p, VoxelProduct product) { d2p.build(product); }
static public void Run(VoxelMeshBuild vmb, VoxelProduct product) { vmb.build(product); }
/** * do reduce faces number of product */ public void build(VoxelProduct product) { initVertices(product.draw); removeFaces(); updateVerticesAndTriangleOfProduct(product); }
private void updateVerticesAndTriangleOfProduct(VoxelProduct product) { List<VoxelDrawData.Vertice> temp_draw_vertices = new List<VoxelDrawData.Vertice>(); List<Triangle> temp_triangles_list = new List<Triangle>(); Dictionary<int, int> id_to_vertex_index = new Dictionary<int, int>(); for (int i = 0, vertices_count = vertices.Count; i < vertices_count; ++i) { temp_draw_vertices.Add(product.draw.vertices[vertices[i].id]); id_to_vertex_index[vertices[i].id] = i; for (int j = 0, faces_count = vertices[i].adjacent_faces.Count; j < faces_count; ++j) { temp_triangles_list.Add(vertices[i].adjacent_faces[j]); } } product.draw.vertices = temp_draw_vertices; distinct(temp_triangles_list); List<int> triangles = new List<int>(); for (int i = 0, faces_count = temp_triangles_list.Count; i < faces_count; ++i) { for (int j = 0; j < 3; ++j) { triangles.Add(id_to_vertex_index[temp_triangles_list[i].triangle_vertices[j].id]); } } product.draw.triangles = triangles; }
public Task task(VoxelProduct product) { Task task = new Task (); task.init = delegate { build(product); }; return task; }
public void build(VoxelProduct product) { voxels = product.voxels; foreach (KeyValuePair<VectorInt3, VoxelHandler> kv in voxels) { kv.Value.lightShadowBegin (); shadowTest (kv.Value, new VectorInt3 (-1, -1, 1), new VectorInt3 (-1, 0, 1), 0, Vector3.back); shadowTest (kv.Value, new VectorInt3 (0, -1, 1), new VectorInt3 (0, 0, 1), 1, Vector3.back); shadowTest (kv.Value, new VectorInt3 (1, -1, 1), new VectorInt3 (1, 0, 1), 2, Vector3.back); shadowTest (kv.Value, new VectorInt3 (-1, -1, 0), new VectorInt3 (-1, 0, 0), 3, Vector3.back); shadowTest (kv.Value, new VectorInt3 (1, -1, 0), new VectorInt3 (1, 0, 0), 4, Vector3.back); shadowTest (kv.Value, new VectorInt3 (-1, -1, -1), new VectorInt3 (-1, 0, -1), 5, Vector3.back); shadowTest (kv.Value, new VectorInt3 (0, -1, -1), new VectorInt3 (0, 0, -1), 6, Vector3.back); shadowTest (kv.Value, new VectorInt3 (1, -1, -1), new VectorInt3 (1, 0, -1), 7, Vector3.back); //========================= shadowTest (kv.Value, new VectorInt3 (-1, 1, -1), new VectorInt3 (-1, 0, -1), 0, Vector3.forward); shadowTest (kv.Value, new VectorInt3 (0, 1, -1), new VectorInt3 (0, 0, -1), 1, Vector3.forward); shadowTest (kv.Value, new VectorInt3 (1, 1, -1), new VectorInt3 (1, 0, -1), 2, Vector3.forward); shadowTest (kv.Value, new VectorInt3 (-1, 1, 0), new VectorInt3 (-1, 0, 0), 3, Vector3.forward); shadowTest (kv.Value, new VectorInt3 (1, 1, 0), new VectorInt3 (1, 0, 0), 4, Vector3.forward); shadowTest (kv.Value, new VectorInt3 (-1, 1, 1), new VectorInt3 (-1, 0, 1), 5, Vector3.forward); shadowTest (kv.Value, new VectorInt3 (0, 1, 1), new VectorInt3 (0, 0, 1), 6, Vector3.forward); shadowTest (kv.Value, new VectorInt3 (1, 1, 1), new VectorInt3 (1, 0, 1), 7, Vector3.forward); //========================= shadowTest (kv.Value, new VectorInt3 (-1, -1, -1), new VectorInt3 (-1, -1, 0), 0, Vector3.down); shadowTest (kv.Value, new VectorInt3 (0, -1, -1), new VectorInt3 (0, -1, 0), 1, Vector3.down); shadowTest (kv.Value, new VectorInt3 (1, -1, -1), new VectorInt3 (1, -1, 0), 2, Vector3.down); shadowTest (kv.Value, new VectorInt3 (-1, 0, -1), new VectorInt3 (-1, 0, 0), 3, Vector3.down); shadowTest (kv.Value, new VectorInt3 (1, 0, -1), new VectorInt3 (1, 0, 0), 4, Vector3.down); shadowTest (kv.Value, new VectorInt3 (-1, 1, -1), new VectorInt3 (-1, 1, 0), 5, Vector3.down); shadowTest (kv.Value, new VectorInt3 (0, 1, -1), new VectorInt3 (0, 1, 0), 6, Vector3.down); shadowTest (kv.Value, new VectorInt3 (1, 1, -1), new VectorInt3 (1, 1, 0), 7, Vector3.down); shadowTest (kv.Value, new VectorInt3 (-1, 1, 1), new VectorInt3 (-1, 1, 0), 0, Vector3.up); shadowTest (kv.Value, new VectorInt3 (0, 1, 1), new VectorInt3 (0, 1, 0), 1, Vector3.up); shadowTest (kv.Value, new VectorInt3 (1, 1, 1), new VectorInt3 (1, 1, 0), 2, Vector3.up); shadowTest (kv.Value, new VectorInt3 (-1, 0, 1), new VectorInt3 (-1, 0, 0), 3, Vector3.up); shadowTest (kv.Value, new VectorInt3 (1, 0, 1), new VectorInt3 (1, 0, 0), 4, Vector3.up); shadowTest (kv.Value, new VectorInt3 (-1, -1, 1), new VectorInt3 (-1, -1, 0), 5, Vector3.up); shadowTest (kv.Value, new VectorInt3 (0, -1, 1), new VectorInt3 (0, -1, 0), 6, Vector3.up); shadowTest (kv.Value, new VectorInt3 (1, -1, 1), new VectorInt3 (1, -1, 0), 7, Vector3.up); //========================= shadowTest (kv.Value, new VectorInt3 (1, 1, -1), new VectorInt3 (0, 1, -1), 0, Vector3.left); shadowTest (kv.Value, new VectorInt3 (1, 0, -1), new VectorInt3 (0, 0, -1), 1, Vector3.left); shadowTest (kv.Value, new VectorInt3 (1, -1, -1), new VectorInt3 (0, -1, -1), 2, Vector3.left); shadowTest (kv.Value, new VectorInt3 (1, 1, 0), new VectorInt3 (0, 1, 0), 3, Vector3.left); shadowTest (kv.Value, new VectorInt3 (1, -1, 0), new VectorInt3 (0, -1, 0), 4, Vector3.left); shadowTest (kv.Value, new VectorInt3 (1, 1, 1), new VectorInt3 (0, 1, 1), 5, Vector3.left); shadowTest (kv.Value, new VectorInt3 (1, 0, 1), new VectorInt3 (0, 0, 1), 6, Vector3.left); shadowTest (kv.Value, new VectorInt3 (1, -1, 1), new VectorInt3 (0, -1, 1), 7, Vector3.left); //========================= shadowTest (kv.Value, new VectorInt3 (-1, -1, -1), new VectorInt3 (0, -1, -1), 0, Vector3.right); shadowTest (kv.Value, new VectorInt3 (-1, 0, -1), new VectorInt3 (0, 0, -1), 1, Vector3.right); shadowTest (kv.Value, new VectorInt3 (-1, 1, -1), new VectorInt3 (0, 1, -1), 2, Vector3.right); shadowTest (kv.Value, new VectorInt3 (-1, -1, 0), new VectorInt3 (0, -1, 0), 3, Vector3.right); shadowTest (kv.Value, new VectorInt3 (-1, 1, 0), new VectorInt3 (0, 1, 0), 4, Vector3.right); shadowTest (kv.Value, new VectorInt3 (-1, -1, 1), new VectorInt3 (0, -1, 1), 5, Vector3.right); shadowTest (kv.Value, new VectorInt3 (-1, 0, 1), new VectorInt3 (0, 0, 1), 6, Vector3.right); shadowTest (kv.Value, new VectorInt3 (-1, 1, 1), new VectorInt3 (0, 1, 1), 7, Vector3.right); /**/ kv.Value.lightShadowEnd (); } }
static public void Run(VoxelRemoveFace vrf, VoxelProduct product) { vrf.build(product); }
public Task task(VoxelProduct product) { return new TaskPack (delegate { return task_(product); }); }
/*private Task removeFacesTask(){ Task task = new Task (); task.init = delegate { removeFaces(); }; return task; }*/ private Task updateVerticesAndTriangleOfProductTask(VoxelProduct product) { Task task = new Task (); task.init = delegate { updateVerticesAndTriangleOfProduct(product); }; return task; }
public void build(VoxelProduct product) { mesh_ = VoxelGroups.Draw2Mesh (product.draw); VoxelGroups.Mesh2Draw (mesh_, product.draw); }
static public void Run(VoxelMerge vm, VoxelProduct product) { vm.build(product); }
static public void Run(VoxelSplitSmall vss, VoxelProduct product) { vss.build(product); }
public abstract void build(VoxelProduct product);