how to properly set a texture?
my question is how to set the noiseMap texture thats made in mapDisplay to the gameobj i transform into a plane/map in meshbuilder as you can see i have commented lines that i have tried but everything i have done has failed. for what ever reason the noise will not appear. i even tried getting rid of mapDisplay and mapgenerator completely adding everything to meshbuilder and calling it in gamefunctionality but that also did not work. does anyone know what mistake i'm making? just to be clear mapDisplay,noise, and mapgenerator are scripts i made using a tutorial
script one mapDisplay
using UnityEngine;
using System.Collections;
public class mapDisplay : MonoBehaviour
{ //public Renderer textureRender;
//public static Color[] colorMap;
//public Texture2D texture;
public void drawNoiseMap(float[,] noiseMap)
{ int width = noiseMap.GetLength (0);
int height = noiseMap.GetLength (1);
Texture2D texture = new Texture2D (width,height);
Color[] colorMap = new Color[width * height];
for (int y = 0; y < height; y++)
{ for (int x = 0; x < width; x++)
{colorMap [y * width + x] = Color.Lerp (Color.black, Color.white, noiseMap [x, y]);
}
Debug.Log("yes made it" + colorMap);
}
texture.SetPixels (colorMap);
texture.Apply ();
gameObject.GetComponent<MeshRenderer>().sharedMaterial.mainTexture = texture;
gameObject.transform.localScale = new Vector3 (width, 1, height);
}
}
script two meshbuilder
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
public class meshbuilder : MonoBehaviour
{
//GameObject Cube = new GameObject();
//public Material Red = Resources.Load("Red",typeof(Material)) as Material;
//public vertices for shapes that may use the same vertices
public static Vector3 vertice0 = new Vector3(-1,1,1);// cubevertice 0 lft 0
public static Vector3 vertice1 = new Vector3(1,1,1);// cubevertice 1 rtf 1
public static Vector3 vertice2 = new Vector3(-1,-1,1);// cubevertice 2 lbmf 2
public static Vector3 vertice3 = new Vector3(1,-1,1);// cubevertice 3 rbmf 3
public static Vector3 vertice4 = new Vector3(1,1,-1);// cubevertice 4 rtb 4
public static Vector3 vertice5 = new Vector3(-1,1,-1);// cubevertice 5 ltb 5
public static Vector3 vertice6 = new Vector3(1,-1,-1);// cubevertice 6 rbmb 6
public static Vector3 vertice7 = new Vector3(-1,-1,-1);// cubevertice 7 lbmb 7
//front face hex
public static Vector3 vertice8 = new Vector3(-0.5f,1,1.5f);// vertice 8 hex 0 ltb 5
public static Vector3 vertice9 = new Vector3(0.5f,1,1.5f);// vertice 9 hex 1 lft 0
public static Vector3 vertice10 = new Vector3(-0.5f,-1,1.5f);// vertice 10 hex 2 lbmb 7
public static Vector3 vertice11 = new Vector3(0.5f,-1,1.5f);// vertice 11 hex 3 lbmf 2
//back face hex
public static Vector3 vertice12 = new Vector3(0.5f,1,-1.5f);// vertice 12 hex 4 rtf 1
public static Vector3 vertice13 = new Vector3(-0.5f,1,-1.5f);// vertice 13 hex 5 rtb 4
public static Vector3 vertice14 = new Vector3(0.5f,-1,-1.5f);// vertice 14 hex 6 rbmf 3
public static Vector3 vertice15 = new Vector3(-0.5f,-1,-1.5f);// vertice 15 hex 7 rbmb 6
public static Vector3 vertice16 = new Vector3(-1,1,-1);// vertice 16 ltb 5
public static Vector3 vertice17 = new Vector3(1,1,-1);// vertice 17 rtb 4
public static Vector3 vertice18 = new Vector3(-1,1,1);// vertice 18 lft 0
public static Vector3 vertice19 = new Vector3(1,1,1);// vertice 19 rtf 1
public static Vector3 vertice20 = new Vector3(-1,-1,1);// vertice 20 lbmf 2
public static Vector3 vertice21 = new Vector3(1,-1,1);// vertice 21 rbmf 3
public static Vector3 vertice22 = new Vector3(-1,-1,-1);// vertice 22 lbmb 7
public static Vector3 vertice23 = new Vector3(1,-1,-1);// vertice 23 rbmb 6
//gui stuff
//void OnGui()
//{
//if(GUI.Button(new Rect(0,0,100,20),"Cube Me!"))
//{
// CubeMe ();
//}
//}
// mesh building properties
//Hexagon vertices array
public static Vector3[] hexVertArray = new Vector3[]
{
//front Face
vertice8,//0
vertice9,//1
vertice10,//2
vertice11,//3
//Back Face
vertice12,//4
vertice13,//5
vertice14,//6
vertice15,//7
//Left Face
vertice5,//8
vertice0,//9
vertice7,//10
vertice2,//11
//Right Face
vertice1,//12
vertice4,//13
vertice3,//14
vertice6,//15
//Left Front Face
vertice0,//16
vertice8,//17
vertice2,//18
vertice10,//19
//Right Back Face
vertice1,//20
vertice12,//21
vertice6,//22
vertice14,//23
//Right Front Face
vertice9,//24
vertice1,//25
vertice11,//26
vertice3,//27
//Left Back Face
vertice13,//28
vertice5,//29
vertice15,//30
vertice7,//31
//Top Face
vertice13,//32
vertice12,//33
vertice8,//34
vertice9,//35
vertice0,//36
vertice5,//37
vertice1,//38
vertice4,//39
//Bottom face
vertice10,//40
vertice11,//41
vertice15,//42
vertice14,//43
vertice2,//44
vertice7,//45
vertice3,//46
vertice6//47
};
//hexTriArray
public static int[] hexTriArray = new int[]
{
//front face
0,2,3,
3,1,0,
//back face
4,6,7,
7,5,4,
//left face
8,10,11,
11,9,8,
//right face
12,14,15,
15,13,12,
//left front face
16,18,19,
19,17,16,
//right back face
20,22,23,
23,21,20,
//right front face
24,26,27,
27,25,24,
//left back face
28,30,31,
31,29,28,
//top face
33,32,34,35,33,
37,36,39,38,37,
36,34,32,39,36,
33,37,38,35,33,
//bottom face
40,42,41,43,40,
45,44,47,46,45,
44,40,42,47,44,
40,45,46,43,40
};
//hexUvs
public static Vector2[] hexUvs = new Vector2[]
{
//frontface
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//backface
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//leftface
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//rightface
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//leftfrontface
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//rightbackface
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//rightfrontface
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//leftbackface
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//topface
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//bottomface
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0)
};
//CubeVerticesArray
public static Vector3[] cubeVertArray = new Vector3[]
{
//Front Face
vertice0,//0
vertice1,//1
vertice2,//2
vertice3,//3
//back face
vertice4,//4
vertice5,//5
vertice6,//6
vertice7,//7
//left face
vertice5,//8
vertice0,//9
vertice7,//10
vertice2,//11
//right face
vertice1,//12
vertice4,//13
vertice3,//14
vertice6,//15
//top face
vertice5,//16
vertice4,//17
vertice0,//18
vertice1,//19
//bottom face
vertice2,//20
vertice3,//21
vertice7,//22
vertice6//23
};
//cubetriangles array clockwise points equals visablity
public static int[] cubeTriArray = new int[]
{
//front face
0,2,3,
3,1,0,
//back face
4,6,7,
7,5,4,
//left face
8,10,11,
11,9,8,
//right face
12,14,15,
15,13,12,
//top face
16,18,19,
19,17,16,
//bottom face
20,22,23,
23,21,20
};
//uvs
public static Vector2[] cubeUvs = new Vector2[]
{
//front face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//back face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//left face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//right face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//top face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//bottom face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0)
};
public static Vector3[] mapVertArray = new Vector3[]
{//top face
vertice5,//16
vertice4,//17
vertice0,//18
vertice1,//19
};
public static int[] mapTriArray = new int[]
{
//top face
0,2,3,
3,1,0
};
public static Vector2[] mapUvs = new Vector2[]
{
//top face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
};
//holds the information to form a topview plane or panel
public void MapMe()
{
MeshFilter terrain = GetComponent<MeshFilter> ();
Mesh MapMorph = terrain.mesh;
GetComponent<MeshRenderer> ().material = Resources.Load ("Blue", typeof(Material)) as Material;
MapMorph.Clear();
MapMorph.vertices = mapVertArray;
MapMorph.triangles = mapTriArray;
MapMorph.uv = mapUvs;
MapMorph.Optimize ();
MapMorph.RecalculateNormals();
}
//will hold information for hexagon currently broken
public void HexMe()
{MeshFilter Hexaganel = GetComponent<MeshFilter>();
Mesh HexMorph = Hexaganel.mesh;
GetComponent<MeshRenderer> ().material = Resources.Load ("Blue", typeof(Material)) as Material;
HexMorph.Clear();
HexMorph.vertices = hexVertArray;
HexMorph.triangles = hexTriArray;
HexMorph.uv = hexUvs;
HexMorph.Optimize();
HexMorph.RecalculateNormals();
}
//holds the information to turn GameObject to a cube
public void CubeMe()
{MeshFilter Filtration = GetComponent<MeshFilter>();
Mesh CubeMorph = Filtration.mesh;
GetComponent<MeshRenderer> ().material = Resources.Load ("Red", typeof(Material)) as Material;
CubeMorph.Clear();
CubeMorph.vertices = cubeVertArray;
CubeMorph.triangles = cubeTriArray;
CubeMorph.uv = cubeUvs;
CubeMorph.Optimize();
CubeMorph.RecalculateNormals();
}
void awake ()
{
}
void start()
{
}
// Update is called once per frame
void Update ()
{
}
}
script three mapgenerator
public class mapGenerator : MonoBehaviour
{
public int mapWidth = 100;
public int mapHeight = 100;
public float noiseScale = 0.4f;
public float persistance = 1f;
public float lacurnarity = 0.8f;
public int octaves = 11;
public void generateMap()
{
float[,] noiseMap = noise.generateNoiseMap (mapWidth, mapHeight, noiseScale, octaves, persistance, lacurnarity);
mapDisplay display = FindObjectOfType<mapDisplay> ();
display.drawNoiseMap (noiseMap);
}
}
script four gamefunctionality
using System;
using UnityEngine.UI;
using UnityEngine.EventSystems;
//[RequireComponent()]
public class gameFunctionality : MonoBehaviour
{
public int mapWidth = 100;
public int mapHeight = 100;
public float noiseScale = 0.4f;
public float persistance = 1f;
public float lacurnarity = 0.8f;
public int octaves = 11;
//public Texture2D mapterrain;
public float Xp = 0f; // experience points used in lvl up, enemy and player scripts, items
public float XpToNxtLvl = 0f; // player and item equation to determin amount of points required for lvling up
public float Health = 100f; // a stat for the player
public float StatPoints = 0f; // number of points to allocate into stats for items and player
public float Level = 1f; // current lvl of item or character
public Texture2D button; // texture of a button
public GameObject cube;
//onclick event for button
public void OnMouseDown()
{
Xp += 1000000000;
//print("+10Xp");
}
//onclickRelease event for button
public void OnMouseUp()
{
XpGain ();
if(Xp >= XpToNxtLvl)
{
//print("Level Up!");
}
}
//function to acquire meshbuilder script
void Cubebuild ()
{
meshbuilder cubebuild = GetComponent <meshbuilder>()as meshbuilder;
cubebuild.CubeMe();
}
//void HexBuild()
//{
// meshbuilder hexbuild = GetComponent<meshbuilder> ()as meshbuilder;
// hexbuild.HexMe ();
//}
//MapGeneration
void mapGen()
{
float[,] noiseMap = noise.generateNoiseMap (mapWidth, mapHeight, noiseScale, octaves, persistance, lacurnarity);
mapDisplay mapBuild = GetComponent<mapDisplay> () as mapDisplay;
mapBuild.drawNoiseMap (noiseMap);
}
void Mapbuild()
{
meshbuilder mapbuild = GetComponent <meshbuilder>()as meshbuilder;
mapbuild.MapMe();
}
// not sure if this is needed but possible use in future probaly could just combine it with nxtLvl function
void XpGain()
{
if( Xp >= XpToNxtLvl)
{
NextLvl();
}
}
//acquiring xp
void XpToLvl()
{
XpToNxtLvl = ((1+Mathf.Pow(Mathf.Sqrt(5f),Level)) - (1f-(Mathf.Pow(Mathf.Sqrt(5f),Level)))) /(Mathf.Pow(2,Level)*Mathf.Sqrt(5f));
}
// LevelUP!
void NextLvl()
{
Xp -= Xp;
Health += 50;
StatPoints += 10;
Level += 1;
}
// Button and other user interface things
void OnGUI()
{
if(GUI.Button(new Rect(25,25,100,20),"map Me!"))
{
mapGen ();
}
if(GUI.Button(new Rect(5,5,100,20),"Cube Me!"))
{
Cubebuild ();
}
if(GUI.Button(new Rect(100,100,100,20),"MapItUPSon!"))
{
Mapbuild();
}
if (GUI.Button (new Rect (10, 70, 100, 20), "Click"))
{
OnMouseDown ();
OnMouseUp ();
}
}
//intializes before start
public void awake()
{
}
// Use this for initialization
void Start ()
{
gameObject.AddComponent<meshbuilder> ();
gameObject.AddComponent<mapDisplay> ();
//gameObject.AddComponent<mapGenerator> ();
}
// Update is called once per frame
void Update ()
{
XpToLvl();
}
}
script five noise using UnityEngine; using System.Collections;
public static class noise
{
public static float [,] generateNoiseMap(int mapWidth, int mapHeight, float scale, int octaves, float persistance, float lacurnarity)
{float[,] noiseMap = new float[mapWidth, mapHeight];
if (scale <= 0)
{scale = 0.00001f;
}
float maxNoiseHeight = float.MinValue;
float minNoiseHeight = float.MinValue;
for(int y =0; y < mapHeight; y++)
{for(int x =0; x < mapWidth; x++)
{float amplitude = 1;
float frequency = 1;
float noiseHeight = 0;
for (int i =0; i < octaves; i++)
{float sampleY = y/scale * frequency;
float sampleX = x/scale * frequency;
float perlinValue = Mathf.PerlinNoise(sampleX,sampleY)*2-1;
noiseHeight += perlinValue * amplitude;
amplitude *= persistance;
frequency *= lacurnarity;
}
if (noiseHeight>maxNoiseHeight)
{maxNoiseHeight = noiseHeight;
}
else if(noiseHeight < minNoiseHeight)
{minNoiseHeight = noiseHeight;
}
noiseMap [x, y] = noiseHeight;
}
}
for (int y = 0; y < mapHeight; y++)
{for (int x = 0; x < mapWidth; x++)
{noiseMap [x, y] = Mathf.InverseLerp (minNoiseHeight, maxNoiseHeight, noiseMap [x, y]);
}
}
return noiseMap;
}
}
Are you trying to use the legacy materials or the new ones?, because there's a difference between those types.
not sure about that im new still. im trying to get the texture2d that is made in mapDisplay and apply it the the plane made in meshbuilder from pressing the button thats in gamefunctionality. so far my attemps have failed to being just white. no texture at all or just red. which is why i set it to blue atm.
void mapGen() was orignally
void mapGen(){
mapGenerator mapBuild = GetComponent<mapGenerator> () as mapGenerator;
mapGenerator.generate$$anonymous$$ap();
}
line 21 of map display is it that im getting the compent the problem would it work if i just did
gameObject.GetComponent<$$anonymous$$eshRenderer>().shared$$anonymous$$aterial.mainTexture = texture;
to
gameObject.$$anonymous$$eshRenderer.shared$$anonymous$$aterial.mainTexture = texture;
in map display i uncommented the public renderer and changed lines 21-22 to use the public renderer ins$$anonymous$$d of gameobj still doesnt show the noise...
Sorry, but I don't understand what do you really want, and here is where I said - "To get the right answer you need the right question" -.