예제 #1
0
    private void FractureMeshVertices(Dictionary <int, float> movedVertexToForceMagnitudeMap, Vector3 impactForce)
    {
        // If we're not moving ALL of our vertices, then we need to store vertex information about the
        // vertices that we'll be changing for our new SubHull meshes.
        if (movedVertexToForceMagnitudeMap.Keys.Count >= m_Vertices.Count)
        {
            return;
        }

        // Set up our initial SubHulls. This divides our vertices between the two SubHulls, along with
        // all vertex information, and assigns triangles each SubHull
        SetupSubHulls(movedVertexToForceMagnitudeMap);

        // This goes through the SubHulls, finds all the edge vertices, and adds new vertices and triangles
        // to close up the open end of each SubHull mesh
        FixUpSubHulls(impactForce);

        // Set the first SubHull's mesh information into all our fields
        m_Vertices     = m_FirstSubHull.m_Vertices;
        m_Normals      = m_FirstSubHull.m_Normals;
        m_Tangents     = m_FirstSubHull.m_Tangents;
        m_Uvs          = m_FirstSubHull.m_Uvs;
        m_Triangles    = m_FirstSubHull.m_Triangles;
        m_FirstSubHull = null;
    }
예제 #2
0
	public void SetupSubHulls(Dictionary<int, float> movedVertexToForceMagnitudeMap)
	{
		// Create new SubHulls for us to work with
		m_FirstSubHull = new SubHull();
		m_SecondSubHull = new SubHull();
		
		// Add all the information about each vertex to the proper SubHull
		Dictionary<SubHull, Dictionary<int, int>> subHullToIndexMap = new Dictionary<SubHull, Dictionary<int, int>>();
		for (int i = 0; i < m_Vertices.Count; i++)
		{
			// Firgure out which SubHull we're adding this vertex to
			SubHull subHull = m_FirstSubHull;
			if (movedVertexToForceMagnitudeMap.ContainsKey(i))
				subHull = m_SecondSubHull;

			// Add this vertex information to the correct SubHull
			subHull.m_Vertices.Add(m_Vertices[i]);
			if (m_Normals.Count > i)
				subHull.m_Normals.Add(m_Normals[i]);
			if (m_Tangents.Count > i)
				subHull.m_Tangents.Add(m_Tangents[i]);
			if (m_Uvs.Count > i)
				subHull.m_Uvs.Add(m_Uvs[i]);
			
			// Make sure out maps are set up appropriately
			if (!subHullToIndexMap.ContainsKey(subHull))
				subHullToIndexMap.Add(subHull, new Dictionary<int, int>());
			
			subHullToIndexMap[subHull].Add(i, subHull.m_Vertices.Count - 1);
		}
		
		// TODO (micmah): I might eventually want to see how many triangles are split by the fracture, and
		//  if it's not enough, I could break those triangles down until I have a nicely triangulated edge...
		
		// Build vertex equivalence map. Sometimes multiple vertices occupy the exact same location, and we
		// need to be able to track that when we figure out which vertex positions share what triangles.
		Dictionary<Vector3, List<int>> vertexToIndicesMap = new Dictionary<Vector3, List<int>>();
		for (int i = 0; i < m_Vertices.Count; i++)
		{
			Vector3 vertex = m_Vertices[i];
			if (!vertexToIndicesMap.ContainsKey(vertex))
				vertexToIndicesMap.Add(vertex, new List<int>());
			
			List<int> vertexIndices = vertexToIndicesMap[vertex];
			vertexIndices.Add(i);
		}

		// Add each triangle to the proper SubHull
		for (int i = 0; i < m_Triangles.Count; i = i + 3)
		{
			// Count the number of vertices in this triangle that are being moved. This is used
			// to figure out what SubHull this triangle goes with
			List<int> movedVertexIndices = new List<int>();
			for (int triangleIndex = i; triangleIndex < i + 3; triangleIndex++)
			{
				if (movedVertexToForceMagnitudeMap.ContainsKey(m_Triangles[triangleIndex]))
					movedVertexIndices.Add(m_Triangles[triangleIndex]);
			}

			// This triangle has vertices in both SubHulls. Add these vertex indices
			// to m_EdgeVertexIndices in the proper SubHulls and continue on.
			if (movedVertexIndices.Count != 3 && movedVertexIndices.Count != 0)
			{
				for (int triangleIndex = i; triangleIndex < i + 3; triangleIndex++)
				{
					int vertexIndex = m_Triangles[triangleIndex];

					// Figure out which SubHull this vertex belongs to
					SubHull vertexSubHull = m_FirstSubHull;
					if (movedVertexIndices.Contains(vertexIndex))
						vertexSubHull = m_SecondSubHull;
					
					// Figure out the index of this vertex within its SubHull
					Dictionary<int, int> oldIndexToNewIndexMap = subHullToIndexMap[vertexSubHull];
					
					// Add this vertex as an edge vertex in our SubHull
					foreach (int index in vertexToIndicesMap[m_Vertices[vertexIndex]])
						vertexSubHull.m_EdgeVertexIndices.Add(oldIndexToNewIndexMap[index]);
				}

				continue;
			}
			
			// Figure out which SubHull this triangle is being added to
			SubHull subHull = m_FirstSubHull;
			if (movedVertexIndices.Count == 3)
				subHull = m_SecondSubHull;
			
			// Add this triangle to the right SubHull
			for (int triangleIndex = i; triangleIndex < i + 3; triangleIndex++)
			{
				int vertexIndex = m_Triangles[triangleIndex];
				Dictionary<int, int> oldIndexToNewIndexMap = subHullToIndexMap[subHull];
				if (oldIndexToNewIndexMap.ContainsKey(vertexIndex))
					subHull.m_Triangles.Add(oldIndexToNewIndexMap[vertexIndex]);
			}
		}
		
		// Have each SubHull setup edge information for use later on
		m_FirstSubHull.CalculateEdges();
		m_SecondSubHull.CalculateEdges();
	}
