Help with adding texture to car

Hi,

I am new to Babylon.js and I am trying to add a texture to a car model I have. I was successful in adding a diffuseTexture to one of the playground examples but when I add it to the car model, Babylon seems to be selecting a color instead of rendering the full texture as I expect.

  var createScene = function () {
    // Create a scene.
    var scene = new BABYLON.Scene(engine);

    var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 2, Math.PI / 4, 10, BABYLON.Vector3.Zero(), scene);
    camera.attachControl(canvas, true);

    var light = new BABYLON.HemisphericLight("hemiLight", new BABYLON.Vector3(-1, 1, 0), scene);
    light.diffuse = new BABYLON.Color3(1, 1, 1);
    light.specular = new BABYLON.Color3(1, 1, 1);
    light.groundColor = new BABYLON.Color3(1, 1, 1);

    // Append glTF model to scene.
    BABYLON.SceneLoader.LoadAssetContainer("glb/", "truck.glb", scene, function (container) {
      // container.materials[12].albedoColor = new BABYLON.Color3(0, 1, 1);
      container.materials[12].albedoTexture = new BABYLON.Texture("https://miro.medium.com/max/805/1*2_dN3nx1FOEKnNjvtqno_w.jpeg", scene);
      // Also tried diffuseTexture
      container.addAllToScene();
    });

    return scene;
  };

Truck.glb

This is was I see instead of the expected Texture
Imgur

Hi @dcaixfl and welcome to the forum!

Are you sure your truck has texture coordinates applied to it?

Note that your glb file has some validation issues. Try to open it in the Babylon sandbox (https://sandbox.babylonjs.com/) and look at the bottom of the inspector pane.

Hi! Thank you for the welcome!

I think what you’re specifying is UV coordinates? I took a look at the Texture API, how would I go about applying the coordinates?

https://doc.babylonjs.com/api/classes/babylon.texture

I took a look at a few other examples and it seems I need to apply it to the mesh rather than the material? If you could provide an example from the docs, tutorial, or an explanation, I would very much appreciate it!

Hi

Can you share the texture you are using? I want to test it out. :slight_smile:

Sure! I just grabbed a Tide advertisement from Google. Haha

I wanna see this! Have done something like this before? Where you grab a random image and try to use it as a texture?

https://www.babylonjs-playground.com/#20OAV9#802

var createScene = function () {
    var scene = new BABYLON.Scene(engine);
    var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 2,  Math.PI / 4, 5, BABYLON.Vector3.Zero(), scene);
    camera.attachControl(canvas, true);
	
	scene.ambientColor = new BABYLON.Color3(1, 1, 1);
	
	//Light direction is up and left
	var light = new BABYLON.HemisphericLight("hemiLight", new BABYLON.Vector3(-1, 1, 0), scene);
	light.diffuse = new BABYLON.Color3(1, 1, 1);
	light.specular = new BABYLON.Color3(1, 1, 1);
	light.groundColor = new BABYLON.Color3(1, 1, 1);
	
	var material = new BABYLON.StandardMaterial("redMat", scene);
	material.diffuseTexture = new BABYLON.Texture("https://miro.medium.com/max/805/1*2_dN3nx1FOEKnNjvtqno_w.jpeg", scene);
	
	var sphere = BABYLON.MeshBuilder.CreateSphere("sphere0", {}, scene);
	sphere.material = material;			
	    
    return scene;

};

UV coordinates are normally created in the modeling software, you won’t be able to do that in Babylon with already existing objects (or it will be very difficult / tedious to say the least).

1 Like

Hey @dcaixfl welcome to the forum! It looks like the texture is being sampled correctly. I made a quick PG to modify and test:
https://playground.babylonjs.com/#RZUDBM#1

As @Evgeni_Popov is suggesting, the UV coordinates of the model may be a bit messed up. This PG attempts to load in a UV checkerboard texture, and it looks very distorted:

https://playground.babylonjs.com/#RZUDBM#2

Looking at the UV coordinates in Blender seem to confirm this:

The verts assigned to the “chrome” material all have the same UV coordinates:

same for “mirror”

and “shield”

and “body”

Unfortunately, this asset will need to be re-unwrapped if you want to apply graphical textures and decals properly to the body. I’d assume that this was created to only allow for flat-color/palette coloring of the chrome, and main body pieces, and easy texturing of the wheel treads

1 Like