Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimize Normalmap rotation of a vector
    primarykey
    data
    text
    <p>I use a quaternion to rotate the normal vector of a mesh into the direction the normal map vector. I thought I could use the Quaternion. So I create a quaternion with the angle between the normal of the mesh and (0,0,1). Shortened the methods in libgdx, creation of the Quaternion looks like this</p> <pre><code>vec4 createQuaternion(vec3 normal) { float l_ang = acos(clamp(normal.z, -CONST_ONE, CONST_ONE))/CONST_TWO; float l_sin = sin(l_ang); return normalize(vec4(-normal.y * l_sin, normal.x * l_sin, CONST_ZERO, cos(l_ang))); } </code></pre> <p>with this Quaternion I can rotate the normal of the mesh in the direction of the normal map. Therefore I use a method I've seen in the libgdx Quaternion (while v is the normal from the normalmap and m is the quaternion):</p> <pre><code>vec3 rotateNormal(vec3 v, vec4 m) { vec4 tmp1 = vec4(v,CONST_ZERO); vec4 tmp2 = vec4(m); // conjugate tmp2.x = -tmp2.x; tmp2.y = -tmp2.y; tmp2.z = -tmp2.z; tmp2 = mulLeft(tmp1, tmp2); tmp1 = mulLeft(m, tmp2); v.x = tmp1.x; v.y = tmp1.y; v.z = tmp1.z; return v; } </code></pre> <p>The method mulLeft looks like that:</p> <pre><code>vec4 mulLeft(vec4 q, vec4 a) { float newX = q.w * a.x + q.x * a.w + q.y * a.z - q.z * a.y; float newY = q.w * a.y + q.y * a.w + q.z * a.x - q.x * a.z; float newZ = q.w * a.z + q.z * a.w + q.x * a.y - q.y * a.x; float newW = q.w * a.w - q.x * a.x - q.y * a.y - q.z * a.z; a.x = newX; a.y = newY; a.z = newZ; a.w = newW; return a; } </code></pre> <p>For the use in the shader I just call:</p> <pre><code>normal = rotateNormal(normalMap, createQuaternion(normalMesh)); </code></pre> <p>and <strong>it works as expected</strong>.</p> <p>The only thing I am considered about is that I can imagine that there is a shorter way to write that. Especially the mulLeft Method. Is there?</p> <p>What do you think about the whole method, turning the vector by quaternion instead of using NTB? ntb looks a bit like too much calculation while animating the mesh.</p> <p>Edit: This is the a test with my sourcecode</p> <p><img src="https://i.stack.imgur.com/a569j.png" alt="My Code"></p> <p>This is a Test with Tenfour's suggestion</p> <p><img src="https://i.stack.imgur.com/OxpEB.png" alt=""></p> <p>Edit2:</p> <p>I shortened the whole thing to:</p> <pre><code>vec3 rotateNormal(vec3 normalMap, vec3 normal) { // create quaternion from cross(normal, vec3(0,0,1)) float l_ang = acos(clamp(normal.z, -CONST_ONE, CONST_ONE))/CONST_TWO; float l_sin = sin(l_ang); vec4 quat = normalize(vec4(-normal.y * l_sin, normal.x * l_sin, CONST_ZERO, cos(l_ang))); // shortened function to double mulQuat the normalMap on the quaternion return vec3( quat.x*quat.x*normalMap.x - quat.y*quat.y*normalMap.x - quat.z*quat.z*normalMap.x + quat.w*quat.w*normalMap.x - CONST_TWO*quat.z*quat.w*normalMap.y + CONST_TWO*quat.y*quat.w*normalMap.z + CONST_TWO*quat.x *(quat.y*normalMap.y + quat.z*normalMap.z), -(quat.x*quat.x*normalMap.y) - quat.z*quat.z* normalMap.y + (quat.y*quat.y + quat.w*quat.w ) * normalMap.y + CONST_TWO*quat.z *(quat.w*normalMap.x + quat.y*normalMap.z) + CONST_TWO*quat.x* (quat.y*normalMap.x - quat.w*normalMap.z), quat.y*(-CONST_TWO*quat.w*normalMap.x + CONST_TWO*quat.z*normalMap.y) + CONST_TWO*quat.x*(quat.z*normalMap.x + quat.w*normalMap.y) - quat.x*quat.x*normalMap.z - quat.y*quat.y*normalMap.z + (quat.z*quat.z + quat.w*quat.w )* normalMap.z ); } </code></pre> <p>Input is the normal from the normal map and the normal of the mesh. I am sure there's something better than that. what could that be?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload