Creating a 3D World
Greg Brown, Olivia Ortiz, Ravenna Thielstrom
Background/Theory 2
Completed Project Design 5
Using MATLAB, we designed a three-dimensional cityscape. We developed three
functions to make rectangular prisms, cylinders, and spheres, as collections of two-dimensional
patches. We used these functions to rapidly populate a three-dimensional plot with the varied
scenery of a cityscape. Lighting, colors, and shading added to the realism of the city. A series of
for-loops that adapt the camera’s position, angle, and target move the observer along a preset,
real-time path to showcase a comprehensive fly-through of the city.
Our goal in completing this project was to design a three-dimensional landscape—
whether it be a city, forest, or barn yard—on MATLAB and use the camera-properties of
MATLAB to show it to others. While part of our motivation was to complete the project, our
group was drawn to the computer-related aspects of engineering. Coding MATLAB is very
computer-related, so this project became a way to further explore our interests. We found the
idea of making a three-dimensional landscape particularly interesting because we had only used
MATLAB in two-dimensions in class, and graphic design is an interesting subject. Likewise, we
had never worked with the different camera properties of MATLAB and were curious as to how
to use them.
Data in MATLAB may be plotted on the z-axis in addition to the x- and y-axes. In class,
we created patches on the x-and-y plane. By aggregating groups of patches that also contain zdata, different three-dimensional shapes can be created; the perspective of each face of these
shapes will change according to the camera properties of position and target to create an illusion
of a three-dimensional landscape.
The greater complexity of objects visible in the demo is emerges from the composition of
multiple basic three-dimensional objects, namely rectangular prisms, cylinders, and spheres. We
created functions that would assemble patches in a predefined way so that we could easily
develop a cityscape. Most of the properties that are applied to patches are meant to be applied to
every patch, so that functions can easily be called to set the shape properties that vary: location,
dimensions, and color.
Each function varies in complexity. The rectangular prism function, for instance, creates
six separate patches, one for each of the sides of a rectangular prism. Because the dimensions of
each shape could be specified, the rectangular prism function was versatile: it could be used to
make flat surfaces as well as rectangular structures like buildings. The cylinder and sphere
functions both internally used functions that would return matrices of x-, y-, and z-coordinates
for their respective shapes. It was necessary to create our own functions, nonetheless, because
this data had to be edited to accommodate location and plotted with the necessary properties; the
function that returned values for a cylinder also did return values for the bases. The cylinder
function creates a three-dimensional shape by using a for-loop that creates a given number of
“sides” that make up the cylinder’s round portion to create the illusion of a smoothly curving
surface and then adding two bases. The sphere function effectively creates a set of patches using
another function, the ‘surf’ function, which better accommodates the complexity of a sphere’s
many “faces,” which are not all rectangular.
When MATLAB displays a 3D graph to a user, the resulting image depends on how the
camera is positioned and where it is pointing. In order to alter the default camera properties, we
can call the campos, camtarget, and camva functions, which respectively control position, target,
and view angle. Position is defined as a point of x, y, and z coordinates somewhere on the graph
where the camera is located. Target is defined as a point of x, y, and z coordinates where the
camera is aimed at. To “move” the camera around, therefore, we changed the position and target
of the camera incrementally with a brief pause for every iteration of a for-loop. A smaller view
angle “zooms in” the camera by shrinking the window that the camera looks through—whatever
can be seen through this window is what fills the screen when the program is run. This helps to
reduce clipping, which occurs when the camera gets too close to the buildings, by basically
zooming in to a point that most of the clipping occurs out of the camera’s window of sight.
Patches have a number of properties that can be recalled and modified. Patch properties
that we modified at various points were ‘Clipping,’ ‘EdgeColor,’ and ‘BackFaceLighting.’ We
turned ‘Clipping’ to ‘off’ so that MATLAB will show portions of a patch outside the axes,
although additional modifications to camera view angle were still necessary. Turning
‘EdgeColor’ to ‘none’ removes the black outline to add realism, particularly in the cases of
cylinders and spheres which aggregate many patches to create the illusion of a smooth surface.
Turning ‘BackFaceLighting’ to ‘unlit’ makes faces less reflective by preventing light from
changing their coloration; this was applied just for the roads, which would unrealistically turn
bright white at certain angles. Turning ‘EdgeLighting’ to ‘phong’ changes the shading of patches
so that differences in shading as a result of lighting manifest as a gradient rather than along a
line. This issue was most apparent on the surfaces of spheres and tops of buildings; as such,
finding that applying it to every patch in the rectangular prism function decreased the processing
speed, we decided to only apply change this property for the top patch.
MATLAB plots have many properties; two that we changed were color and lighting. We
changed the background of the plot and the plot-window by changing their ‘Color’ properties
with ‘set(gcf,...)’ and ‘set(gca,...).’ We modified the lighting by recalling ‘light’ and changing its
‘Position’ and ‘Style’ properties, which change where the light comes from and whether it is a
localized source.
Completed Project Design:
The previously described functions were used to populate the city with buildings and
other structures. The base of the city is a grey rectangle. The rows and columns of roads were
iterated to create 36 blocks in which structures could be added. Many buildings are singular,
simple rectangles; others are composed of multiple shapes. The sphere function was made to
create buildings with “domes” since the bottom hemisphere is embedded in a rectangle or
cylinder below. A green with trees was added as well; the trees consist of a cylindrical trunk and
spherical bunch of leaves. The tree placement and trunk height is actually randomized, so each
time the script is run they will appear in a new orientation, and the number of trees can be easily
manipulated since they are created with an iterative for-loop. As the script is run, the camera
(with a constant view angle of 30 degrees) will follow several paths through the city, each path
defined in our code by a separate for-loop. Within the for-loop, the camera position and target
are changed as needed: keeping the position coordinates constant changing the target coordinates
will cause the camera to rotate in place, keeping the target and changing the position will result
in the camera flying along a path but always focused on one spot, regardless of its position to the
camera, and changing both at the same time by the same factor will cause the camera to look a
constant distance ahead of itself, so that the view remains the same. With these tactics, the
camera essentially circles around the city to look at it from all angles, taking wildly different
routes along the way, and specifically lingering on a shot of the city park to show off the trees.
We achieved our goal in completing the project by designing a three-dimensional
landscape and learning how to show it with different camera-functions. Considering we started
with virtually no knowledge, we managed a great deal. Because our initial goal was so general,
though, it was easy to accomplish. Ideas and potential goals that surfaced after the city began
construction, however, were less easily obtained or not obtained at all. The idea of making a
cityscape likely emerged because the rectangular prism function was developed first, and a city
is primarily rectangular. makeCylinder and makeSphere were completed later and showcased in
specific parts of the city to demonstrate that they worked as intended. makeSphere in particular
was difficult to complete because the patches needed to create one are more complex and
numerous; for instance, even the patches comprising a cylinder, excluding the bases, are
rectangular, but some values returned by the sphere function create rectangular patches while
others create triangular patches. This complexity required that the ‘surf’ function be used instead.
Originally, we had hoped to use more diverse shapes in our city. It was originally
intended to add other shapes, such as triangular prisms; however, we have amply demonstrated
that a variety of functions can be created and applied. The specific variety that we have used
does not particularly matter. Another potential direction the project could have taken was to add
more realistic details like windows, streetlamps, and textures. This idea was not thoroughly
explored, but we did encounter some performance limitations in what we tried to do already, so
additional patches and textures could have limited our ability to display the city. Another area to
explore would be adding motion of objects to the city. Specifically, we attempted to add a car,
but it would be difficult to animate while the camera was moving through the city. Patches must
be deleted after each “frame” so that the car can move without leaving behind a “trail” of its
previous patches, but the functions we developed were not amenable to the deletion of patches
since we could not easily assign handles to them. Another option would be to clear the figure
between each frame, but recreating the whole city, which consists of perhaps thousands of
patches, is extremely intensive. The movement of the car, because it happens frame-by-frame
and simultaneously with the motion of the camera, would have to be added during the for-loops
regulating the camera’s motion. Adaptation to the functions used to make the city, perhaps, could
be made to circumvent these issues, but it would be another project entirely.
One detail that we completely succeeded with was the creation of trees. By making
spheres directly on top of cylinders, we created balloon-ish trees. To add more excitement, we
randomized their placement and their heights in the large park space. All of our trees are
deciduous most likely Norway Maples. If we had a triangular prism-making function, there
would have also been pine trees.
For the most part, we found our efforts in MATLAB to be fruitful—the buildings were
easy to construct and place within the city blocks, and once we figured out the how to change the
camera properties, manipulating the camera into a fly-through was fairly easy. We were even
somewhat surprised at how realistic the final city ended up looking, which is due in a large part
to the extra measures we went to in lighting the city correctly. Smoothness of the buildings was a
last-minute but highly effective detail. Something that could have run more effectively, however,
was the speed of the fly-through, which varied drastically if we added or subtracted large overall
properties, and tended to increase during certain paths and not others. This made the final
animation look lot more jerky on a big screen than we had prepared it to look on a small
computer screen, and theoretically could not be easily fixed since it was the processing speed of
MATLAB itself that slowed the program, not the set time to pause between steps.
Another problem that never quite worked out was the issue of clipping. We did a lot of
investigating into how MATLAB operates in order to shed some light on this problem, but in the
end there was always still some clipping present in our run of the fly-through, no matter what we
did to repair it (such as adjusting the camera view angle and the “Clipping” property of patches).
An obvious extension of this would be to simply make the city bigger—add more streets
and buildings so that the “city” is not just a couple of blocks floating in a square in the sky.
Another would be to add moving cars, or details like windows, as discussed above.

Lapwriteup - wikis for Swarthmore