コード例 #1
0
    public static SgtPatch CreatePatch(string name, SgtTerrain terrain, SgtPatch parent, Vector3 pointBL, Vector3 pointBR, Vector3 pointTL, Vector3 pointTR, Vector3 coordBL, Vector3 coordBR, Vector3 coordTL, Vector3 coordTR, int depth)
    {
        if (terrain != null)
        {
            var parentTransform = parent != null ? parent.transform : terrain.transform;
            var patch           = SgtPatch.Create(name, terrain, parentTransform);

            patch.Parent  = parent;
            patch.Depth   = depth;
            patch.PointBL = pointBL;
            patch.PointBR = pointBR;
            patch.PointTL = pointTL;
            patch.PointTR = pointTR;
            patch.CoordBL = coordBL;
            patch.CoordBR = coordBR;
            patch.CoordTL = coordTL;
            patch.CoordTR = coordTR;

            patch.UpdateState();

        #if UNITY_EDITOR
            if (Application.isPlaying == false)
            {
                patch.UpdateSplitMerge();
            }
        #endif

            return patch;
        }

        return null;
    }
コード例 #2
0
    // This static method will rotate the transform to the surface of the terrain below
    public static void SnapTransformRotation(SgtTerrain terrain, Transform transform, float rightDistance = 1.0f, float forwardDistance = 1.0f)
    {
        if (terrain != null && transform != null)
        {
            var newNormal = default(Vector3);

            // Rotate to surface normal?
            if (rightDistance != 0.0f && forwardDistance != 0.0f)
            {
                var worldRight   = transform.right   * rightDistance;
                var worldForward = transform.forward * forwardDistance;

                newNormal = terrain.GetSurfaceNormalWorld(transform.position, worldRight, worldForward);
            }
            // Rotate to planet center?
            else
            {
                newNormal = terrain.GetSurfaceNormalWorld(transform.position);
            }

            var oldRotation = transform.rotation;
            var newRotation = Quaternion.FromToRotation(transform.up, newNormal) * oldRotation;

            //if (oldRotation != newRotation)
            {
                transform.rotation = newRotation;
            }
        }
    }
コード例 #3
0
    public void Spawn(SgtTerrain terrain, SgtTerrainLevel level, SgtVector3D localPoint)
    {
        if (OnSpawn != null)
        {
            OnSpawn();
        }

        transform.SetParent(level.transform, false);

        // Snap to surface
        localPoint = terrain.GetLocalPoint(localPoint);

        // Rotate up
        var up = Quaternion.Euler(0.0f, Random.Range(0.0f, 360.0f), 0.0f) * Vector3.up;

        // Spawn on surface
        transform.localPosition = (Vector3)localPoint;
        transform.localRotation = Quaternion.FromToRotation(up, terrain.transform.TransformDirection(transform.localPosition));
        transform.localScale    = Prefab.transform.localScale * Random.Range(ScaleMin, ScaleMax);
        //transform.rotation   = Quaternion.FromToRotation(up, terrain.transform.TransformDirection(localPosition));

        if (AlignToNormal != 0.0f)
        {
            //var worldRight   = transform.right   * AlignToNormal;
            //var worldForward = transform.forward * AlignToNormal;
            //var worldNormal  = terrain.GetLocalNormal(localPoint, worldRight, worldForward);

            //transform.rotation = Quaternion.FromToRotation(up, worldNormal);
        }
    }
コード例 #4
0
    public static SgtPatch CreatePatch(string name, SgtTerrain terrain, SgtPatch parent, Vector3 pointBL, Vector3 pointBR, Vector3 pointTL, Vector3 pointTR, Vector3 coordBL, Vector3 coordBR, Vector3 coordTL, Vector3 coordTR, int depth)
    {
        if (terrain != null)
        {
            var parentTransform = parent != null ? parent.transform : terrain.transform;
            var patch           = SgtPatch.Create(name, terrain, parentTransform);

            patch.Parent  = parent;
            patch.Depth   = depth;
            patch.PointBL = pointBL;
            patch.PointBR = pointBR;
            patch.PointTL = pointTL;
            patch.PointTR = pointTR;
            patch.CoordBL = coordBL;
            patch.CoordBR = coordBR;
            patch.CoordTL = coordTL;
            patch.CoordTR = coordTR;

            patch.UpdateState();

#if UNITY_EDITOR
            if (Application.isPlaying == false)
            {
                patch.UpdateSplitMerge();
            }
#endif

            return(patch);
        }

        return(null);
    }
コード例 #5
0
    public static SgtPatch Create(string name, SgtTerrain terrain, Transform parent)
    {
        var patch = SgtComponentPool <SgtPatch> .Pop(name, parent);

        patch.Terrain = terrain;

        return(patch);
    }
コード例 #6
0
    public static SgtPatch Create(string name, SgtTerrain terrain, Transform parent)
    {
        var patch = SgtComponentPool<SgtPatch>.Pop(name, parent);

        patch.Terrain = terrain;

        return patch;
    }
コード例 #7
0
    protected virtual void OnEnable()
    {
        if (terrain == null)
        {
            terrain = GetComponent <SgtTerrain>();
        }

        MarkAsDirty();
    }
コード例 #8
0
    protected void MarkAsDirty()
    {
        if (terrain == null)
        {
            terrain = GetComponent <SgtTerrain>();
        }

        terrain.MarkMeshAsDirty();
    }
コード例 #9
0
    public void DirtyTerrain()
    {
        if (terrain == null)
        {
            terrain = GetComponent <SgtTerrain>();
        }

        terrain.DirtyMeshes();
    }
