Esempio n. 1
0
    // Gets the normal under the input point in local space based on 2 samples, and assumes localPoint is already on the surface
    public Vector3 GetLocalNormalFast2(SgtVector3D localPoint, SgtVector3D right, SgtVector3D forward)
    {
        var b = GetLocalPoint(localPoint + right);
        var c = GetLocalPoint(localPoint + forward);

        return((Vector3)SgtVector3D.Cross(localPoint - b, c - localPoint).normalized);
    }
    private void CalculateHeight(SgtVector3D localPosition, ref float height)
    {
        localPosition /= localPosition.magnitude;
        localPosition *= Density;

        var weight = 1.0f;
        var total  = 0.0f;

        for (var i = 0; i < Octaves; i++)
        {
            total         += generators[i].Generate((float)localPosition.x, (float)localPosition.y, (float)localPosition.z) * weight;
            weight        *= 0.5f;
            localPosition *= 2.0;
        }

        // Scale to -2 .. 2
        total *= scale * 2.0f;

        // Abs to 0 .. 2
        total = System.Math.Abs(total);

        if (Invert == true)
        {
            total = 2.0f - total;
        }

        // Shift to -1 .. 1
        total -= 1.0f;

        // Scale to strength and add
        height += total * Strength;
    }
Esempio n. 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);
        }
    }
Esempio n. 4
0
    // Gets the normal under the input point in local space based on 4 samples
    public Vector3 GetLocalNormal2(SgtVector3D localPoint, SgtVector3D right, SgtVector3D forward)
    {
        var a = GetLocalPoint(localPoint - right);
        var b = GetLocalPoint(localPoint + right);
        var c = GetLocalPoint(localPoint - forward);
        var d = GetLocalPoint(localPoint + forward);

        return((Vector3)SgtVector3D.Cross(a - b, d - c).normalized);
    }
Esempio n. 5
0
    // Gets the surface height under the input point in local space
    public float GetLocalHeight(SgtVector3D localPoint)
    {
        var height = Radius;

        if (OnCalculateHeight != null)
        {
            OnCalculateHeight(localPoint, ref height);
        }

        return(height);
    }
Esempio n. 6
0
    private int CalculateAxis(SgtVector3D vector)
    {
        vector.x = System.Math.Abs(vector.x);
        vector.y = System.Math.Abs(vector.y);
        vector.z = System.Math.Abs(vector.z);

        if (vector.y > vector.x && vector.y > vector.z)
        {
            return(1);
        }
        if (vector.z > vector.x && vector.z > vector.y)
        {
            return(2);
        }

        return(0);
    }
Esempio n. 7
0
    public static SgtVector3D InvCube(SgtVector3D v)
    {
        var a = new SgtVector3D(System.Math.Abs(v.x), System.Math.Abs(v.y), System.Math.Abs(v.z));

        if (a.x > a.y && a.x > a.z)
        {
            return(v / a.x);
        }
        else if (a.y > a.x && a.y > a.z)
        {
            return(v / a.y);
        }
        else
        {
            return(v / a.z);
        }
    }
Esempio n. 8
0
    private void CalculateHeight(SgtVector3D localPosition, ref float height)
    {
        localPosition /= localPosition.magnitude;
        localPosition *= Density;

        var weight = 1.0f;
        var total  = 0.0f;

        for (var i = 0; i < Octaves; i++)
        {
            total         += generators[i].Generate((float)localPosition.x, (float)localPosition.y, (float)localPosition.z) * weight;
            weight        *= 0.5f;
            localPosition *= 2.0;
        }

        // Scale to -1 .. 1
        total *= scale;

        // Scale to strength and add
        height += total * Strength;
    }
Esempio n. 9
0
    private SgtVector3D CalculateTarget()
    {
        var point = new SgtVector3D(transform.InverseTransformPoint(Target.position));

        // Deform by cube shape
        if (point.sqrMagnitude > 0.0)
        {
            var cube = InvCube(point.normalized);

            point *= cube.magnitude;
        }

        // Deform by terrain displacement
        var height = GetLocalHeight(point);

        if (height > 0.0f)
        {
            point /= height;
        }

        return(point);
    }
Esempio n. 10
0
    private void CalculateHeight(SgtVector3D localPosition, ref float height)
    {
        if (Heightmap != null)
        {
            var uv    = SgtHelper.CartesianToPolarUV((Vector3)localPosition);
            var color = SampleBilinear(uv);

            switch (Encoding)
            {
            case EncodingType.Alpha:
            {
                height += Mathf.Lerp(DisplacementMin, DisplacementMax, color.a);
            }
            break;

            case EncodingType.RedGreen:
            {
                height += Mathf.Lerp(DisplacementMin, DisplacementMax, (color.r * 255.0f + color.g) / 256.0f);
            }
            break;
            }
        }
    }
Esempio n. 11
0
 public static SgtVector3D Cube(SgtVector3D v)
 {
     return(v.normalized);
 }
Esempio n. 12
0
 // Gets the surface point under the input point in local space
 public SgtVector3D GetLocalPoint(SgtVector3D localPoint)
 {
     return(localPoint.normalized * GetLocalHeight(localPoint));
 }
Esempio n. 13
0
 public static SgtVector3D Cross(SgtVector3D a, SgtVector3D b)
 {
     return(new SgtVector3D(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x));
 }