https://www.cs.utah.edu/~ladislav/dq/dqs.cg
https://www.cs.utah.edu/~ladislav/dq/index.html

/* dqs.cg

  Dual quaternion skinning vertex shaders (no shading computations)

  Version 1.0.3, November 1st, 2007

  Copyright (C) 2006-2007 University of Dublin, Trinity College, All Rights 
  Reserved

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the author(s) be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Author: Ladislav Kavan, ladislav.kavan@gmail.com

*/

struct inputs
{
	float4 position : POSITION;
	float4 normal : NORMAL;
	float4 weights : TEXCOORD1;
	float4 matrixIndices : TEXCOORD2;	
};

struct outputs	
{
	float4 hPosition : POSITION;
	float4 hNormal : TEXCOORD1;
};

float3x4 DQToMatrix(float4 Qn, float4 Qd)
{	
	float3x4 M;
	float len2 = dot(Qn, Qn);
	float w = Qn.x, x = Qn.y, y = Qn.z, z = Qn.w;
	float t0 = Qd.x, t1 = Qd.y, t2 = Qd.z, t3 = Qd.w;
		
	M[0][0] = w*w + x*x - y*y - z*z; M[0][1] = 2*x*y - 2*w*z; M[0][2] = 2*x*z + 2*w*y;
	M[1][0] = 2*x*y + 2*w*z; M[1][1] = w*w + y*y - x*x - z*z; M[1][2] = 2*y*z - 2*w*x; 
	M[2][0] = 2*x*z - 2*w*y; M[2][1] = 2*y*z + 2*w*x; M[2][2] = w*w + z*z - x*x - y*y;
	
	M[0][3] = -2*t0*x + 2*w*t1 - 2*t2*z + 2*y*t3;
	M[1][3] = -2*t0*y + 2*t1*z - 2*x*t3 + 2*w*t2;
	M[2][3] = -2*t0*z + 2*x*t2 + 2*w*t3 - 2*t1*y;
	
	M /= len2;
	
	return M;	
}

// basic dual quaternion skinning:
outputs dqs(inputs IN,
			uniform float4x4 modelViewProj,
			uniform float4x4 modelViewIT,
			uniform float2x4 boneDQ[100])
{
	outputs OUT;					
		
	float2x4 blendDQ = IN.weights.x*boneDQ[IN.matrixIndices.x];
	blendDQ += IN.weights.y*boneDQ[IN.matrixIndices.y];
	blendDQ += IN.weights.z*boneDQ[IN.matrixIndices.z];
	blendDQ += IN.weights.w*boneDQ[IN.matrixIndices.w];	
	
	float3x4 M = DQToMatrix(blendDQ[0], blendDQ[1]);
	float3 position = mul(M, IN.position);
	float3 normal = mul(M, IN.normal);
		
	OUT.hPosition = mul(modelViewProj, float4(position, 1.0));
	OUT.hNormal = mul(modelViewIT, float4(normal, 0.0));
	
	return OUT;			
}

// per-vertex antipodality handling (this is the most robust, but not the most efficient way):
outputs dqsAntipod(inputs IN,
			uniform float4x4 modelViewProj,
			uniform float4x4 modelViewIT,
			uniform float2x4 boneDQ[100])
{
	outputs OUT;					
		
	float2x4 dq0 = boneDQ[IN.matrixIndices.x];
	float2x4 dq1 = boneDQ[IN.matrixIndices.y];
	float2x4 dq2 = boneDQ[IN.matrixIndices.z];
	float2x4 dq3 = boneDQ[IN.matrixIndices.w];

	if (dot(dq0[0], dq1[0]) < 0.0) dq1 *= -1.0;
	if (dot(dq0[0], dq2[0]) < 0.0) dq2 *= -1.0;	
	if (dot(dq0[0], dq3[0]) < 0.0) dq3 *= -1.0;
	
	float2x4 blendDQ = IN.weights.x*dq0;
	blendDQ += IN.weights.y*dq1;
	blendDQ += IN.weights.z*dq2;
	blendDQ += IN.weights.w*dq3;
	
	float3x4 M = DQToMatrix(blendDQ[0], blendDQ[1]);
	float3 position = mul(M, IN.position);
	float3 normal = mul(M, IN.normal);
		
	OUT.hPosition = mul(modelViewProj, float4(position, 1.0));
	OUT.hNormal = mul(modelViewIT, float4(normal, 0.0));
	
	return OUT;			
}

