Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
komietty committed Dec 16, 2019
0 parents commit 7be5c1e
Show file tree
Hide file tree
Showing 29 changed files with 2,367 additions and 0 deletions.
60 changes: 60 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Mm]emoryCaptures/
/[Cc]apture*/

# Never ignore Asset meta data
!/[Aa]ssets/**/*.meta

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.unitypackage

# Crashlytics generated file
crashlytics-build.properties
8 changes: 8 additions & 0 deletions Assets/Boids.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 168 additions & 0 deletions Assets/Boids/Boids.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Jobs;
using Unity.Jobs;
using Unity.Burst;
using Random = Unity.Mathematics.Random;

public class Boids : MonoBehaviour {

#region Job
[BurstCompile]
struct UpdateMove : IJobParallelForTransform {
public NativeArray<Vector3> position;
public NativeArray<Vector3> velocity;
public NativeArray<Vector3> accel;
public Vector2 limit;
public float dt;

void IJobParallelForTransform.Execute(int id, TransformAccess t) {
var vel = velocity[id] + accel[id] * dt;
var dir = Vector3.Normalize(vel);
var mag = Vector3.Magnitude(vel);
vel = math.clamp(mag, limit.x, limit.y) * dir;
t.position += vel * dt;
t.rotation = Quaternion.LookRotation(dir);
accel[id] = Vector3.zero;
position[id] = t.position;
velocity[id] = vel;
}
}

[BurstCompile]
struct UpdateWall : IJobParallelFor {
[ReadOnly] public NativeArray<Vector3> position;
public NativeArray<Vector3> accel;
public Vector3 scale;

public void Execute(int id) {
var p = position[id];
var s = scale * 0.5f;
accel[id] += Condition(-s.x - p.x, Vector3.right) +
Condition(-s.y - p.y, Vector3.up) +
Condition(-s.z - p.z, Vector3.forward) +
Condition(+s.x - p.x, Vector3.left) +
Condition(+s.y - p.y, Vector3.down) +
Condition(+s.z - p.z, Vector3.back);
}

Vector3 Condition(float dst, Vector3 dir) {
var threshold = 3f;
var weight = 2f;
var d = math.abs(dst);
return d < threshold ? dir * (weight / (d / threshold)) : Vector3.zero;
}
}

[BurstCompile]
struct UpdateSmlt : IJobParallelFor {
[ReadOnly] public NativeArray<Vector3> position;
[ReadOnly] public NativeArray<Vector3> velocity;
public NativeArray<Vector3> accel;
public Vector3 weights;
public float dstThreshold;
int n => position.Length;

public void Execute(int id) {
Vector3 avgSpr = Vector3.zero,
avgVel = Vector3.zero,
avgPos = Vector3.zero,
pos = position[id],
vel = velocity[id];

for (int j = 0; j < n; j++) {
if (j == id) continue;
var tgtPos = position[j];
var tgtVel = velocity[j];
var tgtDif = pos - tgtPos;
if (tgtDif.magnitude < dstThreshold) {
avgSpr += tgtDif.normalized;
avgVel += tgtVel;
avgPos += tgtPos;
}
}
accel[id] += (avgSpr * weights.x + avgVel * weights.y + avgPos * weights.z) / n;
}
}

[Unity.Burst.BurstCompile]
struct CountJob : IJob {
public NativeArray<Vector3> position;
public NativeArray<int> result;
int n => position.Length;

public void Execute() {
result[0] = 0;
for (int i = 0; i < n; i++) {
if(position[i].x > 0) result[0] += 1;
}
}
}

#endregion

[SerializeField] protected int num;
[SerializeField] protected GameObject prefab;
[SerializeField] protected Vector3 areaSize;
[SerializeField] protected float distThreshold;
[SerializeField] Vector2 velThreshold;
[SerializeField] Vector3 simWeight;
protected Transform[] objs;
protected NativeArray<Vector3> pos, vel, acc;
protected TransformAccessArray trs;
protected NativeArray<Random> rnd;
protected NativeArray<int> rst;
protected Random seed;

void Start() {
objs = new Transform[num];
for (int i = 0; i < num; i++) {
var obj = Instantiate(prefab).transform;
obj.position = Vector3.zero;
objs[i] = obj;
}

pos = new NativeArray<Vector3>(num, Allocator.Persistent);
vel = new NativeArray<Vector3>(num, Allocator.Persistent);
acc = new NativeArray<Vector3>(num, Allocator.Persistent);
trs = new TransformAccessArray(objs);
rst = new NativeArray<int>(1, Allocator.Persistent);
rnd = new NativeArray<Random>(num, Allocator.Persistent);

for (int i = 0; i < num; i++) {
pos[i] = Vector3.zero;
vel[i] = UnityEngine.Random.insideUnitSphere;
acc[i] = Vector3.zero;
}
seed = new Random(1);
}

void Update() {
for (int i = 0; i < num; i++) rnd[i] = new Random(seed.NextUInt());
var jobWall = new UpdateWall { position = pos, accel = acc, scale = areaSize};
var jobSmlt = new UpdateSmlt { position = pos, velocity = vel, accel = acc, dstThreshold = distThreshold, weights = simWeight};
var jobMove = new UpdateMove { position = pos, velocity = vel, accel = acc, dt = Time.deltaTime, limit = velThreshold};
var handlerWall = jobWall.Schedule(num, 0);
var handlerSmlt = jobSmlt.Schedule(num, 0, handlerWall);
var handlerMove = jobMove.Schedule(trs, handlerSmlt);
handlerMove.Complete();
}

void OnDrawGizmos() {
Gizmos.DrawWireCube(Vector3.zero, areaSize);
}
void OnGUI() {
}

void OnDestroy() {
pos.Dispose();
vel.Dispose();
acc.Dispose();
trs.Dispose();
rst.Dispose();
rnd.Dispose();
}
}
11 changes: 11 additions & 0 deletions Assets/Boids/Boids.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions Assets/Boids/Cube.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &4950830921021070585
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1895636199557260637}
- component: {fileID: 1927478061566893167}
- component: {fileID: 6563147623067794511}
m_Layer: 0
m_Name: Cube
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1895636199557260637
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4950830921021070585}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0.1, y: 0.1, z: 0.5}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!33 &1927478061566893167
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4950830921021070585}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &6563147623067794511
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4950830921021070585}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
7 changes: 7 additions & 0 deletions Assets/Boids/Cube.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7be5c1e

Please sign in to comment.