Masked motion blur.

This commit is contained in:
Lubos Lenco 2016-04-02 18:19:52 +02:00
parent 905ab70b04
commit 36bd1f4cfa
5 changed files with 96 additions and 31 deletions

View file

@ -2134,15 +2134,20 @@ class ArmoryExporter(bpy.types.Operator, ExportHelper):
c.bind_constants = []
const = Object()
const.id = "lighting"
const.id = 'lighting'
const.bool = material.lighting_bool
c.bind_constants.append(const)
const = Object()
const.id = "receiveShadow"
const.id = 'receiveShadow'
const.bool = material.receive_shadow
c.bind_constants.append(const)
const = Object()
const.id = 'mask'
const.float = material.stencil_mask
c.bind_constants.append(const)
c.bind_textures = []
# TODO: parse and generate from world nodes
@ -2217,8 +2222,30 @@ class ArmoryExporter(bpy.types.Operator, ExportHelper):
ext = ''
for d in defs:
ext += d
ArmoryExporter.asset_references.append('compiled/ShaderResources/' + ArmoryExporter.pipeline_id + '/' + ArmoryExporter.pipeline_id + ext + '.json')
shader_res_name = ArmoryExporter.pipeline_id + ext
# Shader res
shader_res_name = ArmoryExporter.pipeline_id + ext
shader_res_path = 'compiled/ShaderResources/' + ArmoryExporter.pipeline_id + '/' + shader_res_name + '.json'
# Stencil mask
# if material.stencil_mask > 0:
# mask_ext = "_mask" + str(material.stencil_mask)
# shader_res_name_with_mask = shader_res_name + mask_ext
# shader_res_path_with_mask = 'compiled/ShaderResources/' + ArmoryExporter.pipeline_id + '/' + shader_res_name_with_mask + '.json'
# # Copy resource if it does not exist and set stencil mask
# if not os.path.isfile(shader_res_path_with_mask):
# json_file = open(shader_res_path).read()
# json_data = json.loads(json_file)
# res = json_data['shader_resources'][0]
# res['id'] += mask_ext
# for c in res['contexts']:
# c['stencil_pass'] = 'replace'
# c['stencil_reference_value'] = material.stencil_mask
# with open(shader_res_path_with_mask, 'w') as f:
# json.dump(json_data, f)
# ArmoryExporter.asset_references.append(shader_res_path_with_mask)
# o.shader = shader_res_name_with_mask + '/' + shader_res_name_with_mask
# # No stencil mask
# else:
ArmoryExporter.asset_references.append(shader_res_path)
o.shader = shader_res_name + '/' + shader_res_name
# Process all passes from pipeline
for pipe_pass in ArmoryExporter.pipeline_passes:

View file

@ -31,6 +31,7 @@ def initProperties():
bpy.types.Material.receive_shadow = bpy.props.BoolProperty(name="Receive Shadow", default=True)
bpy.types.Material.custom_shader = bpy.props.BoolProperty(name="Custom Shader", default=False)
bpy.types.Material.custom_shader_name = bpy.props.StringProperty(name="Name", default="")
bpy.types.Material.stencil_mask = bpy.props.IntProperty(name="Stencil Mask", default=0)
bpy.types.Material.export_tangents = bpy.props.BoolProperty(name="Export Tangents", default=False)
# Menu in object region
@ -95,6 +96,7 @@ class MatsPropsPanel(bpy.types.Panel):
layout.prop(mat, 'custom_shader')
if mat.custom_shader:
layout.prop(mat, 'custom_shader_name')
layout.prop(mat, 'stencil_mask')
# Registration

View file

@ -27,6 +27,7 @@ uniform sampler2D smm;
#else
uniform float metalness;
#endif
uniform float mask;
in vec3 position;
in vec4 mvpposition;
@ -86,4 +87,5 @@ void main() {
gl_FragData[0] = vec4(n.xyz, depth);
gl_FragData[1] = vec4(position.xyz, roughness);
gl_FragData[2] = vec4(baseColor.rgb, metalness);
gl_FragData[3] = vec4(mask, 0.0, 0.0, 0.0);
}

