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 james-stanton · Dec 30, 2017 at 01:34 PM · graphicsopengl

Create a screen shatter effect

EDIT: SOLUTION FOUND - But still looking for optimizations / suggestions

Through many hours of tinkering and cursing the lack of Shader documentation for Unity I finally achieved what I was looking for.

alt text

I have removed all obsolete code from this thread and also included the up-to-date, working shader code + complementing C# code. It's not perfect by any means (I could probably shift all the transformations into a single matrix but instead I manually move one of the internal positions to satisfy the screen ratio adjustment. The whole process is probably wrong, as I recon I should have passed in screen space positions originally instead of passing in NDC's from the start.

SHADER - Code commented out is for calculating and rendering edges, but this looks crap

 Shader "J/Shatter"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
     }
         SubShader
     {
         Tags
         {
             "Queue" = "Overlay"
         }
 
         Cull Off ZWrite Off ZTest Always
         Blend SrcAlpha OneMinusSrcAlpha
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
                 float4 vbc : TEXCOORD2;
             };
         
             half _ScreenRatio;
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
                 float4 vbc : TEXCOORD2;
             };
 
             v2f vert (appdata v)
             {
                 v2f o;
 
                 // write vertex data to local var
                 o.vertex = v.vertex;
 
                 // adjust x to compensate for screen ratio
                 o.vertex.x = o.vertex.x * _ScreenRatio;
 
                 // transform vertex
                 o.vertex.xyz = UnityObjectToViewPos(o.vertex);
 
                 // revert x value to NDCs
                 o.vertex.x = o.vertex.x / _ScreenRatio;
 
                 // update barycentric coord
                 o.vbc = v.vbc;
                 
                 // I don't know why this works but this prevents clipping when rotating 
                 o.vertex.z = 0;
 
                 // Pass in uv coords
                 o.uv = v.uv;
 
                 return o;
             }
             
             sampler2D _MainTex;
             
             half _Alpha;
 
             fixed4 frag (v2f i) : SV_Target
             {
                 //i.vbc.b = (1 - i.vbc.r - i.vbc.g);
                 //half3 a3 = i.vbc;
 
                 //half3 d = fwidth(i.vbc);
                 //half3 a3 = smoothstep(half3(0, 0, 0), d * 3, i.vbc);
                 //half minimum = min(min(a3.x, a3.y), a3.z);
 
                 fixed4 col = tex2D(_MainTex, i.uv);
 
                 //col = lerp(col, fixed4(1, 1, 1, 1), 1 - minimum);
 
                 col.a = _Alpha;
 
                 return col;
             }
             ENDCG
         }
     }
 }
 

C#- Code commented out is for calculating and rendering edges, but this looks crap

     IEnumerator RenderTriangles()
     {
         if (m_tex == null) yield return null;
 
         float offset = 0;
         float alpha = 1;
         float rotation = 0;
         float transformDelay = 0.5f;
         float transitionTime = 0;
 
         while (alpha > 0)
         {
             yield return new WaitForEndOfFrame();
 
             if (!m_underlayEnabled)
             {
                 GameManager.EnableLoadingScreen();
                 m_underlayEnabled = true;
 
                 Time.timeScale = 1;
             }
 
             if (!m_mat)
             {
                 var shader = Shader.Find("J/Shatter");
                 m_mat = new Material(shader);
                 m_mat.hideFlags = HideFlags.HideAndDontSave;
 
                 m_mat.mainTexture = m_tex;
             }
 
             GL.LoadOrtho();
             m_mat.SetPass(0);
 
             var screenratio = (float)Screen.width / Screen.height;
             m_mat.SetFloat("_ScreenRatio", screenratio);
             m_mat.SetFloat("_Alpha", alpha);
 
             for (int i = 0; i < m_triData.Count; i++)
             {
                 GL.Begin(GL.TRIANGLES);
 
                 for (int j = 0; j < 3; j++)
                 {
                     GL.MultiTexCoord(0, m_triData[i].UV[j]);
                     GL.Vertex(m_triData[i].Vertices[j]);
                     //GL.MultiTexCoord(2, m_triData[i].BC[j]);
                 }
 
                 var c = m_triData[i].Center;
                 c.x *= screenratio;
                 m_triData[i].Matrix = Matrix4x4.Translate(m_triData[i].Dir * offset);
                 m_triData[i].Matrix = m_triData[i].Matrix * Matrix4x4.Translate(c);
                 m_triData[i].Matrix = m_triData[i].Matrix * Matrix4x4.Rotate(Quaternion.Euler(m_triData[i].Rotation * rotation));
                 m_triData[i].Matrix = m_triData[i].Matrix * Matrix4x4.Scale(new Vector3(0.97f, 0.97f, 0.97f));
                 m_triData[i].Matrix = m_triData[i].Matrix * Matrix4x4.Translate(-c);
 
                 GL.MultMatrix(m_triData[i].Matrix);
 
                 GL.End();
             }
 
             if (transformDelay < transitionTime)
             {
                 alpha -= 0.4f * Time.deltaTime;
                 offset += 0.1f * Time.deltaTime;
                 rotation += 0.4f * Time.deltaTime;
             }
             else
             {
                 transitionTime += Time.deltaTime;
             }
         }
 
         //GameManager.DisableLoadingScreen();
     }


I was trying to figure out how to achieve a screen shatter effect like THIS

Comment
Add comment · Show 1
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 Cherno · Dec 30, 2017 at 05:13 PM 1
Share

Sounds about right.For step one, it would probably be easier to render the screen (what the camera sees) to a RenderTexture so it can be accessed to use in a material (to be later applied to the renderers of the triangle objects). The triangles don'T have to be drawn on the screen, you only need the coordinates of their vertices and then you just create a gameobject and generate a triangle mesh for it. I would bet that there is at least one person out there who has already written an algorithm for subdividing a rectangle (screen) into triangles.

0 Replies

· Add your reply
  • Sort: 

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

85 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

Related Questions

Can a Unity app render to an external surface? 1 Answer

Problem with 2D game on android. Random FPS drop with OpenGLES 2.0, sudden white screen with Vulkan 0 Answers

-force-glcore and -force-gles commands are not working,-force-gles or -force-glcore CLI command is not working 0 Answers

Unity equivalent of ARB_occlusion_query (glGetQueryObject)? 0 Answers

Visibility Analysis from point on terrain 0 Answers


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