

































































































































































































;------------------------------------------------------------------------------
; Vertex shader for zsprite
;------------------------------------------------------------------------------











;------------------------------------------------------------------------------
;custom inputs
;c[10,13] - view*proj matrix
;c14     - Stage 2 texture coords (ZScale, 0, ZBias)
;c15     - Stage 3 texture coords (0, 0, 1)
;c17     - Camera position
;c18     - World radius of sprite (default: 50.0)
;c19     - D3DZ_MAX
;c[20,23] - world matrix
;------------------------------------------------------------------------------

;Step 1: Transform the vertex by the World * View * Projection matrix
m4x4		r0,v0,c6

; Add in the appropriate billboard vectors
; Store a copy of the transformed position in R4
; (These are already transformed into projection space)
mad     r0, v13.x, c24, r0
mad     r4, v13.y, c25, r0

mov oPos,r4

;Step 2: Set up initial texture coords from constants & vertex stream

mov oT0, v3
mov oT1, v3

;For stages 2 and 3, we just copy the constants over
mov oT2, c14
mov oT3, c15

;Propagate the color
mov oD0, c2

;Step 3: Calculate stage 2 texture coords
;We need to calculate the Scale factor for T2, because the lookup from the
;depth texture will get transformed to [0, 1].  The second coordinate is used
;to scale those depth values out to the appropriate values in our depth
;buffer.  We calculate this by taking the world coordinates of the center of
;the explosion (c7) and the world coordinates of the near edge of the
;explosion (c8), and transforming them by our view and projection transforms
;(c10-13).  We then project the resultant vectors back into w=1 before
;calculating the distance between them.  Finally, we take that value and
;multiply by D3DZ_MAX for the appropriate depth buffer format, so that we
;end up with values in the appropriate range 

;Transform and project vertex (already done above in r4)
;then project to w=1
rcp r2,r4.w
mul r0,r4,r2

; Hack near edge by pushing the vertex position towards the camera by a
; distance equal to the sprite's world radius
; This will give us a little distortion, but that's ok for the moment

; transform vpos to world space
m4x4 r9, v0, c20 ; this is identity unless the emitter is flagged as relative

; Find camera vector and normalize

; Subtract camera position from vertex position in world space
sub r5,r9,c17
; normalize 
dp3 r8, r5, r5
rsq r6, r8
mul r5, r5, r6

; Push towards camera
mad r5, r5, c18, r9

; Transform and project near edge of explosion (c8)
m4x4 r1,r5,c10
; Project to w=1
rcp r2,r1.w
mul r1,r1,r2

; Subtract vectors and calculate length into r1
sub r0,r0,r1
dp3 r2,r0,r0
rsq r3,r2
rcp r1,r3

; Multiply length by D3DZ_MAX for scale
; Negate or not depending on whether we want
; to push forward or back (this should coordinate
; with the texture), and whether our
; reference point will be the original vertex
; or the near vertex
mul oT2.x,r1,c19

; The pixel shader debugger seems to bound this
; to 0.0-1.0, so try giving it a value in that range
;mov oT2.x,r1

; Still not working (values are ~60)
; Try dividing by ZMAX?
;rcp r2, c19
;mul oT2.x,r1,r2

; Calculate transformed Z - this is the Zbias component of 
; oT2, which represents the depth of the near edge of the
; explosion, in the depth buffer format.  We take the transformed
; vertex from above, project into w=1, and scale by D3DZ_MAX

rcp r1,r4.w
mul r1,r1,r4.z
;TMP - zero the zbias
mov oT2.z, c0.z
; mul oT2.z,r1,c19
