Particle Morphing System

With this tool you can make a city with custom roads and buinding.

Code

– Blueprint Scripts

Pure CSS3 Tabs
  • BP_GameMode

  • BP_Morph

  • BP_Morph_Control

  • Ground Generator

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class GroundGenerator : MonoBehaviour
    {
        public float radius = 1f;
        public Vector2 regionSize = Vector2.one;
        public int rejectionSamples = 30;
    
        public List<Vector2> points;
    
        public float gizmosCircleRadius = 1;
        //public GameObject spawnIt;
        public GameObject plane;
        public GameObject parent;
        [SerializeField]int counter ;
        [SerializeField] private int pointsCounter = 0;
    
        public int[] percentage = new[] {40, 30, 20, 19};
        public GameObject[] trees;
        
        public int randomNumber;
    
        public List<Vector3> randomLocations;
        public List<int> randomIndex;
        public int numberOfItems = 5;
        public GameObject[] itemTrees;
    
        public int numberOfItemsCounter;
        public GameObject monster;
    
        public Transform player;
        public Transform collected;
        public Transform playerPlaneOrigin;
        public Transform playerPlaneSpawn;
        public Transform spawnPlayer;
        public Transform spawnCollected;
    
        private Vector2 sampleRangeTree;
        private Vector3 sampleRangePlane;
        
        
    
        //public Matrix4x4 TreeToWorldSpace; // to calculate Surface rotation.
        private void OnValidate()
        {
            // randomIndex.Clear();
            // randomLocations.Clear();
            //
            // points = PoissonDiscSampling.GeneratePoint(radius, regionSize, rejectionSamples);
            // counter = points.Count;
            //
            // for (int i = 0; i < numberOfItems; i++)
            // {
            //     int randomNumber = UnityEngine.Random.Range(0, points.Count);
            //     randomIndex.Add(randomNumber);
            //     randomLocations.Add(points[randomNumber]);
            // }
            
            ////TreeToWorldSpace= Matrix4x4.TRS(plane.transform.position,Quaternion.identity, Vector3.one); // to calculate Surface rotation.
            
        }
        //
        //
        // public void OnDrawGizmos()
        // {
        //     if (points !=null)
        //     {
        //         foreach (var point in points)
        //         {
        //             Gizmos.DrawSphere(point,gizmosCircleRadius);
        //         }
        //     }
        // }
    
    
        private void Awake()
        {
            // randomIndex.Clear();
            // randomLocations.Clear();
            // sampleRangePlane = new Vector3(GameState.groundX, 1, GameState.groundY);
            // plane.transform.localScale = sampleRangePlane;
            // GameState.collectableItems = numberOfItems;
            // sampleRangeTree = new Vector2(GameState.treesX, GameState.treesY);
            // regionSize = sampleRangeTree;
            // radius = GameState.treesPurpotion;
            // points = PoissonDiscSampling.GeneratePoint(radius, regionSize, rejectionSamples);
            // counter = points.Count;
            // numberOfItemsCounter = numberOfItems;
            //
            // for (int i = 0; i < numberOfItems+1; i++)
            // {
            //     int randomNumber = UnityEngine.Random.Range(0, points.Count);
            //     if (isRandomNumberValid(randomNumber, randomIndex.ToArray()))
            //     {
            //         randomIndex.Add(randomNumber);
            //         randomLocations.Add(points[randomNumber]);
            //     }
            //     else
            //     {
            //         --i;
            //     }
            // }
            //
            // AdjustingPositions(player, collected,playerPlaneOrigin,playerPlaneSpawn,spawnPlayer,spawnCollected);
            
        }
    
        // Start is called before the first frame update
        void Start()
        {
            // setup
            randomIndex.Clear();
            randomLocations.Clear();
            sampleRangePlane = new Vector3(GameState.groundX, 1, GameState.groundY);
            plane.transform.localScale = sampleRangePlane;
            numberOfItems = GameState.collectableItems;
            sampleRangeTree = new Vector2(GameState.treesX, GameState.treesY);
            regionSize = sampleRangeTree;
            radius = GameState.treesPurpotion;
            points = PoissonDiscSampling.GeneratePoint(radius, regionSize, rejectionSamples);
            counter = points.Count;
            
            numberOfItemsCounter = numberOfItems;
    
            for (int i = 0; i < numberOfItems+1; i++)
            {
                int randomNumber = UnityEngine.Random.Range(0, points.Count);
                if (isRandomNumberValid(randomNumber, randomIndex.ToArray()))
                {
                    randomIndex.Add(randomNumber);
                    randomLocations.Add(points[randomNumber]);
                }
                else
                {
                    --i;
                }
            }
            
            AdjustingPositions(player, collected,playerPlaneOrigin,playerPlaneSpawn,spawnPlayer,spawnCollected);
            
            // start to generate
           
            // points = PoissonDiscSampling.GeneratePoint(radius, regionSize, rejectionSamples);
            // counter = points.Count;
            
            while (pointsCounter<counter)
            {
    
                if (isLocationForTrees(pointsCounter,randomIndex.ToArray(),itemTrees.Length))
                {
                    GameObject randomObjectSpawn = randomTrees(percentage, trees, out randomNumber);
                
                    GameObject mySpawn = Instantiate(randomObjectSpawn,parent.transform.TransformPoint
                        (new Vector3(points[pointsCounter].x,0,points[pointsCounter].y)), Quaternion.identity);
                    mySpawn.transform.parent = parent.transform;
                    
                    // to calculate Surface rotation.
                    // GameObject mySpawn = Instantiate(randomObjectSpawn,TreeToWorldSpace.MultiplyPoint3x4
                    //     (new Vector3(points[pointsCounter].x,0,points[pointsCounter].y)), Quaternion.identity);
                    
                    //mySpawn.transform.parent = parent.transform;
                    //TreeToWorldSpace.MultiplyPoint3x4(mySpawn.transform.position);
    
                }
                else
                {
                    if (numberOfItemsCounter>0)
                    {
                        int random = UnityEngine.Random.Range(0, itemTrees.Length);
                    
                        //GameObject randomObjectSpawn = randomTrees(percentage, trees, out randomNumber);
                
                        GameObject mySpawn = Instantiate(itemTrees[random],parent.transform.TransformPoint
                            (new Vector3(points[pointsCounter].x,0,points[pointsCounter].y)), Quaternion.identity);
                        mySpawn.transform.parent = parent.transform;
    
                        numberOfItemsCounter--;
                    }
                    else
                    {
                        GameObject mySpawn = Instantiate(monster,parent.transform.TransformPoint
                            (new Vector3(points[pointsCounter].x,monster.transform.position.y,points[pointsCounter].y)), Quaternion.identity);
                        mySpawn.transform.parent = parent.transform;
    
    
                        //  GameObject mySpawn = Instantiate(monster,TreeToWorldSpace.MultiplyPoint3x4
                        //      (new Vector3(points[pointsCounter].x,0,points[pointsCounter].y)), Quaternion.identity);
                        //
                        // mySpawn.transform.parent = parent.transform;
                        // TreeToWorldSpace.MultiplyPoint3x4(mySpawn.transform.position);
                    }
                    
                    
                }
                
                
                // parent.transform.TransformPoint is changing the points position to parent space from world space.
                //mySpawn.transform.localPosition = new Vector3(points[pointsCounter].x,0,points[pointsCounter].y); //"try to do smth like parent.transform.TransformPoint "
                
                pointsCounter++;
            }
        }
    
        // Update is called once per frame
        void Update()
        {
            
    
            // while (pointsCounter<counter)
            // {
            //
            //     if (isLocationForTrees(pointsCounter,randomIndex.ToArray(),itemTrees.Length))
            //     {
            //         GameObject randomObjectSpawn = randomTrees(percentage, trees, out randomNumber);
            //     
            //         GameObject mySpawn = Instantiate(randomObjectSpawn,parent.transform.TransformPoint
            //             (new Vector3(points[pointsCounter].x,0,points[pointsCounter].y)), Quaternion.identity);
            //         mySpawn.transform.parent = parent.transform;
            //         
            //         // to calculate Surface rotation.
            //         // GameObject mySpawn = Instantiate(randomObjectSpawn,TreeToWorldSpace.MultiplyPoint3x4
            //         //     (new Vector3(points[pointsCounter].x,0,points[pointsCounter].y)), Quaternion.identity);
            //         
            //         //mySpawn.transform.parent = parent.transform;
            //         //TreeToWorldSpace.MultiplyPoint3x4(mySpawn.transform.position);
            //
            //     }
            //     else
            //     {
            //         int random = UnityEngine.Random.Range(0, itemTrees.Length);
            //         
            //         //GameObject randomObjectSpawn = randomTrees(percentage, trees, out randomNumber);
            //     
            //         GameObject mySpawn = Instantiate(itemTrees[random],parent.transform.TransformPoint
            //             (new Vector3(points[pointsCounter].x,0,points[pointsCounter].y)), Quaternion.identity);
            //         mySpawn.transform.parent = parent.transform;
            //         
            //     }
            //     
            //     
            //     // parent.transform.TransformPoint is changing the points position to parent space from world space.
            //     //mySpawn.transform.localPosition = new Vector3(points[pointsCounter].x,0,points[pointsCounter].y); //"try to do smth like parent.transform.TransformPoint "
            //     
            //     pointsCounter++;
            // }
            
            ////counter--;
        }
    
    
        GameObject randomTrees( int[]percentage ,GameObject[] trees,out int randomNum)
        {
            
            int totalPercentage = 0 ;
            for (int i = 0; i < percentage.Length; i++)
            {
                totalPercentage += percentage[i];
            }
    
            int randomNumber = UnityEngine.Random.Range(0, totalPercentage);
            randomNum = randomNumber;
            
            for (int i = 0; i < trees.Length; i++)
            {
                if (randomNumber<=percentage[i])
                {
                    return trees[i];
                }
                else
                {
                    randomNumber -= percentage[i];
                }
            }
    
            return trees[0];
        }
    
    
        bool isLocationForTrees(int pointCounter ,int[] randomIndex,int itemsCount) // check the location for just trees not points
        {
            for (int i = 0; i < randomIndex.Length; i++)
            {
                if (pointCounter==randomIndex[i])
                {
                    return false;
                }
            }
            return true;
        }
    
        bool isRandomNumberValid(int randomNumber, int[] randomIndex) // same random number is not Valid
        {
            if (randomIndex.Length <1)
            {
                return true;
            }
    
            for (int i = 0; i < randomIndex.Length; i++)
            {
                if (randomNumber == randomIndex[i])
                {
                    return false;
                }
            }
    
            return true;
        }
    
        void AdjustingPositions(Transform player, Transform collected, Transform playerPlane,Transform positionSpawn,Transform spawnPlayer,Transform spawnCollected)
        {
    
            playerPlane.position = positionSpawn.position;
            playerPlane.rotation= positionSpawn.rotation;
    
            player.position = new Vector3(spawnPlayer.position.x,player.position.y,spawnPlayer.position.z);
            player.rotation = positionSpawn.rotation;
            collected.position = new Vector3(spawnCollected.position.x,collected.position.y,spawnCollected.position.z);
    
        }
    }
    
Made with Unreal Engine V5.2