예제 #1
0
    private void Init()
    {
        _cubes = GetChildrenByDepth(this.gameObject, 0);
        Debug.Log(_cubes.Length);
        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(2, 2, 2), NvBlastChunkDesc.Flags.SupportFlag));
        settings.extents      = new Vector3(10, 10, 10);
        settings.staticHeight = 1.0f;

        _thisCubeAsset = CubeAsset.generate(settings);

        NvBlastAssetDesc desc = _thisCubeAsset.solverAssetDesc;

        _blastAsset  = new NvBlastAsset(desc);
        _blastFamily = new NvBlastFamily(_blastAsset);

        NvBlastActorDesc actorDesc = new NvBlastActorDesc();

        actorDesc.uniformInitialBondHealth = 1.0f;
        actorDesc.uniformInitialLowerSupportChunkHealth = 1.0f;
        var actor = new NvBlastActor(_blastFamily, actorDesc);

        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)));
    }
예제 #2
0
    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;
        //razz
        //const int BUILDING_COUNT_MAX = 20;
        const int BUILDING_COUNT_MAX = 0;
        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 void Init()
 {
     CubeAsset.Settings settings = new CubeAsset.Settings();
     //First depth for first state, so it will be unfractred (1,1,1)
     settings.depths.Add(new CubeAsset.DepthInfo(new Vector3(1, 1, 1), NvBlastChunkDesc.Flags.NoFlags));
     //Second depth for first fracture, so it will fractured by 2 per axis. You can add more depths or increase this depths count.
     settings.depths.Add(new CubeAsset.DepthInfo(new Vector3(2, 2, 2), NvBlastChunkDesc.Flags.SupportFlag));
     //this is size
     settings.extents = new Vector3(10, 10, 10);
     //this is static parts of object, so 1.0 means first row will be static.
     settings.staticHeight = 1.0f;
     _thisCubeAsset        = CubeAsset.generate(settings);
     _objectToBlastFamily.Initialize(_thisCubeAsset);
     _objectToBlastFamily.transform.localPosition = this.transform.localPosition;
 }
예제 #4
0
    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)));
    }
예제 #5
0
    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);
    }