View file

@ -14,7 +14,7 @@ in vec2 texCoord;
const float focus_depth = 0.5;
const float vignout = 1.3; // vignetting outer border
const float vignout = 1.6; // vignetting outer border
const float vignin = 0.0; // vignetting inner border
const float vignfade = 90.0; // f-stops till vignete fades
const float fstop = 20; // f-stop value

View file

@ -9,6 +9,7 @@ precision mediump float;
uniform sampler2D gbuffer0;
uniform sampler2D gbuffer1;
uniform sampler2D gbuffer2;
uniform sampler2D gbuffer3;
uniform sampler2D tex;
uniform mat4 prevVP;
@ -16,13 +17,15 @@ uniform mat4 invVP;
in vec2 texCoord;
const int samples = 8;
vec2 getVelocity(vec2 texCoord, float depth) {
// Get the depth buffer value at this pixel
float zOverW = depth; // * 2.0 - 1.0
// H is the viewport position at this pixel in the range -1 to 1
vec4 H = vec4(texCoord.x * 2.0 - 1.0, (texCoord.y) * 2.0 - 1.0, zOverW, 1.0);
vec4 H = vec4(texCoord.x * 2.0 - 1.0, (texCoord.y) * 2.0 - 1.0, zOverW, 1.0);
// Transform by the view-projection inverse
vec4 D = invVP * H;
vec4 D = invVP * H;
// Divide by w to get the world position
vec4 worldPos = D / D.w;
@ -36,46 +39,77 @@ vec2 getVelocity(vec2 texCoord, float depth) {
return velocity;
}
void main() {
void main() {
vec4 color = texture(tex, texCoord);
// Do not blur masked objects
if (texture(gbuffer3, texCoord).r == 1.0) {
gl_FragColor = color;
return;
}
vec4 g0 = texture(gbuffer0, texCoord);
float depth = g0.a;
float blurScale = 1.0; //currentFps / targeFps;
vec2 velocity = getVelocity(texCoord, depth) * blurScale;
const int samples = 8;
vec4 color = texture(tex, texCoord);
vec2 velocity = getVelocity(texCoord, depth) * blurScale * (-1.0);
vec2 offset = texCoord;
int processed = 1;
// for(int i = 1; i < samples; ++i) {
// Sample the color buffer along the velocity vector
// offset += velocity;
// color += texture(tex, offset);
// offset += velocity;
// color += texture(tex, offset);
offset += velocity;
color += texture(tex, offset);
offset += velocity;
if (texture(gbuffer3, offset).r != 1.0) {
color += texture(tex, offset);
processed++;
}
offset += velocity;
color += texture(tex, offset);
offset += velocity;
if (texture(gbuffer3, offset).r != 1.0) {
color += texture(tex, offset);
processed++;
}
offset += velocity;
color += texture(tex, offset);
offset += velocity;
if (texture(gbuffer3, offset).r != 1.0) {
color += texture(tex, offset);
processed++;
}
offset += velocity;
color += texture(tex, offset);
offset += velocity;
if (texture(gbuffer3, offset).r != 1.0) {
color += texture(tex, offset);
processed++;
}
offset += velocity;
color += texture(tex, offset);
offset += velocity;
if (texture(gbuffer3, offset).r != 1.0) {
color += texture(tex, offset);
processed++;
}
offset += velocity;
color += texture(tex, offset);
offset += velocity;
if (texture(gbuffer3, offset).r != 1.0) {
color += texture(tex, offset);
processed++;
}
offset += velocity;
color += texture(tex, offset);
offset += velocity;
if (texture(gbuffer3, offset).r != 1.0) {
color += texture(tex, offset);
processed++;
}
offset += velocity;
color += texture(tex, offset);
offset += velocity;
if (texture(gbuffer3, offset).r != 1.0) {
color += texture(tex, offset);
processed++;
}
// }
vec4 finalColor = color / samples;
// vec4 finalColor = color / samples;
vec4 finalColor = color / processed;
gl_FragColor = finalColor;
}