## Halfway there

The 16th July midterm deadline just passed and I haven’t posted in a while, so I am going to make a short presentation about what I have done so far and what is still to be done for this GSoC project.

Done:

- Generated geometry – iFurMaterial
- Animated geometry – iFurPhysicsControl
- Written specific shaders – iFurStrandGenerator

TODO’s:

- LOD – working on it
- Shadows – at least receiving shadows from other objects
- Blender integration – if there is any time left

Recently I have finished adding support for density and height maps, and I will soon write how I have done this. I think that the way in which hair strands are generated, based on the density map, is quite general and could be used even for an adaptive tessellation project, so I will try to write my next post about this as soon as possible.

Until then I leave you with this video, showing more or less what I have implemented so far (YouTubeHD):

## Marschner Shader Part III

This is the last part of the three post regarding the Marschner shader. I will explain how to efficiently make the shader for this model, how to add ambient and diffuse lighting and at the end of the post I will also give source code for generating Marschner lookup textures and a video showing the results I had in CS.

**Lookup Textures**

Because there are too many computations done in *M* and *N* functions to be put in the pixel shader, the best optimization is to use lookup textures, that need to be updated as rarely as possible.

We can easily observe that apart from the constants defined in Table 1 ( page 8 ) from Marschner’s paper, the *M* function only depends on *q* _{ i }and *q* _{r }, and *N *on* q* _{ d }and *f* _{ d}. Although this might seem a good optimization at first, taking into account that all these angles must be computed from inverse trigonometric functions, such as acos and asin, which aren’t fast at all, indexing the lookup textures directly by cos and sin sounds a better idea.

The way in which sinus and cosinus values can be computed for all these angles can be found in GPU Gems 2, Chapter 23:

- sin
*q*_{ i }= (*light*·*Tangent*), - sin
*q*_{ o }= (*eye*·*Tangent*). *lightPerp*=*light*– (*light*·*tangent*) x*tangent*,*eyePerp*=*eye*– (*eye*·*tangent*) x*tangent*.- cos
*f*_{ d }= (*eyePerp*·*lightPerp*) x ((*eyePerp*·*eyePerp*) x (*lightPerp*·*lightPerp*))^{-0.5}

As for the cos *q* _{d} if we observe that *q* _{ d} depends on *q* _{ i } and *q* _{r }then we figure out that we can use a channel from the lookup texture indexed by the sins of these two angles.

The easiest way to build these two textures is to make a lookup texture for *M*, having *M _{R, }*

*M*

_{TT, }*M*and cos

_{TRT}*q*

_{d, }and a lookup texture for

*N.*However, in the original paper

*N*and

_{TT }*N*each have three channels, but they can be reduced to only one channel if we consider the absorption to have one channel as well.

_{TRT }These are the lookup textures obtained with my first implementation of the Marschner project:

**Ambient and diffuse lighting**

The Marschner model only specifies the specular component for lighting, so in order to obtain nice visual effects, both ambient and diffuse lighting were added to this model.

I used the lighting from the Nalu Demo, presented in detail in one of my previous posts:

/* Compute diffuse lighting with phi-dependent component */ float diffuse = sqrt(max(0.0001, 1 - uv1.x * uv1.x)); /* Pass colors */ float4 diffuseColor; diffuseColor.rgb = diffuse * objColor.rgb * DiffuseCol; diffuseColor.a = objColor.a; float3 ambientColor; ambientColor = objColor.rgb * AmbientCol; float3 lighting = (( M.r * N.r + M.g * N.g + M.b * N.b ) / (cos_qd * cos_qd)); lighting += diffuseColor.rgb; OUT.xyz = lighting + diffuseColor.rgb * 0.2 + IN.AmbientColor;

**Source code**

Here you can find the first version of my Marschner C# Project, which generates the lookup textures needed for a shader similar to the one presented in the Nalu Demo post.

There are still some things that can be improved, but I plan to release another version for that, as soon as I get a chance. Until then feel free to improve the project yourself.

These are the two adjustments done to the original model, as described in Marschner:

- The absorption is specify by only one channel.
- Instead of the standard
*N*component, the simplify version was used._{TRT}

You can find more information in the README, INSTALL and LICENSE files from the archive.

**Demo**

Next you can see the effects this shader has on Krystal’s hair. If you want to play with the application yourself checkout the hair branch from CS main repository.