三个JS:CTM文件未加载

时间:2019-05-20 07:12:40

标签: three.js

在这里,我尝试使用CTM Loader在三个JS中加载.ctm文件。但是CTM文件没有被加载,并且对象在屏幕上不可见,而当我加载具有几何属性的js文件时,它将被加载并且对象在屏幕上可见。因此,当我尝试加载ctm文件时,该对象不可见,而当我加载具有几何属性的js文件时,该对象也将加载。

我已附上以下代码

var SCREEN_WIDTH = window.innerWidth;
var SCREEN_HEIGHT = window.innerHeight;
var FLOOR = -250;

var container;
var camera, scene, controls;
var renderer;
var mesh;
var textureCube;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

init();
animate();

function init() {

    container = document.createElement('div');
    document.body.appendChild(container);
    camera = new THREE.PerspectiveCamera(30, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000);
    camera.position.set(185, 40, 170);

    controls = new THREE.OrbitControls(camera);
    controls.maxPolarAngle = Math.PI / 2;
    controls.minDistance = 150;
    controls.maxDistance = 500;

    scene = new THREE.Scene();
    scene.background = textureCube;

    var light = new THREE.PointLight(0xffffff, 1);
    light.position.set(2, 5, 1);
    light.position.multiplyScalar(30);
    scene.add(light);

    var light = new THREE.PointLight(0xffffff, 0.75);
    light.position.set(-12, 4.6, 2.4);
    light.position.multiplyScalar(30);
    scene.add(light);

    scene.add(new THREE.AmbientLight(0x050505));

    renderer = new THREE.WebGLRenderer({ antialias: true });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
    renderer.domElement.style.position = "relative";

    container.appendChild(renderer.domElement);

    renderer.gammaInput = true;
    renderer.gammaOutput = true;

    window.addEventListener('resize', onWindowResize, false);
    window.addEventListener('mousemove', onDocumentMouseMove, false);

    var start = Date.now();

    var position = new THREE.Vector3(-105, -78, -30);
    var scale = new THREE.Vector3(30, 30, 30);

    var loader = new THREE.CTMLoader(true);
    // loader.loadParts("models/ctm/camaro/camaro.js", function (geometries, materials) {

    loader.load("models/ctm/camaro/camaro.ctm", function (geometries, materials) {

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

            var mesh = new THREE.Mesh(geometries[i], materials[i]);
            mesh.name = "camaro car"
            mesh.position.copy(position);
            mesh.scale.copy(scale);
            scene.add(mesh);

        }

        var end = Date.now();

        console.log("load time:", end - start, "ms");

    }, { useWorker: true });

}

function onWindowResize(event) {

    SCREEN_WIDTH = window.innerWidth;
    SCREEN_HEIGHT = window.innerHeight;
    renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
    camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
    camera.updateProjectionMatrix();

}


function onDocumentMouseMove(event) {

    mouseX = (event.clientX - windowHalfX);
    mouseY = (event.clientY - windowHalfY);

}

function animate() {

    requestAnimationFrame(animate);
    render();

}

function render() {
    renderer.render(scene, camera);
}

0 个答案:

没有答案