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 szmate1618 · Sep 11, 2018 at 07:34 AM · shadervertexvertex shader

Fisheye effect vertex shader fail - asking for help - video and code included

Video here


Hi people!

Sorry for the probably noobish question, but I'd greatly appreciate if you could help me out. I'm a somewhat experienced programmer, but this is my first time trying my hands at shader programming. I figured a basic fisheye effect vertex shader would be a fun first program to write. I am aware that there are better ways to do this, in fact there are multiple existing solutions available, but I want to make this work anyway, because I need to learn it.


My idea was to simply calculate the distance of the vertices from the camera, and take the square root of that. E.g. if the camera is at (0,0) and point P is at (15,20) its original distance would be 25 units, so it should be transformed into (3,4) making the new distance exactly 5 units, which is the square root of the original 25.


Should be simple enough, but as you can see on the video, I failed, almost completely. That rainbow background is 2 Planes on top of each other, to make the distortion visible. The most obvious issues:

00:01.00 - There is a hole in the middle of the Plane. Wtf. It disappears later, and doesn't come back.

00:04.72 - The other spaceship on the right appears out of nowhere. It's supposed to appear as a 1-pixel wide line first, but it doesn't. It's completely invisible until it's about 20 pixels wide.

00:05.17 - The nicely distorted Plane suddenly becomes completely straight as the camera moves away from it. And not just that, but somehow it's taller than it's wide. It's supposed to be a square. Also, the Z-order is somehow reversed now, the Plane partially covering the main spaceship.

00:13.34 - If I got too far away from the plane, it completely and instantly disappears.

00:47.00 - Deleting the smaller plane breaks the effect on the remaining bigger plane. No fisheye distortion anymore, no matter where the camera is.


I think that's all. Here's the shader code:

 Shader "Unlit/FishEyeShader"
 {
     Properties
     {
         _MainTex("Texture", 2D) = "white" {}
     }
     SubShader
     {
         Tags { "RenderType" = "Opaque" }
         LOD 100
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
             };
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
 
             v2f vert(appdata v)
             {
                 v2f o;
                 float2 ObjSpaceCameraPos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1.0)).xy;
                 float2 offset = v.vertex.xy - ObjSpaceCameraPos;
                 float2 l = length(offset);
                 v.vertex.xy = ObjSpaceCameraPos + offset / sqrt(l);
 
                 o.vertex = UnityObjectToClipPos(v.vertex);
 
                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                 return o;
             }
 
             fixed4 frag(v2f i) : SV_Target
             {
                 fixed4 col = tex2D(_MainTex,  i.uv);
                 return col;
             }
             ENDCG
         }
     }
 }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by szmate1618 · Sep 12, 2018 at 10:26 PM

I asked the same question on reddit and fortunately some did help me out. See the thread for details, but tldr; I had to change the code to do the transformation in screen space instead of object space, then create a custom culling matrix (essentially disabling frustum culling). Also translated the background, so that there are no vertices at (0,0) which is important, because apparently square root is calculated from the inverse square root.

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

144 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 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

Adding transparency to vertex shader? 0 Answers

Reuse vertices modified in a previous shader pass 1 Answer

[Shader, sprite] frag shade entire sprite image? 1 Answer

Add new vertex attributes for a shader,Create vertex attributes 0 Answers

Combine Vertex / Fragment and Surface Shader 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