gpt4 book ai didi

three.js - 从场景中移除对象

转载 作者:行者123 更新时间:2023-12-04 08:00:25 25 4
gpt4 key购买 nike

我的问题是我有按钮:启用 OrbitControls 的“ View ”按钮,禁用 OrbitControls 的“移动”按钮,以便我可以使用 DragControls,“立方体”按钮将一个立方体(或多个立方体)添加到场景中,一切正常,但是当我添加了一个“删除”按钮以便我可以删除多维数据集时,它不起作用,它说未定义多维数据集。所以我该怎么做 ? `

//variables : 
var objects = [];

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

//camera
var camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 1000;



//renderer
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );




//adding light
scene.add( new THREE.AmbientLight( 0x0f0f0f ) );
var light = new THREE.SpotLight( 0xffffff, 1 );
light.position.set( 0, 500, 2000 );
scene.add(light);





//controls

const orbitControls = new THREE.OrbitControls( camera, renderer.domElement );
const dragControls = new THREE.DragControls( objects, camera, renderer.domElement );


//fix the window resize problem
window.addEventListener('resize', function(){
renderer.setSize(window.innerWidth,window.innerHeight) ;
camera.aspect = window.innerWidth / window.innerHeight ;
camera.updateProjectionMatrix();
}) ;






//animate
function animate() {
requestAnimationFrame( animate );
renderer.render(scene, camera);
};

//cloning
function createCube () {

var geometry = new THREE.BoxGeometry( 120, 120, 120 );
var cube = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0x00004B } ) );
scene.add( cube );
objects.push( cube );
cube.position.x = 0;
cube.position.y = -30;

}



function createRockCube() {
var texture = new THREE.TextureLoader().load( 'rock.jpg' );
var rock_geometry = new THREE.BoxGeometry( 120, 120, 120 );
var rock_material = new THREE.MeshBasicMaterial( {map: texture} );
var rock_cube = new THREE.Mesh( rock_geometry, rock_material );
scene.add( rock_cube );
objects.push( rock_cube );
rock_cube.position.x = -200;
rock_cube.position.y = -30;
}


function createRobot() {
var objLoader = new THREE.OBJLoader();
objLoader.setPath('/examples/3d-obj-loader/assets/') ;
objLoader.load('r2-d2.obj', function (object) {
object.position.x = 500 ;
object.position.y = -100 ;
object.traverse( ( o )=> {

if ( o.isMesh ) objects.push( o );

} );
scene.add(object);
}) ;
}

function createRobot2() {
var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath('/examples/3d-obj-loader/assets/') ;
mtlLoader.load('r2-d2.mtl', (materials)=> {
materials.preload();

var objLoader2 = new THREE.OBJLoader();
objLoader2.setMaterials(materials);
objLoader2.setPath('/examples/3d-obj-loader/assets/') ;
objLoader2.load('r2-d2.obj', (object2) =>{
object2.position.x = 300 ;
object2.position.y = -100 ;
object2.traverse( ( o ) => {

if ( o.isMesh ) objects.push( o );

} );
scene.add(object2);
});
})
}

function disableControl(){
orbitControls.enabled = false;
}

function enableControl(){
orbitControls.enabled = true;
}

function removeCube () {

cube.geometry.dispose();
cube.material.dispose();
scene.remove(cube);
}




animate();
body { 
margin: 0px;
background-color: rgb(240, 240, 240);
}


canvas {
width: 100%;
height: 100vh ;
position: absolute;
}

/* objects buttons */

#btn-cube {
background-color: rgb(0, 0, 75);
position: absolute;
left: 70px;
height: 50px;
width: 100px;
cursor: pointer;
}

#rock-btn img {
position: absolute;
top: 75px;
left: 70px;
height: 50px;
width: 100px;
cursor: pointer;
}


#robot-btn {
position: absolute;
left: 70px;
height: 50px;
width: 100px;
cursor: pointer;
}

#robot2-btn {
position: absolute;
top: 55%;
left: 70px;
height: 50px;
width: 100px;
cursor: pointer;
}

#move-btn {
position: absolute;
top: 90%;
left: 55%;
height: 50px;
width: 100px;
background-color: Transparent;
background-repeat:no-repeat;
border: none;
cursor:pointer;
overflow: hidden;
font-size: 20px;
font-family: cursive;
}
#view-btn {
position: absolute;
top: 90%;
left: 45%;
height: 50px;
width: 100px;
background-color: Transparent;
background-repeat:no-repeat;
border: none;
cursor:pointer;
overflow: hidden;
font-size: 20px;
font-family: cursive;
}

/*menu*/

nav {
width: 250px;
position: absolute;
top: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.1);
border-radius: 10px;
box-shadow: 5px 7px 10px rgba(0, 0, 0, 0.5);
transition: 0.3s ease;
}

nav ul {
position: relative;
top: 50px;
list-style-type: none;
}

nav ul li a {
display: flex;
align-items: center;
font-family: cursive;
font-size: 20px;
text-decoration: none;
text-transform: capitalize;
color:black;
padding: 10px 30px;
height: 50px;
transition: 0.5s ease;

}

nav ul li a:hover {
cursor: pointer;
color: #fff;
}

nav ul .cubes {
position: absolute;
left: 250px;
width: 200px;
top : 0;
display: none;
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
background: rgba(0, 0, 0, 0.1);
}

nav ul .robots {
position: absolute;
left: 250px;
width: 200px;
top : 80;
display: none;
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
background: rgba(0, 0, 0, 0.1);
}

nav ul span {
position: absolute;
right: 20px;
font-size: 20px;
}



nav ul .dropdown:hover ul{
display: initial;
}


nav ul .dropdown2:hover ul{
display: initial;
}

#open-btn {
display: none;
position: absolute;
top: 12px;
cursor: pointer;
font-size: 30px;
}

.close-btn {
position: absolute;
top: 10px;
right: 5px;
cursor: pointer;
font-size: 36px;
color: rgb(66, 64, 40);
}

#remove-btn {
position: absolute;
top: 90%;
left: 30%;
height: 50px;
width: 100px;
background-color: Transparent;
background-repeat:no-repeat;
border: none;
cursor:pointer;
overflow: hidden;
font-size: 20px;
font-family: cursive;
}
<html>
<head>
<title>Kitchen Planner Ama Mn Jumia </title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<script src="./three.min.js"></script>
<script src="./DragControls.js"></script>
<script src="./OrbitControls.js"></script>
<script src="./OBJLoader.js"></script>
<script src="./MTLLoader.js"></script>
<script src="./app.js"></script>
<script src="./menu.js"></script>



<nav id="menu" >
<div id="open-btn" onclick="openFunction()"> &#9776; </div>
<a href="#" class="close-btn" onclick="closeFunction()">&times;</a>
<ul>
<li id="dropdown" class="dropdown"><a href="#">Cubes <span>&rsaquo;</span></a>
<ul class="cubes">
<li><a href="#"><button id="btn-cube" onclick="createCube()"></button></a>
<li><a href="#"><button id="rock-btn" onclick="createRockCube()" ><img src="./rock.jpg"></button></a>
</ul>
</li>
<li id="dropdown2" class="dropdown2"><a href="#">Robots <span>&rsaquo;</span></a>
<ul class="robots">
<li><a href="#"><button id="robot-btn" onclick="createRobot()"> Robot </button></a>
<li><a href="#"><button id="robot2-btn" onclick="createRobot2()"> Robot 2 </button></a>
</ul>
</li>
</ul>

</nav>

<button id="move-btn" onclick="disableControl()">move</button>
<button id="view-btn" onclick="enableControl()"> view </button>
<button id="remove-btn" onclick="removeCube()"> remove </button>


</body>
</html>

`

最佳答案

您必须申报您的 cube createCube() 之外的变量.然后它在一个可以被 removeCube() 访问的范围内。 .

关于three.js - 从场景中移除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66505845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com