三j和下拉列表:如何仅显示选定的模型?
我想仅显示下拉选项中的选定值(在这种情况下为3D模型)。相机应只能看到一个。这是我坚持的代码部分:
const loader = new GLTFLoader().setPath('models/gltf/modeldatabase/');
for (let i = 0; i < prefabcontainer.length; i++) {
let prefabResource = prefabcontainer[i];
loader.load(prefabResource, function (gltf) {
object = gltf.scene;
object.scale.set(5, 5, 5);
var select = document.getElementById("selectPrefab");
prefabResource = prefabResource.replace(/\..+$/, '');
var prefabElement = document.createElement("option");
prefabElement.textContent = prefabResource;
prefabElement.value = prefabResource;
select.appendChild(prefabElement);
scene.add(object);
render();
}, undefined, function (error) {
console.error(error);
})
}
PrefabContainer只是一个简单的JS脚本:
let prefabcontainer = [
'shotgun.gltf',
'pistol.gltf'];
export{prefabcontainer};
这是HTML部分:
<div><select id="selectPrefab">
感谢您的帮助,谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在循环的
之外,声明一个数组以保持加载对象。还可以创建一个
document fragment
在生成&gt;
时保持它们。这样,一旦生成了所有&gt;
s,就只会对DOM进行一个更改。还将的声明选择
移至此范围。更改设置
&lt; option&gt;
的值的行,以便使用索引,并在第一个defaultSelected
属性设置第一个&lt;
promelt /代码>。为此,使用 [MDN] 。声明用于更改
可见
属性的函数。将加载的对象添加到上面声明的数组中。在第二个和后续的对象上,将
可见
属性设置为false。在此处查找第一个对象的条件下,将调用呼叫Render()
。最后,在循环的
之外,将生成的
&gt;
的片段添加到&lt; select&gt;
中。通过切换可见的
预制状态,添加一个事件侦听器以在下拉栏中处理选择。Outside the
for
loop, declare an array to hold the loaded objects. Also create aDocumentFragment
to hold the<options>
while you're generating them; this way, you're only making one change to the DOM, once all the<option>
s have been generated. Also move the declaration ofselect
to this scope.Change the line that sets the
<option>
's value so that it uses the index, and set thedefaultSelected
property on the first<option>
. For this, use the Option(text, value, defaultSelected) constructor[MDN].Declare a function for changing the
visible
property on a Three.jsObject3D
and its children.Add the loaded object to the array declared above. On the second and subsequent objects, set the
visible
property to false. Wrap the call torender()
here in a conditional looking for the first object.Finally, outside the
for
loop, add the generated<option>
s fragment to the<select>
. Add an event listener to handle selections in the drop-down by toggling thevisible
state of the prefabs.