Using UV Mappings created in Blender

I’ve create a 3D model, along with UV Mappings in Blender:

Using the Blender to Three exporter, I then export my geometry to Three: (https://github.com/mrdoob/three.js/tree/master/utils/exporters/blender)

After doing that, I can then import my JSON:

It looks like:

{
  "uvs":[[0.775568,0.779311,...
  "faces":[41,0,527,843,84...
  "normals":[0.0973846,-0.111087...
  "vertices":[0.173144,0.225188,...
  "metadata":{
    "faces":660,
    "normals":156,
    "generator":"io_three",
    "version":3,
    "vertices":1078,
    "uvs":1,
    "type":"Geometry"
  }
}

I can then pull that into Three.js

const json = jsonLoader.parse(json);
const geometry = json.geometry; // The only thing in the parsed json is `geometry`

What I’d like to do now, is take an image and apply it to this geometry, using the same UV Mappings I set up in blender.

Note that when I say “an image”, I don’t mean the exact image that was used in blender. User’s of this application will be filling in details of that particular template image and uploading it to my application. However, since it follows the same template from Blender, the same UV Mappings will apply.

I’m stuck on this piece of it … what would the code look like to use UV Mappings from blender, to create and apply a texture to this geometry given a provided image?

Right now, I do this, but the UV Mappings from Blender seem to have no bearing on how this is rendered:

const loader = new THREE.TextureLoader();
loader.load('http://www.example.com/some-image-url.png', artworkTexture => {

  const json = jsonLoader.parse(json); // The JSON from blender
  const geometry = json;
  
  // Most likely unrelated code ommitted here

  artworkTexture.anisotropy = renderer.getMaxAnisotropy();

  const artworkMaterial = new THREE.MeshBasicMaterial({
     side: THREE.FrontSide,
     map: artworkTexture,
     depthWrite: false,
     depthTest: false
  });

  objectMaterials.push(artworkMaterial);

   const mainObject = THREE.SceneUtils.createMultiMaterialObject(
     geometry,
     objectMaterials
   );

});

I figured this one out. This WAS actually working. However, in the code I provide, I have a section commented out that says // Most likely unrelated code ommitted here. Turned out to be relevant. I was doing some transformation to the texture that was actually throwing this off. By simply removing those transformations, I solved my problem.

2 Likes

Hi Jason,

This looks amazing! After searching for months online I think I have finally found someone with exactly the same project that I have been trying to do… except you succeded!!!

Is there any way I could download your project resources to help me out?

Thanks,
Daniel

Hey DDsmoove,

I unfortunately don’t think I could send you the project resources for download. However, the site is public, it can be found here: https://www.templi.com/.

If you have any specific questions I’d be happy to give you some guidance.

1 Like

Hi Jason,

Thanks for the quick response.

My first hurdle is the uv mapping itself on blender. At the moment I just can’t understand why it is not working for me.

Either it is the model itself or I have made the wrong seams in the cup.

Could you give me any pointers?

01

That looks almost certainly to be a problem with your seams.

You want your seams to look like this:
1 On the lip.
2 On the bottom.
3 Down the side.

latest

Once you’ve marked the seams correctly, select all on the left side, select “Unwrap”, and it should look like this:

latest

Ok so i’ve managed to quickly remodel the cup and im getting a bit closer now but only by taking out the bottom of the cup. (will go over this at a later stage once i understand the whole process)

So from here, I just convert the blend file to three?(three.js/utils/exporters/blender at master · mrdoob/three.js · GitHub)

Does the code already understand what the UV map is or are there further steps to take? Also, if an image is uploaded how do I link user uploaded images to the UV map (provided i have already build an upload button funtion)?

Thanks again for all the help again :slight_smile:

When you export to three, make sure you have “UVs” checked. The UV map will be part of your Geometry that gets exported.

latest

After you export it, you’ll have a .json file, which you’ll need to read in the geometry for your scene.

Look above at my original post for how you might read this in, along with the artwork.