Unity2D Combine Sprites - c#

I would like to take a two-dimensional array of Sprites and turn it into one single sprite image at runtime. All sprites are square and exactly the same size, but the resulting image does not necessarily need to be square, as the width and height of the array can vary.
I have yet found this resource: Combine Array of Sprite objects into One Sprite - Unity
But I don't think it works for my purposes.

If you have those sprites in your project already you can simply edit their import settings to Advanced and check the Read/Write Enable toggle.
Then you should be able to read your sprites content and merge them like this:
public Sprite CombineSpriteArray(Sprite[][] spritesArray)
{
// Set those two or get them from one the the sprites you want to combine
int spritesWidth = (int)spritesArray[0][0].rect.width;
int spritesHeight = (int)spritesArray[0][0].rect.height;
Texture2D combinedTexture = new Texture2D(spritesWidth * spritesArray.Length, spritesHeight * spritesArray[0].Length);
for(int x = 0; x < spritesArray.Length; x++)
{
for(int y = 0; y < spritesArray[0].Length; y++)
{
combinedTexture.SetPixels(x * spritesArray.Length, y * spritesArray[0].Length, spritesWidth, spritesHeight, spritesArray[x][y].texture.GetPixels((int)spritesArray[x][y].textureRect.x, (int)spritesArray[x][y].textureRect.y, (int)spritesArray[x][y].textureRect.width, (int)spritesArray[x][y].textureRect.height));
// For a working script, use:
// combinedTexture.SetPixels32(x * spritesWidth, y * spritesHeight, spritesWidth, spritesHeight, spritesArray[x][y].texture.GetPixels32());
}
}
combinedTexture.Apply();
return Sprite.Create(combinedTexture, new Rect(0.0f, 0.0f, combinedTexture.width, combinedTexture.height), new Vector2(0.5f, 0.5f), 100.0f);
}
Warning: code untested
Be aware that such an operation is heavy and that doing it asynchronously in a coroutine may be a good idea to avoid a freeze.
EDIT:
Since you seem new to Stack Overflow, please keep in mind it's not a script providing service, people are here to help each others: this means code provided won't always be perfect but may simply guide you to the right path (this is also why I added the "Warning: code untested" after my code).
You claimed that the code was "completely broken" and "puts out errors all over the place". I wrote a small piece of script to test the script and the only error I got was that one (agreed it popped-up multiple times):
So after searching for it on Google (what you should have done by yourself), I noticed there were GetPixels32() / SetPixels32() methods that could also be used instead of GetPixels() / SetPixels() (here are the 3rd and 5th results that showed this methods). By simply changing this, the code now worked flawlessly.
Only problems I obtained was sprites were packed together at the bottom left of the texture: my bad on this I made a small mistake. Not hard to find where: just change
x * spritesArray.Length, y * spritesArray[0].Length, ...
to
x * spritesWidth, y * spritesHeight, ...
inside the SetPixels method.
So please find the whole test script I wrote and feel free to use it:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TestScript : MonoBehaviour
{
public Image m_DisplayImage;
public Sprite m_Sprite1, m_Sprite2;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(CombineSpritesCoroutine());
}
}
private IEnumerator CombineSpritesCoroutine()
{
Sprite[][] spritesToCombine = new Sprite[4][];
for (int i = 0; i < spritesToCombine.Length; i++)
{
spritesToCombine[i] = new Sprite[4];
}
for (int x = 0; x < spritesToCombine.Length; x++)
{
for (int y = 0; y < spritesToCombine[x].Length; y++)
{
spritesToCombine[x][y] = ((x + y) % 2 == 0 ? m_Sprite1 : m_Sprite2);
}
}
Sprite finalSprite = null;
yield return finalSprite = CombineSpriteArray(spritesToCombine);
m_DisplayImage.sprite = finalSprite;
}
public Sprite CombineSpriteArray(Sprite[][] spritesArray)
{
// Set those two or get them from one the the sprites you want to combine
int spritesWidth = (int)spritesArray[0][0].rect.width;
int spritesHeight = (int)spritesArray[0][0].rect.height;
Texture2D combinedTexture = new Texture2D(spritesWidth * spritesArray.Length, spritesHeight * spritesArray[0].Length);
for (int x = 0; x < spritesArray.Length; x++)
{
for (int y = 0; y < spritesArray[0].Length; y++)
{
combinedTexture.SetPixels32(x * spritesWidth, y * spritesHeight, spritesWidth, spritesHeight, spritesArray[x][y].texture.GetPixels32());
}
}
combinedTexture.Apply();
return Sprite.Create(combinedTexture, new Rect(0.0f, 0.0f, combinedTexture.width, combinedTexture.height), new Vector2(0.5f, 0.5f), 100.0f);
}
}

