Example #1
0
        private void CalculateHeight(SgtVector3D localPosition, ref double height)
        {
            if (Heightmap != null)
            {
                var uv       = SgtHelper.CartesianToPolarUV((Vector3)localPosition);
                var color    = SampleBilinear(uv);
                var height01 = default(double);

                switch (Encoding)
                {
                case EncodingType.Alpha:
                {
                    height01 = color.a;
                }
                break;

                case EncodingType.RedGreen:
                {
                    height01 = (color.r * 255.0 + color.g) / 256.0;
                }
                break;
                }

                height += DisplacementMin + (DisplacementMax - DisplacementMin) * height01;
            }
        }
Example #2
0
        private void CalculateHeight(SgtVector3D localPosition, ref double height)
        {
            localPosition /= localPosition.magnitude;
            localPosition *= Frequency;

            var contribution = 0.0;
            var weight       = 1.0;

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

            // Scale maximum constribution to -2..2
            contribution *= scale * 2.0;

            // Abs to 0 .. 2
            if (contribution < 0.0)
            {
                contribution = -contribution;
            }

            if (Invert == true)
            {
                contribution = 2.0 - contribution;
            }

            // Scale and add
            height += contribution * Amplitude;
        }
Example #3
0
        public void Spawn(SgtTerrain terrain, SgtTerrainFace face, SgtVector3D localPoint)
        {
            if (OnSpawn != null)
            {
                OnSpawn();
            }

            transform.SetParent(face.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
            var twist = Quaternion.Euler(0.0f, Random.Range(0.0f, 360.0f), 0.0f);

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

            if (AlignToNormal != 0.0f)
            {
                var localRight   = transform.right * AlignToNormal;
                var localForward = transform.forward * AlignToNormal;
                var localNormal  = terrain.GetLocalNormal(localPoint, new SgtVector3D(localRight), new SgtVector3D(localForward));

                transform.rotation = Quaternion.FromToRotation(up, (Vector3)localNormal) * twist;
            }
        }
Example #4
0
        private void CalculateLocalValues()
        {
            LocalPoints.Clear();

            if (Targets != null)
            {
                for (var i = Targets.Count - 1; i >= 0; i--)
                {
                    var target = Targets[i];

                    if (target != null)
                    {
                        var point = new SgtVector3D(transform.InverseTransformPoint(target.position));

                        LocalPoints.Add(point);
                    }
                }
            }

            LocalDistances.Clear();

            for (var i = 0; i < 32; i++)
            {
                if (Distances != null && i < Distances.Count)
                {
                    var distance = Distances[i];

                    LocalDistances.Add(distance * distance);
                }
                else
                {
                    LocalDistances.Add(double.NegativeInfinity);
                }
            }
        }
        public SgtTerrainCompute.Output SampleLocalOutput(SgtVector3D localPoint)
        {
            SgtTerrainCompute.BeginPoint(ForceCPU);
            SgtTerrainCompute.SetMain(Radius, NormalStep, NormalStrength, HeightMap, HeightScale);
            SgtTerrainCompute.SetDetail(MaskMap, DetailTiling, DetailMapA, DetailScaleA, DetailMapA, DetailScaleB);

            return(SgtTerrainCompute.DispatchPoint((Vector3)localPoint));
        }
Example #6
0
        /*
         * public static bool Overlap(SgtVector3D a, SgtVector3D b, SgtVector3D p, double eps = 0.00001)
         * {
         *      var ba   = b - a;
         *      var baba = Dot(ba, ba);
         *
         *      if (baba != 0.0f)
         *      {
         *              var d = Dot(p - a, ba) / baba;
         *
         *              if (d >= -eps && d <= 1.0f + eps)
         *              {
         *                      var z = (a + ba * d) - p;
         *
         *                      return z.sqrMagnitude <= eps * eps;
         *              }
         *      }
         *
         *      return false;
         * }
         */

        public static double SquareDistance(SgtVector3D a, SgtVector3D b)
        {
            a.x -= b.x;
            a.y -= b.y;
            a.z -= b.z;

            return(a.x * a.x + a.y * a.y + a.z * a.z);
        }
        public Vector3 GetWorldPoint(Vector3 position)
        {
            var localPosition = transform.InverseTransformPoint(position);
            var localPoint    = new SgtVector3D(localPosition);

            localPoint = GetLocalPoint(localPoint);

            return(transform.TransformPoint((Vector3)localPoint));
        }
        public SgtVector3D GetLocalNormal(SgtVector3D point, SgtVector3D right, SgtVector3D up)
        {
            var localPoint  = GetLocalPoint(point);
            var localPointR = GetLocalPoint(localPoint + right);
            var localPointU = GetLocalPoint(localPoint - up);
            var vectorR     = localPointR - localPoint;
            var vectorU     = localPointU - localPoint;

            return(SgtVector3D.Cross(vectorR, vectorU).normalized);
        }
Example #9
0
        // Gets the surface height under the input point in local space
        public double GetLocalHeight(SgtVector3D localPoint)
        {
            var height = Radius;

            if (OnCalculateHeight != null)
            {
                OnCalculateHeight(localPoint.normalized * Radius, ref height);
            }

            return(height);
        }
Example #10
0
        public static bool Overlap(SgtVector3D a, SgtVector3D b, SgtVector3D p, float eps = 0.001f)
        {
            var ba = b - a;
            var d  = Dot(p - a, ba) / Dot(ba, ba);

            if (d >= -eps && d <= 1.0f + eps)
            {
                var z = (a + ba * d) - p;

                return(z.sqrMagnitude <= eps * eps);
            }

            return(false);
        }
        private SgtTerrainFace Create(string childName, SgtVector3D cornerBL, SgtVector3D cornerBR, SgtVector3D cornerTL, SgtVector3D cornerTR)
        {
            var face = Create(childName, gameObject.layer, transform);

            face.Terrain  = Terrain;
            face.Side     = Side;
            face.Depth    = Depth + 1;
            face.CornerBL = cornerBL;
            face.CornerBR = cornerBR;
            face.CornerTL = cornerTL;
            face.CornerTR = cornerTR;

            return(face);
        }
        private SgtVector3D GetPosition(SgtVector3D origin, SgtVector3D sideDelta, SgtVector3D edgeDelta)
        {
            var eps      = 0.00000001;
            var position = origin + sideDelta;

            // Wrap position around cube if it goes off the edge
            if (position.x < -1.0 - eps || position.x > 1.0 + eps || position.y < -1.0 - eps || position.y > 1.0 + eps || position.z < -1.0 - eps || position.z > 1.0 + eps)
            {
                position = origin + edgeDelta;
            }

            position = position.normalized * Terrain.GetLocalHeight(position);

            return(position);
        }
 public static void SetFaceCorners(CubemapFace side, SgtVector3D cornerBL, SgtVector3D cornerBR, SgtVector3D cornerTL)
 {
     if (computeSupported == true && computeDisable == false)
     {
         computeShader.SetInt(SgtShader._Side, (int)side);
         computeShader.SetVector(SgtShader._CornerBL, (Vector3)cornerBL);
         computeShader.SetVector(SgtShader._CornerBR, (Vector3)cornerBR);
         computeShader.SetVector(SgtShader._CornerTL, (Vector3)cornerTL);
     }
     else
     {
         _Side     = (int)side;
         _CornerBL = (Vector3)cornerBL;
         _CornerBR = (Vector3)cornerBR;
         _CornerTL = (Vector3)cornerTL;
     }
 }
Example #14
0
        private SgtTerrainFace CreateFace(CubemapFace side, SgtVector3D cornerBL, SgtVector3D cornerBR, SgtVector3D cornerTL, SgtVector3D cornerTR)
        {
            var face = SgtTerrainFace.Create(side.ToString(), gameObject.layer, transform);

            face.Terrain  = this;
            face.Side     = side;
            face.Depth    = 0;
            face.CornerBL = cornerBL;
            face.CornerBR = cornerBR;
            face.CornerTL = cornerTL;
            face.CornerTR = cornerTR;
            face.CoordBL  = new SgtVector2D(0.0, 0.0);
            face.CoordBR  = new SgtVector2D(1.0, 0.0);
            face.CoordTL  = new SgtVector2D(0.0, 1.0);
            face.CoordTR  = new SgtVector2D(1.0, 1.0);

            return(face);
        }
Example #15
0
        private void CalculateHeight(SgtVector3D localPosition, ref double height)
        {
            localPosition /= localPosition.magnitude;
            localPosition *= Frequency;

            var contribution = 0.0;
            var weight       = 1.0;

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

            // Scale maximum constribution to -1..1
            contribution *= scale;

            // Scale and add
            height += contribution * Amplitude;
        }
Example #16
0
        public static bool Overlap(SgtVector3D a, SgtVector3D b, SgtVector3D c, SgtVector3D d, double eps = 0.001)
        {
            var total = 0;

            if (Overlap(a, b, c) == true)
            {
                total += 1;
            }
            if (Overlap(a, b, d) == true)
            {
                total += 1;
            }
            if (Overlap(c, d, a) == true)
            {
                total += 1;
            }
            if (Overlap(c, d, b) == true)
            {
                total += 1;
            }

            return(total >= 2);
        }
Example #17
0
 public void Add(SgtVector3D xyz)
 {
     if (set == false)
     {
         set  = true;
         minX = maxX = xyz.x;
         minY = maxY = xyz.y;
         minZ = maxZ = xyz.z;
     }
     else
     {
         if (xyz.x < minX)
         {
             minX = xyz.x;
         }
         else if (xyz.x > maxX)
         {
             maxX = xyz.x;
         }
         if (xyz.y < minY)
         {
             minY = xyz.y;
         }
         else if (xyz.y > maxY)
         {
             maxY = xyz.y;
         }
         if (xyz.z < minZ)
         {
             minZ = xyz.z;
         }
         else if (xyz.z > maxZ)
         {
             maxZ = xyz.z;
         }
     }
 }
 public SgtVector3D GetLocalPoint(SgtVector3D localCube)
 {
     return(localCube.normalized * GetLocalHeight(localCube));
 }
Example #19
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));
 }
Example #20
0
 public static double Dot(SgtVector3D a, SgtVector3D b)
 {
     return(a.x * b.x + a.y * b.y + a.z * b.z);
 }
 public Vector3 GetLocalNormal(SgtVector3D point)
 {
     return(SampleLocalOutput(point).Normal.normalized);
 }
Example #22
0
 public bool Contains(SgtVector3D xyz)
 {
     return(xyz.x >= minX && xyz.x < maxX && xyz.y >= minY && xyz.y < maxY && xyz.z >= minZ && xyz.z < maxZ);
 }
 // Gets the surface height under the input point in local space
 public double GetLocalHeight(SgtVector3D localPoint)
 {
     return(SampleLocalOutput(localPoint).Vertex.magnitude);
 }
 public Vector3 GetWorldNormal(SgtVector3D point)
 {
     return(transform.TransformDirection(GetLocalNormal(point)));
 }
Example #25
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();
            }
        }