Hello,
New try with a new project design highlighting the problem.
Therefore i deleted my old post.
With the following code i try to make a plane with a user specific material on it.
To do this I have a image that contains all the tiles to make this material.
For this example i created this img:
If u add this image in a new project and add a plane and a empty material the following code will run after some settings:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class test : MonoBehaviour {
public int size_x;
public int size_z;
public float tileSize = 1.0f;
public int tileResolution;
public Texture2D terrainTiles;
int[,] textureTile;
int number;
void Loadmap()
{
//toekomst map uit database halen
textureTile = new int[size_z, size_x];
for (int z = 0; z < size_z; z++)
{
for (int x = 0; x < size_x; x++)
{
if (z == 0)
{
textureTile[z, x] = 0;
}
else
{
textureTile[z, x] = 2;
}
}
}
}
void Start()
{
int textWidth = size_x * tileResolution;
int textHeight = size_z * tileResolution;
Texture2D texture = new Texture2D(textWidth, textHeight);
Texture2D normal = new Texture2D(textWidth, textHeight, TextureFormat.ARGB32, true);
Loadmap();
Color[][] tiles = chopUpTiles();
for (int z = 0; z < size_z; z++)
{
for (int x = 0; x < size_x; x++)
{
Color[] p = tiles[textureTile[z, x]];
texture.SetPixels(x * tileResolution, z * tileResolution, tileResolution, tileResolution, p);
if (z > 0)//borderchecker
{
if (!(textureTile[z - 1, x] == textureTile[z, x]))
{
Color[] t = tiles[textureTile[z, x] + 1];
Color[] v = tiles[textureTile[z - 1, x] + 1];
texture.SetPixels(x * tileResolution, z * tileResolution, tileResolution, 5, t);
texture.SetPixels(x * tileResolution, z * tileResolution - 5, tileResolution, 5, v);
}
}
}
}
texture.filterMode = FilterMode.Bilinear;
texture.wrapMode = TextureWrapMode.Clamp;
texture.Apply();
MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
mesh_renderer.sharedMaterials[0].mainTexture = texture;
}
Color[][] chopUpTiles()
{
int numTiles_x = terrainTiles.width / tileResolution;
int numTiles_z = terrainTiles.height / tileResolution;
Color[][] tiles = new Color[numTiles_x * numTiles_z][];
for (int z = 0; z < numTiles_z; z++)
{
for (int x = 0; x < numTiles_x; x++)
{
tiles[z * numTiles_x + x] = terrainTiles.GetPixels(x * tileResolution, z * tileResolution, tileResolution, tileResolution);
}
}
return tiles;
}
}
the settings are:
-
The image needs to be Read/Write Enabled in the inspector
-
( edit forgot this) Set the img on clamp and Mobile defuse in the inspector.
-
drag the code on the plane
this enables the settings. select the plane and fill the public variables with info
-
Size_x = 2
-
Size_z = 2
-
Tile Size = 1
-
Tile Resolution = 64
-
Drag the image on Terrain Tiles
Also set the empty material to the Plane.
If u run the project you can see it adds the border (right side of the image) correctly but it shows the bottom border of the image on the green and the blue square while the blue one should show the top one.
I know i should address the Color for the top part but i can’t figure out how to do so.
Got that part from a tutorial i followed before i went in a different direction.
Please help me out.
Nivey