Skip to content

“再谈运动模糊”效果改进建议 #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ljcduo opened this issue Dec 4, 2017 · 4 comments
Closed

“再谈运动模糊”效果改进建议 #195

ljcduo opened this issue Dec 4, 2017 · 4 comments

Comments

@ljcduo
Copy link

ljcduo commented Dec 4, 2017

乐乐你好,关于13.2再谈运动模糊,P275(第七次印刷)的效果我有些改进的想法。
文件位置:Unity_Shaders_Book\Assets\Shaders\Chapter13\Chapter13-MotionBlurWithDepthTexture.shader

原来的代码是:

float2 velocity = (currentPos.xy - previousPos.xy)/2.0f;
			
float2 uv = i.uv;
float4 c = tex2D(_MainTex, uv);
uv += velocity * _BlurSize;
for (int it = 1; it < 3; it++, uv += velocity * _BlurSize) {
	float4 currentColor = tex2D(_MainTex, uv);
	c += currentColor;
}
c /= 3;

return fixed4(c.rgb, 1.0);

1

这个代码虽然计算出了速度,但是没有在纹理采样的时候根据距离运动点远近进行合适的颜色权重分配,而是平均分,这样就会造成物体“不知道向哪里方向运动”,给人一种眼睛"散光"的感觉,而不是运动的感觉。所以我认为应该距离运动点越近颜色权重越高,这样会看出物体在向哪个方向运动,更符合运动模糊的效果。(前一章基于累积缓存的实现,是当前帧的颜色权重高,以往帧的颜色权重低,这样也可以造成方向感,所以没有问题。)

改进后的效果:
2

改进后的代码:

float2 velocity = (currentPos.xy - previousPos.xy)/2.0f;
			
float2 uv = i.uv;
float vecColRate[3] = { 0.7,0.2,0.1 };
float4 c = tex2D(_MainTex, uv) * vecColRate[0];
uv += velocity * _BlurSize;
for (int it = 1; it < 3; it++, uv += velocity * _BlurSize) {
	float4 currentColor = tex2D(_MainTex, uv);
	c += currentColor * vecColRate[it];
}

			
return fixed4(c.rgb, 1.0);

很少有机会可以跟原书作者交流,非常荣幸~
希望有帮助!

@candycat1992
Copy link
Owner

candycat1992 commented Dec 10, 2017

哈哈的确是很好的建议,非常感谢,也很荣幸可以有这样的读者 :) 会在后续的版本里改进的,已添加到勘误列表里。

@jiahaodev
Copy link

1 similar comment
@tiomke
Copy link

tiomke commented Jun 26, 2019

@xujianan13
Copy link

另外关于运动方向问题,一般的真实效果应该是拖影吧,所以速度是不是应该用原始方向减当前方向previousPos.xy-currentPos.xy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants