使用a-frame,我使用obj
加载<a-assets>
模型,其中包含大约114个人的网格物体。然后我想在该数组中找到一个特定的网格,然后在整个场景中我将clone()
多次。使用普通的旧的three.js我能够以最小的努力做到这一点,但我似乎无法在<a-entity>
结构中找到一种方法。我已经尝试了this.el.sceneEl.appendChild()
,但错误地说parameter 1 is not a node
;我也尝试了this.el.sceneEl.add()
,但错误时间为Trying to add an element that doesn't have an object3D
。有关解决这个问题的最佳方法的任何想法吗?
people.js
AFRAME.registerComponent('people', {
schema: {
samplePerson: {default: {}}
},
init: function () {
var el = this.el;
// Listen for when the model is loaded
el.sceneEl.addEventListener('model-loaded', this.loadPersonModel.bind(this));
},
loadPersonModel: function () {
// Update all meshes from the object3D which is made up with 114 child meshes
this.el.object3DMap.mesh.children.forEach(function (obj) {
obj.material.color = new THREE.Color(0xff0000);
obj.visible = false;
});
// Get a sample person for which we will instantiate all over the place
this.data.samplePerson = this.el.object3D.getObjectByName("people_silhouette73");
// Clone ten people throughout the scene
for (var i = 0; i < 10; i++) {
console.log(this.data.samplePerson);
if (this.data.samplePerson) {
var clone = this.data.samplePerson.clone();
clone.visible = true;
// Randomly position the object
clone.position.x += Math.random() * 10;
clone.position.y += 0.01;
clone.position.z = -300 + Math.random() * 25;
// Add the object to the scene
this.el.sceneEl.appendChild(clone);
}
}
}
});
的index.html
<a-scene>
<a-assets>
<a-asset-item id="people-obj" src="./assets/obj/silhouette_people_lowpoly_obj.obj"
name="testname"></a-asset-item>
</a-assets>
<a-entity obj-model="obj: #people-obj;" obj-name="person" people></a-entity>
</a-entity>
</a-scene>
答案 0 :(得分:3)
我会分两个步骤来解决这个问题。首先,加载模型并隐藏它(或理想地将其从场景中完全删除)。其次,有一个或多个额外的实体来提取他们需要的对象的一部分:
<a-entity obj-model="obj: #people-obj;" id="group" visible="false"></a-entity>
<a-entity position="0 0 0"
model-subset="target: #group;
name: person1;"></a-entity>
<a-entity position="3 0 0"
model-subset="target: #group;
name: person2;"></a-entity>
<a-entity position="6 0 0"
model-subset="target: #group;
name: person3;"></a-entity>
然后定义model-subset
组件:
AFRAME.registerComponent('model-subset', {
schema: {
target: {default: '', type: 'selector'},
name: {default: ''}
},
init: function () {
var el = this.el;
var data = this.data;
data.target.addEventListener('model-loaded', function (e) {
var model = e.detail.model;
var subset = model.getObjectByName(data.name);
el.setObject3D('mesh', subset.clone());
});
},
update: function () { /* ... */ },
remove: function () { /* ... */ }
});