Es/LOD Models

Este tutorial enseñará a crear un para los modelos de forma eficiente. «Eficiente» significa que aumenta el rendimiento para que el juego funcione lo más fluido posible.
==Fase de planeamiento==
Antes de empezar, hay que preguntarse si el modelo debe tener un LOD que logre optimizar el juego o si va a ser una pérdida de tiempo.
===Cuando usar LOD en modelos===
Puede merecer la pena crear uno o más LOD si el modelo tiene:
*Detalles pequeños que aumenten los polígonos pero no se ven a distancia.
*Zonas redondeadas que usan muchos polígonos para parecer más «suaves».
*Objetos físicos irrelevantes.
*Sombras de alto consumo (como o ).
Otros casos donde los LOD de los modelos son prescindibles son:
*Modelos que ya tienen pocos polígonos.
*Modelos que no se pueden reducir en cantidad de polígonos ni un 30 %.
*Modelos estáticos que a penas tengan uso.
De aquí en adelante ya es la decisión del usuario, pero puede resultar de ayuda recurrir a los modelos de HL2 para ver cómo Valve creó LOD para los modelos aunque no fueran necesarios.
==Niveles de detalle al modelar==
===Consejos===
The work in the model editor should be kept to a minimum. You don't want to spend as much time on your LOD models as you did on the original model! Our goal is to make a few modifications on the model that will reduce the polycount within a few minutes. We don't want to make a whole new model, redo our UV map, or do something else that consumes a lot of time.
Get used to the fact that a LOD model can only be seen from a large distance. If a player wants to see details, he'll step closer. Since the player does not focus on the details, we can remove them without him noticing if it is done in a subtle way. Things you don't want to do:
*Change the outline of the model or anything that gives cover. You can remove the seats within a car, but don't remove the roof.
*Moving details on the UV map. The texture will most likely get stretched during the process regardless; however, if specific details move from one place to another, it'll be more noticeable.
*Smoothing groups changing in a way that one large area turns from shadowed into brightened or similar.
===Técnicas===
;Removing small details:Often, details are too small to be seen from a distance—they simply won't be drawn with the resolution of modern-day hardware or can't be noticed.
;Reducing polys of complex surfaces:This can be done by using the tools your modeling package offers. It'll weld vertices without destroying the UV map.
;Rebuilding complex geometry to be simpler:This is only recommended if the automatic tools fail, as it is rather time-consuming and needs a new UV map (use the same skin in any case). Rebuild the basic shape of the model (or a part of it) as low-poly as possible.
;Use QC commands to simplify the model:By using preexisting , you can automatically remove or replace parts of your models mesh during compile without having to edit them yourself.
;Use a low-polycount mesh for your model's shadow:Often overlooked, you should create or reuse an existing low-poly LOD mesh as the mesh used for dynamic shadow rendering.
;Replace an expensive texture/shader with a simpler one:If you're using an shader on a model, such as or a , you can replace the texture that uses it with a less expensive one for specific LOD levels.
;Simplify your model's skeleton:More useful for character models, you can simplify the skeleton by replacing or collapsing bones when the model is a certain distance from the viewer. For example, you could simplify the hands by removing the finger bones, leaving just the hand bone. This will result in the fingers being out straight, but at the distance the model is, the viewer wouldn't notice this.
;Disable facial animation:For character models, it's pointless having the face animated when it's in the distance and it can't be seen.
===Ejemplo (usando )===
This animated outdoor camera is highly detailed. Bolts, cables, and the rotating camera-holder are small details with lots of polys. The chassis of the motor is round and highly detailed as well.
The mounting on the wall is pretty straight forward. Few polys are used here, so an optimization is unnecessary.
The goal is to make two different LOD models: One for a distance of 25 units per pixel and one for 50 units per pixel.
For the first one, some very small details (bolts, cable) are deleted. If we check the difference of the reference and the LOD model in the model viewer, the missing bolts cannot be noticed. The loss of the cable might seem a bit drastic, but in-game you'll most likely have a brighter background so the contrast between cable and wall will not be as big. Furthermore, the player doesn't focus on the cable—he will simply not notice it.
 
