Why overlap when learning glf external model loading?

I am from Taiwan, i’m taking this course.

I followed the instructions above to successfully load the external model of the bird.
But I encountered the problem of overlapping models.I tried to adjust the far and near values of the camera, but they always overlap.

Birds overlap…0.0

my code

// these need to be accessed inside more than one function so we'll declare them first
let container;
let camera;
let controls;
let renderer;
let scene;

//three.js動畫系統負責將動畫附加到模型
const mixers = [];
//時間 幀速
const clock = new THREE.Clock();

function init() {

  container = document.querySelector( '#scene-container' );

  //創造場景
  scene = new THREE.Scene();

  //背景顏色
  scene.background = new THREE.Color( 0x222222 );

  //精簡後的函數
  createCamera();
  createControls();
  createLights();
  loadModels();
  createRenderer();

  renderer.setAnimationLoop( () => {

    update();
    render();

  } );

}

//相機
function createCamera() {

  camera = new THREE.PerspectiveCamera( 30, container.clientWidth / container.clientHeight, 0.1, 100 );
  camera.position.set( -1.5, 1.5, 6.5 );

}

//控制器
function createControls() {

  controls = new THREE.OrbitControls( camera, container );

}

//燈光
function createLights() {

  //半球光源
  const ambientLight = new THREE.HemisphereLight( 0xddeeff, 0x0f0e0d, 5 );

  //主光源
  const mainLight = new THREE.DirectionalLight( 0xffffff, 5 );
  mainLight.position.set( 10, 10, 10 );

  scene.add( ambientLight, mainLight );

}

//模型
function loadModels() {

  const loader = new THREE.GLTFLoader();

  // 重複使用的函數來設定模型
  // 可分將多個模型別放在場景中
  const onLoad = ( gltf, position ) => {

    const model = gltf.scene.children[ 0 ];
    model.position.copy( position );

    //模型動畫分割
    const animation = gltf.animations[ 0 ];
    
    //為每個模型創建動畫混合器
    const mixer = new THREE.AnimationMixer( model );
    mixers.push( mixer );

    //可控制影片播放 停止 暫停
    const action = mixer.clipAction( animation );
    action.play();

    scene.add( model );

  };

  // 模型載入進度通知
  const onProgress = () => {};

  // 模型載入錯誤訊息
  const onError = ( errorMessage ) => { console.log( errorMessage ); };

  //Vector3 模型位置x, y, z

  const parrotPosition = new THREE.Vector3( 0, 0, 2.5 );
  loader.load( 'models/Parrot.glb', gltf => onLoad( gltf, parrotPosition ), onProgress, onError );

  const flamingoPosition = new THREE.Vector3( 7.5, 0, -10 );
  loader.load( 'models/Flamingo.glb', gltf => onLoad( gltf, flamingoPosition ), onProgress, onError );

  const storkPosition = new THREE.Vector3( 0, -2.5, -10 );
  loader.load( 'models/Stork.glb', gltf => onLoad( gltf, storkPosition ), onProgress, onError );

}

//渲染器
function createRenderer() {

  // create a WebGLRenderer and set its width and height
  renderer = new THREE.WebGLRenderer( { antialias: true } );
  renderer.setSize( container.clientWidth, container.clientHeight );

  renderer.setPixelRatio( window.devicePixelRatio );

  renderer.gammaFactor = 2.2;
  renderer.gammaOutput = true;

  renderer.physicallyCorrectLights = true;

  container.appendChild( renderer.domElement );

}

function update() {

  //動畫更新
  const delta = clock.getDelta();

  for ( const mixer of mixers ) {

    mixer.update( delta );

  }

}

function render() {

  renderer.render( scene, camera );

}

//響應式
function onWindowResize() {
  // console.log( '你調整瀏覽器大小' );

  camera.aspect = container.clientWidth / container.clientHeight;

  // update the camera's frustum
  camera.updateProjectionMatrix();

  renderer.setSize( container.clientWidth, container.clientHeight );

}

window.addEventListener( 'resize', onWindowResize );

init();

Can you modify the following live example to demonstrate your issue? It seems your posted code looks correct so there might be an issue elsewhere.

The values of the near and far plane are unrelated to this issue. According to your screenshot, it just seems the models are not transformed correctly.

1 Like

@Mugen87 Thank you for your reply, I tried to upload my code part as you said, it is still normal, but the execution of the bird on the local end is overlapping

my uploaded code:

Local screenshot

Strange :thinking:. Are you maybe using outdated library files on your computer?

Consider to share your entire code as a github repo. That might it easier to reproduce the issue.

1 Like

Hey, this code works perfectly, it handles position of models

let numLoadedModels=0,numLevels = 0;
const LodedModels = [];
const manager = new THREE.LoadingManager();
manager.onLoad = () => {

    // start something

  }
const MODELS = [
  {name:"Level"},
  {name:"Player"},
  {name:"Gun"},
];

function loadGltfModel( model, onLoaded ) {

    let loader = new THREE.GLTFLoader(manager);

    let modelName = "../models/" + model.name + ".glb";

    loader.load( modelName, function ( gltf ) {

        let scene = gltf.scene;

        model.animations = gltf.animations;

        model.scene = scene;

        onLoaded();

    });

  }

  

  function getModelByName( name ) {

    for ( var i = 0; i < MODELS.length; ++ i ) {

        if ( MODELS[ i ].name === name ) {

            return MODELS[ i ];

        }

    }

    return null;

  }

  

  function instantiateUnits() {

    for ( var i = 0; i < MODELS.length; ++ i ) {

        var u = MODELS[ i ];

        var models = getModelByName( u.name );

        if ( models ) {

          if(models.name === "Level"){

            models.scene.children.forEach((model) => {     

              let mesh = new THREE.Mesh( model.geometry, model.material );  

             mesh.name = model.name;

              mesh.scale.copy( model.scale );

              mesh.quaternion.copy( model.quaternion );

              mesh.position.copy( model.position );

                 mesh.receiveShadow = true;

                if(model.name !=="Ground"){

                  mesh.castShadow = true;

                }

               
    

                 scene.add( mesh );

   
            });

        }

    }

  };

  function loadModels() {

    for ( var i = 0; i < MODELS.length; ++ i ) {

        var m = MODELS[ i ];

        loadGltfModel( m, function () {     

            ++ numLoadedModels;

            if ( numLoadedModels === MODELS.length ) {

                instantiateUnits()

            }

        } );


    }

  }
1 Like

This is mine github:

Please help me look at it again, Thanks.

I can solve the issue by just using the models (Flamingo.glb, Parrot.glb and Stork.glb) from the original codesandbox live example.

BTW: When you making tutorials, it’s highly recommended to use the same revision of three.js as well as the same assets. The original codesandbox uses R102, but your code uses R115dev. It’s not surprising when things might look different.

Thank you for your reply. I finally solved the problem of the model itself …, I downloaded the compressed version of the model may be broken.