Search Unity

How can I draw a circle with shaders

Discussion in 'Shaders' started by Yourking77, Sep 16, 2017.

  1. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I am trying to learn Shaders, I need to create a cloud inside of a box, that way with my cloud system i can spawn a box, assign a material with my cloud Shader and have it render a cloud.

    I need to know where to start, I am getting good with c# but am finding the transition into Shaders extremely confusing.

    My idea is to start by rendering a transparent circle, I will render many small transparent circles inside the box, this should give a somewhat cartoon like cloud effect.

    So to begin I need advice on whether or not this will be an efficient way to go about creating clouds.and if it will run fine on mobile devices or lower end pc's. What I really need help with though is the actual Shader. I think the best way to go would to be to get the circle to generate first, if anyone can point me in teh right direction I would really appreciate it.
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    What you're proposing is less efficient than just using a texture.
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
  4. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Thanks for the resources, I have not seen either of those sites yet.

    I was reading up on this and although it doesn't look like i can get the whole tutorial at least it looked like a good place to start for me, however I am really struggling to grasp the code of them, I got too used to cSharp and now I just am struggling to learn how to do any of this.
    http://www.alanzucconi.com/2016/07/01/volumetric-rendering/

    That is what I was afraid of, but the only way I know of creating clouds that would react to light would be to have a bunch of crap spawning in the hierarchy, if there was a way to avoid that, it would be great.

    I turned to shaders because I was hoping I could render clouds easier within a gameobjects bounds easier than instatiating a couple hundred png images within a set of bounds, if I could just create one empty gameobject then adjust the scale and then add a material to render a cloud within theobjects scale that would be perfect.
     
  5. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Put a normal map on them.

    Ok, now you're talking about a real problem. What you're suggesting, a single huge quad with some clouds at some assigned points (either gigantic array of props or rendering a texture) is going to over draw your entire screen (more or less), that's going to cause a ton of overdraw, which is bad. With transparent stuff you want the smallest number of transparent pixels possible on screen.

    You're probably going to want to DrawMeshInstanced on these. Don't have any objects for the individual clouds, just tell the gpu to render the meshes at the given positions and update as need be. A bunch of pre-allocated pooled objects probably wouldn't be bad either, certainly not in the hundreds. It's the creating and destroying that makes the most garbage, just having stuff around and moving it isn't that bad.
     
  6. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Is what you are talking about doing scripted with just regular cSharp? cause that is what I was talking about. I need to be able to destroy stuff and create it though, its the best way to go about doing what i am trying to do.

    Imagine a empty gameObject in the scene that is scripted to follow the player cameras position, now that gameObject has a script attached to it, that script rotates that empty gameObject as the player increases in x and y coordinates. The reason it does that is because the gameObject spawn clouds in a sphere like pattern around the scene, however it has weather types attached to it, within 120 degrees of that gameObject it spawns one weather type, within 240 and 360 it spawns two different ones. this way as you walk farther in any direction you can actually walk into different weather types. and it also rotates freely over time and stuff like that but that is the general idea of the cloud system I am trying to set up.

    Now the clouds it is spawning for now is just empty GameObjects, what I had before was hundreds of gameObjects with sprite renderers, I had no performance drop due to my powerful computer, but I figured it would be highly inefficient. hence teh reason I turned to trying to create a shader. Now the number of clouds and how they are spawned will be limited by me as I go. But I almost have to destroy and create them. Not sure if you followed a lot of that or if you got confused, its hard for me to describe some of my ideas in text and I know that there is a lot of text here, but I think needed to give a clearer idea of what I am doing. I will look into the ideas you had, since I have never heard of them, I could not find anything on google besides volumetric clouds and nobody before now was helping at all on any of teh forums here.
     
  7. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    You don't HAVE to destroy them. Just move them out of view and manage an array or linked list of the active ones ;) Moving them away is even faster than SetActive(true/false).

    We're doing up to a thousand real-time AI characters as flipbook meshes in The Walking Dead March To War this way. Its a ton of pre-allocated objects, and that's a mobile title.
     
  8. gaxx

    gaxx

    Joined:
    Oct 14, 2012
    Posts:
    13
    Clouds/Gasses/Fluids and generally speaking volumes are usually very complex to achieve in realtime engine. Most of the time they are very resource heavy and really not very well performant in large numbers. There is no single title on mobile that I am aware of that uses volume rendering for clouds.

    It all depends on how important those clouds are in your game, and how you can 'look' at them, hardware and platform considerations and so on. Ask yourself first are they only visual addition or are they connected to any mechanics or gameplay features? There is also the question of realism or interaction with the clouds - you see the point here. All comes down to what you really need.

    If the answer is strictly visual there are many different techniques to achieve appealing effect. Here are some resources.

    If you want some complex system for managing clouds try reading this:
    https://www.guerrilla-games.com/read/the-real-time-volumetric-cloudscapes-of-horizon-zero-dawn

    There are simpler ways, like for example the technique from battlefield 1:

    simple yet very good looking.
     
    Last edited: Sep 18, 2017
  9. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    The game I have planned will never really make it to mobile, I don't think anyway. Simply because I am making an open world game that is going to be massive in size and all done in one scene. So mobile gaming is not really my focus. Although when you said the other day that destroying things and creating them is performance heavy I did think of just putting them directly below the camera, since the cloud layer is going to render last. So I am glad to hear that I can think of some good techniques for this.

    Clouds are not too important yet, but thinking ahead the ocean in my game is going to be massive and I want sailing to be a optional yet enjoyable feature, so when you are out at sea I want there to be storms that you can see coming in, and of course the clouds need to be good enough to make a dramatic enough feel to when you are stranded at sea and you sea a massive storm about to destroy your boat. I don't think a blob of gray taking over the blobs of white in the sky is going to really scare anybody. So I would like a blend of realistic yet sort of cartoonist clouds, as for the mechanics I have some of that figured out.

    I was thinking drawmeshInstanced would be fine I could just create a bunch of mesh objects and move those around the scene, since brownboots doesn't think volumetric would be the best way to go anyway. i will still need to learn to use shaders anyway though since water is next on my list and there is no way around not using a shader for that.
     
  10. marserMD

    marserMD

    Joined:
    Aug 29, 2014
    Posts:
    191
    Hey there! Since you are looking for a way to create stylized volumetric clouds, you might be interested in True Clouds:
    the most efficient and artistic-driven way to render clouds.

    And you can do a heck of a lot with it!