Related

Raycast not capturing all Vector coordinates

I have a gameobject that occupies the whole screen just for testing purposes. I'm drawing a line btw. What I'm trying to achieve is if the mouse position hits a gameobject it will store the vector2 coordinates in a list. But raycast is not storing all the coordinates. Below is my code
private void Update()
{
if (Input.GetMouseButton(0))
{
Vector2 mousePos = Input.mousePosition;
Vector2 Pos = _camera.ScreenToWorldPoint(mousePos);
if(!mousePositions.Contains(Pos))
mousePositions.Add(Pos);
if (Physics.Raycast(Camera.main.ScreenPointToRay(mousePos), out RaycastHit hit))
{
Vector2 textureCoord = hit.textureCoord;
int pixelX = (int)(textureCoord.x * _templateDirtMask.width);
int pixelY = (int)(textureCoord.y * _templateDirtMask.height);
Vector2Int paintPixelPosition = new Vector2Int(pixelX, pixelY);
if (!linePositions.Contains(paintPixelPosition))
linePositions.Add(paintPixelPosition);
foreach (Vector2Int pos in linePositions)
{
int pixelXOffset = pos.x - (_brush.width / 2);
int pixelYOffset = pos.y - (_brush.height / 2);
for (int x = 0; x < _brush.width; x++)
{
for (int y = 0; y < _brush.height; y++)
{
_templateDirtMask.SetPixel(
pixelXOffset + x,
pixelYOffset + y,
Color.black
);
}
}
}
_templateDirtMask.Apply();
}
}
}
Everytime I checked the element count mousePositions are always greater than linePositions. I don't know what's causing this
the element count mousePositions are always greater than linePosition
well it is quite simple: In
int pixelX = (int)(textureCoord.x * _templateDirtMask.width);
int pixelY = (int)(textureCoord.y * _templateDirtMask.height);
you are casting to int and cut off any decimals after the comma (basically like doing Mathf.FloorToInt).
So you can totally have multiple mouse positions which result in float pixel positions like e.g.
1.2, 1.2
1.4, 1.7
1.02, 1.93
...
all these will map to
Vector2Int paintPixelPosition = new Vector2Int(1, 1);
Besides, you might want to look at some better line drawing algorithms like e.g. this simple one
And then note that calling SetPixel repeatedly is quite expensive. You want to do a single SetPixels call like e.g.
var pixels = _templateDirtMask.GetPixels();
foreach (Vector2Int pos in linePositions)
{
int pixelXOffset = pos.x - (_brush.width / 2);
int pixelYOffset = pos.y - (_brush.height / 2);
for (int x = 0; x < _brush.width; x++)
{
for (int y = 0; y < _brush.height; y++)
{
pixels[(pixelXOffset + x) + (pixelYOffset + y) * _templateDirtMask.width] = Color.black;
}
}
}
_templateDirtMask.SetPixels(pixels);
_templateDirtMask.Apply();
It happens because there is really could be a case, when several elements from mousePositions are associated with one elment from linePositions.
Rough example: your texture resolution is only 1x1px. In this case you linePositons will contain only one element. And this element will be associated with all elements from mosePositions.
So, relation of the number of elements in these lists depends on relation of your texture and screen resolutions.

How to fix terrain Z axis going up on the end and on the X axis its going down?

so I'm rewriting my old code for a hydraulic erosion terrain generator, I knew about this problem on my old code but never asked why, but now I'm rewriting it and want to get everything working smoothly
this is the issue on my Z-axis at the edges the terrain has a step up and on the X-axis it goes down but it's hard to see in the photo plz tell me why
I'm using the built-in terrain generator FYI if you need more code just tell me
public void GenerateHeightMap () {
mapSizeWithBorder = mapSize + erosionBrushRadius * 2;
map = FindObjectOfType<HeightMapGenerator> ().GenerateHeightMap (mapSizeWithBorder);
terrain.terrainData = GenerateTerrain(terrain.terrainData);
}
TerrainData GenerateTerrain(TerrainData terrainData) {
Erode();
float[,] noiseMap = new float[mapSizeWithBorder,mapSizeWithBorder];
int x = 0;
int y = 0;
for (int i = 0; i < map.Length; ++i)
{
noiseMap[x, y] = map[i];
y++;
if (y == mapSizeWithBorder)
{
y = 0;
x++;
}
}
terrainData.size = new Vector3(mapSizeWithBorder * scale, elevationScale, mapSizeWithBorder * scale);
terrainData.SetHeights(0, 0, (noiseMap));
return terrainData;
}

