Пример #1
0
    // Use this for initialization
    public ParticleModel(ClothSimulation settings)
    {
        // Create particles
        int RES = settings.particles;

        this.particlesSize = RES;
        SIZE = settings.totalSize;
        float D      = SIZE / RES; // dx, dy, dz = total size devided by resolution
        float zCoord = 1;

        this.Res = RES;
        initPoints(D, D, RES, RES, zCoord);
        Debug.Log(positions.Length + " particles created");

        // Create constraints on particles
        initConstraints( );

        // Initialize simulation calculation
        velocities = new Vector3[positions.Length];
        forces     = new Vector3[positions.Length];
        if (settings.parrallel)
        {
            pmc = new ParrallelPMCalculator(this);
        }
        else
        {
            pmc = new SimplePMCalculator(this);
        }
        pmc.Iterations = settings.iterations;
    }
Пример #2
0
 void Awake()
 {
     midparticle = clothsimulation.GetComponent<ClothSimulation>();
     // Saves the initial transform values for later resetting
     originPos = transform.position;
     originRot = transform.localEulerAngles;
     originScl = transform.localScale;
 }
Пример #3
0
 public void controller()
 {
     anchor = GetComponent <ClothSimulation>();
     linker();
     initLight(lightgameObject);
     initMat();
     ready = true;
 }
Пример #4
0
    // Use this for initialization
    void Start()
    {
        Application.runInBackground = true;
#if UNITY_EDITOR
        //SceneView.FocusWindowIfItsOpen( typeof( SceneView ) );
#endif
        model     = new ParticleModel(this);
        meshModel = new TriangularModelMesh(model, this);
        simpleVis = new ParticleVisualisation(model, this);
        gInst     = this;
    }
Пример #5
0
 void OnEnable()
 {
     clothSim = (ClothSimulation)target;
 }
Пример #6
0
 public ParticleVisualisation(ParticleModel model, ClothSimulation settings) : this(model)
 {
     // Could store extra settings from the Unity GUI via the ClothSimulation class
 }
Пример #7
0
    public TriangularModelMesh(ParticleModel model, ClothSimulation settings)
    {
        this.model = model;

        GameObject meshGameObject = new GameObject("TriangularModelMesh");

        MeshFilter meshFilter = meshGameObject.AddComponent <MeshFilter>();

        this.mesh = meshFilter.mesh;

        MeshRenderer meshRenderer    = meshGameObject.AddComponent <MeshRenderer>();
        Material     defaultMaterial = new Material(Shader.Find("VR/SpatialMapping/Wireframe"));

        meshRenderer.sharedMaterial = defaultMaterial;

        this.mesh.Clear();

        int subdivisions = 6;
        int modelWH      = this.model.getWidthHeight();

        points = new Vector3[(int)Math.Pow((modelWH + (modelWH - 1)) * subdivisions, 2)];
        Array.Copy(model.positions, points, model.positions.Length);
        int index = model.positions.Length;

        List <int> triangles = new List <int>();

        List <SubLine> SubLines = new List <SubLine>();

        // horizontal
        for (int j = 0; j < modelWH; j++)
        {
            for (int i = 0; i < modelWH - 1; i++)
            {
                int left  = j * modelWH + i;
                int right = left + 1;

                SubLine subLine = bezier > 0 ? new BezierSubline(left, right, index, index + subdivisions) : new SubLine(left, right, index, index + subdivisions);
                index = index + subdivisions;

                if (i > 0)
                {
                    SubLine prev = SubLines[(modelWH - 1) * j + (i - 1)];
                    subLine.link(ref prev);
                }

                SubLines.Add(subLine);
            }
        }
        // vertical
        for (int j = 0; j < modelWH - 1; j++)
        {
            for (int i = 0; i < modelWH; i++)
            {
                int bottom = j * modelWH + i;
                int top    = bottom + modelWH;

                SubLine subLine = bezier > 0 ? new BezierSubline(bottom, top, index, index + subdivisions) : new SubLine(bottom, top, index, index + subdivisions);
                index = index + subdivisions;

                if (j > 0)
                {
                    SubLine prev = SubLines[modelWH * (modelWH - 1) + modelWH * (j - 1) + i];
                    subLine.link(ref prev);
                }

                SubLines.Add(subLine);
            }
        }
        List <SubGrid> SubGrids = new List <SubGrid>();

        for (int j = 0; j < modelWH - 1; j++)
        {
            for (int i = 0; i < modelWH - 1; i++)
            {
                int a = modelWH * j + i;
                int b = a + 1;
                int c = a + modelWH;
                int d = c + 1;

                SubLine bottom = SubLines[(modelWH - 1) * j + i];
                SubLine top    = SubLines[(modelWH - 1) * (j + 1) + i];
                SubLine left   = SubLines[(modelWH - 1) * modelWH + modelWH * j + i];
                SubLine right  = SubLines[(modelWH - 1) * modelWH + modelWH * j + i + 1];

                SubGrid grid = new SubGrid(ref bottom, ref right, ref left, ref top, index, subdivisions, subdivisions);
                index += subdivisions * subdivisions;

                if (i > 0)
                {
                    SubGrid prev = SubGrids[(modelWH - 1) * j + i - 1];
                    grid.linkHorizontal(ref prev);
                }
                if (j > 0)
                {
                    SubGrid prev = SubGrids[(modelWH - 1) * (j - 1) + i];
                    grid.linkVertical(ref prev);
                }

                SubGrids.Add(grid);

                // Add triangles for the grid
                triangles.AddRange(grid.GenTriangles());
            }
        }

        List <int> included = new List <int>(new HashSet <int>(triangles));

        // Calculate the uvs
        Vector2[] uvs = new Vector2[points.Length]; // uv maps texture to points
        float     du  = 1.0f / modelWH;
        float     dv  = 1.0f / modelWH;

        for (int u = 0; u < modelWH; u++)
        {
            for (int v = 0; v < modelWH; v++)
            {
                uvs[u * modelWH + v] = new Vector2(u * du, v * dv);
            }
        }

        if (showGridPoints)
        {
            gridPointsObjectManager = new MonoManager <SceneObject>();
            GameObject unitPrefab = new GameObject();
            unitPrefab.AddComponent <SceneObject>();
            unitPrefab.AddComponent <MeshFilter>();
            unitPrefab.AddComponent <MeshRenderer>();
            gridPointsObjectManager.OverrideGameObject(unitPrefab);

            Mesh     mesh     = Assets.Scripts.Tools.Geometry.PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Sphere);
            Material material = new Material(Shader.Find("Transparent/Diffuse"));
            material.color = Color.yellow;


            for (int i = model.positions.Length; i < points.Length; i++)
            {
                var newObj = gridPointsObjectManager.New();
                newObj.Init(mesh, material);
                newObj.transform.position = points[i];
            }
        }

        SubLines.ForEach(sl => {
            sl.setUV(ref uvs);
        });
        this.subLines = SubLines.ToArray();
        SubGrids.ForEach(sl => {
            sl.setUV(ref uvs);
        });
        this.subGrids = SubGrids.ToArray();

        SubLines.ForEach(sl => {
            sl.setPoints(ref points);
        });
        SubGrids.ForEach(sl => {
            sl.setPoints(ref points);
        });

        // Set unit mesh
        this.mesh.vertices  = points;
        this.mesh.uv        = uvs;
        this.mesh.triangles = addBothSide(triangles.ToArray());
    }