我們可以在運行時更改使用Three.js創建的3D立方體的尺寸「寬度/高度/長度」嗎?使用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將爲我工作!看起來簡單,容易和快速...非常感謝你! – UID
在小提琴中它的工作正常!!! ......但是當我在我的代碼中加入了更改時,它的拋出錯誤「無法調用方法」更改爲空「......任何想法爲什麼? – UID
嗨,我已經在您的答案中添加了我的解決方案...請檢查並讓我知道這是否是一種正確的方法..如果是的話,爲什麼它不在小提琴中工作? – UID