x


Apply material to imported mesh

I'm trying to put a material onto a mesh that I've imported from a .obj, but instead of the texture showing up, it shows a solid color that approximates the average hue of the texture.

I can instantiate a cube from the GameObject menu, then drag the material onto it in the Hierarchy view and it shows up as expected, but if I drag my mesh from the project view onto the hierarchy view, then drag the material onto it, I only get a solid color.

In case this is a strange way of doing things, I should probably also point out that I want to change the textures during the course of the game, so I don't want to apply them in the 3d app.

more ▼

asked Mar 30 '10 at 05:57 AM

Jaimse Jorms gravatar image

Jaimse Jorms
3 1 1 2

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

2 answers: sort voted first

You need to UV map your objects. Without UV mapping, the hardware has no way of knowing how to apply the texture, so you just get a solid color. The built-in cube is UV mapped.

more ▼

answered Mar 30 '10 at 07:45 AM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

Of course! Kicking myself now, I should've thought of that. Thanks.

Mar 30 '10 at 10:41 AM straydogstrut

Facepalm I knew it should be something simple. Thanks also straydogstrut for your example of using material objects.

Mar 30 '10 at 08:28 PM Jaimse Jorms
(comments are locked)
10|3000 characters needed characters left

Hi Jaimse,

I don't use .obj much, but I threw one I had lying around into Unity and applied a texture no problem. The normal workflow is to create a material first, unless you already have one from your 3d app, then set its texture and apply the material to the mesh. Don't confuse materials and textures. Materials define how your object looks through properties such as color, texture and which shader you're using. However, Unity usually creates a material for you if you don't have one when you apply a texture straight to an object, so I don't see how you would have problems. Do you see a material in the Project View?

As for changing textures at runtime, look up renderer.material.mainTexture. The following assigns a texture to the object this script is attached to when it is initialised (myTexture being the texture you've chosen in the Editor):

var myTexture: Texture2D; 

function Start(){ 

    renderer.material.mainTexture = myTexture;
}

See the Docs: http://unity3d.com/support/documentation/ScriptReference/Material-mainTexture.html

While the following assigns a texture through code that is stored in the resources folder (example from the Docs):

// Assigns a texture named "Assets/Resources/glass" to a Plane.
function Start () {
    var go = new GameObject.CreatePrimitive(PrimitiveType.Plane);
    go.renderer.material.mainTexture = Resources.Load("glass");
}

http://unity3d.com/support/documentation/ScriptReference/Resources.Load.html

Hope that helps, sorry I don't have an answer for why your texture isn't showing up.

more ▼

answered Mar 30 '10 at 06:56 AM

straydogstrut gravatar image

straydogstrut
1.2k 29 38 60

(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:

x2280
x1403
x841

asked: Mar 30 '10 at 05:57 AM

Seen: 6035 times

Last Updated: Mar 30 '10 at 05:57 AM