【问题标题】:Three.js Animate Scale of Multiple ObjectsThree.js 动画缩放多个对象
【发布时间】:2018-12-01 02:12:29
【问题描述】:

three.js这里是初学者!我正在学习几个教程来学习如何使用three.js 为 3D 对象制作动画,所以这里的部分代码看起来很熟悉。

我想做的事:找到一种方法在一段时间内(3 到 5 秒)循环更改每个对象的比例。理想情况下,这些对象的大小(可能还有形状)会发生变化。

我有什么

var Decoration = function() {

    // Run the Group constructor with the given arguments
    THREE.Group.apply(this, arguments);

    this.rotationSpeed = Math.random() * 0.01 + .001 + .001;
    this.rotationPosition = Math.random() * 0.01 + .001 + .001;
	this.scale = Math.random() * 0.21 + .051 + .021;
	
	this.rotation.x += 0.01;
    this.rotation.y += 0.02;
	this.scale.x += 1;  
    this.scale.y += 1;  
	this.scale.z = 1;  

    // A random color assignment
    var colors = ['#ff0051', '#f56762','#a53c6c','#f19fa0','#72bdbf','#47689b'];

    // The main bauble is an Octahedron
    var bauble = new THREE.Mesh(
        addNoise(new THREE.OctahedronGeometry(15,0), 0),
        new THREE.MeshStandardMaterial( {
            color: colors[Math.floor(Math.random()*colors.length)],
            shading: THREE.FlatShading ,
            metalness: 0,
            roughness: 0.8,
            refractionRatio: 0.25
    } )
    );
    bauble.castShadow = true;
    bauble.receiveShadow = true;
    bauble.rotateZ(Math.random()*Math.PI*2);
    bauble.rotateY(Math.random()*Math.PI*2);
    this.add(bauble);

};
Decoration.prototype = Object.create(THREE.Group.prototype);
Decoration.prototype.constructor = Decoration;
Decoration.prototype.updatePosition = function() {
    this.rotationPosition += this.rotationSpeed;
    this.rotation.y = (Math.sin(this.rotationPosition));
};

var clock = new THREE.Clock();


// Create a scene which will hold all our meshes to be rendered
var scene = new THREE.Scene();

// Create and position a camera
var camera = new THREE.PerspectiveCamera(
    60,                                   // Field of view
    window.innerWidth/window.innerHeight, // Aspect ratio
    0.1,                                  // Near clipping pane
    1000                                  // Far clipping pane
);

// Reposition the camera
camera.position.set(0,30,50);

// Point the camera at a given coordinate
camera.lookAt(new THREE.Vector3(0,15,0))

// Create a renderer
var renderer = new THREE.WebGLRenderer({ antialias: true });

// Size should be the same as the window
renderer.setSize( window.innerWidth, window.innerHeight );

// Set a near white clear color (default is black)
renderer.setClearColor( 0xfff6e6 );

// Append to the document
document.body.appendChild( renderer.domElement );


var decorations = [];

// Add some new instances of our decoration
var decoration1 = new Decoration();
decoration1.position.y += 10;
scene.add(decoration1);
decorations.push(decoration1);

var decoration2 = new Decoration();
decoration2.position.set(20,15,-10);
decoration2.scale.set(.8,.8,.8);
scene.add(decoration2);
decorations.push(decoration2);

var decoration3 = new Decoration();
decoration3.position.set(20,10,-10);
scene.add(decoration3);
decorations.push(decoration3);

var decoration4 = new Decoration();
decoration3.position.set(20,10,-10);
scene.add(decoration4);
decorations.push(decoration4);

// Render the scene/camera combnation
renderer.render(scene, camera);

requestAnimationFrame(render);

function render() {

    controls.update();
	
	var t = clock.getElapsedTime();
    
    if (t >= 3.0)
    {
        clock = new THREE.Clock();
        this.scale.set(1,1,1);
    }
    else
    {
	    this.scale.x = 1-(t/3.0);
    	this.scale.y = 1-(t/3.0);
		this.scale.z = 1-(t/3.0);   
    }

    // Update the decoration positions
    for(var d = 0; d < decorations.length; d++) {
        decorations[d].updatePosition();
    }

    // Render the scene/camera combnation
    renderer.render(scene, camera);

    // Repeat
    requestAnimationFrame(render);
}

这里的任何帮助将不胜感激!!谢谢你:)

【问题讨论】:

    标签: javascript animation three.js 3d


    【解决方案1】:

    您设置的任何decoration 元素的动画/渲染都发生在render 函数中。通过在render 函数中将您所追求的转换添加到decoration 元素,您可以将其应用于多个变量。

    试试这个:

    首先,声明你的时钟变量:

    var clock = new THREE.Clock();
    

    然后,将您的 render 函数更新为:

    function render() {
    
        for(var d = 0; d < decorations.length; d++) {
            decorations[d].rotation.x += 0.01;
            decorations[d].rotation.y += 0.01;
        }
    
        var t = clock.getElapsedTime();
    
        if (t >= 3.0)
        {
            clock = new THREE.Clock();
            // Update the decoration positions
            for(var d = 0; d < decorations.length; d++) {
                decorations[d].scale.set(1,1,1);
            }
        }
        else
        {
            // Update the decoration positions
            for(var d = 0; d < decorations.length; d++) {
                decorations[d].scale.x = 1-(t/3.0);
                decorations[d].scale.y = 1-(t/3.0);
                decorations[d].scale.z = 1-(t/3.0);
            }
        }
    
        // Render the scene/camera combnation
        renderer.render(scene, camera);
    
        // Repeat
        requestAnimationFrame(render);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-25
      • 2013-10-09
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      相关资源
      最近更新 更多