aboutsummaryrefslogtreecommitdiff
path: root/examples/UnityExample/Assets/Scripts
diff options
context:
space:
mode:
authorBryan Galdrikian <[email protected]>2017-02-24 09:32:20 -0800
committerBryan Galdrikian <[email protected]>2017-02-24 09:32:20 -0800
commite1bf674c16e3c8472b29574159c789cd3f0c64e0 (patch)
tree9f0cfce09c71a2c27ff19589fcad6cd83504477c /examples/UnityExample/Assets/Scripts
parentfirst commit (diff)
downloadblast-e1bf674c16e3c8472b29574159c789cd3f0c64e0.tar.xz
blast-e1bf674c16e3c8472b29574159c789cd3f0c64e0.zip
Updating to [email protected] and [email protected] with a new directory structure.
NvBlast folder is gone, files have been moved to top level directory. README is changed to reflect this.
Diffstat (limited to 'examples/UnityExample/Assets/Scripts')
-rw-r--r--examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs183
-rw-r--r--examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs.meta12
-rw-r--r--examples/UnityExample/Assets/Scripts/CubeFamily.cs202
-rw-r--r--examples/UnityExample/Assets/Scripts/CubeFamily.cs.meta12
-rw-r--r--examples/UnityExample/Assets/Scripts/Demo.cs120
-rw-r--r--examples/UnityExample/Assets/Scripts/Demo.cs.meta12
-rw-r--r--examples/UnityExample/Assets/Scripts/FreeCamera.cs61
-rw-r--r--examples/UnityExample/Assets/Scripts/FreeCamera.cs.meta12
8 files changed, 614 insertions, 0 deletions
diff --git a/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs b/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs
new file mode 100644
index 0000000..fdb6fa0
--- /dev/null
+++ b/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs
@@ -0,0 +1,183 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class CubeAsset
+{
+ public struct DepthInfo
+ {
+ public DepthInfo(Vector3 slices, NvBlastChunkDesc.Flags flag_ = NvBlastChunkDesc.Flags.NoFlags)
+ {
+ this.slicesPerAxis = slices;
+ this.flag = flag_;
+ }
+
+ public Vector3 slicesPerAxis;
+ public NvBlastChunkDesc.Flags flag;
+ };
+
+ public enum BondFlags
+ {
+ NO_BONDS = 0,
+ X_BONDS = 1,
+ Y_BONDS = 2,
+ Z_BONDS = 4,
+ ALL_BONDS = X_BONDS | Y_BONDS | Z_BONDS
+ };
+
+ public class Settings
+ {
+ public List<DepthInfo> depths = new List<DepthInfo>();
+ public Vector3 extents;
+ public BondFlags bondFlags = BondFlags.ALL_BONDS;
+ public float staticHeight = float.NegativeInfinity;
+ };
+
+ public struct BlastChunkCube
+ {
+ public BlastChunkCube(Vector3 position_, Vector3 extents_, bool isStatic_)
+ {
+ this.position = position_;
+ this.extents = extents_;
+ this.isStatic = isStatic_;
+ }
+
+ public Vector3 position;
+ public Vector3 extents;
+ public bool isStatic;
+ };
+
+ public List<BlastChunkCube> chunks = new List<BlastChunkCube>();
+ public NvBlastAssetDesc solverAssetDesc = new NvBlastAssetDesc();
+ public Vector3 extents { get; private set; }
+
+ public static CubeAsset generate(Settings settings)
+ {
+ CubeAsset asset = new CubeAsset();
+ asset.extents = settings.extents;
+
+ List<NvBlastChunkDesc> solverChunks = new List<NvBlastChunkDesc>();
+ List<NvBlastBondDesc> solverBonds = new List<NvBlastBondDesc>();
+
+ // initial params
+ List<uint> depthStartIDs = new List<uint>();
+ List<Vector3> depthSlicesPerAxisTotal = new List<Vector3>();
+ uint currentID = 0;
+ Vector3 extents = settings.extents;
+
+ // Iterate over depths and create children
+ for (int depth = 0; depth<settings.depths.Count; depth++)
+ {
+ Vector3 slicesPerAxis = settings.depths[depth].slicesPerAxis;
+ Vector3 slicesPerAxisTotal = (depth == 0) ? slicesPerAxis : Vector3.Scale(slicesPerAxis, (depthSlicesPerAxisTotal[depth - 1]));
+ depthSlicesPerAxisTotal.Add(slicesPerAxisTotal);
+
+ depthStartIDs.Add(currentID);
+
+ extents.x /= slicesPerAxis.x;
+ extents.y /= slicesPerAxis.y;
+ extents.z /= slicesPerAxis.z;
+
+ for (uint z = 0; z< (uint)slicesPerAxisTotal.z; ++z)
+ {
+ uint parent_z = z / (uint)slicesPerAxis.z;
+ for (uint y = 0; y< (uint)slicesPerAxisTotal.y; ++y)
+ {
+ uint parent_y = y / (uint)slicesPerAxis.y;
+ for (uint x = 0; x< (uint)slicesPerAxisTotal.x; ++x)
+ {
+ uint parent_x = x / (uint)slicesPerAxis.x;
+ uint parentID = depth == 0 ? uint.MaxValue :
+ depthStartIDs[depth - 1] + parent_x + (uint)depthSlicesPerAxisTotal[depth - 1].x * (parent_y + (uint)depthSlicesPerAxisTotal[depth - 1].y * parent_z);
+
+ Vector3 position;
+ position.x = ((float)x - (slicesPerAxisTotal.x / 2) + 0.5f) * extents.x;
+ position.y = ((float)y - (slicesPerAxisTotal.y / 2) + 0.5f) * extents.y;
+ position.z = ((float)z - (slicesPerAxisTotal.z / 2) + 0.5f) * extents.z;
+
+ NvBlastChunkDesc chunkDesc;
+
+ chunkDesc.c0 = position.x;
+ chunkDesc.c1 = position.y;
+ chunkDesc.c2 = position.z;
+ chunkDesc.volume = extents.x * extents.y * extents.z;
+ chunkDesc.flags = (uint)settings.depths[depth].flag;
+ chunkDesc.userData = currentID++;
+ chunkDesc.parentChunkIndex = parentID;
+ solverChunks.Add(chunkDesc);
+
+ bool isStatic = false;
+
+ if (settings.depths[depth].flag == NvBlastChunkDesc.Flags.SupportFlag)
+ {
+ isStatic = position.y - (extents.y - asset.extents.y) / 2 <= settings.staticHeight;
+
+ // x-neighbor
+ if (x > 0 && (settings.bondFlags & BondFlags.X_BONDS) != 0)
+ {
+ Vector3 xNeighborPosition = position - new Vector3(extents.x, 0, 0);
+ uint neighborID = chunkDesc.userData - 1;
+
+ fillBondDesc(solverBonds, chunkDesc.userData, neighborID, position, xNeighborPosition, extents, extents.y* extents.z);
+ }
+
+ // y-neighbor
+ if (y > 0 && (settings.bondFlags & BondFlags.Y_BONDS) != 0)
+ {
+ Vector3 yNeighborPosition = position - new Vector3(0, extents.y, 0);
+ uint neighborID = chunkDesc.userData - (uint)slicesPerAxisTotal.x;
+
+ fillBondDesc(solverBonds, chunkDesc.userData, neighborID, position, yNeighborPosition, extents, extents.z* extents.x);
+ }
+
+ // z-neighbor
+ if (z > 0 && (settings.bondFlags & BondFlags.Z_BONDS) != 0)
+ {
+ Vector3 zNeighborPosition = position - new Vector3(0, 0, extents.z);
+ uint neighborID = chunkDesc.userData - (uint)slicesPerAxisTotal.x * (uint)slicesPerAxisTotal.y;
+
+ fillBondDesc(solverBonds, chunkDesc.userData, neighborID, position, zNeighborPosition, extents, extents.x* extents.y);
+ }
+ }
+
+ asset.chunks.Add(new BlastChunkCube(position, extents, isStatic));
+ }
+ }
+ }
+ }
+
+ // Prepare solver asset desc
+ asset.solverAssetDesc.chunkCount = (uint)solverChunks.Count;
+ asset.solverAssetDesc.chunkDescs = solverChunks.ToArray();
+ asset.solverAssetDesc.bondCount = (uint)solverBonds.Count;
+ asset.solverAssetDesc.bondDescs = solverBonds.ToArray();
+
+ // Reorder chunks
+ uint[] chunkReorderMap = new uint[asset.solverAssetDesc.chunkCount];
+ NvBlastExtUtilsWrapper.ReorderAssetDescChunks(asset.solverAssetDesc, chunkReorderMap);
+ BlastChunkCube[] chunksTemp = asset.chunks.ToArray();
+ for (uint i = 0; i < chunkReorderMap.Length; ++i)
+ {
+ asset.chunks[(int)chunkReorderMap[i]] = chunksTemp[i];
+ }
+
+ return asset;
+ }
+
+ static void fillBondDesc(List<NvBlastBondDesc> bondDescs, uint id0, uint id1, Vector3 pos0, Vector3 pos1, Vector3 size, float area)
+ {
+ NvBlastBondDesc bondDesc = new NvBlastBondDesc();
+ bondDesc.chunk0 = id0;
+ bondDesc.chunk1 = id1;
+ bondDesc.bond.area = area;
+ Vector3 centroid = (pos0 + pos1) * 0.5f;
+ bondDesc.bond.c0 = centroid.x;
+ bondDesc.bond.c1 = centroid.y;
+ bondDesc.bond.c2 = centroid.z;
+ Vector3 normal = (pos0 - pos1).normalized;
+ bondDesc.bond.n0 = normal.x;
+ bondDesc.bond.n1= normal.y;
+ bondDesc.bond.n2 = normal.z;
+ bondDescs.Add(bondDesc);
+ }
+}
diff --git a/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs.meta b/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs.meta
new file mode 100644
index 0000000..0a37254
--- /dev/null
+++ b/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs.meta
@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: efa082786df108947a6fb5d672c1fb1a
+timeCreated: 1481121968
+licenseType: Free
+MonoImporter:
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/examples/UnityExample/Assets/Scripts/CubeFamily.cs b/examples/UnityExample/Assets/Scripts/CubeFamily.cs
new file mode 100644
index 0000000..fda0760
--- /dev/null
+++ b/examples/UnityExample/Assets/Scripts/CubeFamily.cs
@@ -0,0 +1,202 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Runtime.InteropServices;
+using UnityEngine;
+
+
+public class CubeFamily : MonoBehaviour
+{
+ public void Initialize(CubeAsset asset)
+ {
+ // Blast asset creation
+ _cubeAsset = asset;
+
+ NvBlastAssetDesc desc = _cubeAsset.solverAssetDesc;
+ _blastAsset = new NvBlastAsset(desc);
+ Debug.Log(_blastAsset.leafChunkCount);
+
+ // Actual Cubes
+ var cubePrefab = Resources.Load<GameObject>("CubePrefab");
+ _cubes = new GameObject[desc.chunkCount];
+ for (int i = 0; i < desc.chunkCount; ++i)
+ {
+ GameObject cube = GameObject.Instantiate<GameObject>(cubePrefab);
+ cube.transform.parent = transform;
+ cube.transform.localScale = _cubeAsset.chunks[i].extents;
+ cube.transform.localPosition = _cubeAsset.chunks[i].position;
+ cube.transform.localRotation = Quaternion.identity;
+ cube.SetActive(false);
+ Color color = Color.HSVToRGB(UnityEngine.Random.Range(0f, 1f), 0.42f, 1.0f);
+ cube.GetComponent<Renderer>().material.color = color;
+ _cubes[i] = cube;
+ }
+
+ // First actor
+ _blastFamily = new NvBlastFamily(_blastAsset);
+
+ NvBlastActorDesc actorDesc = new NvBlastActorDesc();
+ actorDesc.uniformInitialBondHealth = 1.0f;
+ actorDesc.uniformInitialLowerSupportChunkHealth = 1.0f;
+ var actor = new NvBlastActor(_blastFamily, actorDesc);
+ Debug.Log(actor.visibleChunkCount);
+
+ OnActorCreated(actor, Vector3.zero, Quaternion.identity);
+
+ // Reserved buffers
+ _fractureBuffers = new NvBlastFractureBuffers();
+ _fractureBuffers.chunkFractures = Marshal.AllocHGlobal((int)desc.chunkCount * Marshal.SizeOf(typeof(NvBlastChunkFractureData)));
+ _fractureBuffers.bondFractures = Marshal.AllocHGlobal((int)desc.bondCount * Marshal.SizeOf(typeof(NvBlastBondFractureData)));
+ _leafChunkCount = (uint)_blastAsset.leafChunkCount;
+ _newActorsBuffer = Marshal.AllocHGlobal((int)_leafChunkCount * Marshal.SizeOf(typeof(IntPtr)));
+ }
+
+ private void OnActorCreated(NvBlastActor actor, Vector3 localPosition, Quaternion localRotation)
+ {
+ var rigidBodyGO = new GameObject("RigidActor");
+ rigidBodyGO.transform.SetParent(this.transform, false);
+ var rigidbody = rigidBodyGO.AddComponent<Rigidbody>();
+ rigidbody.transform.localPosition = localPosition;
+ rigidbody.transform.localRotation = localRotation;
+
+ // chunks
+ var chunkIndices = actor.visibleChunkIndices;
+ foreach (var chunkIndex in chunkIndices)
+ {
+ var chunkCube = _cubes[chunkIndex];
+ chunkCube.transform.SetParent(rigidbody.transform, false);
+ chunkCube.SetActive(true);
+ }
+
+ // search for static chunks
+ var graphNodeIndices = actor.graphNodeIndices;
+ var chunkGraph = _blastAsset.chunkGraph;
+ foreach(var node in graphNodeIndices)
+ {
+ var chunkIndex = Marshal.ReadInt32(chunkGraph.chunkIndices, Marshal.SizeOf(typeof(UInt32)) * (int)node);
+ var chunkCube = _cubeAsset.chunks[chunkIndex];
+ if(chunkCube.isStatic)
+ {
+ rigidbody.isKinematic = true;
+ break;
+ }
+ }
+
+ actor.userData = rigidbody;
+ _actors.Add(rigidbody, actor);
+ }
+
+ private void OnActorDestroyed(NvBlastActor actor)
+ {
+ var chunkIndices = actor.visibleChunkIndices;
+ foreach (var chunkIndex in chunkIndices)
+ {
+ var chunkCube = _cubes[chunkIndex];
+ chunkCube.transform.SetParent(transform, false);
+ chunkCube.SetActive(false);
+ }
+
+ var rigidbody = (actor.userData as Rigidbody);
+ _actors.Remove(rigidbody);
+ Destroy(rigidbody.gameObject);
+ actor.userData = null;
+ }
+
+ public void ApplyRadialDamage(Vector3 position, float minRadius, float maxRadius, float compressive)
+ {
+ var hits = Physics.OverlapSphere(position, maxRadius);
+ foreach (var hit in hits)
+ {
+ var rb = hit.GetComponentInParent<Rigidbody>();
+ if (rb != null)
+ {
+ ApplyRadialDamage(rb, position, minRadius, maxRadius, compressive);
+ }
+ }
+ }
+
+ public bool ApplyRadialDamage(Rigidbody rb, Vector3 position, float minRadius, float maxRadius, float compressive)
+ {
+ if (_actors.ContainsKey(rb))
+ {
+ Vector3 localPosition = rb.transform.InverseTransformPoint(position);
+ ApplyRadialDamage(_actors[rb], localPosition, minRadius, maxRadius, compressive);
+ return true;
+ }
+ return false;
+ }
+
+ private void ApplyRadialDamage(NvBlastActor actor, Vector3 localPosition, float minRadius, float maxRadius, float compressive)
+ {
+ _fractureBuffers.chunkFractureCount = _cubeAsset.solverAssetDesc.chunkCount;
+ _fractureBuffers.bondFractureCount = _cubeAsset.solverAssetDesc.bondCount;
+
+ NvBlastExtRadialDamageDesc desc = new NvBlastExtRadialDamageDesc();
+ desc.minRadius = minRadius;
+ desc.maxRadius = maxRadius;
+ desc.compressive = compressive;
+ desc.p0 = localPosition.x;
+ desc.p1 = localPosition.y;
+ desc.p2 = localPosition.z;
+
+ if (actor.DamageRadialFalloff(_fractureBuffers, desc, 1, null))
+ {
+ Split(actor);
+ }
+ }
+
+ private void Split(NvBlastActor actor)
+ {
+ NvBlastActorSplitEvent split = new NvBlastActorSplitEvent();
+ split.newActors = _newActorsBuffer;
+ var count = actor.Split(split, _leafChunkCount);
+
+ Vector3 localPosition = Vector3.zero;
+ Quaternion localRotation = Quaternion.identity;
+
+ if (split.deletedActor != IntPtr.Zero)
+ {
+ if (actor.userData != null)
+ {
+ var parentRigidbody = (actor.userData as Rigidbody);
+ localPosition = parentRigidbody.transform.localPosition;
+ localRotation = parentRigidbody.transform.localRotation;
+ }
+ OnActorDestroyed(actor);
+ }
+ for (int i = 0; i < count; i++)
+ {
+ int elementSize = Marshal.SizeOf(typeof(IntPtr));
+ var ptr = Marshal.ReadIntPtr(split.newActors, elementSize * i);
+ OnActorCreated(new NvBlastActor(_blastFamily, ptr), localPosition, localRotation);
+ }
+ }
+
+ private void OnDestroy()
+ {
+ if (_fractureBuffers != null)
+ {
+ Marshal.FreeHGlobal(_fractureBuffers.chunkFractures);
+ Marshal.FreeHGlobal(_fractureBuffers.bondFractures);
+ _fractureBuffers = null;
+ }
+
+ if (_newActorsBuffer != IntPtr.Zero)
+ {
+ Marshal.FreeHGlobal(_newActorsBuffer);
+ }
+
+ _actors.Clear();
+ _blastFamily = null;
+ _blastAsset = null;
+ }
+
+ private CubeAsset _cubeAsset;
+ private NvBlastAsset _blastAsset;
+ private NvBlastFamily _blastFamily;
+ private Dictionary<Rigidbody, NvBlastActor> _actors = new Dictionary<Rigidbody, NvBlastActor>();
+ private GameObject[] _cubes;
+ private NvBlastFractureBuffers _fractureBuffers;
+ private IntPtr _newActorsBuffer;
+ private uint _leafChunkCount;
+} \ No newline at end of file
diff --git a/examples/UnityExample/Assets/Scripts/CubeFamily.cs.meta b/examples/UnityExample/Assets/Scripts/CubeFamily.cs.meta
new file mode 100644
index 0000000..f61e39a
--- /dev/null
+++ b/examples/UnityExample/Assets/Scripts/CubeFamily.cs.meta
@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 0fe5c9affaa641644b99cb8e384234d4
+timeCreated: 1481725814
+licenseType: Free
+MonoImporter:
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/examples/UnityExample/Assets/Scripts/Demo.cs b/examples/UnityExample/Assets/Scripts/Demo.cs
new file mode 100644
index 0000000..eb60d26
--- /dev/null
+++ b/examples/UnityExample/Assets/Scripts/Demo.cs
@@ -0,0 +1,120 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class Demo : MonoBehaviour
+{
+ public GameObject hitSphere;
+
+ void Awake ()
+ {
+ generateCity();
+ }
+
+ private void generateCity()
+ {
+ const int BUILDING_TYPE_COUNT = 5;
+ Vector3 BUILDING_MIN_SIZE = new Vector3(10, 10, 10);
+ Vector3 BUILDING_MAX_SIZE = new Vector3(50, 200, 50);
+
+ List<CubeAsset> buildingTypes = new List<CubeAsset>(BUILDING_TYPE_COUNT);
+ for (int i = 0; i < BUILDING_TYPE_COUNT; ++i)
+ {
+ CubeAsset.Settings settings = new CubeAsset.Settings();
+ settings.depths.Add(new CubeAsset.DepthInfo(new Vector3(1, 1, 1), NvBlastChunkDesc.Flags.NoFlags));
+ settings.depths.Add(new CubeAsset.DepthInfo(new Vector3(1, 2, 1), NvBlastChunkDesc.Flags.NoFlags));
+ settings.depths.Add(new CubeAsset.DepthInfo(new Vector3(2, 3, 2), NvBlastChunkDesc.Flags.NoFlags));
+ settings.depths.Add(new CubeAsset.DepthInfo(new Vector3(2, 2, 2), NvBlastChunkDesc.Flags.SupportFlag));
+ settings.extents = new Vector3(Random.Range(BUILDING_MIN_SIZE.x, BUILDING_MAX_SIZE.x), Random.Range(BUILDING_MIN_SIZE.y, BUILDING_MAX_SIZE.y), Random.Range(BUILDING_MIN_SIZE.z, BUILDING_MAX_SIZE.z));
+ settings.staticHeight = 10.0f;
+
+ CubeAsset cubeAsset = CubeAsset.generate(settings);
+
+ buildingTypes.Add(cubeAsset);
+ }
+
+ int totalBuildings = 0;
+
+ const float CITY_HALF_SIZE = 200.0f;
+ const float SPARSITY = 30.0f;
+ const int BUILDING_COUNT_MAX = 20;
+ Vector2 pos = new Vector2(-CITY_HALF_SIZE, -CITY_HALF_SIZE);
+ float buildingYMax = 0.0f;
+ while (pos.y < CITY_HALF_SIZE && totalBuildings < BUILDING_COUNT_MAX)
+ {
+ // random jump
+ pos.x += Random.Range(0.0f, SPARSITY);
+ if(pos.x > CITY_HALF_SIZE)
+ {
+ pos.x = -CITY_HALF_SIZE;
+ pos.y += buildingYMax + Random.Range(0.0f, SPARSITY);
+ buildingYMax = 0.0f;
+ continue;
+ }
+
+ // random bulding type spawn
+ int type = Random.Range(0, buildingTypes.Count);
+ var cubeFamily = (new GameObject("Cube Actor #" + type)).AddComponent<CubeFamily>();
+ CubeAsset asset = buildingTypes[type];
+ cubeFamily.transform.localPosition = new Vector3(pos.x, asset.extents.y / 2.0f, pos.y);
+ pos.x += asset.extents.x;
+ buildingYMax = Mathf.Max(buildingYMax, asset.extents.z);
+ cubeFamily.Initialize(asset);
+ totalBuildings++;
+ }
+ }
+
+ private IEnumerator applyRadialDamage(Vector3 position, float minRadius, float maxRadius, float compressive, float explosive = 3000.0f)
+ {
+ var hits = Physics.OverlapSphere(position, maxRadius);
+ foreach (var hit in hits)
+ {
+ var rb = hit.GetComponentInParent<Rigidbody>();
+ var family = hit.GetComponentInParent<CubeFamily>();
+ if (rb != null && family != null)
+ {
+ family.ApplyRadialDamage(rb, position, minRadius, maxRadius, compressive);
+ }
+ }
+
+ yield return new WaitForEndOfFrame();
+
+ hits = Physics.OverlapSphere(position, maxRadius);
+ foreach (var hit in hits)
+ {
+ var rb = hit.GetComponentInParent<Rigidbody>();
+ if(rb != null)
+ {
+ rb.AddExplosionForce(explosive, position, maxRadius, 3.0f);
+ }
+ }
+ }
+
+ private void Update()
+ {
+ hitSphere.SetActive(false);
+ bool isActive = false;
+ if (true)
+ {
+ var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
+ RaycastHit hit;
+ if(Physics.Raycast(ray, out hit))
+ {
+ hitSphere.transform.position = hit.point;
+ isActive = true;
+ }
+ }
+
+ _hitSphereSize += Input.GetAxis("Mouse ScrollWheel") * 10.0f;
+
+ if (Input.GetMouseButton(0))
+ {
+ StartCoroutine(applyRadialDamage(hitSphere.transform.position, 0.0f, _hitSphereSize, 10.0f));
+ }
+
+ hitSphere.SetActive(isActive);
+ hitSphere.transform.localScale = new Vector3(_hitSphereSize, _hitSphereSize, _hitSphereSize);
+ }
+
+ private float _hitSphereSize = 25.0f;
+}
diff --git a/examples/UnityExample/Assets/Scripts/Demo.cs.meta b/examples/UnityExample/Assets/Scripts/Demo.cs.meta
new file mode 100644
index 0000000..edd0d3b
--- /dev/null
+++ b/examples/UnityExample/Assets/Scripts/Demo.cs.meta
@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 7a62789bffb0bef4e9757f9848620e3f
+timeCreated: 1481120125
+licenseType: Free
+MonoImporter:
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/examples/UnityExample/Assets/Scripts/FreeCamera.cs b/examples/UnityExample/Assets/Scripts/FreeCamera.cs
new file mode 100644
index 0000000..1e9fd67
--- /dev/null
+++ b/examples/UnityExample/Assets/Scripts/FreeCamera.cs
@@ -0,0 +1,61 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class FreeCamera : MonoBehaviour
+{
+ public float cameraSensitivity = 90;
+ public float climbSpeed = 4;
+ public float normalMoveSpeed = 10;
+ public float slowMoveFactor = 0.25f;
+ public float fastMoveFactor = 3;
+
+ private float rotationX = 0.0f;
+ private float rotationY = 0.0f;
+
+ void Start()
+ {
+ Cursor.lockState = CursorLockMode.Confined;
+ rotationX = transform.localRotation.eulerAngles.y;
+ rotationY = -transform.localRotation.eulerAngles.x;
+ }
+
+ void Update()
+ {
+ if(!Input.GetMouseButton(1))
+ {
+ return;
+ }
+
+ rotationX += Input.GetAxis("Mouse X") * cameraSensitivity * Time.deltaTime;
+ rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity * Time.deltaTime;
+ rotationY = Mathf.Clamp(rotationY, -90, 90);
+
+ transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
+ transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
+
+ if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
+ {
+ transform.position += transform.forward * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
+ transform.position += transform.right * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
+ }
+ else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
+ {
+ transform.position += transform.forward * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
+ transform.position += transform.right * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
+ }
+ else
+ {
+ transform.position += transform.forward * normalMoveSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
+ transform.position += transform.right * normalMoveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
+ }
+
+ if (Input.GetKey(KeyCode.Q)) { transform.position -= transform.up * climbSpeed * Time.deltaTime; }
+ if (Input.GetKey(KeyCode.E)) { transform.position += transform.up * climbSpeed * Time.deltaTime; }
+
+ //if (Input.GetKeyDown(KeyCode.End))
+ //{
+ // Cursor.lockState = (Cursor.lockState == CursorLockMode.Confined) ? CursorLockMode.None : CursorLockMode.Confined;
+ //}
+ }
+}
diff --git a/examples/UnityExample/Assets/Scripts/FreeCamera.cs.meta b/examples/UnityExample/Assets/Scripts/FreeCamera.cs.meta
new file mode 100644
index 0000000..081cae7
--- /dev/null
+++ b/examples/UnityExample/Assets/Scripts/FreeCamera.cs.meta
@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 29f022fb3768b8c4196631a9526aebcc
+timeCreated: 1482928025
+licenseType: Free
+MonoImporter:
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant: