Exemplo n.º 1
0
        public void OnBuildBlock(ref LiveMesh mesh, ref int index, Vector3 pos, Vector3 scale, VoxelVisiableFaces faces)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int n = index * 4, k = 0; k < 4; k++, n++)
                {
                    Vector3 v = _vertices[i, k];
                    v.x *= scale.x;
                    v.y *= scale.y;
                    v.z *= scale.z;
                    v.x += pos.x;
                    v.y += pos.y;
                    v.z += pos.z;

                    mesh.vertices[n] = v;
                    mesh.normals[n]  = _normals[i];
                    mesh.uv[n]       = _uvs[i, k];
                }

                for (int j = index * 6, k = 0; k < 6; k++, j++)
                {
                    mesh.indices[j] = index * 4 + _indices[i, k];
                }

                index++;
            }
        }
Exemplo n.º 2
0
        private void BuildGameObject(ChunkDataContext context, LiveMesh data)
        {
            if (data.indices.Length > 0)
            {
                var actors = new GameObject(this.name);
                actors.isStatic           = this.gameObject.isStatic;
                actors.tag                = gameObject.tag;
                actors.layer              = this.gameObject.layer;
                actors.transform.parent   = context.parent.transform;
                actors.transform.position = context.parent.transform.position;

                if (msehMaterial != null)
                {
                    var clone = actors.AddComponent <MeshRenderer>();
                    clone.material = msehMaterial;
                }

                var mesh = data.mesh;
                actors.AddComponent <MeshFilter>().mesh = mesh;

                if (physicMaterial)
                {
                    var meshCollider = actors.AddComponent <MeshCollider>();
                    meshCollider.sharedMesh = mesh;
                    meshCollider.material   = physicMaterial;
                }
            }
        }
Exemplo n.º 3
0
        public override void OnBuildChunk(ChunkDataContext context)
        {
            var writeCount = 0;
            var data       = new LiveMesh((context.faceCount / 6) * 16, (context.faceCount / 6) * 24);

            foreach (VoxelPrimitive it in context.model.GetEnumerator(this.material.GetInstanceID()))
            {
                Vector3 pos, scale;
                it.GetTranslateScale(out pos, out scale);
                OnBuildBlock(ref data, ref writeCount, pos, scale, it.faces);
            }

            if (data.indices.Length > 0)
            {
                var actors = new GameObject(this.name);
                actors.isStatic           = this.gameObject.isStatic;
                actors.tag                = gameObject.tag;
                actors.layer              = this.gameObject.layer;
                actors.transform.parent   = context.parent.transform;
                actors.transform.position = context.parent.transform.position;

                var mesh = data.mesh;
                actors.AddComponent <MeshFilter>().mesh = mesh;

                if (_renderer != null)
                {
                    var clone = actors.AddComponent <MeshRenderer>();
                    clone.material          = _renderer.material;
                    clone.receiveShadows    = _renderer.receiveShadows;
                    clone.shadowCastingMode = _renderer.shadowCastingMode;

                    if (_lodGroup != null)
                    {
                        var lods = _lodGroup.GetLODs();
                        for (int i = 0; i < lods.Length; i++)
                        {
                            if (lods[i].renderers.Length > 0)
                            {
                                lods[i].renderers[0] = clone;
                            }
                        }

                        actors.AddComponent <LODGroup>().SetLODs(lods);
                    }
                }

                if (_meshCollider && _meshCollider.enabled)
                {
                    var meshCollider = actors.AddComponent <MeshCollider>();
                    meshCollider.sharedMesh = _meshCollider.sharedMesh ? _meshCollider.sharedMesh : mesh;
                    meshCollider.material   = _meshCollider.material;
                }
            }
        }
Exemplo n.º 4
0
        private void BuildBlocks(ChunkDataContext context, out LiveMesh mesh)
        {
            var writeCount = 0;

            mesh = new LiveMesh(context.faceCount * 4, context.faceCount * 6);

            foreach (VoxelPrimitive it in context.model.GetEnumerator(this.material.GetInstanceID()))
            {
                Vector3 translate, scale;
                it.GetTranslateScale(out translate, out scale);

                float s = tilePadding > 0 ? 1.0f / tilePadding : 0;

                float a = s;
                float b = 1.0f / tileSize - a;

                for (int i = 0; i < 6; i++)
                {
                    if (!it.faces[i])
                    {
                        continue;
                    }

                    float du = (tiles[i] % tileSize) / (float)tileSize;
                    float dv = (tiles[i] / (float)tileSize) / tileSize;

                    for (int n = writeCount * 4, k = 0; k < 4; k++, n++)
                    {
                        Vector3 v = _vertices[i, k];
                        v.x *= scale.x;
                        v.y *= scale.y;
                        v.z *= scale.z;
                        v.x += translate.x;
                        v.y += translate.y;
                        v.z += translate.z;

                        mesh.vertices[n] = v;
                        mesh.normals[n]  = _normals[i];
                        mesh.uv[n].x     = du + (_uvs[i, k].x > 0 ? b : a);
                        mesh.uv[n].y     = dv + (_uvs[i, k].y > 0 ? b : a);
                    }

                    for (int j = writeCount * 6, k = 0; k < 6; k++, j++)
                    {
                        mesh.indices[j] = writeCount * 4 + _indices[i, k];
                    }

                    writeCount++;
                }
            }
        }
Exemplo n.º 5
0
 public override void OnBuildChunk(ChunkDataContext context)
 {
     if (context.async)
     {
         _task = Task.Run(() => { LiveMesh mesh = null; doBuildMesh(context, out mesh); return(mesh); });
         _task.GetAwaiter().OnCompleted(() =>
         {
             doBuildGameObject(context, _task.Result);
         });
     }
     else
     {
         LiveMesh mesh;
         doBuildMesh(context, out mesh);
         doBuildGameObject(context, mesh);
     }
 }
Exemplo n.º 6
0
        private void doBuildMesh(ChunkDataContext context, out LiveMesh mesh)
        {
            mesh = new LiveMesh(context.faceCount * 4, context.faceCount * 6);

            var writeCount = 0;

            foreach (VoxelPrimitive it in context.model.GetEnumerator(this.material.GetInstanceID()))
            {
                Vector3 translate, scale;
                it.GetTranslateScale(out translate, out scale);

                for (int i = 0; i < 6; i++)
                {
                    if (!it.faces[i])
                    {
                        continue;
                    }

                    for (int n = writeCount * 4, k = 0; k < 4; k++, n++)
                    {
                        Vector3 v = _vertices[i, k];
                        v.x *= scale.x;
                        v.y *= scale.y;
                        v.z *= scale.z;
                        v.x += translate.x;
                        v.y += translate.y;
                        v.z += translate.z;

                        mesh.vertices[n] = v;
                        mesh.normals[n]  = _normals[i];
                        mesh.uv[n]       = _uvs[i, k];
                    }

                    for (int j = writeCount * 6, k = 0; k < 6; k++, j++)
                    {
                        mesh.indices[j] = writeCount * 4 + _indices[i, k];
                    }

                    writeCount++;
                }
            }
        }