Week 8 Part 1 - Some Updated on Image Processing in Unreal

Image Processing in Unreal

In the game, our maze needs to observe the changes made by the player (engineer) to the maze in real-time through a camera, so as to prevent aliens from passing through the maze.

Earlier in this class, I wrote a script similar to this function in Unity. However, I ran into a problem this week when I was trying to convert something like this into Unreal. I found that this image processing method cannot be carried out in the Blueprint method, but can only start using C++  to implement.

I spent a lot of time this week trying to convert my C# script to a C++ script, but I found that in the process, I didn't know enough about how C++ is used in Unreal and which functions or methods can help me achieve similar functionality.

I reach a problem with this part. However, our goal for this week is not to achieve this purpose, so I plan to continue to spend next week on this part.

C# Script I currently have

public class Script : MonoBehaviour
{
    [SerializeField]
    private Texture2D[] images;
    private Texture2D image;
   
    [SerializeField]
    public GameObject Ground;
    public GameObject Wall;

    private List<GameObject> spawnedObjects = new List<GameObject>();
    private int spawnCount = 0;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            image = images[Random.Range(0, images.Length)];
            Generate(image);
        }

        if (Input.GetKeyDown(KeyCode.G))
        {
            DestroyBlackObjects();
        }

        if (Input.GetKeyDown(KeyCode.W))
        {
            DestroyWhiteObjects();
        }
    }

    private void Generate(Texture2D image)
    {
        //Get the pixel colors from the image
        Color[] pix = image.GetPixels();

        //Dimension of the image
        int worldX = image.width;
        int worldZ = image.height;

       
        //Calculate the starting point
        Vector3 startingSpawnPosition;
        if (spawnedObjects.Count > 0)
        {
            startingSpawnPosition = new Vector3(-Mathf.Round(worldX / 2),
            3 * spawnCount, -Mathf.Round(worldZ / 2));
            spawnCount += 1;
        }
        else
        {
            startingSpawnPosition = new Vector3(-Mathf.Round(worldX / 2),
            0, -Mathf.Round(worldZ / 2));
            spawnCount = 1;
        }
       
        //Array to store the spawn positions for the objects
        Vector3[] spawnPositions = new Vector3[pix.Length];

        //Calculate the current starting point
        Vector3 currentSpawnPos = startingSpawnPosition;

        int counter = 0;

        //Loop through the pixel to generate the spawn position
        for (int z = 0; z < worldZ; z++)
        {
            for (int x = 0; x < worldX; x++)
            {
                spawnPositions[counter] = currentSpawnPos;
                counter++;
                currentSpawnPos.x++;
            }

            currentSpawnPos.x = startingSpawnPosition.x;
            currentSpawnPos.z++;
        }

        counter = 0;
       
        float threshold = 0.5f;

        //Loop through the spawn posistion to instantiate objects
        foreach (Vector3 pos in spawnPositions)
        {
            Color c = pix[counter];

            if (c.grayscale > threshold)
            {
                GameObject wall = Instantiate(Wall, pos,
                 Quaternion.identity);
                spawnedObjects.Add(wall);
            }
            else
            {
                GameObject ground = Instantiate(Ground, pos,
                Quaternion.identity);
                spawnedObjects.Add(ground);
            }

            counter++;
        }
    }

    private void DestroyBlackObjects()
    {
        for (int i = spawnedObjects.Count - 1; i >= 0; i--)
        {
            GameObject obj = spawnedObjects[i];
            if (obj.CompareTag("Ground"))
            {
                spawnedObjects.RemoveAt(i);
                Destroy(obj);
            }
        }
    }

    private void DestroyWhiteObjects()
    {
        for (int i = spawnedObjects.Count - 1; i >= 0; i--)
        {
            GameObject obj = spawnedObjects[i];
            if (obj.CompareTag("Wall"))
            {
                spawnedObjects.RemoveAt(i);
                Destroy(obj);
            }
        }
    }

}

Comments