%% Import FBX models from Paint 3D into a Virtual World
% This example shows a workflow for prototyping 3D models in Microsoft&trade;'s Paint 
% 3D and using them in the Simulink&reg; 3D Animation&trade; editor and viewer. This example 
% assumes that you are working on a Microsoft Windows platform. 
%% 
% The example uses a model of a bouncing ball, whose motion is displayed in 
% the 3D Animation viewer.
% 
mdl = 'fbximportmodel';
%%
open_system(mdl);
close_system('fbximportmodel/Y position');
close_system('fbximportmodel/VR Sink');
open_system(mdl);
snapnow;

%% 
% 
%% Create 3D Model Using Paint 3D
% In this example, we will be modifying the virtual world to add
% a suitable shape created from Paint 3D.
% 
% Open Paint 3D and select *3D shapes* from the menu. 
% 
% 
% 
% Create a suitable object for your simulation. In this case, something that 
% could conceivably bounce. The example illustration shows a cone mounted on top 
% of a hemisphere to form a contiguous solid. 
%%
% <<../cone_hemisphere_ui.png>>
% 
% 
%%
% To create a solid, select an object from the *3D Shapes*  pane to the right 
% and drag it onto the canvas. Use the in-canvas controls to rotate, flip, resize, 
% or move the object around the canvas. Repeat this process to add more solids 
% to the editor. 
% 
% To manipulate or edit all the solids as an assembly, select all of the solids 
% and select *Group* in the *Grouping* section of the *3D Selection* pane to the 
% right. 
% 
% 
%% 
% When you're ready to export the model, open the Paint 3D menu by selecting 
% *Menu* in the toolbar and select *Export File*. Choose the *3D - FBX* file type 
% when prompted and save the resulting |.fbx| file in a suitable folder.
% 
% 
%%
% <<../export_options.png>>
% 
%%
% 
%% Import FBX Model into Virtual World
% The |fbximportmodel| uses the attached |vrbounce_modified.x3d| world file to define the
% structure of the virtual world. Open the virtual world file in the 3D
% World Editor

%%
% Select *Nodes* > *Import From* > *FBX File* to import the saved 3D model
% into the node tree as a child of the ROOT node. The example includes a
% sample file |Untitled.fbx|.
%%
% <<../fbx_import_editor.png>>
% 
%% 
% To quickly find the inserted body in the virtual world, right click on
% the name of the newly inserted node and select *View Node*. 
%
% You may find that the imported node is not in the right place nor is it
% the right size. Update the *translation* property of the imported node to
% match that of the *Ball* node and adjust the *scale* property to make the
% imported model a similar size to the ball. Save the modified virtual
% world.
%%
% <<../imported_node_adjusted.png>>
%% Display Simulation using Imported Model 
% Open |fbximportmodel| and add a new VR Sink Block. Configure the block
% settings to use the included |vrbounce_modified.x3d| virtual world.
% Select the *scale* and *translation* properties of the *Ball* and
% *FBX_Untitled_Transform* nodes. 
% <<../vrsink_settings.png>>
%%
% Connect the output of the VR Signal Expander block to the ports labeled
% *Ball.translation* and *FBX_Untitled_Transform.translation*. The dynamics
% of a bouncing ball will be used to animate the movement of the ball and
% the imported object.
%%
% Connect the output of the Mux block to the ports labeled *Ball.scale*
% and *FBX_Untitled_Transform*. The object deformation calculations are
% animated by adjusting the *Scale* property of the nodes.
open_system(mdl);
close_system('fbximportmodel/Y position');
close_system('fbximportmodel/VR Sink');
sim(mdl,'StopTime','10');
open_system('fbximportmodel/VR Sink');
snapnow;

%% 
% _Used with permission from Microsoft._
%% 
% _Copyright 2019 The MathWorks, Inc._
