Textures
Textures add detailed surface information to materials:
// Load a basic texture
const diffuseTexture = new BABYLON.Texture("textures/wood.jpg", scene);
material.diffuseTexture = diffuseTexture;
// Configure texture properties
diffuseTexture.uScale = 2; // Repeat texture horizontally 2 times
diffuseTexture.vScale = 2; // Repeat texture vertically 2 times
diffuseTexture.wrapU = BABYLON.Texture.MIRROR_ADDRESSMODE; // Mirror wrapping horizontally
diffuseTexture.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE; // Clamp edges vertically
Add additional texture maps for detailed surface information:
// Add additional texture maps
material.bumpTexture = new BABYLON.Texture("textures/woodNormal.png", scene); // Normal map
material.ambientTexture = new BABYLON.Texture("textures/woodAO.jpg", scene); // Ambient occlusion
material.specularTexture = new BABYLON.Texture("textures/woodSpecular.jpg", scene); // Specular map
// Adjust normal map intensity
material.bumpTexture.level = 1.5; // Intensity of normal mapping
Using textures with PBR materials for realistic rendering:
// Using textures with PBR materials
pbr.albedoTexture = new BABYLON.Texture("textures/metal/albedo.png", scene);
pbr.metallicTexture = new BABYLON.Texture("textures/metal/metallic.png", scene);
pbr.roughnessTexture = new BABYLON.Texture("textures/metal/roughness.png", scene);
pbr.normalTexture = new BABYLON.Texture("textures/metal/normal.png", scene);
pbr.ambientOcclusionTexture = new BABYLON.Texture("textures/metal/ao.png", scene);
// Using a single channel from a texture
pbr.useRoughnessFromMetallicTextureAlpha = false;
pbr.useRoughnessFromMetallicTextureGreen = true; // Use green channel for roughness
pbr.useMetallicFromMetallicTextureBlue = true; // Use blue channel for metallic
Add environment reflections to materials:
// Environment mapping for reflections
const envTexture = new BABYLON.CubeTexture("textures/environment/skybox", scene);
pbr.environmentTexture = envTexture;