babylonjs 动画特效(一)

babylonjs 动画特效

使用babylonjs的animation 动画来制作,方框围绕方形物体做心跳在这里插入图片描述

show me the code

<HTML>
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>

<body>

    <canvas width="1280" height="720"></canvas>
</body>    

<script>
    var canvas = document.querySelector("canvas");
    var engine = new BABYLON.Engine(canvas, true);
    
    
    var delayCreateScene = function () {
    var scene = new BABYLON.Scene(engine);

    var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(0, 100, 100), scene);
    var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 100, new BABYLON.Vector3.Zero(), scene);
    camera.attachControl(canvas, true);

    var box1 = BABYLON.Mesh.CreateBox("Box1", 3.0, scene);

    var materialBox = new BABYLON.StandardMaterial("mat", scene);
    materialBox.diffuseColor = new BABYLON.Color3(1, 0, 0);

    box1.material = materialBox;

    //Create a scaling animation at 30 FPS
    var animationBox = new BABYLON.Animation("tutoAnimation", "scaling.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
                                                                    BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
    // Animation keys
    var keys = [];
    //At the animation key 0, the value of scaling is "1"
    keys.push({
        frame: 0,
        value: 1
    });

    //At the animation key 20, the value of scaling is "0.2"
    keys.push({
        frame: 20,
        value: 0.2
    });

    //At the animation key 100, the value of scaling is "1"
    keys.push({
        frame: 100,
        value: 1
    });

    animationBox.setKeys(keys);

    box1.animations.push(animationBox);

    setTimeout(async () => {
        var anim = scene.beginAnimation(box1, 0, 100, false);

        console.log("before");
        await anim.waitAsync();
        console.log("after");
    });

    return scene;
}

var scene = delayCreateScene();


var points = [];
var linesArr = [];
var hl;
var LineColor = new BABYLON.Color3(1, 1, 0)
function create() {
    
        linesArr.length = 0;
        points.push(new BABYLON.Vector3(5, 0, 5));
        points.push(new BABYLON.Vector3(5, 0, -5));
        points.push(new BABYLON.Vector3(-5, 0, -5));
        points.push(new BABYLON.Vector3(-5, 0, 5));
        points.push(points[0]); 
        hl = new BABYLON.HighlightLayer("hl1", scene, true);
        hl.blurVerticalSize = 5; //模糊
        hl.blurHorizontalSize = 5;
    
        for (let i = 0; i < 1; i++) {
            let LineMesh = BABYLON.MeshBuilder.CreateLines("rect" + i, { points: points }, scene);
            linesArr.push(LineMesh);
            hl.addMesh(LineMesh, this.LineColor);
        }

    };
    function startAnimation() {
        const frameRate = 10;
        const xSlide = new BABYLON.Animation("xSlide", "position.y", frameRate, 
        BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
        const keyFrames = [];
        keyFrames.push({
            frame: 0,
            value: 0
        });
        keyFrames.push({
            frame: 1 * frameRate,
            value: 5
        });
        keyFrames.push({
            frame: 2 * frameRate,
            value: 10        });
        xSlide.setKeys(keyFrames);
        linesArr.forEach((item, index) => {
            console.log(index)
            item.animations.push(xSlide);
            this.scene.beginAnimation(item, 0, (index + 1.5) * 3, true);
        })

    };

    create();
    startAnimation();
engine.runRenderLoop(function() {
    scene.render();
});

window.addEventListener("resize", function() {
    engine.resize();
})

</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qianbo_insist

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值