That’s how my model is looking on https://gltf-viewer.donmccurdy.com/.
And How it’s looking in my code?
I know I should share the code too and will but before that, I would like to know what might be going wrong.
- Is this a problem with lighting?
- Is this a problem of shadow? (I don’t think so)
- Is this related to the opacity of the object? (I don’t think so because it shows opacity: 1 in code)
- Is this related to the material of the object?
Here are some snippets of code. I’m using Angular.
HTML file:
<div class="canvas-container">
<canvas class="webgl" linear flat></canvas>
</div>
JavaScript:
const canvas: HTMLCanvasElement = document.querySelector('canvas.webgl');
const scene = new THREE.Scene();
const sizes = {
width: window.innerWidth,
height: window.innerHeight
};
const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100);
camera.position.set(10, 35, 20);
camera.layers.enableAll();
camera.layers.toggle(1);
this.loadTexture(scene);
this.loadModels(scene);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true
});
renderer.physicallyCorrectLights = true;
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.textureEncoding = THREE.sRGBEncoding;
renderer.toneMappingExposure = 0.2;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setSize(sizes.width, sizes.height);
renderer.setPixelRatio(window.devicePixelRatio);
const canvas_container = document.querySelector('.canvas-container');
const controls = new OrbitControls(camera, canvas_container);
controls.maxDistance = 50;
const tick = () => {
window.requestAnimationFrame(tick);
controls.update();
renderer.render(scene, camera);
};
tick();
LoadTexture Fn:
loadTexture(scene) {
const cubeTextureLoader = new THREE.CubeTextureLoader();
const debugObject: any = {};
const environmentMap = cubeTextureLoader.load([
'/assets/3d.js/textures/environmentMaps/4/px.jpg',
'/assets/3d.js/textures/environmentMaps/4/nx.jpg',
'/assets/3d.js/textures/environmentMaps/4/py.jpg',
'/assets/3d.js/textures/environmentMaps/4/ny.jpg',
'/assets/3d.js/textures/environmentMaps/4/pz.jpg',
'/assets/3d.js/textures/environmentMaps/4/nz.jpg'
]);
environmentMap.encoding = THREE.sRGBEncoding;
scene.background = environmentMap;
scene.environment = environmentMap;
debugObject.envMapIntensity = 0.001;
}
LoadModels Fn:
loadModels(scene) {
const gltfLoader = new GLTFLoader();
const updateAllMaterials = () => {
scene.traverse(child => {
if (child instanceof THREE.Mesh && child.material instanceof THREE.MeshStandardMaterial) {
child.material.needsUpdate = true;
child.castShadow = true;
child.receiveShadow = true;
console.log(child.material);
}
});
};
gltfLoader.load('/assets/3d.js/models/Tank.glb', gltf => {
console.log(gltf);
gltf.scene.scale.set(1, 1, 1);
gltf.scene.position.set(-4, 0, 0);
gltf.scene.rotation.y = Math.PI * 0.5;
scene.add(gltf.scene);
updateAllMaterials();
});
}
I’ve commented all other lines which I think are not necessary.