// optimized version (avoids dual quaternion - matrix conversion):
outputs dqsFast(inputs IN,
			uniform float4x4 modelViewProj,
			uniform float4x4 modelViewIT,
			uniform float2x4 boneDQ[100])
{
	outputs OUT;			
		
	float2x4 blendDQ = IN.weights.x*boneDQ[IN.matrixIndices.x];
	blendDQ += IN.weights.y*boneDQ[IN.matrixIndices.y];
	blendDQ += IN.weights.z*boneDQ[IN.matrixIndices.z];
	blendDQ += IN.weights.w*boneDQ[IN.matrixIndices.w];	
		
	float len = length(blendDQ[0]);
	blendDQ /= len;

	float3 position = IN.position.xyz + 2.0*cross(blendDQ[0].yzw, cross(blendDQ[0].yzw, IN.position.xyz) + blendDQ[0].x*IN.position.xyz);
	float3 trans = 2.0*(blendDQ[0].x*blendDQ[1].yzw - blendDQ[1].x*blendDQ[0].yzw + cross(blendDQ[0].yzw, blendDQ[1].yzw));
	position += trans;

	float3 inpNormal = IN.normal.xyz;
	float3 normal = inpNormal + 2.0*cross(blendDQ[0].yzw, cross(blendDQ[0].yzw, inpNormal) + blendDQ[0].x*inpNormal);

	OUT.hPosition = mul(modelViewProj, float4(position, 1.0));
	OUT.hNormal = mul(modelViewIT, float4(normal, 0.0));
		
	return OUT;
}

float3x3 adjointTransposeMatrix(float3x3 M)
{
	float3x3 atM;
	atM._m00 = M._m22 * M._m11 - M._m12 * M._m21;
	atM._m01 = M._m12 * M._m20 - M._m10 * M._m22;
	atM._m02 = M._m10 * M._m21 - M._m20 * M._m11;

	atM._m10 = M._m02 * M._m21 - M._m22 * M._m01;
	atM._m11 = M._m22 * M._m00 - M._m02 * M._m20;
	atM._m12 = M._m20 * M._m01 - M._m00 * M._m21;

	atM._m20 = M._m12 * M._m01 - M._m02 * M._m11;
	atM._m21 = M._m10 * M._m02 - M._m12 * M._m00;
	atM._m22 = M._m00 * M._m11 - M._m10 * M._m01;

	return atM;
}

// two-phase skinning: dqsFast combined with scale/shear transformations:
outputs dqsScale(inputs IN,
			uniform float4x4 modelViewProj,
			uniform float4x4 modelViewIT,
			uniform float2x4 boneDQ[100],
			uniform float3x4 scaleM[100])
{
	outputs OUT;			

	// first pass:
	float3x4 blendS = IN.weights.x*scaleM[IN.matrixIndices.x];
	blendS += IN.weights.y*scaleM[IN.matrixIndices.y];
	blendS += IN.weights.z*scaleM[IN.matrixIndices.z];
	blendS += IN.weights.w*scaleM[IN.matrixIndices.w];	
		
	float3 pass1_position = mul(blendS, IN.position);
	float3x3 blendSrotAT = adjointTransposeMatrix(float3x3(blendS));
	float3 pass1_normal = normalize(mul(blendSrotAT, IN.normal.xyz));
		
	// second pass:
	float2x4 blendDQ = IN.weights.x*boneDQ[IN.matrixIndices.x];
	blendDQ += IN.weights.y*boneDQ[IN.matrixIndices.y];
	blendDQ += IN.weights.z*boneDQ[IN.matrixIndices.z];
	blendDQ += IN.weights.w*boneDQ[IN.matrixIndices.w];	
		
	float len = length(blendDQ[0]);
	blendDQ /= len;

	float3 position = pass1_position + 2.0*cross(blendDQ[0].yzw, cross(blendDQ[0].yzw, pass1_position) + blendDQ[0].x*pass1_position);
	float3 trans = 2.0*(blendDQ[0].x*blendDQ[1].yzw - blendDQ[1].x*blendDQ[0].yzw + cross(blendDQ[0].yzw, blendDQ[1].yzw));
	position += trans;

	float3 normal = pass1_normal + 2.0*cross(blendDQ[0].yzw, cross(blendDQ[0].yzw, pass1_normal) + blendDQ[0].x*pass1_normal);

	OUT.hPosition = mul(modelViewProj, float4(position, 1.0));
	OUT.hNormal = mul(modelViewIT, float4(normal, 0.0));
		
	return OUT;
}