Esempio n. 1
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;
            }
        }
Esempio n. 2
0
        public void Spawn(SgtTerrain terrain, SgtTerrainFace face, SgtTerrainCompute.Output output)
        {
            if (OnSpawn != null)
            {
                OnSpawn();
            }

            transform.localScale = Prefab.transform.localScale * Random.Range(ScaleMin, ScaleMax);
            transform.SetParent(face.transform, true);

            var normal = Vector3.Lerp(output.Vertex, output.Normal, AlignToNormal);

            // Spawn on surface
            var twist = Quaternion.Euler(0.0f, Random.Range(0.0f, 360.0f), 0.0f);

            transform.localPosition = output.Vertex;
            transform.localRotation = Quaternion.FromToRotation(Vector3.up, normal) * twist;

            //transform.rotation   = Quaternion.FromToRotation(up, terrain.transform.TransformDirection(localPosition));

            sharedMaterial = terrain.SharedMaterial;

            if (Renderers != null && sharedMaterial != null)
            {
                for (var i = Renderers.Count - 1; i >= 0; i--)
                {
                    var renderer = Renderers[i];

                    if (renderer != null)
                    {
                        sharedMaterial.AddRenderer(renderer);
                    }
                }
            }
        }
        protected virtual void OnEnable()
        {
            cachedTerrain = GetComponent <SgtTerrain>();

            cachedTerrain.OnDrawQuad += HandleDrawQuad;

            cachedTerrain.OnScheduleHeights         += HandleScheduleHeights;
            cachedTerrain.OnScheduleCombinedHeights += HandleScheduleHeights;
        }
        protected virtual void OnEnable()
        {
            cachedTerrain = GetComponent <SgtTerrain>();

            cachedTerrain.OnScheduleHeights         += HandleScheduleHeights;
            cachedTerrain.OnScheduleCombinedHeights += HandleScheduleHeights;

            PrepareTexture();
        }
Esempio n. 5
0
        private SgtTerrain GetTerrain()
        {
            if (terrain == null)
            {
                return(SgtTerrain.FindNearest(transform.position));
            }

            return(terrain);
        }
Esempio n. 6
0
        public void UpdateColliders()
        {
            if (terrainSet == false)
            {
                terrain    = GetComponent <SgtTerrain>();
                terrainSet = true;
            }

            terrain.DelayUpdateColliders = true;
        }
Esempio n. 7
0
        public void Rebuild()
        {
            if (terrainSet == false)
            {
                terrain    = GetComponent <SgtTerrain>();
                terrainSet = true;
            }

            terrain.DelayRebuild = true;
        }
Esempio n. 8
0
        public SgtTerrainCube(SgtTerrain newTerrain)
        {
            terrain = newTerrain;

            quads[0] = new SgtTerrainQuad(this, 0, 0.5, SgtTerrainTopology.CubeC[0], SgtTerrainTopology.CubeH[0], SgtTerrainTopology.CubeV[0]);
            quads[1] = new SgtTerrainQuad(this, 1, 0.0, SgtTerrainTopology.CubeC[1], SgtTerrainTopology.CubeH[1], SgtTerrainTopology.CubeV[1]);
            quads[2] = new SgtTerrainQuad(this, 2, 0.0, SgtTerrainTopology.CubeC[2], SgtTerrainTopology.CubeH[2], SgtTerrainTopology.CubeV[2]);
            quads[3] = new SgtTerrainQuad(this, 3, 0.0, SgtTerrainTopology.CubeC[3], SgtTerrainTopology.CubeH[3], SgtTerrainTopology.CubeV[3]);
            quads[4] = new SgtTerrainQuad(this, 4, 0.5, SgtTerrainTopology.CubeC[4], SgtTerrainTopology.CubeH[4], SgtTerrainTopology.CubeV[4]);
            quads[5] = new SgtTerrainQuad(this, 5, 0.0, SgtTerrainTopology.CubeC[5], SgtTerrainTopology.CubeH[5], SgtTerrainTopology.CubeV[5]);
        }
        protected virtual void OnEnable()
        {
            cachedTerrain = GetComponent <SgtTerrain>();

            cachedTerrain.OnScheduleHeights         += HandleScheduleHeights;
            cachedTerrain.OnScheduleCombinedHeights += HandleScheduleHeights;

            cachedTerrain.MarkAsDirty();

            tempWeights = new NativeArray <float>(0, Allocator.Persistent);
        }
Esempio n. 10
0
        private static string[] GetAreaNames(SgtTerrain terrain)
        {
            var names = new List <string>();

            names.Add("Everywhere");

            if (terrain.Areas != null)
            {
                foreach (var splat in terrain.Areas.Splats)
                {
                    names.Add(splat.Name);
                }
            }

            return(names.ToArray());
        }
Esempio n. 11
0
        public static bool DrawArea(SerializedProperty property, SgtTerrain terrain)
        {
            var names    = GetAreaNames(terrain);
            var modified = false;
            var content  = new GUIContent("Area", "This allows you to control where this biome appears based on the SgtTerrainTerra component's Biomes splatmap.");

            EditorGUI.BeginChangeCheck();

            var newValue = EditorGUILayout.Popup(content, property.intValue + 1, names) - 1;

            if (EditorGUI.EndChangeCheck() == true)
            {
                property.intValue = newValue;

                modified = true;
            }

            return(modified);
        }
Esempio n. 12
0
        protected virtual void OnEnable()
        {
            cachedTerrain = GetComponent <SgtTerrain>();

            cachedTerrain.OnDrawQuad += HandleDrawQuad;
        }
Esempio n. 13
0
        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] = (Vector3)terrain.GetLocalPoint(new SgtVector3D(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);
        }
Esempio n. 14
0
        protected virtual void OnEnable()
        {
            cachedTerrain = GetComponent <SgtTerrain>();

            tempWeights = new NativeArray <float>(0, Allocator.Persistent);
        }