I have a 3D model of a car. I would like to add a function to change the color of the vehicle upon clicking on one of the color choices. Here(Top Class Bottom Class) you can find the class structure of the 3D model. I’m aware that I need to apply a material upon the car body but I don’t know how exactly because this is a gltf model and not a standard three.js geometry shape.
Here’s the code I find the most relevant, if something is missing please tell.
function main() {
// Color Palette
const palette = document.getElementById('js-palette-slide');
// Model Path
let theModel = './cars/scene.gltf'
// 3D Model Loader
const gltfLoader = new THREE.GLTFLoader();
gltfLoader.load(theModel, (gltf) => {
const root = gltf.scene;
scene.add(root);
});
// Colors To Choose From
const colors = [
{
color: "000000",
},
{
color: "545252",
},
];
// Add Colors To Palette
function buildColors(colors) {
for (let [i, color] of colors.entries()) {
let colorpalette = document.createElement('div');
colorpalette.classList.add('pallete__colors');
colorpalette.style.background = '#' + color.color;
colorpalette.setAttribute('data-key', i);
palette.append(colorpalette)
}
}
buildColors(colors)
main()