GithubHelp home page GithubHelp logo

jongallant / unity-bullet-hell Goto Github PK

View Code? Open in Web Editor NEW
298.0 15.0 40.0 485 KB

An extremely efficient projectile generator for unity.

License: MIT License

ShaderLab 7.48% C# 92.52%
unity3d bullet-hell gpu-instance unity projectiles particles

unity-bullet-hell's Introduction

Unity-Bullet-Hell

An extremely efficient projectile and particle generator for Unity.

What is it?

Animated Gif

This system makes use of Unity's Graphics.DrawMeshInstancedIndirect functionality, resulting in a single draw call per Projectile Type. This highly efficient system is able to handle an incredibly large number of projectiles at the same time.

This projectile system is tuned for 2D usage, and provides a multitude of options and configurations.

How do I use this?

This system is composed of a couple of parts.

Projectile Prefabs

Projectile Example

Projectile Prefabs are prefabs with an attached "ProjectilePrefab.cs" Monobehaviour derived script. They are stored in the "/Resources/ProjectilePrefabs" folder. These define the Mesh, Texture, Z-Index and other properties related to the Projectile Shader. It defines the appearance of the projectile, and the maximum number of projectiles per material instance.

You will want to tweak the "Max Projectile Count" value to be as low as possible for your application, in order to maximize efficiency. This caps the size of the buffers and data pools. If it is set too low, you will run out of projectiles to fire. If set too high, you will have reserved too much memory for your application.

Emitters

Emitter Example

Emitters spawn projectiles into the world, based on a set of properties. All of these properties can be programmatically controller or animated.

Shape Emitters

Shape Example

You can also create a custom shape template, and place it in "/Resources/ShapeTemplates". You can then use this Shape Template, on the ProjectileEmitterShape instance, and it will spawn the pattern you specified.

One Draw Call

The most amazing part is that you can draw large amounts of meshes in a single draw call. In the example below, every dot of color is a sphere mesh, and is all done in a single draw call.

http://www.jgallant.com/images/unitybullethell/test.gif

unity-bullet-hell's People

Contributors

jongallant avatar mpozek avatar reakain avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unity-bullet-hell's Issues

Doesn't seem to work (Unity 2019.1.13f1)

Opened in Unity 2019.1.13f1, loaded the two sample scenes and neither showed any bullets.

The bullet count did increase though. Projectile prefab wasn't present in the projectile slot on one emitter. Added it, but to no avail.

Maybe it's a shader issue? I have an older graphics card (8800GT)

bullets are not shown in build version

It works perfectly in unity, but when I tried to build the SampleScene in either windows or webgl, the bullets do not load and this is the error message I got:

ArgumentNullException: Value cannot be null.
Parameter name: shader
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
NullReferenceException: Object reference not set to an instance of an object

I'm using Unity 2020.3.0f1, I'm on the latest master. I tried to turn off Enable GPU instancing in materials but it makes no difference.

Building Project

When trying to build the project I got the error "EditorPrefs" does not exist in the current context. I commented it and the references in the EditorOverride script. Once the executable is ready, the bullets nor the bullet counter seem to appear. Is it related?
If so, how do I fix the ""EditorPrefs" does not exist in the current context" (Bullets and counter still appear in the editor).

Thanks in advance.
Captura

Guideline for Emitters attached to game objects with lyfecycle?

I'm currently working on a project that has the following conditions:

  • When a weapon is equipped, a new emitter is created for that weapon.
  • If I unequip this weapon, and equip another weapon a new emitter is created for this new equipped weapon.
  • For now, all weapons are using the same Emitter Prefab (I'm wondering if this is causing the issue, but I'd like to have the option of re-using emitter prefabs, and just changing certain data from the code)

Every time I switch a weapon, and therefore a new Emitter is created, I get a weird Overflow Exception in IndirectRenderer class.

I did some dive into the code, and the issue seems to happen because the previous emitter, will pass all the assigned projectiles + 1 to the new emitter active projectiles.
This results in 50001 which overflows the TransformBuffer in the Draw method for IndirectRenderer.

