I’m trying to render a randomly generated texture.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class RandomTexture : MonoBehaviour {
void Start () {
BuildQuad();
BuildTexture();
}
void BuildTexture() {
Texture2D texture = new Texture2D(16, 16, TextureFormat.RGBA32, false);
// fill texture with random color
for (int x = 0; x < texture.width; x++) {
for (int y = 0; y < texture.height; y++) {
texture.SetPixel(x, y, Random.Range(0, 2) == 0 ? Color.red : Color.blue);
}
}
texture.Apply();
// create a material holding the texture
MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
Material material = new Material(Shader.Find("Sprites/Default"));
material.color = Color.white;
material.SetTexture("texture", texture);
// apply material to mesh
meshRenderer.sharedMaterial = material;
renderer.material.mainTexture = texture;
}
void BuildQuad() {
Mesh mesh = new Mesh();
// [...]
// create vertices, triangles, normals and uv for a single quad
MeshFilter meshFilter = GetComponent<MeshFilter>();
meshFilter.mesh = mesh;
}
}
At the end of the BuildTexture
method there are two statements that I don’t understand.
- If I remove
meshRenderer.sharedMaterial = material
the quad is not rendered at all (or is invisible). - If I remove
renderer.material.mainTexture = texture
the quad is rendered completely white. - If I keep both lines the quad is rendered as expected.
What is the difference between the two statements? To me it looks like one or the other should be enough. Or is there a better way to do this that I’m missing?