summaryrefslogtreecommitdiff
path: root/src/shaders
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2020-02-25 23:41:30 +0800
committerchai <chaifix@163.com>2020-02-25 23:41:30 +0800
commit1a94259666a0d98e98e6999f19cf07475b618e65 (patch)
tree502450d9ce77dca2234898badaf22a43ecf89d7e /src/shaders
parent87b9482459c1a27b8756514473ae392453db39ec (diff)
*camera
Diffstat (limited to 'src/shaders')
-rw-r--r--src/shaders/common.c12
-rw-r--r--src/shaders/common.h15
-rw-r--r--src/shaders/pbr.c11
3 files changed, 34 insertions, 4 deletions
diff --git a/src/shaders/common.c b/src/shaders/common.c
index 3b58524..411c7e4 100644
--- a/src/shaders/common.c
+++ b/src/shaders/common.c
@@ -1,6 +1,9 @@
#include "common.h"
-Vec3 normal_from_color(Color32 c32) {
+Vec4 _proj_params;
+Vec2 _time;
+
+Vec3 unpacknormal(Color32 c32) {
Vec3 normal = {
c32.r * 2 - 1,
c32.g * 2 - 1,
@@ -25,4 +28,11 @@ Vec2 texsize(Texture* texture) {
return size;
}
+float linear01depth(float depth) {
+ float n = _proj_params.x, f = _proj_params.y;
+ return n / ((n-f)*depth + f);
+}
+float lineareyedepth(float depth) {
+ return _proj_params.y * linear01depth(depth);
+} \ No newline at end of file
diff --git a/src/shaders/common.h b/src/shaders/common.h
index 7e0ca5f..36f80c0 100644
--- a/src/shaders/common.h
+++ b/src/shaders/common.h
@@ -25,12 +25,22 @@
#define _mvp_matrix (uniforms->mvp)
#define _it_model_matrix /*inverse-transpose model matrix if needed*/
+// near
+// far
+// fov
+// aspect
+Vec4 _proj_params;
+
+// dt
+// duration
+Vec2 _time;
+
/************************************************************************/
/* functions */
/************************************************************************/
/*shader built in functions*/
-Vec3 normal_from_color(Color32 c32);
+Vec3 unpacknormal(Color32 c32);
Mat4 mat4(Vec4* c1, Vec4* c2, Vec4* c3, Vec4* c4);
Mat3 mat3(Vec3* c1, Vec3* c2, Vec3* c3);
@@ -62,4 +72,7 @@ out_v3->x = color.x * 2 - 1; \
out_v3->y = color.y * 2 - 1; \
out_v3->z = color.z * 2 - 1;
+float linear01depth(float depth);
+float lineareyedepth(float depth);
+
#endif \ No newline at end of file
diff --git a/src/shaders/pbr.c b/src/shaders/pbr.c
index 6db3d68..666f758 100644
--- a/src/shaders/pbr.c
+++ b/src/shaders/pbr.c
@@ -15,7 +15,7 @@
#define _rough reg_num_00
#define _world_pos reg_v3_00
#define _depth_pos reg_v3_01
-#define _clip_pos reg_v4_00
+#define _clip_pos reg_v2_01
#define _world_normal reg_v3_02
#define _world_tangent reg_v3_03
#define _world_bitangent reg_v3_04
@@ -34,6 +34,8 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoor
//*rough = 1 - vec3_dot(&worldnormal, light);
//*vnormal = in->vertex->normal;
*_texcoord = in->vertex->texcoord;
+ _clip_pos->x = clipcoord->z;
+ _clip_pos->y = clipcoord->w;
}
static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) {
@@ -46,11 +48,15 @@ static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* col
//(*color).a = 1;
//return 1;
//float rough = 1- vec3_dot(&in->normal, light);
+ float depth = _clip_pos->x / _clip_pos->y;
+ depth = (depth + 1) / 2;
+ depth = linear01depth(depth);
Color32 c = tex2d(_albedo_tex, _texcoord);
//Color32 nc = tex2d(noramltex, in->texcoord);
//vec3_scale(&c, roughness, &c);
color32_saturate(&c);
*color = c;
+ vec3_scale(color, 1 - depth, color);
return 1;
}
@@ -64,5 +70,6 @@ Program ssr_built_in_shader_pbr = {
//VARYING_V3_04 |
//VARYING_V4_00 |
VARYING_V2_00 |
- VARYING_V3_05
+ VARYING_V3_05 |
+ VARYING_V2_01
};