How to smooth between multiple perlin noise chunks?

Smoothing Between Chunks
So I've been working on a game in unity and want to expand my world from a 150x150 map into a seemingly infinite procedural world. My plan is to use Perlin Noise as the base and use the different values from 0-1 to determine the terrain type. The issue I'm running into is when I draw out my chunks and offset accordingly my chunks do not line up correctly, which kind of break the illusion of an infinite world.
(seen here)
WorldChunk.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Unity.Mathematics;
[System.Serializable]
public class WorldChunk
{
public int2 Position;
public int[,] Data;
public float[,] Sample;
public WorldChunk(int chunkSize = 16){
Data = new int[chunkSize, chunkSize];
Sample = new float[chunkSize, chunkSize];
}
}
WorldGenerator.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Unity.Mathematics;
public class WorldGenerator : MonoBehaviour
{
// Base World Data
public int ChunkSize = 75;
public string Seed = "";
[Range(1f, 40f)]
public float PerlinScale = 10f;
// Pseudo Random Number Generator
private System.Random pseudoRandom;
// Chunk Data Split into Sections (Each Chunk having Coords (x, y))
public Dictionary<string, WorldChunk> chunks = new Dictionary<string, WorldChunk>();
//============================================================
// Set Warm-Up Data
//============================================================
private void Awake() {
// Get/Create Seed
if (Seed == ""){
Seed = GenerateRandomSeed();
}
// Get Random Number Generator
pseudoRandom = new System.Random(Seed.GetHashCode());
// Using to Clear while Making Test Adjustments
chunks.Clear();
// Generate Starting Chunk
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
// Draw Test Chunks
GenerateChunk(x, y);
}
}
}
//============================================================
// Generation Code
//============================================================
// ===
// Create New Chunks
// ===
public void GenerateChunk(int x, int y){
// Set Key to use
string key = $"{x},{y}";
// Check if key exists if not Generate New Chunk
if (!chunks.ContainsKey(key)){
// Add Chunk, Set Position in chunk grid (for calling and block data later), Then Generate data
chunks.Add(key, new WorldChunk(ChunkSize));
chunks[key].Position = new int2(x, y);
GenerateChunkData(chunks[key]);
}
}
// ===
// Fill Chunks with Perlin Data
// ===
private void GenerateChunkData(WorldChunk chunk){
// Set Offsets
float xOffset = (float)chunk.Position.x * ChunkSize;
float yOffset = (float)chunk.Position.y * ChunkSize;
// Set Data to Chunk
for (int x = 0; x < ChunkSize; x++)
{
for (int y = 0; y < ChunkSize; y++)
{
// Get Perlin Map
float px = (float)(x) / ChunkSize * PerlinScale + xOffset;
float py = (float)(y) / ChunkSize * PerlinScale + yOffset;
// Set Temp Sample For Testing (This will change for Map Data (Hills and Water) later)
chunk.Sample[x,y] = Mathf.PerlinNoise(px, py);
}
}
}
// ===
// Generate Random Seed of Length
// ===
private string GenerateRandomSeed(int maxCharAmount = 10, int minCharAmount = 10){
//Set Characters To Pick from
const string glyphs= "abcdefghijklmnopqrstuvwxyz0123456789";
//Set Length from min to max
int charAmount = UnityEngine.Random.Range(minCharAmount, maxCharAmount);
// Set output Variable
string output = "";
// Do Random Addition
for(int i=0; i<charAmount; i++)
{
output += glyphs[UnityEngine.Random.Range(0, glyphs.Length)];
}
// Output New Random String
return output;
}
//============================================================
// Draw Example
//============================================================
private void OnDrawGizmos() {
// Do this because I'm lazy and don't want to draw pixels to generated Sprites
Awake();
// For Each WorldChunk in the chunk Data
foreach (WorldChunk c in chunks.Values)
{
// Check if it exists (Foreach is stupid sometimes... When live editing)
if (c != null){
// Get World Positions for Chunk (Should probably Set to a Variable in the Chunk Data)
Vector3 ChunkPosition = new Vector3(c.Position.x * ChunkSize, c.Position.y * ChunkSize);
// For Each X & For Each Y in the chunk
for (int x = 0; x < ChunkSize; x++)
{
for (int y = 0; y < ChunkSize; y++)
{
// Get Cell position
Vector3 cellPos = new Vector3((ChunkPosition.x - ChunkSize/2f) + x, (ChunkPosition.y - ChunkSize/2f) + y);
// Get Temp Sample and set to color
float samp = c.Sample[x,y];
Gizmos.color = new Color(samp, samp, samp);
// Draw Tile as Sample black or white.
Gizmos.DrawCube(cellPos, Vector3.one);
}
}
// Size for Cubes
Vector3 size = new Vector3(ChunkSize, ChunkSize, 1f);
// Set Color Opaque Green
Gizmos.color = new Color(0f, 1f, 0f, 0.25f);
// Draw Chunk Borders (Disable to show issue)
// Gizmos.DrawWireCube(ChunkPosition, size);
}
}
}
}
I would like to point out when I use:
// Get Perlin Map
float px = (float)(x + xOffset) / ChunkSize * PerlinScale;
float py = (float)(y + yOffset) / ChunkSize * PerlinScale;
instead of
// Get Perlin Map
float px = (float)(x) / ChunkSize * PerlinScale + xOffset;
float py = (float)(y) / ChunkSize * PerlinScale + yOffset;
Everything aligns up correctly but the perlin noise just repeats.
What would be the best way for me to smooth between the chunks so that everything matches up?
Is there a better way to write this maybe?
EDIT:
Thanks for the help Draykoon D! here is the updated info and links to the updated scripts on pastebin if anyone needs them!
Here is the update code for anyone who wants it:
** WorldGenerator.cs**
https://pastebin.com/3BjLy5Hk
** WorldGenerator.cs**
https://pastebin.com/v3JJte3N
Hope that helps!
The key word you are looking for is tileable.
But I have a great news for you, noise function such as perlin are periodic in nature.
So instead of calling ChunckSize * ChunkSize a noise function you should only call it once and then divide the results.
I will advice you to read this excellent tutorial:
https://www.scratchapixel.com/lessons/procedural-generation-virtual-worlds/procedural-patterns-noise-part-1/creating-simple-1D-noise
Don't use Perlin noise. It has heavy bias towards the 45 and 90 degree directions. Your hills are all aligned to these, and aren't oriented along a more interesting variety of directions. You could use Unity.mathematics.noise.snoise(float2) but its repeat period is rather small, and it might not be very fast if you aren't using Unity Burst jobs. this is what I created/use/recommend, but it's certainly not the only option out there! Note that all these noises are range -1 to 1 rather than 0 to 1, so if that's important than do value=value*0.5+0.5; to rescale it.
Now that that's out of the way, to solve your issue you need to separate the idea of chunks and generation. This is a good idea in general, and I always believe in hiding backend implementation details (e.g chunks) from gameplay as much as possible (e.g. avoid visible boundaries). Each time you generate a chunk, you should find its start coordinate in the world, so that coordinates continue seamlessly with the rest. For example, if the chunks are 128x128, then the chunk starting at (0, 0) should have starting coordinate (0, 0), then the chunk starting at (0, 1) should have starting coordinate (0, 128). Only then, convert a world coordinate into a noise coordinate by multiplying by your desired frequency.