コード例 #10
0
    // This static method will move the transform down to the surface of the terrain
    public static void SnapTransformPosition(SgtTerrain terrain, Transform transform, float offset = 0.0f)
    {
        if (terrain != null && transform != null)
        {
            var oldPosition = transform.position;
            var newPosition = terrain.GetSurfacePositionWorld(oldPosition, offset);

            if (oldPosition != newPosition)
            {
                transform.position = newPosition;
            }
        }
    }
コード例 #11
0
    // This static method will move the transform down to the surface of the terrain
    public static void SnapTransformPosition(SgtTerrain terrain, Transform transform, float offset = 0.0f, float dampening = 0.0f)
    {
        if (terrain != null && transform != null)
        {
            var oldPosition = transform.position;
            var newPosition = terrain.GetSurfacePositionWorld(oldPosition, offset);

            if (oldPosition != newPosition)
            {
                if (dampening > 0.0f)
                {
                    transform.position = SgtHelper.Dampen3(transform.position, newPosition, dampening, Time.deltaTime);
                }
                else
                {
                    transform.position = newPosition;
                }
            }
        }
    }
コード例 #12
0
    // This static method will move the transform down to the surface of the terrain
    public static void SnapTransformPosition(SgtTerrain terrain, Transform transform, float offset = 0.0f, float dampening = 0.0f)
    {
        if (terrain != null && transform != null)
        {
            var oldPosition = transform.position;
            var newPosition = terrain.GetWorldPoint(oldPosition, offset);

            if (oldPosition != newPosition)
            {
                if (dampening > 0.0f)
                {
                    transform.position = SgtHelper.Dampen3(transform.position, newPosition, dampening, Time.deltaTime);
                }
                else
                {
                    transform.position = newPosition;
                }
            }
        }
    }
コード例 #13
0
    // This static method will rotate the transform to the surface of the terrain below
    public static void SnapTransformRotation(SgtTerrain terrain, Transform transform, float rightDistance = 1.0f, float forwardDistance = 1.0f, float dampening = 0.0f)
    {
        if (terrain != null && transform != null)
        {
            var newNormal = default(Vector3);

            // Rotate to surface normal?
            if (rightDistance != 0.0f && forwardDistance != 0.0f)
            {
                var worldRight   = transform.right * rightDistance;
                var worldForward = transform.forward * forwardDistance;

                newNormal = terrain.GetWorldNormal(transform.position, worldRight, worldForward);
            }
            // Rotate to planet center?
            else
            {
                newNormal = terrain.GetWorldNormal(transform.position);
            }

            var oldRotation = transform.rotation;
            var newRotation = Quaternion.FromToRotation(transform.up, newNormal) * oldRotation;

            //if (oldRotation != newRotation)
            {
                if (dampening > 0.0f)
                {
                    transform.rotation = SgtHelper.Dampen(transform.rotation, newRotation, dampening, Time.deltaTime);
                }
                else
                {
                    transform.rotation = newRotation;
                }
            }
        }
    }
コード例 #14
0
    protected virtual void OnEnable()
    {
        if (terrain == null) terrain = GetComponent<SgtTerrain>();

        MarkAsDirty();
    }
コード例 #15
0
    protected void MarkAsDirty()
    {
        if (terrain == null) terrain = GetComponent<SgtTerrain>();

        terrain.MarkMeshAsDirty();
    }
コード例 #16
0
ファイル: SgtTerrainPlane.cs プロジェクト: BackToGround/XGame
    private void Build(SgtTerrain terrain, Vector3 bestPoint)
    {
        if (meshCollider == null)
        {
            var gameObject = new GameObject("Plane");
#if UNITY_EDITOR
            gameObject.hideFlags = HideFlags.DontSave;
#endif
            meshCollider = gameObject.AddComponent <MeshCollider>();
        }

        if (mesh == null)
        {
            mesh = SgtObjectPool <Mesh> .Pop() ?? new Mesh();

#if UNITY_EDITOR
            mesh.hideFlags = HideFlags.DontSave;
#endif
            mesh.name = "Plane";
        }

        var sideE        = Detail;
        var sideP        = Detail + 1;
        var vertexCount  = sideP * sideP;
        var indexCount   = sideE * sideE * 6;
        var rotation     = Quaternion.Inverse(terrain.transform.rotation) * Quaternion.LookRotation(bestPoint);
        var distance     = bestPoint.magnitude;
        var uniformScale = SgtHelper.UniformScale(terrain.transform.lossyScale);
        var size         = Size * SgtHelper.Reciprocal(uniformScale);
        var step         = (size * 2.0f) / Detail;

        if (positions == null || positions.Length != vertexCount)
        {
            positions = new Vector3[vertexCount];
        }

        for (var y = 0; y <= Detail; y++)
        {
            for (var x = 0; x <= Detail; x++)
            {
                var index = x + y * sideP;
                var point = rotation * new Vector3(x * step - size, y * step - size, distance);

                positions[index] = terrain.GetLocalPoint(point);
            }
        }

        // Regen indices?
        if (indices == null || indices.Length != indexCount)
        {
            indices = new int[indexCount];

            for (var y = 0; y < sideE; y++)
            {
                for (var x = 0; x < sideE; x++)
                {
                    var index  = (x + y * sideE) * 6;
                    var vertex = x + y * sideP;

                    indices[index + 0] = vertex;
                    indices[index + 1] = vertex + 1;
                    indices[index + 2] = vertex + sideP;
                    indices[index + 3] = vertex + sideP + 1;
                    indices[index + 4] = vertex + sideP;
                    indices[index + 5] = vertex + 1;
                }
            }

            mesh.Clear();
        }

        mesh.vertices  = positions;
        mesh.triangles = indices;

        meshCollider.sharedMesh = mesh;

        meshCollider.transform.SetParent(terrain.transform, false);
    }