From 7115f60b91b5717d90f643fd692010905c7004db Mon Sep 17 00:00:00 2001 From: Bryan Galdrikian Date: Thu, 31 May 2018 11:36:08 -0700 Subject: Blast 1.1.3. See docs/release_notes.txt. --- .../Assets/Scripts/CubeAssetGenerator.cs | 366 +++++++++---------- .../Assets/Scripts/CubeAssetGenerator.cs.meta | 24 +- examples/UnityExample/Assets/Scripts/CubeFamily.cs | 402 ++++++++++----------- .../UnityExample/Assets/Scripts/CubeFamily.cs.meta | 24 +- examples/UnityExample/Assets/Scripts/Demo.cs | 240 ++++++------ examples/UnityExample/Assets/Scripts/Demo.cs.meta | 24 +- examples/UnityExample/Assets/Scripts/FreeCamera.cs | 122 +++---- .../UnityExample/Assets/Scripts/FreeCamera.cs.meta | 24 +- 8 files changed, 613 insertions(+), 613 deletions(-) mode change 100644 => 100755 examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs mode change 100644 => 100755 examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs.meta mode change 100644 => 100755 examples/UnityExample/Assets/Scripts/CubeFamily.cs mode change 100644 => 100755 examples/UnityExample/Assets/Scripts/CubeFamily.cs.meta mode change 100644 => 100755 examples/UnityExample/Assets/Scripts/Demo.cs mode change 100644 => 100755 examples/UnityExample/Assets/Scripts/Demo.cs.meta mode change 100644 => 100755 examples/UnityExample/Assets/Scripts/FreeCamera.cs mode change 100644 => 100755 examples/UnityExample/Assets/Scripts/FreeCamera.cs.meta (limited to 'examples/UnityExample/Assets/Scripts') diff --git a/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs b/examples/UnityExample/Assets/Scripts/CubeAssetGenerator.cs old mode 100644 new mode 100755 index fdb6fa0..55f50fb --- 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 depths = new List(); - 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 chunks = new List(); - 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 solverChunks = new List(); - List solverBonds = new List(); - - // initial params - List depthStartIDs = new List(); - List depthSlicesPerAxisTotal = new List(); - uint currentID = 0; - Vector3 extents = settings.extents; - - // Iterate over depths and create children - for (int depth = 0; depth 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 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 depths = new List(); + 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 chunks = new List(); + 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 solverChunks = new List(); + List solverBonds = new List(); + + // initial params + List depthStartIDs = new List(); + List depthSlicesPerAxisTotal = new List(); + uint currentID = 0; + Vector3 extents = settings.extents; + + // Iterate over depths and create children + for (int depth = 0; depth 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 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 old mode 100644 new mode 100755 index 0a37254..a7da492 --- 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 old mode 100644 new mode 100755 index fda0760..0d77853 --- 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("CubePrefab"); - _cubes = new GameObject[desc.chunkCount]; - for (int i = 0; i < desc.chunkCount; ++i) - { - GameObject cube = GameObject.Instantiate(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().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.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(); - 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 _actors = new Dictionary(); - 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("CubePrefab"); + _cubes = new GameObject[desc.chunkCount]; + for (int i = 0; i < desc.chunkCount; ++i) + { + GameObject cube = GameObject.Instantiate(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().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.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(); + 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 _actors = new Dictionary(); + 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 old mode 100644 new mode 100755 index f61e39a..5992474 --- 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 old mode 100644 new mode 100755 index eb60d26..725229e --- 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 buildingTypes = new List(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(); - 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(); - var family = hit.GetComponentInParent(); - 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(); - 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 buildingTypes = new List(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(); + 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(); + var family = hit.GetComponentInParent(); + 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(); + 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 old mode 100644 new mode 100755 index edd0d3b..5e49416 --- 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 old mode 100644 new mode 100755 index 1e9fd67..552ed3b --- 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 old mode 100644 new mode 100755 index 081cae7..f9f0486 --- 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: -- cgit v1.2.3