Using Perlin Noise across multiple Unity Terrain objects

I have a class project in which we are supposed to use Unities Terrain 3D objects and create a 3x3 smoothly generated terrain. For this we have been told to create a central Terrain the has adjacent terrains in the 8 cardinal directions. I have gotten the Perlin Noise to work through this method
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainNoiseGeneration : MonoBehaviour
{
private TerrainData myTerrainData;
public Vector3 worldSize;
public int resolution = 129;
private float userInput = (float)4.2;
public float offsetX;
public float offsetZ;
// Start is called before the first frame update
void Start()
{
myTerrainData = gameObject.GetComponent<TerrainCollider>().terrainData;
worldSize = new Vector3(100, 50, 100);
myTerrainData.size = worldSize;
myTerrainData.heightmapResolution = resolution;
float[,] heightArray = new float[resolution, resolution];
heightArray = PerlinNoise(userInput, offsetX, offsetZ);
myTerrainData.SetHeights(0, 0, heightArray);
}
// Update is called once per frame
void Update()
{
float[,] heightArray = new float[resolution, resolution];
heightArray = PerlinNoise(userInput, offsetX, offsetZ);
myTerrainData.SetHeights(0, 0, heightArray);
}
float[,] PerlinNoise(float userInput, float offsetX, float offsetZ)
{
float[,] heights = new float[resolution, resolution];
for (int z = 0; z < resolution; z++)
{
for (int x = 0; x < resolution; x++)
{
float nx = (x + offsetX) / resolution * userInput;
float ny = (z + offsetZ) / resolution * userInput;
heights[z, x] = Mathf.PerlinNoise(nx, ny);
}
}
return heights;
}
This code allows me to Generate a smooth terrain in the first Terrain object but when I try entering the offset values so that the edges can line-up they do not have the same values.
I would appreciate any assistance on this issue as I have tried a lot of different solutions, none of which are working
Update: I was able to solve the problem with a rather simple solution of the fact that I needed to use my resolution as the offset not the distance between the terrains
I needed to set the OffsetX and OffsetZ equal to that of their respective resolution positions instead of their unity positions.
For example my terrains are 100x100 so I was setting offset to 100 or -100 depending on its location but instead I needed to use 128 or -128 to keep it in line with the resolution

Create 3 dimensional grid two elements, one element in one row but in random columns

I've created a 3 dimensional grid. I have two separate objects filling the spaces of this grid. I want to have one of the objects in one row but on randomly selected columns.
Has anyone done this before or can anyone point me in the right direction?
I'm using Unity and C#. Thank you.
Vector3 towerSize = new Vector3(3, 3, 3);
//create grid tower
for (int x = 0; x < towerSize.x; x++)
{
for (int z = 0; z < towerSize.z; z++)
{
for (int y = 0; y < towerSize.y; y++)
{
//spawn tiles and space them
GameObject obj = (GameObject)Instantiate(tiles);
obj.transform.position = new Vector3(x * 1.2f, y * 1.2f, z * 1.2f);
//add them all to a List
allTiles.Add(obj);
obj.name = "tile " + allTiles.Count;
}
}
}
There is the code for the grid. I tried to have two objects in a singular List move to those tiles but the random column objects get in the same columns when I do that with this code:
for (int i = 0; i < allCubes.Count; i++)
{
allCubes[i].transform.position = Vector3.MoveTowards(
allCubes[i].transform.position,
allTiles[i].transform.position, 10 * Time.deltaTime);
}
Then thought put the two types of cubes in separate Lists themselves. Which ended up being even more messy. haha Does posting that code help?
I know this is an extremely old question of mine. It was a project that got canceled. I randomly came across it and out of curiosity I decided to try to complete this particular issue I was having such trouble doing back then. And I did.
public Vector3 towerSize = new Vector3(3, 3, 3);
public GameObject tiles;
public GameObject randomTile;
//public variables for debugging purposes.
//no real need to be seen in inspector in final. cleaner too if they're hidden
public int randomSelectedTile;
public List<GameObject> allTiles;
void Start()
{
//create grid tower
for (int x = 0; x < towerSize.x; x++)
{
for (int z = 0; z < towerSize.z; z++)
{
for (int y = 0; y < towerSize.y; y++)
{
//spawn cubes and space them
GameObject obj = (GameObject)Instantiate(tiles);
obj.transform.position = new Vector3(x * 1.2f, y * 1.2f, z * 1.2f);
//add them all to a List
allTiles.Add(obj);
obj.name = "tile " + allTiles.Count;
}
}
}
//select a random cube in the list
randomSelectedTile = Random.Range(0, allTiles.Count);
//get the cube object to delete
GameObject deleteObj = allTiles.ElementAt(randomSelectedTile);
//spawn the random cube at the position of the cube we will delete
GameObject rndObj = (GameObject)Instantiate(randomTile);
rndObj.transform.position = deleteObj.transform.position;
//remove the element at that location
allTiles.RemoveAt(randomSelectedTile);
//insert the random cube at that element's location
allTiles.Insert(randomSelectedTile, rndObj);
//destroy the unwanted cube
Destroy(deleteObj);
}
It's nice to see how you've improved over time. Just in case anyone else would benefit from the solution. Again, I apologize for bringing it back up.

Categories