필요한 변수
world,view,projection(mvp) 메트릭스! ,[필요시] [디퓨즈 컬러 ,스페큘러컬러,shiness]
빛 위치,카메라 위치(월드좌표계) ( 뷰좌표계에서 계산할거면 뷰에서 해도됨)
버텍스 인풋값
버텍스 포지션 , 노말 , uv좌표 저장할 변수
픽셀 인풋값
픽셀 포지션,uv좌표 저장할 변수,디퓨즈 rgb,viewDir,reflection[노멀라이즈안한거], _shiness[필요시]
버텍스 쉐이더에서 해야할거
1.포지션에 mvp 곱하기 ( 프로젝션 좌표계로 전환하기)
2.월드 좌표계에서 빛계산 (view에서 해도됨)
3.월드에서 할 시 생성해야할 변수
-worldNormal,worldPosition(좌표계 곱하기 전에 계산해도됨)
4.liightDir,viewDir,reflection 선언후 정의하기
( 월드좌표계시)
lightDir = normalize(worldPosition.xyz-lightWorldPosition.xyz);
viewDir = normalize(worldPosition.xyz-cameraWorldPosition.xyz);
reflection = reflect(lightDir,worldNormal);
5.디퓨즈값 계산하기
ndot( 빛의 밝기 ) = dot(lightDir,worldNormal);
Output.mDiffuse = ndot * diffuseColor[필요시];//안하면 float3(ndot,ndot,ndot);
6.반환
픽셀쉐이더에서 해야할거
전역변수
sampler2D DiffuseSampler(텍스처이미지)
픽셀쉐이더 인풋값
Position (포지션)
mTexCoord : TEXCOOR
전역변수는 이렇게 지정해주자.
버텍스의 인풋정보는 Stream Mapping을 더블클릭하면 되고,
정보를 위처럼 해주자.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
float4x4 gWorldMatrix;
float4x4 gViewMatrix;
float4x4 gProjectionMatrix;
float3 diffuseColor;// 컬러값
float4 gWorldLightPosition;
float4 gWorldCameraPosition;
struct VS_INPUT
{
float4 Position : POSITION0;
float3 Normal : NORMAL0;
float2 mTexCoord : TEXCOORD0;
};
struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 mTexCoord : TEXCOORD0;
float3 mDiffuse : TEXCOORD1;
float3 mViewDir : TEXCOORD2;
float3 mReflection : TEXCOORD3;
};
VS_OUTPUT vs_main( VS_INPUT Input )
{
VS_OUTPUT Output;
Output.Position = mul( Input.Position, gWorldMatrix );
Output.mTexCoord = Input.mTexCoord;
float3 worldNormal = mul(Input.Normal.xyz, gWorldMatrix);
float3 lightDir = normalize(Output.Position.xyz-gWorldLightPosition.xyz);
float3 viewDir = normalize(Output.Position.xyz-gWorldCameraPosition.xyz);
Output.mViewDir =viewDir;
Output.Position = mul( Output.Position, gViewMatrix );
Output.Position = mul( Output.Position, gProjectionMatrix );
float ndot = dot(-lightDir,worldNormal);
Output.mDiffuse = saturate(diffuseColor);
Output.mViewDir =viewDir;
Output.mReflection =reflect(viewDir,worldNormal);
return( Output );
}
|
cs |
버택스 쉐이더
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
sampler2D DiffuseSampler;
struct PS_INPUT
{
float4 Position : POSITION0;
float2 mTexCoord : TEXCOORD0;
float3 mDiffuse : TEXCOORD1; // 밝기만 전달할거 일단.
float3 mViewDir : TEXCOORD2; //
float3 mReflection : TEXCOORD3;
};
float4 ps_main(PS_INPUT Input) : COLOR0
{
float3 diffuseColor = Input.mDiffuse;//엠비언트 포함
float _shiness = 30.0f;
float3 specColor =0;
float3 viewDir = normalize(Input.mViewDir);
float3 reflection = normalize(Input.mReflection); //여기서 다시 노멀라이즈 해주는 이유는 보간기를 거치면서 흐트러질수 있기때문에!
if(diffuseColor.x >0)
{
float specNdot = saturate(dot( reflection , -viewDir));
specColor = pow(specNdot,_shiness);
}
float4 albedo = tex2D(DiffuseSampler , Input.mTexCoord);
return float4(albedo.rgb*diffuseColor.xyz+specColor.rgb,albedo.a);
//return float4(specColor.rgb,albedo.a);
}
|
cs |
픽셀 쉐이더
결과
'개발 > 쉐이더 - 랜더몽키' 카테고리의 다른 글
1일차 - 텍스처매핑 (0) | 2020.06.09 |
---|---|
1일차 - 렌더몽키 툴알기 (0) | 2020.06.09 |