Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Voleuro · Feb 18, 2018 at 06:52 PM · shaderspeedeffectsurface shaderpass

Use Shader Motion Vectors pass in Surface Shader

Hey everybody, I'm trying to create a shader, where the speed of a pixel (meaning the difference before it's previous position and it's current position) is used for coloring. For example, I want to make the parts of my object glow brighter the faster they are moving. I've been trying to save the previous vertex position in my shader for using it in the next frame to calculate the velocity of the pixel. But that does not seem to work. Or at least I could not get it working that way. ^^"

So, after some further googling I found that Unity now has a pass for Motion vectors. I could not get a clear understanding of how they work in the documentation. So my question is: Can I get the values of the Motion vectors in my surface shader to control it's color based on the pixels velocity?

I would be very grateful for any help :)

Kai

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Voleuro · Feb 19, 2018 at 01:16 PM

So,

After a good nights sleep and a few more hours testing with different shaders and scripts, I've now come to the point where I did write a Post Process Shader that displays the Magnitude of the Motion Vector Buffers in Black and White. So I know the Values are there and I can use them. :D I based my Code upon William Chyr's Depth Shader Code. You can find it here: http://willychyr.com/2013/11/unity-shaders-depth-and-normal-textures/

For anyone interested in it, here's my current version:

The c# Script for the Post Processing Shader:

 using UnityEngine;
 using System.Collections;
 
 //so that we can see changes we make without having to run the game
 
 [ExecuteInEditMode]
 public class PostProcessDepthGrayscale : MonoBehaviour {
 
     public Material mat;
 
     void Start () {
         //GetComponent<Camera>().depthTextureMode = DepthTextureMode.Depth;
         GetComponent<Camera>().depthTextureMode = DepthTextureMode.MotionVectors;
     }
 
     void OnRenderImage (RenderTexture source, RenderTexture destination){
         Graphics.Blit(source,destination,mat);
     }
 }

And the Shader itself:

 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
 
 Shader "Custom/DepthGrayscale" {
 SubShader {
 Tags { "RenderType"="Opaque" }
 
 Pass{
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 
 sampler2D _CameraDepthTexture;
 sampler2D _CameraMotionVectorsTexture;
 
 struct v2f {
    float4 pos : SV_POSITION;
    float4 scrPos:TEXCOORD1;
 };
 
 //Vertex Shader
 v2f vert (appdata_base v){
    v2f o;
    o.pos = UnityObjectToClipPos (v.vertex);
    o.scrPos=ComputeScreenPos(o.pos);
    return o;
 }
 
 //Fragment Shader
 half4 frag (v2f i) : COLOR{
    half4 depth;
    depth.r = saturate(abs(300*tex2Dproj(_CameraMotionVectorsTexture, UNITY_PROJ_COORD(i.scrPos)).r));
    depth.g = saturate(abs(300*tex2Dproj(_CameraMotionVectorsTexture, UNITY_PROJ_COORD(i.scrPos)).g));
    depth.b = saturate(abs(300*tex2Dproj(_CameraMotionVectorsTexture, UNITY_PROJ_COORD(i.scrPos)).b));
    depth.a = 1;
 
    float combined = sqrt( depth.r*depth.r + depth.g*depth.g + depth.b*depth.b);
    depth.r = combined;
    depth.g = combined;
    depth.b = combined;
 
 
 
    return depth;
 }
 ENDCG
 }
 }
 FallBack "Diffuse"
 }

The only Thing I'm now struggling with is transfering this effect into a surface shader to use as Material on an object. It always gives me the Warning "Motion vectors require depth texture. Adding this flag to depthTexureMode" and somehow uses the DepthTexture of the Scene Viewport, not the Game Viewport. ^^"

I would appreciate your Help with this.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by agentrsdg · Apr 29, 2018 at 09:34 AM

@Voleuro I used fixed4 frag(v2f i) : SV_Target{ instead of half4 frag (v2f i) : COLOR{ and your solution works great! It helped me in my project, so thanks! alt text


motionvec.png (260.0 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make this shader additive blending? 0 Answers

Losing image effects in build process 1 Answer

Color white does nothing consider mobile... really? 1 Answer

Mysterious syntax error with finalcolor function 1 Answer

Simulate Shutter Speed 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges