Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mpashanin · May 17, 2013 at 12:53 AM · shaderdistancecgmatrixmatrix4x4

Efficient code for shortest distance inside a fragment shader

Hi!

I have a custom shader that needs to calculate a shortest distance from its every fragment and an outside set of 2d(x,y) locations (up to 32). These locations are set in real time.

The way I approached that program, is that I made a Matrix4x4 every time i needed to change position of my custom set locations, and cramp 4 of those per single matrix, kinda like this :


locationMatrix

[x1][y1][x2][y2]

[x3][y3][x4][y4]

[x5][y5][x6][y6]

[x7][y7][x8][y8]


Later on inside a shader I use that matrix to calculate distance. However it looks inefficient and messy, especially with converting from uv to world space position:


uniform float4x4 _Locations; //Location matrix that is being set via renderer.material.SetMatrix();

half _Radius, point1, point2;

point1= (distance(i.position_in_world_space, float2(_Locations[0][0],_Locations[0][1]))/1000.0);

point2= (distance(i.position_in_world_space, float2(_Locations[0][2],_Locations[0][3]))/1000.0);

point1=min(point1,point2);

point2= (distance(i.position_in_world_space, float2(_Locations[1][0],_Locations[1][1]))/1000.0);

point1=min(point1,hole2);

point2= (distance(i.position_in_world_space, float2(_Locations[1][2],_Locations[1][3]))/1000.0);

point1=min(point1,point2);


As you see it is very messy and inefficient, and I only processed 8 locations above. I was wondering if there was a streamlined version of this possible? I need to utilize all 4 of my Matrix4x4s.

Is there anything that can be done here or am I stuck with this?

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

Answer by spalmer · Jun 29, 2015 at 10:25 AM

For one thing, you can divide by 1000 once at the very end after you've done all the minimum's. For another, you can take the minimums of all the squared distances instead, then do a single square root at the end. Using distance hides the operation sqrt(dot(b-a,b-a)) so you can optimize further. You can do the work in a loop instead of manually unrolled, that would be cleaner. You don't have to pack into a matrix, you can pack coefficients into a vector array or whatever is easier to loop over. Many GPUs are massively threaded scalar processors nowadays so this kind of thing isn't as bad as you'd think.

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

14 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

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

Difference between pos.z and pos.w? 0 Answers

Convention of matrices passed to shaders 1 Answer

Set shader builtin matrices by script 0 Answers

Depth map from a second camera at a light position 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