Skip to content

Commit

Permalink
Miasmata: Fix light shafts (new technique)
Browse files Browse the repository at this point in the history
This is the vertex shader for the final stage of the light shafts in
this game, where they are transferred from a radial coordinate system
into screen coordinates.

The vertex shader has an input which I believe is the world coordinates
of the vertex being operated on, which need to be corrected. Problem is
- we have no way to get the correct amount to adjust it by in world
coordinates. As such, we take the world-view-projection matrix of an
object that is never rotated, in this case we just use the water matrix
since we already have it. We run two points back through this shader and
subtract the resulting points, which removes the objects translation
leaving the correction amount in world space.
  • Loading branch information
DarkStarSword committed Jan 28, 2015
1 parent de625c3 commit 8c8f51d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Miasmata/DX9Settings.ini
Expand Up @@ -47,3 +47,8 @@ UseMatrix = true
GetMatrixFromReg = 0
InverseMatrix = true
MatrixReg = 180

[VS0326C994]
; Light shaft shader - uses this matrix to determine the correction amount in world-coordinates
UseMatrix = true
MatrixReg = 180
37 changes: 36 additions & 1 deletion Miasmata/ShaderOverride/VertexShaders/0326C994.txt
Expand Up @@ -64,14 +64,49 @@
dcl_texcoord o1.xy
dcl_texcoord1 o2.xyz
dcl_texcoord2 o3

def c220, 0, 1, 0.0625, 2

dcl_2d s0
texldl r31, c220.z, s0

mad r0.xy, v0, c4.x, c4.y
mul r0.xy, r0, c2
mov r0.z, c4.z
mad o3.xy, r0, r0.z, c3
mad o0.xy, v0, c4.x, c4.y
mov o0.zw, -c4.xywy
mad o1.xy, v0, c5, c5.zxzw
mov o2.xyz, v1
//mov o2.xyz, v1
mov o3.zw, c1.xyxy

// v1 is in world coordinates and needs to be corrected. Unfortunately we don't
// have access to the inverse view-projection matrix, nor do we have any way to
// derive it. So let's use what we *do* have - the world-view-projection,
// copied from the water shader and inverted. The water doesn't rotate, so we
// can use that - we run both the correction amount through it, as well as a
// second point at the origin, then subtract the origin from the correction and
// we wind up with the correction amount in world space independent of the
// translation of the water. Simple, ey?

// Run a reference point at the origin 0,0,0,1 through the matrix:
mov r30.xyzw, c220.xxxy
dp4 r10.x, r30, c180
dp4 r10.y, r30, c181
dp4 r10.z, r30, c182
dp4 r10.w, r30, c183

// Run the correction amount through the matrix:
mov r30.xyzw, c220.xxxy
mov r30.x, r31.x
dp4 r11.x, r30, c180
dp4 r11.y, r30, c181
dp4 r11.z, r30, c182
dp4 r11.w, r30, c183

// Subtract one from the other:
add r12, r11, -r10
// And adjust the coordinate:
add o2.xyz, v1, -r12

// approximately 9 instruction slots used

0 comments on commit 8c8f51d

Please sign in to comment.