【问题标题】:Change width/height/length of 3D Cube created with Three.js at runtime在运行时更改使用 Three.js 创建的 3D Cube 的宽度/高度/长度
【发布时间】:2014-03-14 02:04:45
【问题描述】:

我们可以在运行时更改使用 Three.js 创建的 3D Cube 的尺寸“宽/高/长”吗?

就像我在 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/

如果您需要任何其他信息,请告诉我。

请提出建议。

【问题讨论】:

    标签: javascript jquery three.js


    【解决方案1】:

    解决方案 1(更好)

    您必须使用缩放网格属性来增加(或减少)对象的尺寸。

    http://jsfiddle.net/EtSf3/4/

    首先,您需要将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

    另一种解决方案是删除您的对象并使用给定值创建一个新网格。

    【讨论】:

    • 你这个摇滚人!!!我认为解决方案 1 对我有用!!看起来简单、容易、快速...非常感谢!!!
    • 在小提琴中它工作正常!!!...但是当我将更改合并到我的代码中时,它会抛出错误“无法调用null的方法'更改'”...知道为什么吗? ?
    • 嗨,我已经在你的回答中添加了我的解决方案......请查看并告诉我这是否是正确的方法。如果是,为什么它在 Fiddle 中不起作用?
    • 你也可以看看这个问题吗? stackoverflow.com/questions/21788385/…
    【解决方案2】:

    你想要的是 Mesh.scale(在你的例子中是 cube.scale)。

    http://jsfiddle.net/EtSf3/3/

    这是我添加的代码

    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;
    });
    

    【讨论】:

    • 当我更改尺寸并单击按钮时,小提琴停止工作......我做错了吗??
    • 我在控制台收到此错误:加载资源失败:服务器响应状态为 500(内部服务器错误)jobs.jsfiddle.net/…THREE.WebGLRenderer THREE.WebGLRenderer 57 three.min.js:386
    • 感谢收看!!!欣赏它!虽然不知道为什么它会抛出错误......但是“Paul Rad-Dupuy's”解决方案对我有用......再次感谢
    猜你喜欢
    • 2014-03-12
    • 2015-03-14
    • 2014-03-12
    • 1970-01-01
    • 2017-05-17
    • 2014-07-14
    • 1970-01-01
    • 2017-02-26
    • 2013-02-01
    相关资源
    最近更新 更多