We have adapted the nice contribution of Anselm Ivanovas (2008) to the spirit of Numerical Factory. We will learn, step by step, how to build a 3D Christmas tree using Matlab functions.
1. Build the Tree
1.1 Define the contour
The Christmas tree is essentially a cone that has a small trunk in the basis. To do that in Matlab, you must use the cylinder Matlab function. In fact, cylinder obtains a surface from the revolution of a contour edge. This allows you, for instance, to obtain a cone if you choose a decreasing radious contour. In our case we choose an appropriate contour for our tree in order to represent both the trunk and the crown as it is represented in the following figure
1.2 First 3D tree
After defining the contour, the cylinder function returns the 3D revolution of this contour that can be displayed using the surf Matlab function (see the details on the script below).
1.3 Crease the tree surface
Since the tree is very regular and smooth, we add some diffusion to the surface points of the tree to make it look more real. This diffusion is applied in the radial direction using a small random perturbation as you can see in the figure
1.4 Nice render of the tree
Choosing now a nice green color map we obtain a more realistic view of the tree. We use phong lighting and smooth shading to obtain the final render of the tree (see the details on the script below).
2. Add some present boxes
To enhance the scene we add some presents using a present function included in the final script. Essentially we define a regular cube and then translate and scale it according to the input values. Color is also changed to obtain more variability.
3. Final scene
We add some random snowflakes to the final scene to have a nicer Christmas view.
The script with the Matlab code for this exemple can be found in the following link.
The final exercise proposed on the script must produce a video more or less like this one.