Generate a Mesh Asset Using Compute Shaders in the Unity Editor! ✔️ 2020.3 | Game Dev Tutorial

Sdílet
Vložit
  • čas přidán 13. 09. 2024
  • Hry

Komentáře • 63

  • @peterwilliams4054
    @peterwilliams4054 Před 2 lety +8

    I'm literally nerding out over how high quality this was. I'm pretty picky when it comes to tutorials since I'm a snob when it comes to clean code and I usually stop watching after a little bit if people accidentally or intentionally leave it out to shorten video times. Having said that, you did excellent and I learned a lot.
    You definitely earned a like and a subscribe, thank you!

    • @NedMakesGames
      @NedMakesGames  Před 2 lety +1

      Haha I'm glad you enjoyed the video! I'm also a clean code snob so you're in good company here. Thanks for watching!

  • @CannabisTechLife
    @CannabisTechLife Před 11 měsíci

    Thank you for this tutorial. It's clear from watching other tutorials on the subject when someone has only a high level understanding vs a low level understanding. Too many of the tutorials out there when you search for compute shaders just gloss over stuff without explaining why they use them. Cheers!

  • @ThomasChen-ur2gt
    @ThomasChen-ur2gt Před 3 lety +2

    Love it! Looking forward to grass and other stuff that you can do with it!
    BTW, I'm really impressed by how many high-quality videos you are able to produce.

  • @dreamscheme3862
    @dreamscheme3862 Před 3 lety +1

    channel is such a gem

  • @ChipboardDev
    @ChipboardDev Před rokem

    I'm a new subscriber, enjoying the quality, depth, and complexity of the content you deliver. It can be hard to find good resources on this, specifically in relation to Unity.

    • @NedMakesGames
      @NedMakesGames  Před rokem +1

      Thank you for the kind words! There is definitely a lack of compute shader resources, which I hope to rectify!

  • @brianxu7477
    @brianxu7477 Před 2 lety

    The best compute shader tutorial I have ever seen👍Thank you!

    • @NedMakesGames
      @NedMakesGames  Před 2 lety

      Thank you, means a lot to hear that! Thanks for watching.

  • @ChelseySmithHandle
    @ChelseySmithHandle Před 3 lety

    This is super helpful. I am looking into passing an octree structure to the compute shader and have it generate a voxelized mesh. Your videos are exactly what I have been looking for.

    • @NedMakesGames
      @NedMakesGames  Před 3 lety

      That sounds really interesting! I'm glad I could help a bit!

  • @mrjamerific
    @mrjamerific Před 2 lety

    Enjoyed this quite a bit. Liked, subscribed and leaving a comment for the algorithm!

  • @tombuben
    @tombuben Před 3 lety

    This video is great :) Got here from Jendrik Illner's computer graphics newsletter, will check out some of your other videos as well

  • @dasraiser
    @dasraiser Před 7 měsíci

    Great tutorial

  • @IkikaeruRaimei
    @IkikaeruRaimei Před 3 lety

    As always, a nice tutorial.
    You should look for making a volumetric light tutorial or things like that, as far as I keep searching on youtube, there's no tutorial about how to code them just how to use URP or Aura. It may help you get more subs (or you can use the community tabs to improve your visibility, youtube's algorithm is broken anyway).
    I hope to see more content anyway, keep up the good work!

    • @NedMakesGames
      @NedMakesGames  Před 3 lety +1

      Hi, thank you! That's a good idea, I think volumetric lights are really interesting.
      I've definitely been "taking advantage of" the community tab 😆

  • @florinf2003
    @florinf2003 Před 11 měsíci

    I think this runs slowly because you generate more vertices than needed. You generate 9 vertices, 3 for each triangle, when you only need 4: a,b,c and center.

  • @sammtanX
    @sammtanX Před rokem

    This is great. by using this method you shown, I can do mesh generation and tesselation in GPU. Also i can write the generated data as mesh asset. Thanks!

    • @NedMakesGames
      @NedMakesGames  Před rokem

      Thanks for commenting! I'm glad this helped you out!

  • @anonymoussloth6687
    @anonymoussloth6687 Před 2 lety

    You should do a more in depth video or videos on custom editors. I would love to see that.
    Other ideas:
    ui
    procedural generation (ex: tile based like townscaper - game by Oskar stalberg)
    Coding patterns
    Wave function collapse
    Multithreading
    Sdfs
    Water (shaders, waterfalls, ripple effect around objects in water, etc)
    I am just suggesting these ideas because your videos are really in depth and helpful (unlike other channels that just scratch the surface)

    • @NedMakesGames
      @NedMakesGames  Před 2 lety +1

      Thank you, I really appreciate that!
      Great suggestions too. I definitely plan to do more with editor scripts, SDFs and water in particular, but any of those topics would make a good video.

  • @manofapocalypse
    @manofapocalypse Před měsícem

    what about procedurally generated meshes like minecraft chunks?

  • @krishx007
    @krishx007 Před rokem

    Wow...Super..!!😲😲

  • @ThomasChen-ur2gt
    @ThomasChen-ur2gt Před 3 lety +2

    Why the F don't you have more views. This is confusing... CZcams dumb.

    • @NedMakesGames
      @NedMakesGames  Před 3 lety +1

      The algorithm is very fickle! Your watching and commenting helps though, so thank you!

  • @kapadork2751
    @kapadork2751 Před 3 lety

    Thanks a lot for this amazing tutorial!
    Right now, I'm trying to procedually generate terrains and therefore wanted to learn how to use compute shader in Unity...
    it is hard to get into this stuff cause there are quite a few steps to go until it works ._.
    You explained everything very well and in a nice pace, keep up the good work!

    • @NedMakesGames
      @NedMakesGames  Před 3 lety

      Hi, thank you for watching! I'm glad this video helped you out!
      Yeah, compute shaders take a lot of set up to get going, which makes them difficult to make videos about 😅
      I have more videos on compute shaders coming soon, in particular to generate a grass mesh.
      In terms of using noise in a compute shader, you can check out this video, where I use a white noise function. You can pass a seed value to the compute shader from C#! czcams.com/video/6SFTcDNqwaA/video.html

  • @hibiscusbear
    @hibiscusbear Před 3 lety

    🥳 love it!!

  • @jrdata2k
    @jrdata2k Před rokem

    Very informative tutorial, well done. I'm hitting an issue however on mac, I'm running a fairly new mac from 2019, it's Intel based not Apple Silicon, either way they both use Metal rendering api and I'm getting this error "Metal: Vertex or Fragment Shader "MyCompute" requires a ComputeBuffer at index 1 to be bound, but none provided. Skipping draw calls to avoid crashing." any ideas, googling this hasn't turned up many suggestions.

  • @dechin6144
    @dechin6144 Před 2 lety +1

    Hey! thanks for the great video! quick question though! When I go to create a pyramid, the loading bar will come up but nothing happens, not sure what my issue is but any tips or pointers are greatly appreciated!

    • @NedMakesGames
      @NedMakesGames  Před 2 lety

      Hi! Thank you!
      Do you receive any errors in Unity? Many times, a hanging loading bar means there was some type of error or the loading bar close command wasn't reached.
      Also, unfortunately compute shader bugs often cause know errors. For testing, you can print out the compute buffer contents after calling GetData to see if they're filled with junk or not.
      Feel free to upload your scripts to pastebin or github gists if you'd like! I don't mind to take a look.

  • @__Rizzler__
    @__Rizzler__ Před 3 lety +1

    Ned make games here I go subscribe.

    • @NedMakesGames
      @NedMakesGames  Před 3 lety

      Hi! Thank you very much! I hope you enjoy what I'm working on.

    • @__Rizzler__
      @__Rizzler__ Před 3 lety

      @@NedMakesGames indeed

  • @AtmosMr
    @AtmosMr Před 2 lety

    I suppose you could make this a create convex hull from a list of vertices.... interesting. Have you done this?

    • @NedMakesGames
      @NedMakesGames  Před 2 lety

      Hi! That's definitely possible. I've looked into it a bit but never fully implemented it. Would be a fun project for sure.

  • @wzukay6648
    @wzukay6648 Před 2 lety

    I have an error at 13:13 with the PyramidBaker. The inspector can't acces the baker for some reason.

    • @NedMakesGames
      @NedMakesGames  Před 2 lety

      Hmm, maybe one of the two classes in an editor class and the other is not? Try placing both files in an editor folder.

  • @bifrostbeberast3246
    @bifrostbeberast3246 Před 3 lety

    First of all, thank you for this great tutorial + explanations!
    I am working on a Dungeon Keeper Clone where I create hundreds of minable voxel blocks (block-shaped complex meshes with randomized procedural surfaces)
    The number of vertices for each block can vary depending on the kind of material (gabbro, diorite, marble, etc.) and all surface vertice positions are slightly randomized for a more natural stone surface. Most surfaces are culled, when not exposed, but get rendered, once a neighbouring block is mined.
    Doing it by CPU wrecks the Frame Rate, so I would like to shift my current system to using Compute Shaders.
    My question is now, since I am working a lot with lighting underground and material emission, how would that translate into geometry that is run by GPU only? Can my CPU still access the materials emission and adjust it in runtime and would that directly translate to what the GPU displays? I am pretty new to Shaders especially Compute Shaders, so I have a hard time really following all the memory management stuff.

    • @NedMakesGames
      @NedMakesGames  Před 3 lety +3

      Hi! Thank you for watching!
      That sounds like an interesting situation! Using the technique from this video, you can create a mesh which you can place in a MeshRenderer/MeshFilter like normal. So, you can apply a normal material and manipulate its emission properties at runtime like any other material.
      Since you do want to generate meshes at runtime, you'll need to change a couple of things, mainly call the mesh baker script from a runtime script. You could also make it more efficient by using AsyncGPUReadback ( docs.unity3d.com/ScriptReference/Rendering.AsyncGPUReadback.html ) instead of GetData, but that's not required.
      Alternatively, you can keep all the mesh data in a GraphicsBuffer and use Graphics.DrawProcedural. This would probably be more efficient, but requires you to use custom built graphics shaders. You can still provide a material to DrawProcedural, allowing you to control emission properties. I wrote video using Graphics.DrawProcedural here: czcams.com/video/EB5HiqDl7VE/video.html , although it generates the mesh every frame instead of saving it. Still, might be a good example - you could change things to only run the generating compute shader when needed.

  • @jacobhansen3871
    @jacobhansen3871 Před 2 lety

    Perfect at 0.25 speed :D

    • @NedMakesGames
      @NedMakesGames  Před 2 lety

      😅 No shame in that! I'm always thinking about tutorial speed, so thank you for the feedback.

  • @watercat1248
    @watercat1248 Před 10 měsíci

    is thi in URP ?

  • @anonymoussloth6687
    @anonymoussloth6687 Před 2 lety

    If we create meshes using compute shaders, how do we give them mesh colliders and other things like materials?

    • @NedMakesGames
      @NedMakesGames  Před 2 lety

      You can spawn a prefab with a MeshFilter and MeshRenderer already set up, or you can create it in code. Either way, just set MeshFilter.sharedMesh to your created mesh.

    • @anonymoussloth6687
      @anonymoussloth6687 Před 2 lety

      @@NedMakesGames i am sorry, but i didn't understand this (i am still a bit new to this). Could you elaborate? Basically, there 2 two cases. One, i create a mesh and then somehow save it and the other is when i create it dynamically like in procedural generation of terrains. In both cases, the mesh is created but what about their colliders and other things? Like let's say i have a shader or material that i want to attach. And for terrain, i want it to have a collider so i can walk on it

    • @NedMakesGames
      @NedMakesGames  Před 2 lety +1

      @@anonymoussloth6687 I would first read up on prefabs a little. They're a great part of Unity and super powerful: v3.visualdesigncafe.com/learn/nested-prefabs/
      You can add colliders and everything to a prefab, instantiate it, and then simply set your mesh on it's MeshFilter component.
      Saving prefabs as assets is more complicated, so I would get it working at runtime first. Just make a C# script with a reference to your prefab and mesh asset. Then, on Start, instantiate the prefab, call GetComponent() to get the mesh filter object, and then set filter.sharedMesh to your generated mesh. If your prefab has a mesh collider and mesh renderer, it will automatically display and hook into the physics system.

  • @rodgermccallister1606
    @rodgermccallister1606 Před 3 lety

    I've been following along with this tutorial, but when I got to 10:22, I got an error trying to type GraphicsBuffer.Target.Structured. Structured was underlined in red and it says "GraphicsBuffer.Target does not contain a definition for Structured" This even happens when I copy paste the script from your github, so it's not just a typo. Any idea why this is happening?
    Edit: I wasnt using unity 2020.3, ty for the awesome video!

    • @NedMakesGames
      @NedMakesGames  Před 3 lety

      Oh yes, Unity added that relatively recently! Thanks for watching!

  • @wagnerthomas6780
    @wagnerthomas6780 Před 2 lety

    What do you think would be the best method to quickly extrude a shape(pre defined vertices) along a set of predefined points.
    I can already generate this mesh using the cpu, but I would like to speed it up, I have a list of evenly spaced points from a bezier with normal and tangent vector and a shape.
    Do you think the compute shader route is the way to go or something else?
    If it is how do I pass this data to the compute shader?

    • @NedMakesGames
      @NedMakesGames  Před 2 lety +1

      Hi! Yes, you could do this in a compute shader. The trick is to find a way to compute each vertex, or some group of vertices, independent of the others. If you can figure that out, then your problem is well suited for compute shaders.
      To pass the Bezier curve into a compute shader, you need to create a structure to contain it. Perhaps a Bezier point structure with position, normal and tangent? Then, you can create a ComputeBuffer to hold the list of Bezier points, similarly to how I upload mesh vertices in this tutorial.

    • @wagnerthomas6780
      @wagnerthomas6780 Před 2 lety

      @@NedMakesGames Hi! Thanks, yeah that's beyond my abilities.

  • @steffelix98
    @steffelix98 Před 2 lety

    I followed this tutorial exactly, and receive the following console error:
    IndexOutOfRangeException: Invalid kernelIndex (0) passed, must be non-negative less than 1.
    PyramidBaker.Run (UnityEngine.ComputeShader shader, PyramidBakeSettings settings, UnityEngine.Mesh& generatedMesh) (at Assets/Pyramids/Editor/PyramidBaker.cs:125)
    I've tried to debug myself, but can't seem to find a solution due to my very limited shader/HLSL knowledge.
    Any ideas that might be causing this?
    Thanks!

    • @NedMakesGames
      @NedMakesGames  Před 2 lety

      Hi! The debugging messages for compute shaders aren't always the most revealing... There could be a couple of issues.
      This could be caused by a compilation error in your compute shader. Sometimes they don't show up correctly in the console. Select your compute shader asset and see if there are any errors indicated in the inspector.
      If that's all clear, I would double check that your kernel function is registered with a #pragma and that the name matches exactly in the shader and the FindKernel call in C#.

  • @tristunalekzander5608
    @tristunalekzander5608 Před 3 lety

    Do you know how to draw the mesh directly without having to pass vertex data back to the CPU?

    • @NedMakesGames
      @NedMakesGames  Před 3 lety +1

      Yes! I actually do that in this video: czcams.com/video/EB5HiqDl7VE/video.html But, the gist is to use Graphics.DrawMeshProcedural

  • @SandFoxling
    @SandFoxling Před 3 lety

    At 6:15, why multiply the dispatch id by three to get the first index of the index buffer?? Can't wrap my head around that.

    • @NedMakesGames
      @NedMakesGames  Před 3 lety

      Hi! It is confusing! Triangles are stored as three consecutive indices in the index buffer, so each triangle has three ints in the array. The dispatch ids count triangles, not indices, so you have to multiply by three to get the first index of each triangle.

    • @SandFoxling
      @SandFoxling Před 3 lety

      @@NedMakesGames Thank you so much for the reply. That makes sense. I'm assuming for the very first thread, id.x would be 0. I initially assumed it would start from 1.

    • @NedMakesGames
      @NedMakesGames  Před 3 lety +1

      @@SandFoxling Yes, that's right! IDs start at zero and count upwards to a max value of numThreads * dispatchSize - 1

    • @SandFoxling
      @SandFoxling Před 3 lety

      @@NedMakesGames Thank you and keep the quality content coming! :)

    • @NedMakesGames
      @NedMakesGames  Před 3 lety +1

      @@SandFoxling I am planning on it! Thank you for watching!