<commit_before>using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace UMA
{
	/// <summary>
	/// Utility class for fixing normals along meshe seams.
	/// </summary>
	[ExecuteInEditMode]
	public class SeamRemoval : MonoBehaviour
	{

	    public bool runScript;

	    public float threshold = 0.0001f;
	    public Transform separatedMesh;
	    public Transform unifiedMesh;

	    void Update()
	    {
	        if (runScript && separatedMesh && unifiedMesh)
	        {
	            SkinnedMeshRenderer[] originalMeshes = separatedMesh.gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
	            SkinnedMeshRenderer referenceMesh = unifiedMesh.gameObject.GetComponentInChildren<SkinnedMeshRenderer>();

	            float sqrthreshold = threshold * threshold;

	            for (int meshIndex = 0; meshIndex < originalMeshes.Length; meshIndex++)
	            {
	                SeamRemoval.PerformSeamRemoval(originalMeshes[meshIndex], referenceMesh, sqrthreshold);
	            }
	#if UNITY_EDITOR
	            AssetDatabase.SaveAssets();
	#endif
	        }

	        runScript = false;
	    }

	    public static Mesh PerformSeamRemoval(SkinnedMeshRenderer originalMesh, SkinnedMeshRenderer referenceMesh, float threshold)
	    {
			float sqrthreshold = threshold * threshold;
			
	        Vector3[] referenceVertices = referenceMesh.sharedMesh.vertices;
	        Vector3[] referencenormals = referenceMesh.sharedMesh.normals;

	        Vector3[] normals = originalMesh.sharedMesh.normals;
	        Vector3[] meshIndexVertices = originalMesh.sharedMesh.vertices;

	        for (int vertexIndex = 0; vertexIndex < meshIndexVertices.Length; vertexIndex++)
	        {
	            for (int othervertexIndex = 0; othervertexIndex < referenceVertices.Length; othervertexIndex++)
	            {
	                if ((meshIndexVertices[vertexIndex] - referenceVertices[othervertexIndex]).sqrMagnitude <= sqrthreshold)
	                {
						normals[vertexIndex] = referencenormals[othervertexIndex];
	                }
	            }
	        }		
				
			Mesh tempMesh = new Mesh();
			tempMesh.name = originalMesh.gameObject.name;
			tempMesh.vertices = originalMesh.sharedMesh.vertices;
			tempMesh.normals = normals;
			tempMesh.tangents = originalMesh.sharedMesh.tangents;
			tempMesh.boneWeights = originalMesh.sharedMesh.boneWeights;
			tempMesh.uv = originalMesh.sharedMesh.uv;
			tempMesh.triangles = originalMesh.sharedMesh.triangles;
			tempMesh.bindposes = originalMesh.sharedMesh.bindposes;
			
			calculateMeshTangents(tempMesh);
			
	        return tempMesh;

	    }
		

		public static void calculateMeshTangents(Mesh mesh)
		{
			/*
			Derived from
			<NAME>. Computing Tangent Space Basis Vectors for an Arbitrary Mesh. Terathon Software 3D Graphics Library, 2001.
			http://www.terathon.com/code/tangent.html
			
			Changes provided at
			http://answers.unity3d.com/questions/7789/calculating-tangents-vector4.html
			*/
<commit_msg>added copying blendshapes during seam removal<commit_after>using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace UMA
{
	/// <summary>
	/// Utility class for fixing normals along meshe seams.
	/// </summary>
	[ExecuteInEditMode]
	public class SeamRemoval : MonoBehaviour
	{

	    public bool runScript;

	    public float threshold = 0.0001f;
	    public Transform separatedMesh;
	    public Transform unifiedMesh;

	    void Update()
	    {
	        if (runScript && separatedMesh && unifiedMesh)
	        {
	            SkinnedMeshRenderer[] originalMeshes = separatedMesh.gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
	            SkinnedMeshRenderer referenceMesh = unifiedMesh.gameObject.GetComponentInChildren<SkinnedMeshRenderer>();

	            float sqrthreshold = threshold * threshold;

	            for (int meshIndex = 0; meshIndex < originalMeshes.Length; meshIndex++)
	            {
	                SeamRemoval.PerformSeamRemoval(originalMeshes[meshIndex], referenceMesh, sqrthreshold);
	            }
	#if UNITY_EDITOR
	            AssetDatabase.SaveAssets();
	#endif
	        }

	        runScript = false;
	    }

	    public static Mesh PerformSeamRemoval(SkinnedMeshRenderer originalMesh, SkinnedMeshRenderer referenceMesh, float threshold)
	    {
			float sqrthreshold = threshold * threshold;
			
	        Vector3[] referenceVertices = referenceMesh.sharedMesh.vertices;
	        Vector3[] referencenormals = referenceMesh.sharedMesh.normals;

	        Vector3[] normals = originalMesh.sharedMesh.normals;
	        Vector3[] meshIndexVertices = originalMesh.sharedMesh.vertices;

	        for (int vertexIndex = 0; vertexIndex < meshIndexVertices.Length; vertexIndex++)
	        {
	            for (int othervertexIndex = 0; othervertexIndex < referenceVertices.Length; othervertexIndex++)
	            {
	                if ((meshIndexVertices[vertexIndex] - referenceVertices[othervertexIndex]).sqrMagnitude <= sqrthreshold)
	                {
						normals[vertexIndex] = referencenormals[othervertexIndex];
	                }
	            }
	        }		
				
			Mesh tempMesh = new Mesh();
			tempMesh.name = originalMesh.gameObject.name;
			tempMesh.vertices = originalMesh.sharedMesh.vertices;
			tempMesh.normals = normals;
			tempMesh.tangents = originalMesh.sharedMesh.tangents;
			tempMesh.boneWeights = originalMesh.sharedMesh.boneWeights;
			tempMesh.uv = originalMesh.sharedMesh.uv;
			tempMesh.triangles = originalMesh.sharedMesh.triangles;
			tempMesh.bindposes = originalMesh.sharedMesh.bindposes;

			int shapeCount = originalMesh.sharedMesh.blendShapeCount;
			for (int shapeIndex = 0; shapeIndex < shapeCount; shapeIndex++)
			{
				string shapeName = originalMesh.sharedMesh.GetBlendShapeName(shapeIndex);
				int frameCount = originalMesh.sharedMesh.GetBlendShapeFrameCount(shapeIndex);
				for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
				{
					float frameWeight = originalMesh.sharedMesh.GetBlendShapeFrameWeight(shapeIndex, frameIndex);
					Vector3[] deltaVertices = new Vector3[tempMesh.vertexCount];
					Vector3[] deltaNormals = new Vector3[tempMesh.vertexCount];
					Vector3[] deltaTangents = new Vector3[tempMesh.vertexCount];
	
					originalMesh.sharedMesh.GetBlendShapeFrameVertices(shapeIndex, frameIndex, deltaVertices, deltaNormals, deltaTangents);
					tempMesh.AddBlendShapeFrame( shapeName, frameWeight, deltaVertices, deltaNormals, deltaTangents );
				}
			}
			
			calculateMeshTangents(tempMesh);
			
	        return tempMesh;
	    }
		

		public static void calculateMeshTangents(Mesh mesh)
		{
			/*
			Derived from
			<NAME>. Computing Tangent Space Basis Vectors for an Arbitrary Mesh. Terathon Software 3D Graphics Library, 2001.
			http://www.terathon.com/code/tangent.html
			
			Changes provided at
			http://answers.unity3d.com/questions/7789/calculating-tangents-vector4.html
			*/

{'fraction_non_alphanumeric': 0.08324858346651169, 'fraction_numerical': 0.007845416242917333, 'mean_word_length': 3.117224880382775, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4741003', 'n_tokens_mistral': 2226, 'n_tokens_neox': 1882, 'n_words': 385}