In the second step, the camera-holder was deleted. Furthermore, we want to reduce the number of polygons used on the chassis of the motor. The polygons have been selected and the vertex weld tool is used. By increasing the threshold, vertices are weld together and max adjusts the UV map. Try to find a good value which removes as much polys as possible while retaining the shape of the model.
Checking in the model viewer once again, the difference is once again hardly noticeable, but the polygon count is significantly lowered.
==Técnicas de archivo .qc específicas==
To use LOD levels, you need to add some extra commands to your file.
First of all, you need to specify the $lod <units per pixel> { ... } command. This denotes that further commands between the braces should be applied to your model for that specific LOD level.
===Reemplazar el modelo===
The most common command is replacemodel (model name) (replacement model). You can specify which model is to be replaced and which .smd is to be used for the replacement.
// LOD 1 
$lod 25
{
 replacemodel "mymodelreference.smd" "mymodel_LOD1.smd"
}
// LOD 2 
$lod 50
{
 replacemodel "mymodelreference.smd" "mymodel_LOD2.smd"
}
Here our highest-polycount model (mymodelreference.smd) is being replaced with a simpler, lower-polycount version after 25 units per pixel and 50 units per pixel.
===Modelo de pocos polígonos para la sombra===
There is also a $shadowlod { ... } command, which contains instructions on which specific model is to be used for rendering the model's shadow.
// Shadow LOD 
$shadowlod
{
 replacemodel "mymodelreference.smd" "mymodel_shadow.smd"
}
===Ejemplo .qc simple===
$modelname mymodelfolder/mymodelcompiled.mdl
$cdmaterials modeltextures/mytexturefolder
$surfaceprop metal
$body studio "mymodelreference.smd"
$sequence idle "mymodelreference.smd" loop fps 15
$collisionmodel "mymodel_phy.smd" {
 Mass 5
}
$lod 15
{
 replacemodel "mymodelreference" "mymodel_lod1"
}
$lod 40
{
 replacemodel "mymodelreference" "mymodel_lod2"
}
$shadowlod
{
 replacemodel "mymodelreference" "mymodel_lod2"
}
The above gives a very simple example of using LOD levels. Two LOD models are used to reduce the polys at distance. The second LOD model was also used for the shadow LOD model.
However, there are are a number of additional QC commands you can use within $lod groups to improve performance even more.
===Reemplazar o eliminar una malla específica===
The above example demonstrates the use of the replacemodel, which replaces one mesh with another. You can optionally choose to completely remove that mesh, either by using blank as the replacement mesh name or by using the removemodel command.
The following example is for a fictional character model which comprises of two meshes—one for the body and one for the character's equipment packs. At a distance, we choose to remove the equipment mesh entirely.
// LOD 0 - main models
$model soldier_body "body.smd"
$model soldier_kit "kit.smd"
// LOD 1 
$lod 25
{
 replacemodel "body.smd" "body_LOD1.smd"
 replacemodel "kit.smd" "kit_LOD1.smd"
}
// LOD 2 
$lod 50
{
 replacemodel "body.smd" "body_LOD2.smd"
 removemodel "kit.smd"
 // alternate method
 // replacemodel "kit.smd" blank
}
===Reemplazar una textura o usar otras sombras===
Expensive shaders often have a big performance impact as well. Disabling them after a certain distance is an easy way to increase the performance.
To do this you use the replacematerial , which replaces one material with another. An example of this would be replacing the eye material/shader on a character model with something simpler, such as a texture.
In the following example, the materials righteye.vmt and lefteye.vmt use the expensive shader, so they're replaced at LOD level 30 with the material face.vmt. The result would look odd close up, but as the model will be sufficiently far away from the viewer, they are unlikely to notice.
$lod 30 
{
	replacematerial "righteye" "face"
	replacematerial "lefteye" "face"
}
Another technique is to simply remove the parts of the mesh that use a certain material or shader. For example, on a character model viewed at a great distance, you would not be able to make out the eyes or mouth at all, so it may be beneficial to simply just remove them. This can be achieved with the removemesh command, which will remove any faces in the models mesh which use the specified texture, helping to reduce polycount.
$lod 60
{
	removemesh "righteye"
	removemesh "lefteye"
}
===Simplificar esqueleto del modelo===
Another way to improve performance on things like character models is to simplify the model's skeleton based on its distance. This can be done via the bonetreecollapse and replacebone.
bonetreecollapse will collapse or remove all the bones which are a child of the one you specify. For example, collapsing ValveBiped.Bip01_L_Hand will remove all the finger bones. Any vertices weighted to those bones are reassigned to the hand bone and will move with it. This will mean that the fingers will no longer be animated, but as the model is in the distance, this won't be noticed.
The following example removes the finger bones on the left and right hands of our character model at LOD level 30.
$lod 30 
{
 bonetreecollapse "ValveBiped.Bip01_L_Hand"
 bonetreecollapse "ValveBiped.Bip01_R_Hand"
}
replacebone works in a similar way to bonetreecollapse, but it merely replaces one bone with another. This can be useful for such things as removing helper bones (Ulna, Wrist, Elbow, etc.) and attaching the vertices that used them to another.
The following example removes the helper bones from a character rig by reassigning the vertices attached to them to other bones in the basic skeleton:
$lod 30 
{
	replacebone "ValveBiped.Bip01_L_Elbow" "ValveBiped.Bip01_L_upperarm"
	replacebone "ValveBiped.Bip01_R_Elbow" "ValveBiped.Bip01_R_upperarm"
	replacebone "ValveBiped.Bip01_L_Ulna" "ValveBiped.Bip01_L_Forearm"
	replacebone "ValveBiped.Bip01_R_Ulna" "ValveBiped.Bip01_R_Forearm"
	replacebone "ValveBiped.Bip01_L_Shoulder" "ValveBiped.Bip01_L_upperarm"	
	replacebone "ValveBiped.Bip01_R_Shoulder" "ValveBiped.Bip01_R_upperarm"
	replacebone "ValveBiped.Bip01_L_Trapezius" "ValveBiped.Bip01_L_upperarm"
	replacebone "ValveBiped.Bip01_R_Trapezius" "ValveBiped.Bip01_R_upperarm"
	replacebone "ValveBiped.Bip01_L_Wrist" "ValveBiped.Bip01_L_Forearm"
	replacebone "ValveBiped.Bip01_R_Wrist" "ValveBiped.Bip01_R_Forearm"
	replacebone "ValveBiped.Bip01_L_Bicep" "ValveBiped.Bip01_L_upperarm"
	replacebone "ValveBiped.Bip01_R_Bicep" "ValveBiped.Bip01_R_upperarm"
}
===Desactivar animación facial===
Character models at a distance are often so small that the viewer would never make out any facial animation on them. When this is the case, you can use the nofacial to disable face animation for that LOD level.
$lod 30 
{
	nofacial
}
===Ejemplo de .qc completo===
The following is an example of a complete .qc file for a humanoid character model. You'll see we define 5 LOD levels, a specific shadow LOD, and the texture and skeleton simplification methods.
// *** mode and material paths *** //
//
$cd .\
$modelname candy.mdl
$cdmaterials models\candy\
$cdmaterials models\human\female
$mostlyopaque
// *** start eye/face/body data *** //
//
$eyeposition 0.000 0.000 70.000
$illumposition 1.165 -0.000 34.196
// *** head controllers *** //
//
$attachment "eyes" "ValveBiped.Bip01_Head1" -0.0454 -3.2682 67.3843 absolute
$attachment "mouth" "ValveBiped.Bip01_Head1" 0.80 -5.50 0.10 rotate 0 -80 -90
$attachment "chest" "ValveBiped.Bip01_Spine2" 5.00 4.00 0.00 rotate 0 90 90
// *** include anims *** //
//
$includemodel humans/female_shared.mdl
$includemodel humans/female_ss.mdl
$includemodel humans/female_gestures.mdl
$includemodel humans/female_postures.mdl
// *** head mesh and rig body *** //
//
$model candy_body ".\shared\candy_body_ref.smd"
$model candy_head ".\shared\candy_face_ref.smd"
// *** set-up LOD *** //
//
// LOD 1
$lod 10
{
 // replace LOD 0 meshes with our lower poly ones
	replacemodel ".\shared\candy_face_ref.smd" ".\shared\candy_face_ref_LOD1.smd"
	replacemodel ".\shared\candy_body_ref.smd" ".\shared\candy_body_ref_LOD1.smd"
}
// LOD 2
$lod 20 
{
	replacemodel ".\shared\candy_face_ref.smd" ".\shared\candy_face_ref_LOD2.smd"
	replacemodel ".\shared\candy_body_ref.smd" ".\shared\candy_body_ref_LOD2.smd"
}
// LOD 3
$lod 30 
{
	replacemodel ".\shared\candy_face_ref.smd" ".\shared\candy_face_ref_LOD3.smd"
	replacemodel ".\shared\candy_body_ref.smd" ".\shared\candy_body_ref_LOD3.smd"
	// collapse the hands down to the hand bone, removing the fingers
 bonetreecollapse "ValveBiped.Bip01_L_Hand"
 bonetreecollapse "ValveBiped.Bip01_R_Hand"
	// remove our eye material/shader with something simpler
 replacematerial "candy_righteye" "candy_face"
	replacematerial "candy_lefteye" "candy_face"
 // turn off facial animation
	nofacial
}
// LOD 4
$lod 45 
{
	replacemodel ".\shared\candy_face_ref.smd" ".\shared\candy_face_ref_LOD4.smd"
	replacemodel ".\shared\candy_body_ref.smd" ".\shared\candy_body_ref_LOD4.smd"
 // simplifiy our skeleton further by collapsing extremities
 bonetreecollapse "ValveBiped.Bip01_L_Forearm"
 bonetreecollapse "ValveBiped.Bip01_R_Forearm"
 bonetreecollapse "ValveBiped.Bip01_L_Calf"
 bonetreecollapse "ValveBiped.Bip01_R_Calf"
 bonetreecollapse "ValveBiped.Bip01_Neck1"
 // simplifiy the spine
 replacebone "ValveBiped.Bip01_Neck1" "ValveBiped.Bip01_Spine2"
 replacebone "ValveBiped.Bip01_Spine4" "ValveBiped.Bip01_Spine2"
 replacebone "ValveBiped.Bip01_Spine1" "ValveBiped.Bip01_Spine"
 replacebone "ValveBiped.Bip01_Spine" "ValveBiped.Bip01_Spine2"
	// simplifiy by removing the helper bones
 	replacebone "ValveBiped.Bip01_L_Elbow" "ValveBiped.Bip01_L_upperarm"
	replacebone "ValveBiped.Bip01_R_Elbow" "ValveBiped.Bip01_R_upperarm"
	replacebone "ValveBiped.Bip01_L_Ulna" "ValveBiped.Bip01_L_Forearm"
	replacebone "ValveBiped.Bip01_R_Ulna" "ValveBiped.Bip01_R_Forearm"
	replacebone "ValveBiped.Bip01_L_Shoulder" "ValveBiped.Bip01_L_upperarm"	
	replacebone "ValveBiped.Bip01_R_Shoulder" "ValveBiped.Bip01_R_upperarm"
	replacebone "ValveBiped.Bip01_L_Trapezius" "ValveBiped.Bip01_L_upperarm"
	replacebone "ValveBiped.Bip01_R_Trapezius" "ValveBiped.Bip01_R_upperarm"
	replacebone "ValveBiped.Bip01_L_Wrist" "ValveBiped.Bip01_L_Forearm"
	replacebone "ValveBiped.Bip01_R_Wrist" "ValveBiped.Bip01_R_Forearm"
	replacebone "ValveBiped.Bip01_L_Bicep" "ValveBiped.Bip01_L_upperarm"
	replacebone "ValveBiped.Bip01_R_Bicep" "ValveBiped.Bip01_R_upperarm"
	
	replacematerial "candy_righteye" "candy_face"
	replacematerial "candy_lefteye" "candy_face"
 // remove any meshes covered by the eye textures
	removemesh "models\candy\candy_righteye"
	removemesh "models\candy\candy_lefteye"
	nofacial
}
// LOD 5
$lod 70 
{
	replacemodel ".\shared\candy_face_ref.smd" ".\shared\candy_face_ref_LOD5.smd"
	replacemodel ".\shared\candy_body_ref.smd" ".\shared\candy_body_ref_LOD5.smd"
 bonetreecollapse "ValveBiped.Bip01_L_Forearm"
 bonetreecollapse "ValveBiped.Bip01_R_Forearm"
 bonetreecollapse "ValveBiped.Bip01_L_Calf"
 bonetreecollapse "ValveBiped.Bip01_R_Calf"
 bonetreecollapse "ValveBiped.Bip01_Neck1"
 replacebone "ValveBiped.Bip01_Neck1" "ValveBiped.Bip01_Spine2"
 replacebone "ValveBiped.Bip01_Spine4" "ValveBiped.Bip01_Spine2"
 replacebone "ValveBiped.Bip01_Spine1" "ValveBiped.Bip01_Spine"
 replacebone "ValveBiped.Bip01_Spine" "ValveBiped.Bip01_Spine2"
	
 	replacebone "ValveBiped.Bip01_L_Elbow" "ValveBiped.Bip01_L_upperarm"
	replacebone "ValveBiped.Bip01_R_Elbow" "ValveBiped.Bip01_R_upperarm"
	replacebone "ValveBiped.Bip01_L_Ulna" "ValveBiped.Bip01_L_Forearm"
	replacebone "ValveBiped.Bip01_R_Ulna" "ValveBiped.Bip01_R_Forearm"
	replacebone "ValveBiped.Bip01_L_Shoulder" "ValveBiped.Bip01_L_upperarm"	
	replacebone "ValveBiped.Bip01_R_Shoulder" "ValveBiped.Bip01_R_upperarm"
	replacebone "ValveBiped.Bip01_L_Trapezius" "ValveBiped.Bip01_L_upperarm"
	replacebone "ValveBiped.Bip01_R_Trapezius" "ValveBiped.Bip01_R_upperarm"
	replacebone "ValveBiped.Bip01_L_Wrist" "ValveBiped.Bip01_L_Forearm"
	replacebone "ValveBiped.Bip01_R_Wrist" "ValveBiped.Bip01_R_Forearm"
	replacebone "ValveBiped.Bip01_L_Bicep" "ValveBiped.Bip01_L_upperarm"
	replacebone "ValveBiped.Bip01_R_Bicep" "ValveBiped.Bip01_R_upperarm"
	replacematerial "candy_righteye" "candy_face"
	replacematerial "candy_lefteye" "candy_face"
	removemesh "models\candy\candy_righteye"
	removemesh "models\candy\candy_lefteye"
	nofacial
}
// SHADOW
$shadowlod
{
 // use our existing LOD 4 mesh as a simpler mesh for rendering the models shadow
 //
	replacemodel ".\shared\candy_face_ref.smd" ".\shared\candy_face_ref_LOD4.smd"
	replacemodel ".\shared\candy_body_ref.smd" ".\shared\candy_body_ref_LOD4.smd"
 bonetreecollapse "ValveBiped.Bip01_L_Forearm"
 bonetreecollapse "ValveBiped.Bip01_R_Forearm"
 bonetreecollapse "ValveBiped.Bip01_L_Calf"
 bonetreecollapse "ValveBiped.Bip01_R_Calf"
 bonetreecollapse "ValveBiped.Bip01_Neck1"
 replacebone "ValveBiped.Bip01_Neck1" "ValveBiped.Bip01_Spine2"
 replacebone "ValveBiped.Bip01_Spine4" "ValveBiped.Bip01_Spine2"
 replacebone "ValveBiped.Bip01_Spine1" "ValveBiped.Bip01_Spine"
 replacebone "ValveBiped.Bip01_Spine" "ValveBiped.Bip01_Spine2"
	
 	replacebone "ValveBiped.Bip01_L_Elbow" "ValveBiped.Bip01_L_upperarm"
	replacebone "ValveBiped.Bip01_R_Elbow" "ValveBiped.Bip01_R_upperarm"
	replacebone "ValveBiped.Bip01_L_Ulna" "ValveBiped.Bip01_L_Forearm"
	replacebone "ValveBiped.Bip01_R_Ulna" "ValveBiped.Bip01_R_Forearm"
	replacebone "ValveBiped.Bip01_L_Shoulder" "ValveBiped.Bip01_L_upperarm"	
	replacebone "ValveBiped.Bip01_R_Shoulder" "ValveBiped.Bip01_R_upperarm"
	replacebone "ValveBiped.Bip01_L_Trapezius" "ValveBiped.Bip01_L_upperarm"
	replacebone "ValveBiped.Bip01_R_Trapezius" "ValveBiped.Bip01_R_upperarm"
	replacebone "ValveBiped.Bip01_L_Wrist" "ValveBiped.Bip01_L_Forearm"
	replacebone "ValveBiped.Bip01_R_Wrist" "ValveBiped.Bip01_R_Forearm"
	replacebone "ValveBiped.Bip01_L_Bicep" "ValveBiped.Bip01_L_upperarm"
	replacebone "ValveBiped.Bip01_R_Bicep" "ValveBiped.Bip01_R_upperarm"
	replacematerial "candy_righteye" "candy_face"
	replacematerial "candy_lefteye" "candy_face"
	removemesh "models\candy\candy_righteye"
	removemesh "models\candy\candy_lefteye"
	nofacial
}