Three.js-一次加载JSON模型并多次添加 [英] Three.js - load JSON model once and add it multiple times

查看:242
本文介绍了Three.js-一次加载JSON模型并多次添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以一次加载一个JSON模型并将其以不同的比例,位置等多次添加到场景中?

Is it possible to load a JSON model once and add it to the scene multiple times with different scales, positions, etc?

如果我将Object3D()添加到数组中,给数组中的对象指定位置和比例,将其添加到场景中,然后重复此过程,则数组中每个对象的位置和比例都会被覆盖

If I add the Object3D() to an array, give a position and scale to the object in the array, add it to the scene, and then repeat this process, the position and scale are overwritten for every object in the array.

我想不出任何可行的方法,所以我希望有人能给我一个我要完成的工作示例.

I can't think of anything that works, so I'm hoping someone could give me a working example of what I'm trying to accomplish.

这是我失败的尝试之一.如果我的解释还不够的话,应该给你我要做什么的基本想法.

Here's (one of many) of my failed attempts. Should give you a basic idea of what I'm trying to do, if my explanation wasn't sufficient.

 function addModels(){

            var models = [];    

            var model = new THREE.Object3D();       
            var modelTex = THREE.ImageUtils.loadTexture( "textures/model.jpg" );
            var loader = new THREE.JSONLoader();
            loader.load( "models/model.js", function( geometry ) {
                mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { map: modelTex }) );
                model.add(mesh);
            } );

            for(var i = 0; i < 5; i++){ 
                model.scale.set(i,i,i);
                model.position.set(i,i,i);
                models[i] = model;

                scene.add(models[i]);
            }   

        }

推荐答案

您可以使用相同的几何形状和材质创建新的网格:

You can create new meshes from the same geometries and materials:

loader.load( "models/model.js", function( geometry ) {
        var mat = new THREE.MeshLambertMaterial( { map: modelTex });
        for (var i = 0; i < 5; i++) { 
            var mesh = new THREE.Mesh( geometry, mat );
            mesh.position.set(i, i, i);
            mesh.scale.set(i, i, i);
            scene.add(mesh);
        }
});

这篇关于Three.js-一次加载JSON模型并多次添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