[英]Change width/height/length of 3D Cube created with Three.js at runtime
我們可以在運行時更改使用Three.js創建的3D立方體的尺寸“寬度/高度/長度”嗎?
就像我在SO中有一個小提琴,它在運行時改變了立方體的顏色: http : //jsfiddle.net/mpXrv/1/
同樣可以改變維度嗎?
這是我的代碼:
HTML
<script src="http://www.html5canvastutorials.com/libraries/three.min.js"></script>
<div id="container"></div>
<div class="inputRow clear" id="dimensionsNotRound" data-role="tooltip">
<label class="grid-8">Dimensions (mm):</label>
<br/>
<br/>
<div> <span>Length</span>
<input class="numeric-textbox" type="text" value="">
<br/>
<br/>
</div>
<div> <span>Width</span>
<input class="numeric-textbox" type="text" value="">
<br/>
<br/>
</div>
<div> <span>Height</span>
<input class="numeric-textbox" type="text" value="">
<br/>
<br/>
</div>
<button id="btn">Click me to change the Dimensions</button>
JS
//Script for 3D Box
// revolutions per second
var angularSpeed = 0.2;
var lastTime = 0;
// this function is executed on each animation frame
function animate() {
// update
var time = (new Date()).getTime();
var timeDiff = time - lastTime;
var angleChange = angularSpeed * timeDiff * 2 * Math.PI / 1000;
cube.rotation.y += angleChange;
lastTime = time;
// render
renderer.render(scene, camera);
// request new frame
requestAnimationFrame(function () {
animate();
});
}
// renderer
var container = document.getElementById("container");
var renderer = new THREE.WebGLRenderer();
renderer.setSize(container.offsetWidth, container.offsetHeight);
container.appendChild(renderer.domElement);
// camera
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 700;
// scene
var scene = new THREE.Scene();
// cube
var cube = new THREE.Mesh(new THREE.CubeGeometry(400, 100, 200), new THREE.MeshLambertMaterial({
color: '#cccccc'
}));
cube.overdraw = true;
cube.rotation.x = Math.PI * 0.1;
cube.rotation.y = Math.PI * 0.3;
scene.add(cube);
// add subtle ambient lighting
var ambientLight = new THREE.AmbientLight(0x888888);
scene.add(ambientLight);
// directional lighting
var directionalLight = new THREE.DirectionalLight(0x666666);
directionalLight.position.set(1, 1, 1).normalize();
scene.add(directionalLight);
// start animation
animate();
這是同樣的小提琴! http://jsfiddle.net/EtSf3/1/
如果您需要任何其他信息,請與我們聯系。
請建議。
解決方案1(更好)
必須使用縮放網格屬性來增加(或減少)對象的尺寸。
首先,您需要將cube
變量設置為全局范圍。
我已經將立方體的尺寸替換為1,1,1。
cube = new THREE.Mesh(new THREE.CubeGeometry(1, 1, 1), new THREE.MeshLambertMaterial({
color: '#cccccc'
}));
在btn上附上偶數
var $ = function(id) { return document.getElementById(id); };
$('btn').onclick = function() {
var width = parseInt($('inp-width').value),
height = parseInt($('inp-height').value),
length = parseInt($('inp-length').value);
cube.scale.x = width;
cube.scale.y = height;
cube.scale.z = length;
};
解決方案2
另一種解決方案是刪除對象並使用給定值創建新網格。
你想要的是Mesh.scale(在你的例子中是cube.scale)。
這是我添加的代碼
document.getElementById('btn').addEventListener('click', function(e) {
var inputs = document.getElementsByClassName('numeric-textbox');
cube.scale.x = parseFloat(inputs[0].value) || cube.scale.x;
cube.scale.z = parseFloat(inputs[1].value) || cube.scale.z;
cube.scale.y = parseFloat(inputs[2].value) || cube.scale.y;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.