%% Interpolations
%
% This example demonstrates object properties interpolations.
% Properties of all objects in the scene can be gradually changed -
% interpolated from current value to a target value and then optionally
% back.
%
% Double click on any of the boxes in the scene to observe different
% interpolations:
%  Yellow box - interpolate to a new random position
%  Red box    - interpolate to a new random position in one coordinate only
%  Violet box - interpolate in combined motion to a new random position /
%               rotation, then back to the original position
%  Cyan box   - interpolate box scale to a new random value
%  Gray box   - interpolate color to a new random value, then back to the
%               original color

%   Copyright 1998-2021 HUMUSOFT s.r.o.

%% Initialize the 3D Foundation object
FW = F3D.World(mfilename);
initialize(FW, [0 -8 3]);

% Set the viewport properties
VP = FW.Viewports.Main;
VP.Title = mfilename;
Path = fileparts(which(mfilename));
setLogo(VP, fullfile(Path, 'data', 'MatlabLogo.png'));

Floor = F3D.Geometry.templateFloor(FW.Root, [10 10 0.1]);
Floor.TextureFile = fullfile(Path, 'data', 'RubberMat.png');
Floor.TextureMapping.Blend = 1/2;
Floor.Shininess = 0.8;

% Box with OnActivate callback set to interpolate its position (all coordinates together)
Box1 = addChild(FW.Root, 'Box1');
createShape(Box1, 'box', [1 1 1]);
Box1.Color = [1 0.5 0];
Box1.Location = [-4 0 0.5];
MultistepPath = [-4 4 0.5; 4 4 0.5; 4 -4 0.5; -4 -4 0.5; -4 0 0.5];
Box1.OnActivate = @(this, ~) interpolate(this, 'Location', MultistepPath, 'sweep', 2.0);

% Box with OnActivate callback set to interpolate its position (each coordinate sequentially)
Box2 = addChild(FW.Root, 'Box2');
createShape(Box2, 'box', [1 1 1]);
Box2.Color = [1 0 0];
Box2.Location = [-2 0 0.5];
Box2.OnActivate = @(this, ~) interpolate(this, 'Location(3)', 0.5 + rand*5, 'sweep');

% Box with OnActivate callback set to interpolate its combined motion at 60 FPS
Box3 = addChild(FW.Root, 'Box3');
createShape(Box3, 'box', [1 1 1]);
Box3.Color = [1 0.5 1];
Box3.Location = [0 0 0.5];
Box3.OnActivate = @(this, ~) interpMotion(this, [0 0 2.5], rand(1, 3)*2*pi, 'hill', 1.0, 60);

% Box with OnActivate callback set to interpolate its scale
Box4 = addChild(FW.Root, 'Box4');
createShape(Box4, 'box', [1 1 1]);
Box4.Color = [0 1 1];
Box4.Location = [2 0 0.5];
Box4.OnActivate = @(this, ~) interpolate(this, 'Scale', rand(1, 3)*2, 'linear', 0.5);

% Box with OnActivate callback set to interpolate its color
Box5 = addChild(FW.Root, 'Box5');
createShape(Box5, 'box', [1 1 1]);
Box5.Color = [1 1 1];
Box5.Location = [4 0 0.5];
Box5.OnActivate = @(this, ~) interpolate(this, 'Color', rand(1, 3)*10, 'triangle');