예제 #3
0
	private void FractureMeshVertices(Dictionary<int, float> movedVertexToForceMagnitudeMap, Vector3 impactForce)
	{
		// If we're not moving ALL of our vertices, then we need to store vertex information about the
		// vertices that we'll be changing for our new SubHull meshes.
		if (movedVertexToForceMagnitudeMap.Keys.Count >= m_Vertices.Count)
			return;

		// Set up our initial SubHulls. This divides our vertices between the two SubHulls, along with
		// all vertex information, and assigns triangles each SubHull
		SetupSubHulls(movedVertexToForceMagnitudeMap);
		
		// This goes through the SubHulls, finds all the edge vertices, and adds new vertices and triangles
		// to close up the open end of each SubHull mesh
		FixUpSubHulls(impactForce);
		
		// Set the first SubHull's mesh information into all our fields
		m_Vertices = m_FirstSubHull.m_Vertices;
		m_Normals = m_FirstSubHull.m_Normals;
		m_Tangents = m_FirstSubHull.m_Tangents;
		m_Uvs = m_FirstSubHull.m_Uvs;
		m_Triangles = m_FirstSubHull.m_Triangles;
		m_FirstSubHull = null;
	}
예제 #4
0
    public void SetupSubHulls(Dictionary <int, float> movedVertexToForceMagnitudeMap)
    {
        // Create new SubHulls for us to work with
        m_FirstSubHull  = new SubHull();
        m_SecondSubHull = new SubHull();

        // Add all the information about each vertex to the proper SubHull
        Dictionary <SubHull, Dictionary <int, int> > subHullToIndexMap = new Dictionary <SubHull, Dictionary <int, int> >();

        for (int i = 0; i < m_Vertices.Count; i++)
        {
            // Firgure out which SubHull we're adding this vertex to
            SubHull subHull = m_FirstSubHull;
            if (movedVertexToForceMagnitudeMap.ContainsKey(i))
            {
                subHull = m_SecondSubHull;
            }

            // Add this vertex information to the correct SubHull
            subHull.m_Vertices.Add(m_Vertices[i]);
            if (m_Normals.Count > i)
            {
                subHull.m_Normals.Add(m_Normals[i]);
            }
            if (m_Tangents.Count > i)
            {
                subHull.m_Tangents.Add(m_Tangents[i]);
            }
            if (m_Uvs.Count > i)
            {
                subHull.m_Uvs.Add(m_Uvs[i]);
            }

            // Make sure out maps are set up appropriately
            if (!subHullToIndexMap.ContainsKey(subHull))
            {
                subHullToIndexMap.Add(subHull, new Dictionary <int, int>());
            }

            subHullToIndexMap[subHull].Add(i, subHull.m_Vertices.Count - 1);
        }

        // TODO (micmah): I might eventually want to see how many triangles are split by the fracture, and
        //  if it's not enough, I could break those triangles down until I have a nicely triangulated edge...

        // Build vertex equivalence map. Sometimes multiple vertices occupy the exact same location, and we
        // need to be able to track that when we figure out which vertex positions share what triangles.
        Dictionary <Vector3, List <int> > vertexToIndicesMap = new Dictionary <Vector3, List <int> >();

        for (int i = 0; i < m_Vertices.Count; i++)
        {
            Vector3 vertex = m_Vertices[i];
            if (!vertexToIndicesMap.ContainsKey(vertex))
            {
                vertexToIndicesMap.Add(vertex, new List <int>());
            }

            List <int> vertexIndices = vertexToIndicesMap[vertex];
            vertexIndices.Add(i);
        }

        // Add each triangle to the proper SubHull
        for (int i = 0; i < m_Triangles.Count; i = i + 3)
        {
            // Count the number of vertices in this triangle that are being moved. This is used
            // to figure out what SubHull this triangle goes with
            List <int> movedVertexIndices = new List <int>();
            for (int triangleIndex = i; triangleIndex < i + 3; triangleIndex++)
            {
                if (movedVertexToForceMagnitudeMap.ContainsKey(m_Triangles[triangleIndex]))
                {
                    movedVertexIndices.Add(m_Triangles[triangleIndex]);
                }
            }

            // This triangle has vertices in both SubHulls. Add these vertex indices
            // to m_EdgeVertexIndices in the proper SubHulls and continue on.
            if (movedVertexIndices.Count != 3 && movedVertexIndices.Count != 0)
            {
                for (int triangleIndex = i; triangleIndex < i + 3; triangleIndex++)
                {
                    int vertexIndex = m_Triangles[triangleIndex];

                    // Figure out which SubHull this vertex belongs to
                    SubHull vertexSubHull = m_FirstSubHull;
                    if (movedVertexIndices.Contains(vertexIndex))
                    {
                        vertexSubHull = m_SecondSubHull;
                    }

                    // Figure out the index of this vertex within its SubHull
                    Dictionary <int, int> oldIndexToNewIndexMap = subHullToIndexMap[vertexSubHull];

                    // Add this vertex as an edge vertex in our SubHull
                    foreach (int index in vertexToIndicesMap[m_Vertices[vertexIndex]])
                    {
                        vertexSubHull.m_EdgeVertexIndices.Add(oldIndexToNewIndexMap[index]);
                    }
                }

                continue;
            }

            // Figure out which SubHull this triangle is being added to
            SubHull subHull = m_FirstSubHull;
            if (movedVertexIndices.Count == 3)
            {
                subHull = m_SecondSubHull;
            }

            // Add this triangle to the right SubHull
            for (int triangleIndex = i; triangleIndex < i + 3; triangleIndex++)
            {
                int vertexIndex = m_Triangles[triangleIndex];
                Dictionary <int, int> oldIndexToNewIndexMap = subHullToIndexMap[subHull];
                if (oldIndexToNewIndexMap.ContainsKey(vertexIndex))
                {
                    subHull.m_Triangles.Add(oldIndexToNewIndexMap[vertexIndex]);
                }
            }
        }

        // Have each SubHull setup edge information for use later on
        m_FirstSubHull.CalculateEdges();
        m_SecondSubHull.CalculateEdges();
    }