📄 twosided.nvv
字号:
; This shader does the per-vertex dot3 work.
; It transforms the light vector by the basis vectors
; passed into the shader.
; The basis vectors only need to change if the model's
; shape changes.
; The output vector is stored in the diffuse channel
; (use the menu to look at the generated light vector)
#include "TwoSided.h"
#define V_POSITION v0
#define V_NORMAL v1
#define V_DIFFUSE v2
#define V_TEXTURE v3
#define VEC_VERT_TO_EYE r0
#define VEC_VERT_TO_LIGHT r1
#define ALIGNED r2
#define EYE_LOCAL r3
#define EYE_VECTOR r5
#define TEMP r6
#define COR_NORMAL r7
vs.1.0
; Transform position to clip space and output it
dp4 oPos.x, V_POSITION, c[CV_WORLDVIEWPROJ_0]
dp4 oPos.y, V_POSITION, c[CV_WORLDVIEWPROJ_1]
dp4 oPos.z, V_POSITION, c[CV_WORLDVIEWPROJ_2]
dp4 oPos.w, V_POSITION, c[CV_WORLDVIEWPROJ_3]
; Use camera position, light position, and vertex
; position to determine correct orientation of the
; normal vector -- ie flip the normal if the one-sided
; object's face is pointing the wrong way.
; Camera position was transformed to object space
; in the shader setup. Light position was also
; transformed to object space. This avoids having
; to transform the normals to world space.
; Make vec from vertex to camera
sub VEC_VERT_TO_EYE, c[CV_EYE_POS_OSPACE], V_POSITION
; Dot the v to eye and the normal to see if they point
; in the same direction or opposite
dp3 ALIGNED, V_NORMAL, VEC_VERT_TO_EYE
; If aligned is positive, no correction is needed.
; If aligned is negative, we need to flip the normal
; Do this with tricky SGE logic stuff
sge ALIGNED.x, ALIGNED, c[CV_ZERO] ; if aligned.x >= 0 set x component to one
add ALIGNED.y, c[CV_ONE], -ALIGNED.x ; set y component to logical opposite of x
mul COR_NORMAL,-V_NORMAL, ALIGNED.x
mad COR_NORMAL, V_NORMAL, ALIGNED.y, COR_NORMAL ; corrected normal for one-sided object
; Point lighting
; Normalized vector from vertex to the light:
add VEC_VERT_TO_LIGHT, -c[CV_LIGHT_POS_OSPACE], V_POSITION
dp3 TEMP.w, VEC_VERT_TO_LIGHT, VEC_VERT_TO_LIGHT
rsq TEMP.w, TEMP.w
mul VEC_VERT_TO_LIGHT, VEC_VERT_TO_LIGHT, TEMP.w
; Dot the vectors for the diffuse component from the
; point light
; Point light is not attenuated
dp3 r4, VEC_VERT_TO_LIGHT, COR_NORMAL
; Use lit instruction to clamp for negative values of the dp3
; r5 will have diffuse light value in y component
lit r5, r4
; Light color to output diffuse color
add oD0, r5.y, c[CV_LIGHT_CONST].x ; x holds ambient
; another lit for the light value from opposite normal
; Put it in specular color
lit r5, -r4
mul r5.y, r5.y, r5.y ; square it for more attenuation
mad oD1, r5.y, c[CV_LIGHT_CONST].z, c[CV_LIGHT_CONST].y ; diffuse * atten + ambient
mov oT0, V_TEXTURE
mov oT1, V_TEXTURE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -