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 iasksyouaquestion · Jun 28, 2021 at 10:13 PM · shaderrenderingshaderstexture2dshader programming

Converting c# function to shader.

I have a spline class with a method that draws it to a sprite. The relevant parts are isolated into following code:

     SpriteRenderer sr;
 
     private void Awake()
     {
         sr = GetComponent<SpriteRenderer>();
     }
 
     private void Update()
     {
         Draw();
     }
 
     private void Draw()
     {
         Texture2D texture = new Texture2D(128, 128);
         Sprite sprite = Sprite.Create(texture, new Rect(0, 0, 128, 128), Vector2.zero);
         sr.sprite = sprite;
 
         Vector2 pixelSize = new Vector2(sr.size.x / texture.width, sr.size.y / texture.height);
         Vector2 lowerLeft = (Vector2)transform.position + pixelSize;
 
         Color[] colors = texture.GetPixels();
 
         for (int i = 0; i < colors.Length; ++i)
         {
             Vector2 pixelPos = lowerLeft;
             pixelPos.x += i % texture.width * pixelSize.x;
             pixelPos.y += Mathf.Floor(i / texture.width) * pixelSize.y);
             // Make pixel red if pixel distance to spline is less than radius.
             if (GetDistance() < radius)
             {
                 colors[i] = Color.red;
             }
             else
             {
                 colors[i] = Color.blue;
             }
         }
 
         texture.SetPixels(colors);
         texture.Apply();
     }

Now this is naturally really slow. One spline alone decreases the framerate noticeably. So, i thought this seemed like the kind of thing that would normally be done with shaders, but have absolutely no experience with them. I just want to know if i can actually use my GetDistance() method in a shader, or if i will have to rewrite it. Also, if you know an alternative to shaders that can make this run decently, please tell!

Comment
Add comment · Show 2
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 Namey5 · Jun 28, 2021 at 11:21 PM 0
Share

What you're effectively doing is rendering an SDF, so yes the rendering side is definitely possible. However, the most important part of the equation is your GetDistance() function, which you haven't provided. As such it's impossible to comment on how easy converting to a shader would be.

avatar image iasksyouaquestion Namey5 · Jun 29, 2021 at 02:54 AM 0
Share

Posting the function would end up becom$$anonymous$$g a mess, because it depends on more functions etc. However, essentially what i am doing is using unitys built in HandleUtility.DistancePointBezier to get the distances to a series of cubic bezier curves, and then return the smallest one. So i don't actually have the code for calculating the distance. Anyways, if shaders can do it, i will take the time to learn how they work. Thanks.

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

211 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Scene Color Node in Shader Graph not working with Unity's 2D Renderer and URP 5 Answers

Distortion Shader does not render transparent objects (Shader Graph) 2 Answers

How to create a shader for a dynamic linear gradient 0 Answers

How to write an additive HDRP shader without glow 0 Answers

Graphics.CopyTexture and location fo textures 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