Jump to content

Customized Easing function


basjak
 Share

Recommended Posts

HI guys:

am trying to use customized easing function but not seeing any difference whether it has been used or not. and the CircleEase() function does not see any change any behavior.

Thanks in advance.

 

    <canvas id="RenderTarget"></canvas>
    <script type="text/javascript">
        var Canvas = document.getElementById("RenderTarget");
        var Engine = new BABYLON.Engine(Canvas);
        var Scene = CreateScene();
        Engine.runRenderLoop(function () { Scene.render(); });
        window.addEventListener("resize", function () { Engine.resize(); });        

        function CreateScene() {
            var scene = new BABYLON.Scene(Engine);

            scene.clearColor = new BABYLON.Color3(125 / 255.0, 50 / 255.0, 0);

            var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
            var arc = new BABYLON.ArcRotateCamera("arc", 3 * Math.PI / 2, Math.PI / 8, 40, BABYLON.Vector3.Zero(), scene);
            arc.attachControl(Canvas, true);

            var mGreen = new BABYLON.StandardMaterial("green", scene);
            mGreen.diffuseColor = BABYLON.Color3.Green();
            mGreen.wireframe = true;

            var mRed = new BABYLON.StandardMaterial("red", scene);
            mRed.diffuseColor = BABYLON.Color3.Red();

            var Ground = BABYLON.Mesh.CreateTiledGround("Ground", -20, -20, 20, 20, { h: 8, w: 8 }, { h: 1, w: 1 }, scene);
            Ground.material = mGreen;

            var box = BABYLON.Mesh.CreateBox("box", 5, scene);
            box.material = mRed;

            var animationX = new BABYLON.Animation("animx", "position.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
            var animationZ = new BABYLON.Animation("animz", "position.z", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);

            var keysX = [{ frame: 0, value: -30 }, { frame: 100, value: 30 }, { frame: 200, value: -30 }];
            var keysY = [{ frame: 0, value: -30 }, { frame: 100, value: 30 }, { frame: 200, value: -30 }];

            animationX.setKeys(keysX);
            animationZ.setKeys(keysY);

            var easingFunctionX = new BABYLON.CircleEase();

            var FunnyEase = (function (_super) {
                __extends(FunnyEase, _super);
                function FunnyEase() {
                    _super.apply(this, arguments);
                }
                FunnyEase.prototype.easeInCore = function (gradient) {
                    // Here is the core method you should change to make your own Easing Function
                    // Gradient is the percent of value change
                    box.material = mGreen;

                    return Math.pow(Math.pow(gradient, 4), gradient)*1000;
                };
                return FunnyEase;
            })(BABYLON.EasingFunction).EASINGMODE_EASEINOUT;

            easingFunctionX.setEasingMode(FunnyEase);
            animationX.setEasingFunction(easingFunctionX);

            box.animations.push(animationX);
            box.animations.push(animationZ);

            var animatable = scene.beginAnimation(box, 0, 200, true);

            return scene;
        }
    </script>

 

Link to comment
Share on other sites

Sorry about the different name , it's the iPad keyboard auto correction :).

 

in the script i provided on the top, at line 51,  I declared the variable funnyEase as a function to control the circleEase(), but the circle ease is still behaving as funnyEase does not exist.

Link to comment
Share on other sites

I thought already but funnyEase result is a BABYLON,setEasingMode cast function (or at least, it looks like this to me).

it would be nice to have some working sample to start.

however, I loaded the attachment of the html code.

it's about a box movin on a plane (ground) circumference. I put an alert message inside FunnyEasy but no response at all.

 

HtmlPage.html

Link to comment
Share on other sites

@Deltakosh, @Temechon

you're right, I should have a play ground, I will make nice one tomorrow and put it in the server.

However, coming back to the Babylon.max.js, I managed to understand how the Ease functions is implemented.

so;

1- create funnyEase class (or it could be any variable name u choose).

2- create an object from gunnyEase, such as :

var ease = new funnyEase();

ease.setEasingMode(funnyEase.EASINGMODE_EASEINOUT);

3- set the easing function to the animation.

now, actually if the funnyEase class method returns gradient then, we should not expect any changes in the output so we can control other variables within the class that are independent from the output of the class method.

think the result would be the same as if we try to use complex animation. so we can embed a single ease function were a variable is controlled but the rest are independent but all in a single ease function.

Here is a sample for a box that rotates around a circle were x, z values have there own control functions embedded within the ease function that control the y position:

            var box = BABYLON.Mesh.CreateBox("box", 5, scene);
            //box.material = mRed;

            var animationX = new BABYLON.Animation("animx", "position.y", 30,
                BABYLON.Animation.ANIMATIONTYPE_FLOAT,
                BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
          
            var keysX = [{ frame: 0, value:   5 },  { frame: 1, value:  5 }];

            animationX.setKeys(keysX);

            var angle = 0;
            var FunnyEase = (function (_super) {
                __extends(FunnyEase, _super);
                function FunnyEase() {
                    _super.apply(this, arguments);
                }
                FunnyEase.prototype.easeInCore = function (gradient) {                   
                    box.position.x = 20 * Math.sin(angle);
                    box.position.z = 20 * Math.cos(angle);

                    angle = angle + 0.05;
                    if (angle >= 2 * Math.PI)
                        angle = 0;
             
                    return gradient;
                };
                return FunnyEase;
            })(BABYLON.EasingFunction);

            var ease = new FunnyEase();
            ease.setEasingMode(FunnyEase.EASINGMODE_EASEINOUT);

            animationX.setEasingFunction(ease);
          
            box.animations.push(animationX);

            var animatable = scene.beginAnimation(box, 0, 1, true);

 

 

Thanks guys for your time, at least I can out the solution in here until I make my own playGround. :)

 

Edited by basjak
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...