diff options
| author | Bryan Galdrikian <[email protected]> | 2018-05-31 11:36:08 -0700 |
|---|---|---|
| committer | Bryan Galdrikian <[email protected]> | 2018-05-31 11:36:08 -0700 |
| commit | 7115f60b91b5717d90f643fd692010905c7004db (patch) | |
| tree | effd68c6978751c517d54c2f2bb5bb6e7dc93e18 /examples/UnityExample/Assets/Scripts | |
| parent | Updating BlastTool zip (diff) | |
| download | blast-1.1.3_rc1.tar.xz blast-1.1.3_rc1.zip | |
Blast 1.1.3. See docs/release_notes.txt.v1.1.3_rc1
Diffstat (limited to 'examples/UnityExample/Assets/Scripts')
| -rwxr-xr-x[-rw-r--r--] | examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs | 366 | ||||
| -rwxr-xr-x[-rw-r--r--] | examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs.meta | 24 | ||||
| -rwxr-xr-x[-rw-r--r--] | examples/UnityExample/Assets/Scripts/CubeFamily.cs | 402 | ||||
| -rwxr-xr-x[-rw-r--r--] | examples/UnityExample/Assets/Scripts/CubeFamily.cs.meta | 24 | ||||
| -rwxr-xr-x[-rw-r--r--] | examples/UnityExample/Assets/Scripts/Demo.cs | 240 | ||||
| -rwxr-xr-x[-rw-r--r--] | examples/UnityExample/Assets/Scripts/Demo.cs.meta | 24 | ||||
| -rwxr-xr-x[-rw-r--r--] | examples/UnityExample/Assets/Scripts/FreeCamera.cs | 122 | ||||
| -rwxr-xr-x[-rw-r--r--] | examples/UnityExample/Assets/Scripts/FreeCamera.cs.meta | 24 |
8 files changed, 613 insertions, 613 deletions
diff --git a/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs b/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs index fdb6fa0..55f50fb 100644..100755 --- a/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs +++ b/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs @@ -1,183 +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); - } -} +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 index 0a37254..a7da492 100644..100755 --- a/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs.meta +++ b/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs.meta @@ -1,12 +1,12 @@ -fileFormatVersion: 2 -guid: efa082786df108947a6fb5d672c1fb1a -timeCreated: 1481121968 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: +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 index fda0760..0d77853 100644..100755 --- a/examples/UnityExample/Assets/Scripts/CubeFamily.cs +++ b/examples/UnityExample/Assets/Scripts/CubeFamily.cs @@ -1,202 +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; +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 index f61e39a..5992474 100644..100755 --- a/examples/UnityExample/Assets/Scripts/CubeFamily.cs.meta +++ b/examples/UnityExample/Assets/Scripts/CubeFamily.cs.meta @@ -1,12 +1,12 @@ -fileFormatVersion: 2 -guid: 0fe5c9affaa641644b99cb8e384234d4 -timeCreated: 1481725814 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: +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 index eb60d26..725229e 100644..100755 --- a/examples/UnityExample/Assets/Scripts/Demo.cs +++ b/examples/UnityExample/Assets/Scripts/Demo.cs @@ -1,120 +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; -} +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 index edd0d3b..5e49416 100644..100755 --- a/examples/UnityExample/Assets/Scripts/Demo.cs.meta +++ b/examples/UnityExample/Assets/Scripts/Demo.cs.meta @@ -1,12 +1,12 @@ -fileFormatVersion: 2 -guid: 7a62789bffb0bef4e9757f9848620e3f -timeCreated: 1481120125 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: +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 index 1e9fd67..552ed3b 100644..100755 --- a/examples/UnityExample/Assets/Scripts/FreeCamera.cs +++ b/examples/UnityExample/Assets/Scripts/FreeCamera.cs @@ -1,61 +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; - //} - } -} +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 index 081cae7..f9f0486 100644..100755 --- a/examples/UnityExample/Assets/Scripts/FreeCamera.cs.meta +++ b/examples/UnityExample/Assets/Scripts/FreeCamera.cs.meta @@ -1,12 +1,12 @@ -fileFormatVersion: 2 -guid: 29f022fb3768b8c4196631a9526aebcc -timeCreated: 1482928025 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: +fileFormatVersion: 2
+guid: 29f022fb3768b8c4196631a9526aebcc
+timeCreated: 1482928025
+licenseType: Free
+MonoImporter:
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
|