;stream 0;
;float    v0[3];  // Position
;D3DCOLOR v3;     // Normal
;float    v7[2];  // Tex coord 0
;D3DCOLOR v8;     // Binormal
;D3DCOLOR v9;     // Tangent

; Constants:
; 0..3 - Composite World-View-Projection Matrix
; 4..7 - World Matrix
; 8     - Object Space Light Position
; 9     - Object space Eye vector
//def c10, 0.5f,0.5f,0.5f,0.5f
; 11    - light color


vs.1.0


dcl_position0 v0
dcl_normal0   v3
dcl_texcoord0 v7
dcl_texcoord1 v8
dcl_texcoord2 v9

m4x4 oPos, v0, c0
mov  oT0.xy, v7.xy   ; Base Texture
mov  oT1.xy, v7.xy   ; Normal Texture

;convert from D3DColor to -1 to 1 range
;mad  r3, v3, c95.wwwz, -c95.zzzx ; Normal = (v3*2.0)-1.0
;mad  r9, v8, c95.wwwz, -c95.zzzx ; TangentU = (v8*2.0)-1.0
;mad  r8, v9, c95.wwwz, -c95.zzzx ; TangentV = (v9*2.0)-1.0





; transform light by basis vectors to put it
; into texture space
add r8,c8,-v0
dp3 r2.x, v8.xyz, r8			// light direction
dp3 r2.y, v9.xyz, r8
dp3 r2.z, v3.xyz, r8

dp3	 r11.x, r8, r8	   ; magnitude^2
rsq	 r11.y, r11.x	   ; 1/mag
mul	 r8.xyz, r8, r11.y ; normalize

//dp3 	r7.x, v3, c0
//dp3 	r7.y, v3, c1
//dp3 	r7.z, v3, c2

//dp3	 r11.x, v3, v3	   ; magnitude^2
//rsq	 r11.y, r11.x	   ; 1/mag
//mul	 r7.xyz, v3, r11.y ; normalize

dp3 r1.x, v3, r8    // perform lighting N dot L calculation in world space     
//max r1, r1.x, c10.x     ; if dot < 0 then dot = 0
//mad r1, c10,r1,c10	// -1..1 -> 0..1
mul oD0,r1.rrrr,c11	// *subsurface color
//mul oD0,c10.rrrr,r1	// *0.5

//add oD0,r1,c28
//mov oD0,c10

dp3	 r11.x, r2, r2	   ; magnitude^2
rsq	 r11.y, r11.x	   ; 1/mag
mul	 r4.xyz, r2, r11.y ; normalize
mov  oT2.xyz, r4.xyz   ; Tangent Space Light vector

;tangent space halfangle vector 
sub  r5, c9, v0        ; Object space Eye vector
dp3  r6.x, v8, r5
dp3  r6.y, v9, r5
dp3  r6.z, v3, r5       ; Tangent Space Eye vector
dp3	 r11.x, r6, r6	    ; magnitude^2
rsq	 r11.y, r11.x	    ; 1/mag
mul	 r7.xyz, r6, r11.y  ; normalize
add  r8.xyz, r4, r7     ; Halfangle = Light + Eye
dp3	 r11.x, r8, r8	    ; magnitude^2
rsq	 r11.y, r11.x	    ; 1/mag
mul	 oT3.xyz, r8, r11.y ; normalized tangent space halfangle vector




