Ejemplo n.º 1
0
        private SgtTerrainFace Create(string childName, SgtVector3D cornerBL, SgtVector3D cornerBR, SgtVector3D cornerTL, SgtVector3D cornerTR, SgtVector2D coordBL, SgtVector2D coordBR, SgtVector2D coordTL, SgtVector2D coordTR)
        {
            var face = Create(childName, gameObject.layer, transform);

            face.State    = StateType.Invalid;
            face.Terrain  = Terrain;
            face.Side     = Side;
            face.Depth    = Depth + 1;
            face.CornerBL = cornerBL;
            face.CornerBR = cornerBR;
            face.CornerTL = cornerTL;
            face.CornerTR = cornerTR;
            face.CoordBL  = coordBL;
            face.CoordBR  = coordBR;
            face.CoordTL  = coordTL;
            face.CoordTR  = coordTR;

            return(face);
        }
Ejemplo n.º 2
0
        public void BuildData()
        {
            var verts   = (2 << Terrain.Subdivisions) + 1;
            var step    = 1.0 / (verts - 1);
            var cornerU = (CornerBR - CornerBL) * step;
            var cornerV = (CornerTL - CornerBL) * step;
            var cornerS = SgtVector3D.Cross(cornerU, cornerV).normalized *cornerU.magnitude;
            var coordU  = (CoordBR - CoordBL) * step;
            var coordV  = (CoordTL - CoordBL) * step;

            ValidateVertices();

            Bounds.Clear();

            for (var y = 0; y < verts; y++)
            {
                var cubeY = CornerBL + cornerV * y;

                for (var x = 0; x < verts; x++)
                {
                    var cube     = cubeY + cornerU * x;
                    var normal   = cube.normalized;
                    var height   = Terrain.GetLocalHeight(normal);
                    var position = normal * height;

                    Bounds.Add(position);

                    //if (WriteVertex(x, y) == true)
                    {
                        var index   = x + y * verts;
                        var coord1  = CoordBL + coordU * x + coordV * y;
                        var coord2  = new SgtVector2D(x * step, y * step);
                        var tangent = default(SgtVector3D);
                        var color   = Color.white;

                        tangent = SgtVector3D.Cross(normal, cornerU).normalized;

                        if (Terrain.OnCalculateColor != null)
                        {
                            Terrain.OnCalculateColor(position, height, ref color);
                        }

                        positions[index] = position;
                        vertices[index]  = (Vector3)position;
                        coords1[index]   = (Vector2)coord1;
                        coords2[index]   = (Vector2)coord2;
                        colors[index]    = color;
                    }
                }
            }

            if (Terrain.Normals == SgtTerrain.NormalsType.Normalized)
            {
                for (var y = 0; y < verts; y++)
                {
                    var cubeY = CornerBL + cornerV * y;

                    for (var x = 0; x < verts; x++)
                    {
                        var index   = x + y * verts;
                        var cubeA   = cubeY + cornerU * x;
                        var cubeB   = GetPosition(cubeA, cornerU, cornerS);
                        var normal  = cubeA.normalized;
                        var tangent = (Vector3)(cubeB.normalized - normal).normalized;

                        normals1[index]  = (Vector3)normal;
                        tangents1[index] = new Vector4(tangent.x, tangent.y, tangent.z, -1.0f);
                    }
                }
            }

            if (Terrain.Normals == SgtTerrain.NormalsType.Hierarchical)
            {
                for (var y = 0; y < verts; y++)
                {
                    var cubeY = CornerBL + cornerV * y;

                    for (var x = 0; x < verts; x++)
                    {
                        var index = x + y * verts;
                        var cube  = cubeY + cornerU * x;

                        // Get position from vertex or recalc off edge
                        var positionL = x == 0         ? GetPosition(cube, -cornerU, cornerS) : positions[index - 1];
                        var positionR = x == verts - 1 ? GetPosition(cube, cornerU, cornerS) : positions[index + 1];
                        var positionB = y == 0         ? GetPosition(cube, -cornerV, cornerS) : positions[index - verts];
                        var positionT = y == verts - 1 ? GetPosition(cube, cornerV, cornerS) : positions[index + verts];

                        // Calc normal and tangent
                        var positionH = positionR - positionL;
                        var positionV = positionT - positionB;
                        var normal    = SgtVector3D.Cross(positionV, positionH).normalized;
                        var tangent   = (Vector3)positionH.normalized;

                        normals1[index]  = normals2[index] = (Vector3)normal;
                        tangents1[index] = tangents2[index] = new Vector4(tangent.x, tangent.y, tangent.z, -1.0f);
                    }
                }
            }

            if (Terrain.OnPostProcessVertices != null)
            {
                Terrain.OnPostProcessVertices(this);
            }

            if (Split == true)
            {
                CopyVertiesToChildren();
            }
        }