【问题标题】:How to tween/animate fog three.js如何补间/动画雾three.js
【发布时间】:2021-04-09 17:59:17
【问题描述】:

所以我试图通过补间来改变雾密度,这是可能的,因为它似乎没有改变这是我的默认值:

var camera, densityFog, colorFog2;
                    colorFog2 = 0xfee2ed;
                    densityFog = 0.25;
                    scene.fog = new THREE.FogExp2(colorFog2, densityFog);

这是我使用库 GSAP 和 tweenjs 尝试过的:

tween = new TWEEN.Tween(scene.fog)
                .to({densityFog: 0.02}, 1000 )
                .easing(TWEEN.Easing.Exponential.InOut)
                .onComplete(function() { }).start();
                    
                gsap.to(scene.fog, {
                        duration: 2,
                        densityFog: 0.02,
                        onUpdate: function () {
                            controls.update();
                            isZoomed = 0;
                            controls.enabled = false;
                            
                        },
                    });

谁能指出我正确的方向?

【问题讨论】:

    标签: javascript three.js gsap tween.js


    【解决方案1】:

    这个答案使用gsap

    使用一个对象,例如。 myfog = { value: .5 } 并将其 value 属性补间为您想要的。

    然后在onUpdate 中,将scene.fog 设置为new THREE.FogExp2,并将当前myfog.value 作为参数。

    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    camera.position.z = 5;
    camera.position.y = 2;
    
    // Init the object and fog here
    var myfog = { value: .5 }
    scene.fog = new THREE.FogExp2(0x00ff00, myfog.value);
    
    var geometry = new THREE.BoxGeometry(1, 1, 5);
    var mat = new THREE.MeshBasicMaterial({
      color: 0xff0000
    });
    var mesh = new THREE.Mesh(geometry, mat);
    
    scene.add(mesh);
    
    var renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setClearColor(0x00ff00);
    document.body.appendChild(renderer.domElement);
    
    function render() {
      renderer.render(scene, camera);
    }
    
    function animate() {
      requestAnimationFrame(animate);
      render();
    }
    
    animate();
    
    // This animates the fog
    gsap.to(myfog, {
      duration: 2,
      value: 0.002, // The end value
      onUpdate: function() {
        // New fog with current myfog value
        scene.fog = new THREE.FogExp2(0x00ff00, myfog.value);
      },
      // These just infinitely repeat the animation
      yoyo: true,
      repeat: -1,
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.6.1/gsap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r127/three.min.js"></script>

    【讨论】:

    • 感谢指出如何使用无限循环
    猜你喜欢
    • 2013-11-23
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 2019-09-26
    相关资源
    最近更新 更多