Applying a texture to an imported *.obj file?

edited October 2016 in Questions about Code

I'm playing around with 3D models, and trying to apply textures to them. I'm finding that setTexture() doesn't work with PShapes created from imported obj files, though it seems to work fine with PShapes created from primitives.

Here is some example code, where I load in a model, and create a sphere, and apply the same texture to both. In this case, only the sphere has a texture.

PShape model;
PShape globey;
PImage pizza;

void setup() {
  size(500, 500, P3D);
  pizza = loadImage("pizza.jpg"); // load the texture
  model = loadShape("mammoth.obj");
  model.setTexture(pizza); //this does not work, the model is just black
  globey = createShape(SPHERE, 1000);
  globey.setTexture(pizza); //this DOES work. The sphere has a texture from pizza.jpg

void draw() {
    directionalLight(51, 102, 126, -1, 0, 0);
    directionalLight(255, 0, 100, 1, 0, 0);

Here is the result:

I'm trying to get the same texture to be applied to the mammoth skeleton, but it will only apply itself to the sphere, and I'm not sure why.


  • One thing: I just noticed that that dark blue is the color in the upper-left corner of the texture. When I rotate the texture 180 degrees, the color is different. Maybe this is an issue with setting the bounds of the texture?

  • Did you verify that your OBJ file contains UV coordinates for the object? UVs specify how the image should be mapped onto the geometry of the object. Since OBJ files are text, you can look at the file in a text editor to see if it has UVs. You should see a lot of lines that look like:

    f v1/vt1 v2/vt2 ...

    Here v1, vt1, v2 etc are all integers (indices into the vertex list)
    (There may be a 3rd value in the vertex spec like v1/vt1/vn1 ...)
    You don't want to see 2 slashes like v1//vn1 v2//vn2 ...

    The second value specifies the texture coordinates (UVs) for each vertex in the model.

    Good luck,
    Clay Budin

Sign In or Register to comment.