Ejemplo n.º 1
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;
                }
            }
        }
Ejemplo n.º 2
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;
                }
            }
        }
Ejemplo n.º 3
0
        public override void OnBuildChunk(ChunkDataContext context)
        {
            if (_mesh == null)
            {
                return;
            }

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

                var actors = new GameObject(this.name);
                actors.isStatic                = gameObject.isStatic;
                actors.tag                     = gameObject.tag;
                actors.layer                   = gameObject.layer;
                actors.transform.parent        = context.parent.transform;
                actors.transform.localPosition = transform.position + pos;
                actors.transform.localRotation = transform.rotation;
                actors.transform.localScale    = Vector3.Scale(transform.localScale, scale);

                actors.AddComponent <MeshFilter>().sharedMesh = _mesh;

                if (_meshMaterial)
                {
                    var clone = actors.AddComponent <MeshRenderer>();
                    clone.material = _meshMaterial;

                    if (_lods != null)
                    {
                        LOD[] lods = new LOD[_lods.Length];

                        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 (_physicMaterial != null)
                {
                    var meshCollider = actors.AddComponent <MeshCollider>();
                    meshCollider.sharedMesh = _mesh;
                    meshCollider.material   = _physicMaterial;
                }
            }
        }
Ejemplo 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++;
                }
            }
        }
Ejemplo n.º 5
0
        public override void OnBuildChunk(ChunkDataContext context)
        {
            foreach (VoxelPrimitive it in context.model.GetEnumerator(this.material.GetInstanceID()))
            {
                Vector3 pos, scale;
                it.GetTranslateScale(out pos, out scale);

                var actors = new GameObject(this.name);
                actors.isStatic                = gameObject.isStatic;
                actors.tag                     = gameObject.tag;
                actors.layer                   = this.gameObject.layer;
                actors.transform.parent        = context.parent.transform;
                actors.transform.localPosition = transform.position + pos;
                actors.transform.localRotation = transform.rotation;
                actors.transform.localScale    = transform.localScale;

                actors.AddComponent <MeshFilter>().sharedMesh = _meshFilter.sharedMesh;

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

                    if (_lodGroup != null && _lodGroup.enabled)
                    {
                        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 : _meshFilter.mesh;
                    meshCollider.material   = _meshCollider.material;
                }
            }
        }
Ejemplo n.º 6
0
 public override void OnBuildChunk(ChunkDataContext context)
 {
     if (context.async)
     {
         _task = Task.Run(() => { LiveMesh data; BuildBlocks(context, out data); return(data); });
         _task.GetAwaiter().OnCompleted(() =>
         {
             BuildGameObject(context, _task.Result);
         });
     }
     else
     {
         LiveMesh data;
         BuildBlocks(context, out data);
         BuildGameObject(context, data);
     }
 }
Ejemplo n.º 7
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);
     }
 }
Ejemplo n.º 8
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++;
                }
            }
        }
Ejemplo n.º 9
0
 public abstract void OnBuildChunk(ChunkDataContext context);