less_retarded_wiki/3d_model.md
2024-03-12 21:31:37 +01:00

191 lines
22 KiB
Markdown

# 3D Model
In the world of [computers](computer.md) (especially in [computer graphics](graphics.md), but also e.g. in physics simulations etc.) 3D model is a representation of a [three dimensional](3d.md) object, for example of a real life object such as a car, [tree](tree.md) or a [dog](dog.md), but also possibly something more abstract like a [fractal](fractal.md) or [function](function.md) plot surface. 3D models can be displayed using various [3D rendering](3d_rendering.md) techniques and are used mostly to simulate [real world](real_world.md) on computers (e.g. [games](game.md)), as real world is, as we know, three dimensional. 3D models can be created in several ways, e.g. manually with 3D modelling software (such as [Blender](blender.md)) by 3D [artists](art.md), by 3D scanning real world objects, or automatically by [procedural generation](procgen.md).
3D models can be represented in many ways -- the **mainstream "game" 3D models** that most people are used to seeing are polygonal (made of triangles) boundary-representation (recording only surface, not volume) [textured](texture.md) (with "pictures" on their surface) 3D models, but keep in mind many different ways are possible and used too, for example various volume representations, [voxel](voxel.md) models, [point clouds](point_cloud.md), [implicit surfaces](implicit_surface.md), [spline](spline.md) surfaces, [constructive solid geometry](csg.md), [wireframe](wireframe.md) etc. Models may also bear additional extra information and features, e.g. bone rigs for animation, animation key frames, density information, [LODs](lod.md) and so on.
TODO: classification, operations (subdivision, booleans, ...), texturing, animation, formats, ...
## Example
Let's take a look at a simple polygonal 3D model. The following is a primitive, very [low poly](low_poly.md) model of a house, basically just a cube with roof:
```
I
.:..
.' :':::..
_-' H.' '. ''-.
.' .:...'.......''..G
.' ...'' : '. ..' :
.::''......:.....'.-'' :
E: : :F :
: : : :
: : : :
: :......:.......:
: .' D : .' C
: .'' : -'
: .'' : .'
::'...............:'
A B
```
In a computer it would firstly be represented by an array of vertices, e.g.:
```
-2 -2 -2 (A)
2 -2 -2 (B)
2 -2 2 (C)
2 -2 -2 (D)
-2 2 -2 (E)
2 2 -2 (F)
2 2 2 (G)
2 2 -2 (H)
0 3 0 (I)
```
Along with triangles (specified as indices into the vertex array, here with letters):
```
ABC ACD (bottom)
AFB AEF (front wall)
BGC BFG (right wall)
CGH CHD (back wall)
DHE DEA (left wall)
EIF FIG GIH HIE (roof)
```
We see the model consists of 9 vertices and 14 triangles. Notice that the order in which we specify triangles follows the rule that looking at the front side of the triangle its vertices are specified clockwise (or counterclockwise, depending on chosen convention) -- sometimes this may not matter, but many 3D engines perform so called [backface culling](backface_culling.md), i.e. they only draw the front faces and there some faces would be invisible from the outside if their winding was incorrect, so it's better to stick to the rule if possible.
The following is our house model in obj format -- notice how simple it is (you can copy paste this into a file called `house.obj` and open it in Blender):
```
# simple house model
v 2.000000 -2.000000 -2.000000
v 2.000000 -2.000000 2.000000
v -2.000000 -2.000000 2.000000
v -1.999999 -2.000000 -2.000000
v 2.000001 2.000000 -2.000000
v 1.999999 2.000000 2.000000
v -2.000001 2.000000 2.000000
v -2.000000 2.000000 -2.000000
v -2.000001 2.000000 2.000000
v 0.000000 3.000000 0.000000
vn 1.0000 0.0000 0.0000
vn -0.0000 0.0000 1.0000
vn 0.0000 -1.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn -1.0000 -0.0000 -0.0000
vn -0.0000 0.8944 0.4472
vn 0.4472 0.8944 0.0000
vn 0.0000 0.8944 -0.4472
vn -0.4472 0.8944 -0.0000
s off
f 6 2 5
f 2 1 5
f 6 9 3
f 3 2 6
f 4 1 3
f 2 3 1
f 5 1 8
f 4 8 1
f 8 4 9
f 4 3 9
f 9 6 10
f 6 5 10
f 8 10 5
f 8 9 10
```
And here is the same model again, now in collada format (it is an [XML](xml.md) so it's much more verbose, again you can copy paste this to a file `house.dae` and open it in Blender):
```
<?xml version="1.0" encoding="utf-8"?>
<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- simple house model -->
<asset>
<contributor> <author>drummyfish</author> </contributor>
<unit name="meter" meter="1"/>
<up_axis>Z_UP</up_axis>
</asset>
<library_geometries>
<geometry id="house-mesh" name="house">
<mesh>
<source id="house-mesh-positions">
<float_array id="house-mesh-positions-array" count="30">
2 2 -2 2 -2 -2 -2 -2 -2 -2 2 -2
2 2 2 2 -2 2 -2 -2 2 -2 2 2
-2 -2 2 0 0 3
</float_array>
<technique_common>
<accessor source="#house-mesh-positions-array" count="10" stride="3">
<param name="X" type="float"/>
<param name="Y" type="float"/>
<param name="Z" type="float"/>
</accessor>
</technique_common>
</source>
<vertices id="house-mesh-vertices">
<input semantic="POSITION" source="#house-mesh-positions"/>
</vertices>
<triangles material="Material-material" count="14">
<input semantic="VERTEX" source="#house-mesh-vertices" offset="0"/>
<p>
5 1 4 1 0 4 5 8 2 2 1 5 3 0 2 1 2 0 4 0 7
3 7 0 7 3 8 3 2 8 8 5 9 5 4 9 7 9 4 7 8 9
</p>
</triangles>
</mesh>
</geometry>
</library_geometries>
<library_visual_scenes>
<visual_scene id="Scene" name="Scene">
<node id="house" name="house" type="NODE">
<translate sid="location">0 0 0</translate>
<rotate sid="rotationZ">0 0 1 0</rotate>
<rotate sid="rotationY">0 1 0 0</rotate>
<rotate sid="rotationX">1 0 0 0</rotate>
<scale sid="scale">1 1 1</scale>
<instance_geometry url="#house-mesh" name="house"/>
</node>
</visual_scene>
</library_visual_scenes>
<scene> <instance_visual_scene url="#Scene"/> </scene>
</COLLADA>
```
TODO: other types of models, texturing etcetc.
## 3D Modeling: Learning It And Doing It Right
*WORK IN PROGRESS*
**Do you want to start 3D modeling?** Or do you already know a bit about it and **just want some advice to get better?** Then let us share a few words of advice here.
Let us preface with mentioning the **[hacker](hacking.md) chad way of making 3D models**, i.e. the [LRS](lrs.md) way 3D models should ideally be made. Remeber, **you don't need any program to create 3D models**, you can make 3D models perfectly fine without Blender or any similar program, and even without computers. Sure, a certain kind of highly artistic, animated, very high poly models will be very hard or impossible to make without an interactive tool like Blender, but you can still make very complex 3D models, such as e.g. that of a whole city, without any fancy tools. Of course people were making statues and similar kinds of "physical 3D models" for thousands of years -- sometimes it's actually simpler to make the model by hand out of clay and later scan it into the computer -- you may also make easily make a polygonal model out of paper, BUT even virtual 3D models can simply be made with pen and paper, it's just numbers, verices and triangles, very manageable if you keep it simple and well organized. You can directly write the models in text formats like obj or collada. First computer 3D models were actually made by hand, just with pen and paper, because there were simply no computers fast enough to even allow real time manipulation of 3D models; back then the modellers simply measured positions of someone object's "key points" (verices) in 3D space which can simply be done with tools like rulers and strings, no need for complex 3D scanners (but if you have a digital camera, you have a quite advanced 3D scanner already). They then fed the manually made models to the computer to visualize them, but again, you don't even need a computer to draw a 3D model, in fact there is a whole area called [descriptive geometry](descriptive_geometry.md) that's all about drawing 3D models on paper and which was used by engineers before computers came. Anyway, you don't have to go as far as avoiding computers of course -- if you have a programmable computer, you already have the luxury which the first 3D artists didn't have, a whole new world opens up to you, you can now make very complex 3D models just with your programming language of choice. Imagine you want to make the said 3D model of a city just using the [C](c.md) programming language. You can first define the terrain as [heightmap](heightmap.md) simply as a 2D array of numbers, then you write a simple code that will iterate over this array and converts it to the obj format (a very simple plain text 3D format, it will be like 20 lines of code) -- now you have the basic terrain, you can render it with any tool that can load 3D models in obj format (basically every 3D tool), AND you may of course write your own 3D visualizer, there is nothing difficult about it, you don't even have to use perspective, just draw it in orthographic projection (again, that will be probably like 20 lines of code). Now you may start adding houses to your terrain -- make a C array of vertices and another array of triangle indices, manually make a simple 3D model of a house (a basic shape will have fewer than 20 vertices, you can cut it out of paper to see what it will look like). That's your house geometry, now just keep making instances of this house and placing them on the terrain, i.e. you make some kind of struct that will keep the house transformation (its position, rotation and scale) and each such struct will represent one house having the geometry you created (if you later improve the house model, all houses will be updates like this). You don't have to worry about placing the houses vertically, their height will be computed automatically so they sit right on the terrain. Now you can update your model exporter to take into account the houses, it will output the obj model along with them and again, you can view this whole model in any 3D software or with your own tools. You can continue by adding trees, roads, simple materials (maybe just something like per triangle colors) and so on. This approach may actually even be superior for some projects just as scripting is superior to many GUI programs, you can collaborate on this model just like you can collaborate on any other text program, you can automatize things greatly, you'll be independent of proprietary formats and platforms etcetc. This is how 3D models would ideally be made.
OK, back to the mainstream now. Nowadays as a [FOSS](foss.md) user you will most likely do 3D modeling with [Blender](blender.md) -- we recommended it to start learning 3D modeling as it is powerful, [free](free_software.md), gratis, has many tutorials etc. Do NOT use anything [proprietary](proprietary.md) no matter what anyone tells you! Once you know a bit about the art, you may play around with alternative programs or approaches (such as writing programs that generate 3D models etc.). However **as a beginner just start with Blender**, which is from now on in this article the software we'll suppose you're using.
**Start extremely simple and learn bottom-up**, i.e. learn about fundamentals and low level concepts and start with very simple models (e.g. simple untextured low-poly shape of a house, box with a roof), keep creating more complex models by small steps. Do NOT fall into the trap of "quick and easy magic 3D modeling" such as sculpting or some "[smart](smart.md) [apps](app.md)" without knowing what's going on at the low level, you'll end up creating extremely ugly, inefficient models in bad formats, like someone wanting to create space rockets without learning anything about math or physics first. Remember to **practice, practice, practice** -- eventually you learn by doing, so try to make small projects and share your results on sites such as opengameart to get feedback and some mental satisfaction and reward for your effort. The following is an outline of possible steps you may take towards becoming an alright 3D artist:
1. **Learn what 3D model actually is, basic technical details about how a computer represents it and roughly how [3D rendering](3d_rendering.md) works**. It is EXTREMELY important to have at least some idea about the fundamentals, i.e. you should learn at least the following:
- 3D models that are used today consist of **vertices and [triangles](triangle.md)** (though higher polygons are usually supported in modeling software, everything is broken down to triangles eventually), computers usually store [arrays](array.md) of vertices and triangles as indices pointing to the array of vertices. Triangles have **facing** (front and back side, determined by the order of its vertices). These 3D models only represent the boundary (not the volume). All this is called the model's **geometry**.
- **[Normals](normal.md)** are [vectors](vector.md) "perpendicular to the surface", they can be explicitly modified and stored or computed automatically and they are extremely important because they say how the model interacts with light (they are used in [shading](shading.md) of the model), i.e. which edges appear sharp or smooth. Normal maps are textures that can be used to modify normals to make the surface seem rough or otherwise deformed without actually modifying the geometry. You HAVE TO understand normals.
- **[Textures](texture.md)** are images (or similar image-like data) that can be mapped to the model surface to "paint it" (or give it other material properties). They are mapped to models by giving vertices texturing **UV coordinates**. To make textures you'll need some basics of 2D image editing (see e.g. [GIMP](gimp.md)).
- 3D rendering (and also modeling) works with the concept of a **[scene](scene.md)** in which a number of models reside, as well as a virtual camera (or multiple ones), lights and other objects. These objects have **transformations** (normally translation, rotation and scale, represented by [matrices](matrix.md)) and may form a hierarchy, so called [scene graph](scene_graph.md) (some objects may be parents of other objects, meaning the child transformations are relative to parents) etc.
- A 3D renderer will draw the triangles the model consists of by applying **[shading](shading.md)** to determine color of each [pixel](pixel.md) of the [rasterized](rasterization.md) triangle. Shading takes into account besides others texture(s) of the model, its material properties and light falling on the model (in which the model normals play a big role). Shading can be modified by creating **[shaders](shader.md)** (if you don't create custom shaders, some default one will be used).
- Briefly learn about other concepts such as low/high poly modeling and basic **3D formats** such as [OBJ](obj.md) and [COLLADA](collada.md) (which features they support etc.), possible other models representations ([voxels](voxel.md), [point clouds](point_cloud.md), ...) etc.
2. **Manually create a few extremely simple [low-poly](low_poly.md) untextured models**, e.g. that of a simple house, laptop, hammer, bottle etc. Keep the vertex and triangle count very low (under 100), make the model by MANUALLY creating every vertex and triangle and focus only on learning this low level geometry manipulation well (how to create a vertex, how to split an edge, how to rotate a triangle, ...), making the model conform to good practice and get familiar with tools you're using, i.e. learn the key binds, locking movement direction to principal axes, learn manipulating your 3D view, setting up the free/side/front/top view with reference images etc. Make the model nice! I.e. make it have correctly facing triangles (turn [backface culling](backface_culling.md) on to check this), avoid intersecting triangles, unnecessary triangles and vertices, remove all duplicate vertices (don't have multiple vertices with the same position), connect all that should be connected, avoid badly shaped triangles (e.g. extremely acute/long ones) etc. Also learn about normals and make them nice! I.e. try automatic normal generation (fiddle e.g. with angle thresholds for sharp/smooth edges), see how they affect the model look, try manually marking some edges sharp, try out smoothing groups etc. Save your final models in OBJ format (one of the simplest and most common formats supporting all you need at this stage). All this will be a lot to learn, that's why you must not try to create a complex model at this stage. You can keep yourself "motivated" e.g. by aiming for creating a low-poly model collection you can share at opengameart or somewhere :)
3. **Learn texturing** -- just take the models you have and try to put a simple texture on them by drawing a simple image, then unwrapping the UV coordinates and MANUALLY editing the UV map to fit on the model. Again the goal is to get familiar with the tools and concepts now; experiment with helpers such as unwrapping by "projecting from 3D view", using "smart" UV unwrap etc. Make the UV map nice! Just as model geometry, UV maps also have good practice -- e.g. you should utilize as many texture pixels as possible (otherwise you're wasting space in the image), watch out for [color bleeding](color_bleeding.md), the mapping should have kind of "uniform pixel density" (or possibly increased density on triangles where more details is supposed to be), some pixels of the texture may be mapped to multiple triangles if possible (to efficiently utilize them) etc. Only make a simple diffuse texture (don't do [PBR](pbr.md), material textures etc., that's too advanced now). Try out texture painting and manual texture creation in a 2D image program, get familiar with both.
4. **Learn modifiers and advanced tools**. Modifiers help you e.g. with the creation of symmetric models: you only model one side and the other one gets mirrored. Subdivide modifier will automatically create a higher poly version of your model (but you need to help it by telling it which sides are sharp etc.). [Boolean](bool.md) operations allow you to apply set operations like unification or subtraction of shapes (but usually create a messy geometry you have to repair!). There are many tools, experiment and learn about their pros and cons, try to incorporate them to your modeling.
5. **Learn retopology and possibly sculpting**. Topology is an extremely important concept -- it says what the structure of triangles/polygons is, how they are distributed, how they are connected, which curves their edges follow etc. Good topology has certain rules (e.g. ideally only being composed of quads, being denser where the shape has more detail and sparser where it's flat, having edges so that animation won't deform the model badly etc.). Topology is important for efficiency (you utilize your polygon budget well), texturing and especially animation (nice deformation of the model). Creating more complex models is almost always done in the following two steps:
- Creating the shape while ignoring topology, for example with sculpting (but also other techniques, e.g. just throwing shapes together). The goal is to just make the desired shape.
- Retopology: creating a nice topology for the shape while keeping the shape unchanged. This is done by starting modeling from the start with the "stick to surface" option, i.e. whenever you create or move a vertex, it sticks to the nearest surface (surface of the created shape). Here you just try to create a new "envelope" on the existing shape while focusing on making the envelope's topology nice.
6. **Learn about materials and [shaders](shader.md)**. At this point you may learn about how to create custom shaders, how to create transparent materials, apply multiple textures, how to make realistic skin, [PBR](pbr.md) shaders etc. You should at least be aware of basic shading concepts and commonly encountered techniques such as [Phong shading](phong_shading.md), [subsurface scattering](subsurface_scattering.md), [screen space](screen_space.md) effects etc. because you'll encounter them in shader editors and you should e.g. know what performance penalties to expect.
6. **Learn animation**. First learn about keyframes and [interpolation](interpolation.md) and try to animate basic transformations of a model, e.g. animate a car driving through a city by keyframing its position and rotation. Then learn about animating the model's geometry -- first the simple, old way of morphing between different shapes (shape keys in Blender). Finally learn the hardest type of animation: skeletal animation. Learn about bones, armatures, rigging, inverse kinematics etc.
7. **Now you can go crazy** and learn all the uber features such as hair, physics simulation, [NURBS](nurbs.md) surfaces, boob physics etc.
**Don't forget to stick to [LRS](lrs.md) principles!** This is important so that your models are friendly to good technology. I.e. even if "[modern](modern.md)" desktops don't really care about polygon count anymore, still take the effort to optimize your model so as to not use more polygons that necessary! Your models may potentially be used on small, non-consumerist computers with [software renderers](software_rendering.md) and low amount of RAM. [Low-poly](low_poly.md) is better than high-poly (you can still prepare your model for automatic subdivision so that obtaining a higher poly model from it automatically is possible). Don't use complex stuff such as PBR or skeletal animation unless necessary -- you should mostly be able to get away with a simple diffuse texture and simple keyframe morphing animation, just like in old games! If you do use complex stuff, make it optional (e.g. make a normal map but don't rely on it being used in the end).
Good luck with your modeling!