My simplified problem is to animate some text on a 3D plot.
I have a cube,
vert = [1 1 0;0 1 0;0 1 1;1 1 1;0 0 1;1 0 1;1 0 0;0 0 0];
fac = [1 2 3 4; 4 3 5 6; 6 7 8 5; 1 2 8 7; 6 7 1 4; 2 3 5 8];
patch('Faces',fac,'Vertices',vert,'FaceColor',[.8 .5 .2]);
axis([0, 1, 0, 1, 0, 1]);
axis equal
axis off
Is it possible to get something like this?
Using text
doesn't help (it looks fake!),
Thanks,
I have a solution which renders ok by using
texture mapping
.The idea is to apply an image to the face and let Matlab take care of everything else. The great advantage is that matlab will take care of all the perspective aspects, and the rendering is pretty good. The small disadvantage is that you can only apply texture to
surface
objects, and since you want 6 different images, you'll have to manage 6 different surfaces.The code below shows an example of how to do it:
This will render the following figure:
Sorry for the actual images I took the first one I had available. You will have to generate a nice image of your cube faces, then apply them as shown above.
If you rotate your cube with the figure interactive tool, you will have no problem.
If you want to rotate it programatically, you have 2 options:
if the object is the only one displayed, then the easiest is to only move the point of view (using
view
or other camera manipulation functions).if you have multiple objects and you only want to rotate your cube, then you will have to rotate each surface individually. In this case I would suggest writing a helper function which will rotate the 6 faces for you given an angle and a direction. An even neater way would be to have your cube managed in a class, then add a method to rotate it (internally it will rotate the 6 faces).
Edit: just for fun, the snippet below will animate your dice (not the pretiest way but it shows you an example of the first option above).
Run this block to see your dice dancing ;)
Edit (again)
And this is an example on how to animate the cube object by rotating the object itself
Note that I modified the definition of the cube in order to have all the face surfaces handles in one array. This allow to apply the
rotate
command to all the surfaces in one go just by sending the handle array as parameter.The idea is to use texture mapping as @Hoki showed. I tried to implement this on my end, here is what I came up with.
First we'll need 6 images to apply on the cube faces. These can be any random images of any size. For example:
Better yet, let's use an online service that returns placeholder images containing digits 1 to 6:
Here are the resulting images:
Next let's create a function that renders a unit cube with images texture-mapped as faces:
Here is what it looks like:
Now we can have some fun with this creating interesting animations. Consider the following:
I'm using the
hgtransform
function to manage transformations, this is much more efficient than continuously changing the x/y/z data points of the graphics objects.BTW I've used slightly different images in the animation above.
EDIT:
Let's replace the rotating cubes with images of planet earth mapped onto spheres. First here are two functions to render the spheres (I'm borrowing code from these examples in the MATLAB documentation):
get_earth_sphere1.m
get_earth_sphere2.m
The animation script is similar to before (with minor changes), so I'm not gonna repeat it. Here is the result:
(This time I'm using the new graphics system in R2014b)