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

查看:33
本文介绍了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天全站免登陆