x


Need help with creating dynamic textures.

Hello everyone,

I am currently working on a game based on atoms. The game allows you to play around with all kinds of atoms from the Periodic Table. In order to reduce the art assets I want to create textures dynamically and slap it onto the atoms (spheres). I can do this easily in editor using a RenderTexture. However, when I try doing it in script (C#) the text simply doesn't show up in the RenderTexture. Here is my code:

void GenerateTexturesForAtoms()
{
    for (int i = 0; i < atom_list.Count; ++i)
    {
        GameObject cam_text = Instantiate(Resources.Load("CamText")) as GameObject;

        RenderTexture render_texture = new RenderTexture(256, 256, 0);
        render_texture.isPowerOfTwo = true;
        render_texture.Create();

        cam_text.GetComponent<GUIText>().text = "Hydrogen";
        cam_text.GetComponent<Camera>().transform.Rotate(new Vector3(0, 0, 0));
        cam_text.GetComponent<Camera>().transform.position = new Vector3(0, 15, 0);
        cam_text.GetComponent<Camera>().targetTexture = render_texture;

        Material atom_mat = new Material(Shader.Find("Diffuse"));
        atom_mat.SetTexture("_MainTex", render_texture);
        (atom_list[i] as GameObject).renderer.material = atom_mat;
    }
}

I just need to figure out how to get the GUIText to render onto my target texture. I think there is something really silly that I am doing. Any help would be great!

Thank you very much!

more ▼

asked Aug 09 '10 at 03:44 PM

johnkol gravatar image

johnkol
42 4 4 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Okay I think I figured it out, or rather found an alternative.

I used a 3DText (TextMesh) instead. I created a prefab with a Camera, Plane and 3DText. The Plane will hold the material you want to show up on the atom. The 3DText will go on top of this plane. The Camera is then set perpendicular to them and thus captures the 3dText over the plane onto a RenderTexture. At this point I have pretty much what I wanted. I doubt if this is the most efficient way to do it, but works pretty well for my game. Also, make sure you use the 3DText Shader instead of the default one. Its available here. Else your text will appear scrambled when you try to use custom font.

Here is the code I used in case any wants to try it out:

void GenerateTexturesForAtoms()
{
    for (int i = 0; i < atom_list.Count; ++i)
    {
        GameObject cam_text = Instantiate(Resources.Load("CamText")) as GameObject;

        RenderTexture render_texture = new RenderTexture(256, 256, 0);
        render_texture.isPowerOfTwo = true;
        render_texture.Create();

        cam_text.GetComponentInChildren<TextMesh>().text = atom_list[i].GetComponent<Atom>().element_name;
        cam_text.GetComponent<Camera>().transform.Rotate(new Vector3(-90, 0, 0));
        cam_text.GetComponent<Camera>().transform.position = new Vector3(90, 10 * i, 0);
        cam_text.GetComponent<Camera>().targetTexture = render_texture;

        Material atom_mat = new Material(Shader.Find("Diffuse"));
        atom_mat.SetTexture("_MainTex", render_texture);
        atom_mat.SetTextureOffset("_MainTex", new Vector2(0.2f, -0.025f));
        (atom_list[i] as GameObject).renderer.material = atom_mat;
    }
}
more ▼

answered Aug 09 '10 at 07:13 PM

johnkol gravatar image

johnkol
42 4 4 8

John, I have a similar problem. How did you create the Camera, Plane and 3DText? Did you attach the Plane and 3DText to the Camera? You code doesn't ever reference the plane or 3DText objects so I assume you did the parenting etc. in the editor or inspector. Can you tell me how you did it?

Thanks

Apr 04 at 11:07 PM mterlecki1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2188
x807
x313
x230
x191

asked: Aug 09 '10 at 03:44 PM

Seen: 3588 times

Last Updated: Apr 04 at 11:07 PM