x


Problem Assigning RenderTexture to Material

If I do the following:

RenderTexture newTex = new RenderTexture(256, 256, 0);
Material newMat = new Material(Shader.Find("Diffuse"));
newMat.SetTexture("_MainTex", newTex);

I get

Error assigning 2D rectangle texture to 2D texture property '_MainTex': Dimensions must match

It doesn't matter what dimensions I specify, not to mention that the Material should have no dimensions until I give it a texture. What exactly is it that doesn't "match"?

Even weirder: The code runs fine if I ignore the error (so it's more like a warning)... But I despise leaving warnings in my code.

more ▼

asked Jan 28 '10 at 12:38 AM

Kevin Laity gravatar image

Kevin Laity
214 13 13 25

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

2 answers: sort voted first

The voodoo solution is this:

For some reason, you must tell Unity whether this is a power of two texture or not. The following sequence of lines prevents the error.

    RenderTexture newTex = new RenderTexture(1024, 1024, 0);
    newTex.isPowerOfTwo = true;         // This line prevents the error
    newTex.Create();                    // This line is totally optional
more ▼

answered Feb 03 '10 at 03:05 PM

Kevin Laity gravatar image

Kevin Laity
214 13 13 25

Thanks! This totally solved my bizarro bug.

Jun 30 '10 at 04:23 PM rogerimp
(comments are locked)
10|3000 characters needed characters left

I believe that instantiating a RenderTexture does not actually create the texture -- that happens after it is first rendered or Create() is called. Perhaps adding a call to Create() before assigning it to the material would get rid of the error.

more ▼

answered Jan 28 '10 at 02:26 AM

Molix gravatar image

Molix
4.8k 15 27 66

Good answer but, unfortunately it has no effect

Jan 28 '10 at 07:38 PM Kevin Laity
(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:

x2196
x813

asked: Jan 28 '10 at 12:38 AM

Seen: 2692 times

Last Updated: Jan 28 '10 at 01:08 AM