Here's what I'm doing every time I equip a new weapon:

// I set emitter prefab from the editor. 
// emitterPosition is also set in editor, and it's an empty transform containing the position where I want the emitter to be.
GameObject emitter = Instantiate(emitterPrefab, emitterPosition); 

// Add the emitter to the Projectile Manager.
ProjectileManager.Instance.AddEmitter(emitter.GetComponent<ProjectileEmitterAdvanced>(), 100);
// Register this emitter.          
ProjectileManager.Instance.RegisterEmitter(emitter.GetComponent<ProjectileEmitterAdvanced>());

I tried fixing the bug, but I still need to familiarize a little more with the process.
I'd be happy to help out fixing it, but I'd like to know first your insight on what is going on, and how I Could handle multiple dynamic emitter instantiations or, in other words, emitters attached to equipped weapons aligned to their lyfecycle.

Edit

I tried with two different prefabs, and the bug was gone.
I think I'm ok with creating a new emitter per weapon, I don't think this is a bug anymore.

Thanks for this awesome lib !

Edit 2

Wow, I was so excited by seeing the bug gone that I didn't pay attention that also the running particles that were created by the previous emitter (previous equipped weapon) will also dissapear when I switch weapons.

So this is no longer the bug I created for this issue, but I'd love some insight on this issue, and what would you recommend doing.

Edit 3

I was able to make it work. I had to do some trickery.. For instance, I had to implement a class that handles the recycling of these emitters, if a weapon is equipped that needs an emitter that has been instantiated before, we return the instance of the emitter instead.
Before destroying the equipped weapon, I had to unattach this emitter from it to avoid it being destroyed.

This seems to make everything work as expected. Just one quick question for you: Do you think this implementation is the right one? Is there anything you'd recommend for dynamic usage of emitters?

depth

Is there a way to have the bullets move on the z axis. I'm making a Doom style game, im using Unity 2D but have it in a way that it appears 3d. I need the patterns to be able to manipulate up and down on the zAxis if possible TY!
git
github

How to use shape emitter?

I created an empty game object, attached the shape emitter script and added a shape to the shape template field but no projectiles spawn? The console says the method Update Projectiles is not implemented.

Any help?

Projectiles not displaying in WebGL Build

I'm using Unity 2020.3.15f2. If I don't add the shaders to the project settings, it will throw errors like this. And the projectiles won't display.

ArgumentNullException: Value cannot be null.
Parameter name: shader

After I added the shaders like this, it didn't have errors but the projectiles weren't displaying. The projectile number wasn't showing, too.

image

sorting layer

I've been trying a bunch of different things and i'm falling short every time. Is it possible to somehow change the sorting layer? I'm using tilemaps and so the bullets are spawning "behind" the tilemaps..

flickering bullet

I'm having an issue that I haven't narrowed down completely..

But it seems like when a bullet hits an edge, maybe the player collider or a tilemap corner collider.. the bullets kind of freeze and just flicker over and over again until I go out of frame and it resets.

How to use Shape Emitters?

This is awesome work! But I haven't figured out how to use the shape one. I tried:

  1. add a ProjectileEmitterShape, then add the square shape on it, but when I run the game this code conplaints:
protected override void UpdateProjectiles(float tick)
        {
            throw new System.NotImplementedException();
        }
  1. I tried to find out where to add the shape item on the ProjectileEmitterAdvanced but failed to find it.
  2. I also didn't find an example prefab of it...
    Let me know if I missed anything or how can I make it work. Again this is a great project thanks for sharing!

How can I use collisions with this?

I am making a bullet hell game based on this, but i can't find anything to make it collide with my player. How can i attach a collider to the bullet?

How can I pause the projectiles when the game running

This is a new function I suggest.I'm making a Editor of a bullet hell game,I need to pause the bullets when I press a button and resume when I press it again.
If you have free time,can you tell me have to make it.
Thanks so much

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.