/// <summary>
 /// Creates an ObjectPlacerPreview instance with the passed
 /// TerraSettings (for sampling different placement settings) and
 /// a Mesh to place the objects on.
 /// </summary>
 /// <param name="settings">TerraSettings</param>
 /// <param name="m">A preview mesh to draw on top of</param>
 public ObjectPlacerPreview(TerraSettings settings, Mesh m)
 {
     Settings    = settings;
     PreviewMesh = m;
     Placer      = new ObjectRenderer();
     CreateParentGO();
 }
        /// <summary>
        /// Static version of <see cref="CreateRawMesh(Vector2, Generator)"/>
        /// Creates a "raw" mesh from the passed generator and settings specified
        /// in <c>TerraSettings</c>. This method can be executed off the main thread.
        ///
        /// Because of Unity's incompatibility with multithreading, a <c>MeshData</c>
        /// struct is returned with contents of the generated Mesh, instead of using the
        /// <c>Mesh</c> class.
        /// </summary>
        /// <param name="position">Position in tile grid, used for polling generator</param>
        /// <param name="gen">Generator to apply</param>
        /// <returns>triangles, vertices, normals, and UVs of the generated mesh</returns>
        public static MeshData CreateRawMesh(TerraSettings settings, Vector2 position, Generator gen)
        {
            int   res    = settings.MeshResolution;
            float len    = settings.Length;
            float spread = 1f / (settings.Spread * settings.MeshResolution);

            Vector3[] vertices = new Vector3[res * res];
            for (int z = 0; z < res; z++)
            {
                float zPos = ((float)z / (res - 1) - .5f) * len;

                for (int x = 0; x < res; x++)
                {
                    float xPos = ((float)x / (res - 1) - .5f) * len;
                    float yPos = gen.GetValue(((position.x * len) + xPos) * spread,
                                              ((position.y * len) + zPos) * spread, 0f) * settings.Amplitude;

                    vertices[x + z * res] = new Vector3(xPos, yPos, zPos);
                }
            }

            Vector2[] uvs = new Vector2[vertices.Length];
            for (int v = 0; v < res; v++)
            {
                for (int u = 0; u < res; u++)
                {
                    uvs[u + v * res] = new Vector2((float)u / (res - 1), (float)v / (res - 1));
                }
            }

            int nbFaces = (res - 1) * (res - 1);

            int[] triangles = new int[nbFaces * 6];
            int   t         = 0;

            for (int face = 0; face < nbFaces; face++)
            {
                int i = face % (res - 1) + (face / (res - 1) * res);

                triangles[t++] = i + res;
                triangles[t++] = i + 1;
                triangles[t++] = i;

                triangles[t++] = i + res;
                triangles[t++] = i + res + 1;
                triangles[t++] = i + 1;
            }

            Vector3[] normals = new Vector3[vertices.Length];
            CalculateNormalsManaged(vertices, normals, triangles);

            MeshData mesh = new MeshData();

            mesh.triangles = triangles;
            mesh.vertices  = vertices;
            mesh.normals   = normals;
            mesh.uvs       = uvs;

            return(mesh);
        }
        void OnEnable()
        {
            Details = new DetailManager(this);

            Settings = TerraSettings.Instance;
            if (Settings == null)
            {
                Debug.LogError("Cannot find a TerraSettings object in the scene");
            }
        }
        /// <summary>
        /// Creates a preview mesh from the passed generator and terrain settings.
        /// </summary>
        /// <param name="settings">Settings to apply to the mesh</param>
        /// <param name="gen">Optional generator to pull values from</param>
        /// <returns>A preview mesh</returns>
        public static Mesh GetPreviewMesh(TerraSettings settings, Generator gen)
        {
            MeshData md = CreateRawMesh(settings, new Vector2(0, 0), gen);

            Mesh mesh = new Mesh();

            mesh.vertices  = md.vertices;
            mesh.normals   = md.normals;
            mesh.uv        = md.uvs;
            mesh.triangles = md.triangles;

            return(mesh);
        }
Exemple #5
0
        /// <summary>
        /// Static version of <see cref="GetPositionAt(int, int, int)"/>
        /// </summary>
        /// <returns></returns>
        public static Vector3 GetPositionAt(int xPos, int zPos, int resolution, TerraSettings settings, Generator gen, Vector2 position)
        {
            float amp    = settings.Amplitude;
            float spread = settings.Spread;
            float length = settings.Length;

            float worldX = ((float)xPos / (resolution - 1) - .5f) * length;
            float worldZ = ((float)zPos / (resolution - 1) - .5f) * length;
            float worldY = gen.GetValue(((position.x * length) + xPos) * spread,
                                        ((position.y * length) + zPos) * spread, 0f) * settings.Amplitude;

            return(new Vector3(worldX, worldY, worldZ));
        }
        /// <summary>
        /// Create a TerrainPaint object that paints the passed gameobject. For this to
        /// work, the passed gameobject must have the following components:
        /// MeshFilter, MeshRenderer, and TerrainTile
        /// </summary>
        /// <param name="gameobject">Gameobject to paint</param>
        public TerrainPaint(GameObject gameobject)
        {
            TerrainObject = gameobject;
            Settings      = TerraSettings.Instance;
            SplatSettings = Settings.SplatSettings;


            SetFirstPassShader();
            Mesh = TerrainObject.GetComponent <MeshFilter>().sharedMesh;

            int res = (int)Math.Sqrt(Mesh.vertexCount);

            Sampler = new MeshSampler(Mesh.normals, Mesh.vertices, res);
        }
        /// <summary>
        /// Polls the passed Generator for a value at the passed x / y position.
        /// Applies spread and amplitude to computation.
        /// </summary>
        /// <param name="xPos">X position to get value at</param>
        /// <param name="zPos">Z position to get value at</param>
        /// <param name="settings">Settings instance for amplitude and spread</param>
        /// <param name="gen">Generator to get value from</param>
        /// <returns></returns>
        public static float PollGenerator(float xPos, float zPos, TerraSettings settings, Generator gen)
        {
            float spread = 1f / (settings.Spread * settings.MeshResolution);

            return(gen.GetValue(xPos * spread, zPos * spread, 0f) * settings.Amplitude);
        }
Exemple #8
0
 /// <summary>
 /// Creates a new TerrainPreview instance
 /// </summary>
 /// <param name="settings">Active settings component</param>
 /// <param name="hideInInspector">Optionally hide created components in the inspector</param>
 public TerrainPreview()
 {
     this.Settings = TerraSettings.Instance;
 }
Exemple #9
0
 public TilePool()
 {
     Settings = TerraSettings.Instance;
 }