THREEJS:如何在单独的DIV中渲染不同的模型?

时间:2019-01-04 21:29:40

标签: javascript three.js rendering render

(我是Three.js的新手)


说明

我正在为多个stl模型制作3D Viewer。每个模型都应在单独的div中渲染,因此您将获得一个具有不同模型的网格。 文件名存储在json文件中。通过for循环,我遍历json并将文件名存储在“ var filename”中。我使用该变量来完成模型的路径。


详细信息

var filename = data[i].Filename;

通过for循环,我可以从json文件中获取所有文件名。

loader.load('model/' + filename, function(geometry) {...

然后我添加文件名以完成模型路径。

modelContainer = document.createElement('div');
modelContainer.className = modelClass;
document.getElementById(modelId).appendChild(modelContainer);

具有类名的div将在具有给定ID的div中创建。并且应该将模型渲染到div(带有类的div)内的画布中。


问题

See image 现在发生的是,代码将所有模型(本示例中为3个模型)放入最后一个div。用于模型1和2的前两个div是黑色。

经过大量实验和分析,我仍然找不到解决方案。 也许是我必须将所有场景存储在一个数组中,然后将所有场景循环到render函数中?


完整的HTML和Javascript

您可以在此处找到完整的项目代码(无.stl模型): https://codepen.io/indoguy/project/editor/XqNKGQ

<div class="container">
    <div class="row" id="modelGrid">
        <!-- The models will be uploaded within this grid -->
    </div>
</div>

<script src="js/three.js"></script>
<script src="js/STLLoader.js"></script>
<script src="js/WebGL.js"></script>

<script>
    var modelDiv = document.getElementById('modelGrid');
    var ourRequest = new XMLHttpRequest();

    // In this code snippit I will read the model filenames out of the json.
    // Add URL where the json is stored
    ourRequest.open('GET', 'http://localhost:8888/3D%20Modeling/3D%20Previewer/3D%20Previewer%20Library/models.json');
    ourRequest.onload = function() {
        var queueData = JSON.parse(ourRequest.responseText);
        renderHTML(queueData);
    };
    ourRequest.send();

    function renderHTML(data) {
        // 3D Model Process
        if (WEBGL.isWebGLAvailable() === false) {
            document.body.appendChild(WEBGL.getWebGLErrorMessage());
        }

        // Editable
        var divWidth = 300;
        var divHeight = 300;
        var modelId = 'modelGrid';
        var modelClass = 'col-sm';

        // Don't change
        var modelContainer;
        var camera, cameraTarget, scene, renderer;
        var cameraFov = 45;


        init(data);
        animate();


        // init
        function init(data) {

            // For loop for every model in the json file
            for (i = 0; i < data.length; i++) {

                var filename = data[i].Filename;

                // Scene
                scene = new THREE.Scene();
                scene.background = new THREE.Color(0xffe5e5);

                // Camera
                camera = new THREE.PerspectiveCamera(cameraFov, divWidth / divHeight, 1, 15);
                cameraTarget = new THREE.Vector3(0, -0.25, 0);

                // Creates a div with the name of the value of var className, inside modelGrid id
                modelContainer = document.createElement('div');
                modelContainer.className = modelClass; // Add class to the div
                document.getElementById(modelId).appendChild(modelContainer);

                // STL Loader
                var loader = new THREE.STLLoader();
                loader.load('model/' + filename, function(geometry) {
                    var material = new THREE.MeshStandardMaterial({
                        color: 0xff1919,
                        roughness: 0.5,
                        metalness: 0
                    });
                    var mesh = new THREE.Mesh(geometry, material);
                    mesh.position.set(0, -0.5, 0); // Position with the Y value
                    mesh.rotation.set(0, -Math.PI / 2, 0);
                    mesh.scale.set(0.15, 0.15, 0.15);
                    scene.add(mesh); // add model to the scene
                });

                // Lights
                scene.add(new THREE.HemisphereLight(0xaaaaaa, 0x444444));
                var light = new THREE.DirectionalLight(0xffffff, 0.5);
                light.position.set(1, 1, 1);
                scene.add(light);

                // renderer
                renderer = new THREE.WebGLRenderer({
                    antialias: true
                });
                renderer.setClearColor(0xffffff, 1);
                renderer.setPixelRatio(window.devicePixelRatio);
                renderer.setSize(divWidth, divHeight);
                // renderer.setSize(window.innerWidth, window.innerHeight);
                renderer.gammaInput = true;
                renderer.gammaOutput = true;
                renderer.shadowMap.enabled = true;
                modelContainer.appendChild(renderer.domElement);
                // window.addEventListener('resize', onWindowResize, false);
            }
        }

        // animate
        function animate() {
            render();
            requestAnimationFrame(animate);
        }

        // render
        function render() {
            renderer.setClearColor(0xffffff);
            var timer = Date.now() * 0.0005;
            camera.position.x = Math.cos(timer) * 3;
            camera.position.z = Math.sin(timer) * 3;
            camera.lookAt(cameraTarget);
            renderer.render(scene, camera);
        }
    };
</script>

1 个答案:

答案 0 :(得分:2)

问题是您只保存和渲染一个three.js渲染器。在您的for循环中,此行覆盖了renderer函数中引用的全局render()变量:

renderer = new THREE.WebGLRenderer({
    antialias: true
});

有几种方法可以解决此问题,包括将渲染器和相机添加到数组中,并在单个渲染函数中对其进行迭代,或者为每个渲染器创建新的animate()循环,等等。这是一个粗略的示例第二种选择:

// For loop for every model in the json file
for (i = 0; i < data.length; i++) {

    var filename = data[i].Filename;

    // EDIT Fix3: These variables must be defined inside of the for loop 
    // scope otherwise they will be shared by all loop iterations
    var modelContainer;
    var camera, cameraTarget, scene;

    // ...

    // Fix 1: Add var declaration so the variable is scoped to this
    // block and variables are not defined globally and overwritten on
    // subsequent iterations. This is needed for all the variables
    // in this block (scene, camera, etc)
    // renderer
    var renderer = new THREE.WebGLRenderer({
        antialias: true
    });

    // ...

    // Fix 2: Create an animation loop for each renderer instead
    // of a single global one
    // animate
    function animate() {
        render();
        requestAnimationFrame(animate);
    }

    // render
    function render() {
        renderer.setClearColor(0xffffff);
        var timer = Date.now() * 0.0005;
        camera.position.x = Math.cos(timer) * 3;
        camera.position.z = Math.sin(timer) * 3;
        camera.lookAt(cameraTarget);
        renderer.render(scene, camera);
    }

    animate();    

}

肯定有更清洁的方法,但是希望这可以帮助您入门!让我知道是否可行,或者您需要澄清。

编辑:我刚刚注意到您的图像显示了所有三个模型彼此重叠显示。这再次是因为scene变量是全局声明的,并且在异步load函数被激发时,它被设置为最终的场景对象。