private IEnumerator GenerateSack()
    {
        Mesh mesh = GenerateSheetMesh();

        // create and give a material to both sides of the sack:
        GameObject sheet1 = new GameObject("sack_side1", typeof(MeshFilter), typeof(MeshRenderer), typeof(ObiCloth), typeof(ObiClothRenderer));

        sheet1.GetComponent <MeshRenderer>().materials = new Material[] { outsideMaterial, insideMaterial };

        GameObject sheet2 = new GameObject("sack_side2", typeof(MeshFilter), typeof(MeshRenderer), typeof(ObiCloth), typeof(ObiClothRenderer));

        sheet2.GetComponent <MeshRenderer>().materials = new Material[] { outsideMaterial, insideMaterial };

        // position both sheets around the center of this object:
        sheet1.transform.parent        = transform;
        sheet2.transform.parent        = transform;
        sheet1.transform.localPosition = Vector3.forward;
        sheet2.transform.localPosition = -Vector3.forward;

        // generate blueprint:
        var blueprint = ScriptableObject.CreateInstance <ObiClothBlueprint>();

        blueprint.inputMesh = GenerateSheetMesh();
        yield return(StartCoroutine(blueprint.Generate()));

        // generate cloth for both of them:
        ObiCloth cloth1 = sheet1.GetComponent <ObiCloth>();

        cloth1.clothBlueprint = blueprint;

        ObiCloth cloth2 = sheet2.GetComponent <ObiCloth>();

        cloth2.clothBlueprint = blueprint;

        // sew both sides together:
        ObiStitcher stitcher = gameObject.AddComponent <ObiStitcher>();

        stitcher.Actor1 = cloth1;
        stitcher.Actor2 = cloth2;

        // add stitches: top and bottom edges:
        for (int i = 1; i < resolution; ++i)
        {
            stitcher.AddStitch(i, i);
            stitcher.AddStitch((resolution + 1) * resolution + i, (resolution + 1) * resolution + i);
        }

        // sides:
        for (int i = 0; i <= (resolution + 1) * resolution; i += resolution + 1)
        {
            stitcher.AddStitch(i, i);
            stitcher.AddStitch(i + resolution, i + resolution);
        }

        stitcher.PushDataToSolver();

        // adjust bending constraints to obtain a little less rigid fabric:
        cloth1.maxBending = 0.04f;
        cloth2.maxBending = 0.04f;

        Destroy(this);
    }
示例#2
0
    private void GenerateSack()
    {
        Mesh mesh = GenerateSheetMesh();

        // create and give a material to both sides of the sack:
        GameObject sheet1 = new GameObject("sack_side1", typeof(MeshFilter), typeof(MeshRenderer));

        sheet1.GetComponent <MeshRenderer>().materials = new Material[] { outsideMaterial, insideMaterial };

        GameObject sheet2 = new GameObject("sack_side2", typeof(MeshFilter), typeof(MeshRenderer));

        sheet2.GetComponent <MeshRenderer>().materials = new Material[] { outsideMaterial, insideMaterial };

        // position both sheets around the center of this object:
        sheet1.transform.parent        = transform;
        sheet2.transform.parent        = transform;
        sheet1.transform.localPosition = Vector3.forward;
        sheet2.transform.localPosition = -Vector3.forward;

        // generate cloth for both of them:
        RuntimeClothGenerator generator1 = sheet1.AddComponent <RuntimeClothGenerator>();

        generator1.solver   = solver;
        generator1.topology = topology;
        generator1.mesh     = mesh;

        RuntimeClothGenerator generator2 = sheet2.AddComponent <RuntimeClothGenerator>();

        generator2.solver   = solver;
        generator2.topology = topology;
        generator2.mesh     = mesh;

        // sew both sides together:
        ObiStitcher stitcher = gameObject.AddComponent <ObiStitcher>();

        stitcher.Actor1 = sheet1.GetComponent <ObiCloth>();
        stitcher.Actor2 = sheet2.GetComponent <ObiCloth>();

        // add stitches: top and bottom edges:
        for (int i = 1; i < resolution; ++i)
        {
            stitcher.AddStitch(i, i);
            stitcher.AddStitch((resolution + 1) * resolution + i, (resolution + 1) * resolution + i);
        }

        // sides:
        for (int i = 0; i <= (resolution + 1) * resolution; i += resolution + 1)
        {
            stitcher.AddStitch(i, i);
            stitcher.AddStitch(i + resolution, i + resolution);
        }

        // adjust bending constraints to obtain a little less rigid fabric:
        ObiCloth cloth1 = sheet1.GetComponent <ObiCloth>();
        ObiCloth cloth2 = sheet2.GetComponent <ObiCloth>();

        cloth1.BendingConstraints.maxBending = 0.02f;
        cloth2.BendingConstraints.maxBending = 0.02f;

        cloth1.BendingConstraints.PushDataToSolver(ParticleData.NONE);
        cloth2.BendingConstraints.PushDataToSolver(ParticleData.NONE);
    }