admin管理员组文章数量:1356271
Can we change the dimension "width/height/length" of 3D Cube created with Three.js at runtime?
Like I got a Fiddle in SO which changes Color of Cube at run time : /
Similarly can we change the Dimension??
Here is my code:
HTML
<script src=".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();
Here is the Fiddle for the same! /
Let me know if you need any other information.
Please suggest.
Can we change the dimension "width/height/length" of 3D Cube created with Three.js at runtime?
Like I got a Fiddle in SO which changes Color of Cube at run time : http://jsfiddle/mpXrv/1/
Similarly can we change the Dimension??
Here is my code:
HTML
<script src="http://www.html5canvastutorials./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();
Here is the Fiddle for the same! http://jsfiddle/EtSf3/1/
Let me know if you need any other information.
Please suggest.
Share Improve this question edited Feb 14, 2014 at 18:21 UID asked Feb 14, 2014 at 16:05 UIDUID 4,49411 gold badges47 silver badges76 bronze badges2 Answers
Reset to default 6Solution 1 (better)
You must use the scale mesh property to increase (or decrease) the dimensions of your object.
http://jsfiddle/EtSf3/4/
First, You need to set your cube
variable to the global scope.
I've replaced the dimensions of your cube to 1, 1, 1.
cube = new THREE.Mesh(new THREE.CubeGeometry(1, 1, 1), new THREE.MeshLambertMaterial({
color: '#cccccc'
}));
Attach an even on the 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;
};
Solution 2
An other solution will be to delete your object and create a new mesh with the given values.
What you want is Mesh.scale (cube.scale in you example).
http://jsfiddle/EtSf3/3/
This is the code that I added
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;
});
本文标签: javascriptChange widthheightlength of 3D Cube created with Threejs at runtimeStack Overflow
版权声明:本文标题:javascript - Change widthheightlength of 3D Cube created with Three.js at runtime - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744017973a2576685.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论