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 124c41 · Feb 19, 2014 at 03:00 PM · shaderprocedural-generation

Is it possible to do procedural calculations in shaders?

Hello,

I'm doing procedural generation for a texture of an earth-like planet. It's basically simple diamond-square with some perlin for polar caps and a lot of transformations on top of it, generating a 2048x1536px cubemap texture plus a normal map of the same size. I'm happy with the outcome, but not so happy about performance: It's taking 5-6s per planet, and since i'd like to have more than one, this is a problem. I already did some optimization, bringing calculation time down from about 15s. Some more could be done, but it would only be chipping away tiny fractions.

I've done nothing with shaders so far, so my question is on a basic level: Can some or all of that work be done by shaders? If so, will this improve performance (since gpu is doing the work)?

if anybody could point me in the right direction, i'd be really glad. thank you.

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 Dave-Hampson ♦♦ · Feb 19, 2014 at 03:28 PM 0
Share

Does the texture have to be different each time / are you generating different textures for different planets? Basically just wondering what is stopping you from generating it once, offline. To partially answer your question though, yes using the GPU would help.

avatar image 124c41 · Feb 19, 2014 at 03:29 PM 0
Share

Yes, it is given a different seed for each new planet.

1 Reply

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

Answer by MakeCodeNow · Feb 19, 2014 at 03:54 PM

You can definitely do this on the GPU and it will be very, very fast. However, you'll probably want to have Unity Pro so that you can render into and read out of RenderTargets. A high level algorithm would look like this:

Convert your current code into a shader. Create a dedicated RenderTarget. Create a dedicated camera and point it at the RenderTarget. Render a full screen quad with your shader from that camera. Create a new Texture2D. Copy the pixels out of your RenderTarget into the new Texture2D.

Comment
Add comment · Show 5 · 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 124c41 · Feb 19, 2014 at 04:15 PM 0
Share

Thank you for your answer. Sadly, going Pro is not an option atm. Can it be done without pro features?

avatar image MakeCodeNow · Feb 19, 2014 at 04:34 PM 0
Share

You can also use Texture2D.ReadPixels to make a texture out of the last screen render. You can probably get that to work, and I would expect it to still be faster than the current approach, through I'm not sure how much.

PS - please mark the question as answered if/when you think it is :)

avatar image 124c41 · Feb 20, 2014 at 12:58 AM 0
Share

I'd like to ask one more follow up question, if I may.

the way you describe it (to first draw the texture out to a RenderTarget or the screen and then to grab it and feed it back into a RenderTexture or Texture2D) seems odd to me. Can't it be stored in an array by the shader and accessed on the fly. What is the rationale behind this? If you could elaborate on that point a bit more, I'd be glad.

Sorry for being so persistent, I've been consulting the mighty google about this, but it seems there are not a lot of ressources on this / I cant' find them, and I've never worked with shaders before, so I'm trying to get the basics right.

avatar image MakeCodeNow · Feb 20, 2014 at 02:51 AM 0
Share

$$anonymous$$y assumption is that once generated, this data is static. If so, you definitely want to copy it into CPU memory and/or a texture. If the result is dynamic and changes every frame, then you can just put the shader on your planet sphere, but that didn't sound like what you were doing in the CPU version.

In any case, shaders aren't like CPU code when it comes to memory. They can't allocate data and can only access whatever state (textures, constants, etc) is setup by the CPU when they run.

avatar image 124c41 · Feb 20, 2014 at 03:00 AM 0
Share

Ah, I see, that makes sense. Thank you very much.

Now for some learning.

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

20 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

Related Questions

How to cut a hole in a wall? 3 Answers

Calling Graphics.DrawProcedural multiple times for chunked procedural terrain 2 Answers

how can I set multiple materials to a mesh ? 0 Answers

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

Have part of mesh change color 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