An open source Danmaku development kit for Unity3D.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add DanmakuModifiers

james713 defb173e 82991657

+69 -55
+1 -1
Assets/DanmakuPoolTest.cs
··· 30 30 /// Update is called every frame, if the MonoBehaviour is enabled. 31 31 /// </summary> 32 32 void Update() { 33 - pool.Update().Dispose(); 33 + pool.Update().Complete(); 34 34 timer += Time.deltaTime; 35 35 if (timer > 1/20f) { 36 36 State.Rotation += 20 * Mathf.Deg2Rad;
+18 -45
Assets/src/Core/DanmakuPool.cs
··· 10 10 11 11 public class DanmakuPool : IEnumerable<Danmaku>, IDisposable { 12 12 13 + const int kBatchSize = 32; 14 + 13 15 public int ActiveCount { get; private set; } 14 16 15 17 internal NativeArray<float> Times; ··· 43 45 Transforms = new NativeArray<Matrix4x4>(poolSize, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); 44 46 } 45 47 46 - public UpdateContext Update(JobHandle dependency = default(JobHandle)) { 48 + public JobHandle Update(JobHandle dependency = default(JobHandle)) { 47 49 var bounds = DanmakuManager.Instance.Bounds; 48 50 for (var i = 0; i < ActiveCount; i++) { 49 51 if (bounds.Contains(Positions[i])) continue; ··· 54 56 DestroyInternal(Deactivated.Pop()); 55 57 } 56 58 57 - if (ActiveCount <= 0) return new UpdateContext(); 58 - return new UpdateContext(this, dependency); 59 + if (ActiveCount <= 0) return dependency; 60 + 61 + return new MoveDanmaku { 62 + DeltaTime = Time.deltaTime, 63 + 64 + Times = Times, 65 + 66 + Positions = Positions, 67 + Rotations = Rotations, 68 + 69 + Speeds = Speeds, 70 + AngularSpeeds = AngularSpeeds, 71 + 72 + Transforms = Transforms 73 + }.Schedule(ActiveCount, kBatchSize, dependency); 59 74 } 60 75 61 76 /// <summary> ··· 123 138 Swap(ref Speeds, index, ActiveCount); 124 139 Swap(ref AngularSpeeds, index, ActiveCount); 125 140 Swap(ref Colors, index, ActiveCount); 126 - } 127 - 128 - public struct UpdateContext : IDisposable { 129 - 130 - const int kBatchSize = 32; 131 - 132 - public readonly NativeArray<Vector2> OldPositions; 133 - public readonly NativeArray<float> OldRotations; 134 - public readonly JobHandle UpdateJobHandle; 135 - readonly bool isValid; 136 - 137 - internal UpdateContext(DanmakuPool pool, JobHandle dependency) { 138 - OldPositions = new NativeArray<Vector2>(pool.Positions, Allocator.TempJob); 139 - OldRotations = new NativeArray<float>(pool.Rotations, Allocator.TempJob); 140 - 141 - UpdateJobHandle = new MoveDanmaku { 142 - DeltaTime = Time.deltaTime, 143 - 144 - Times = pool.Times, 145 - 146 - CurrentPositions = OldPositions, 147 - CurrentRotations = OldRotations, 148 - 149 - NewPositions = pool.Positions, 150 - NewRotations = pool.Rotations, 151 - 152 - Speeds = pool.Speeds, 153 - AngularSpeeds = pool.AngularSpeeds, 154 - 155 - Transforms = pool.Transforms 156 - }.Schedule(pool.ActiveCount, kBatchSize, dependency); 157 - 158 - isValid = true; 159 - } 160 - 161 - public void Dispose() { 162 - if (!isValid) return; 163 - UpdateJobHandle.Complete(); 164 - OldPositions.Dispose(); 165 - OldRotations.Dispose(); 166 - } 167 - 168 141 } 169 142 170 143 public struct Enumerator : IEnumerator<Danmaku> {
+33
Assets/src/Core/IDanmakuModifier.cs
··· 1 + using System.Collections; 2 + using System.Collections.Generic; 3 + using UnityEngine; 4 + using Unity.Jobs; 5 + 6 + namespace DanmakU { 7 + 8 + public abstract class DanmakuModifier { 9 + 10 + public virtual JobHandle PreUpdate(DanmakuPool pool, JobHandle dependency) => dependency; 11 + public virtual JobHandle PostUpdate(DanmakuPool pool, JobHandle dependency) => dependency; 12 + 13 + } 14 + 15 + public static class IDanmakuModifierExtensions { 16 + 17 + public static JobHandle RunUpdates(this List<DanmakuModifier> modifiers, 18 + DanmakuPool pool, 19 + JobHandle dependency = default(JobHandle)) { 20 + foreach (var modifier in modifiers) { 21 + dependency = modifier.PreUpdate(pool, dependency); 22 + } 23 + dependency = pool.Update(dependency); 24 + foreach (var modifier in modifiers) { 25 + dependency = modifier.PostUpdate(pool, dependency); 26 + } 27 + return dependency; 28 + } 29 + 30 + 31 + } 32 + 33 + }
+11
Assets/src/Core/IDanmakuModifier.cs.meta
··· 1 + fileFormatVersion: 2 2 + guid: b41165502725c3b49ac6affd6f964d90 3 + MonoImporter: 4 + externalObjects: {} 5 + serializedVersion: 2 6 + defaultReferences: [] 7 + executionOrder: 0 8 + icon: {instanceID: 0} 9 + userData: 10 + assetBundleName: 11 + assetBundleVariant:
+6 -9
Assets/src/Core/Jobs/MoveDanmaku.cs
··· 8 8 9 9 public NativeArray<float> Times; 10 10 11 - [ReadOnly] public NativeArray<Vector2> CurrentPositions; 12 - [ReadOnly] public NativeArray<float> CurrentRotations; 13 - 14 - [WriteOnly] public NativeArray<Vector2> NewPositions; 15 - [WriteOnly] public NativeArray<float> NewRotations; 11 + public NativeArray<Vector2> Positions; 12 + public NativeArray<float> Rotations; 16 13 17 14 [ReadOnly] public NativeArray<float> Speeds; 18 15 [ReadOnly] public NativeArray<float> AngularSpeeds; ··· 20 17 [WriteOnly] public NativeArray<Matrix4x4> Transforms; 21 18 22 19 public void Execute(int index) { 23 - var rotation = (CurrentRotations[index] + AngularSpeeds[index] * DeltaTime) % (Mathf.PI * 2); 24 - var position = CurrentPositions[index] + (Speeds[index] * RotationUtil.ToUnitVector(rotation) * DeltaTime); 20 + var rotation = (Rotations[index] + AngularSpeeds[index] * DeltaTime) % (Mathf.PI * 2); 21 + var position = Positions[index] + (Speeds[index] * RotationUtil.ToUnitVector(rotation) * DeltaTime); 25 22 26 23 var rotationQuat = Quaternion.Euler(0, 0, rotation * Mathf.Rad2Deg); 27 24 28 25 Times[index] += DeltaTime; 29 26 Transforms[index] = Matrix4x4.TRS(position, rotationQuat, Vector3.one); 30 - NewPositions[index] = position; 31 - NewRotations[index] = rotation; 27 + Positions[index] = position; 28 + Rotations[index] = rotation; 32 29 } 33 30 34 31 }