public static IEnumerator CreateMaterial(GameObject selection) { var sampler = new CombinedMeshSampler(selection); var vtex = new VertexTex(sampler); var mesh = sampler.CombinedMesh; //mesh.uv2 = vtex.uv2; mesh.bounds = vtex.Bounds(); var folderPath = DIR_ASSETS + "/" + DIR_ROOT; if (!Directory.Exists(folderPath)) { AssetDatabase.CreateFolder(DIR_ASSETS, DIR_ROOT); } var guid = AssetDatabase.CreateFolder(folderPath, selection.name); folderPath = AssetDatabase.GUIDToAssetPath(guid); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); yield return(0); var posPngPath = folderPath + "/" + selection.name + ".png"; var normPngPath = folderPath + "/" + selection.name + "_normal.png"; var posTex = Save(vtex.positionTex, posPngPath); var normTex = Save(vtex.normalTex, normPngPath); var renderer = selection.GetComponentInChildren <Renderer> (); Material mat = new Material(Shader.Find(ShaderConst.SHADER_NAME)); if (renderer != null && renderer.sharedMaterial != null) { mat.mainTexture = renderer.sharedMaterial.mainTexture; } mat.SetTexture(ShaderConst.SHADER_ANIM_TEX, posTex); mat.SetVector(ShaderConst.SHADER_SCALE, vtex.scale); mat.SetVector(ShaderConst.SHADER_OFFSET, vtex.offset); mat.SetVector(ShaderConst.SHADER_ANIM_END, new Vector4(sampler.Length, vtex.verticesList.Count - 1, 0f, 0f)); mat.SetFloat(ShaderConst.SHADER_FPS, FPS); mat.SetTexture(ShaderConst.SHADER_NORM_TEX, normTex); AssetDatabase.CreateAsset(mat, folderPath + "/" + selection.name + "Mat.mat"); AssetDatabase.CreateAsset(mesh, folderPath + "/" + selection.name + "Mesh.asset"); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); GameObject go = new GameObject(selection.name); go.AddComponent <MeshRenderer>().sharedMaterial = mat; go.AddComponent <MeshFilter>().sharedMesh = mesh; PrefabUtility.CreatePrefab(folderPath + "/" + selection.name + ".prefab", go); }
public VertexTex(CombinedMeshSampler sample) { verticesList = new List <Vector3[]> (); normalsList = new List <Vector3[]> (); for (float t = 0; t < (sample.Length + DT); t += DT) { var combinedMesh = sample.Sample(t); verticesList.Add(combinedMesh.vertices); normalsList.Add(combinedMesh.normals); } var firstVertices = verticesList[0]; var firstVertex = firstVertices[0]; var vertexCount = firstVertices.Length; frameEnd = vertexCount - 1; var minX = firstVertex.x; var minY = firstVertex.y; var minZ = firstVertex.z; var maxX = firstVertex.x; var maxY = firstVertex.y; var maxZ = firstVertex.z; foreach (var vertices in verticesList) { for (var i = 0; i < vertices.Length; i++) { var v = vertices [i]; minX = Mathf.Min(minX, v.x); minY = Mathf.Min(minY, v.y); minZ = Mathf.Min(minZ, v.z); maxX = Mathf.Max(maxX, v.x); maxY = Mathf.Max(maxY, v.y); maxZ = Mathf.Max(maxZ, v.z); } } scale = new Vector4(maxX - minX, maxY - minY, maxZ - minZ, 1f); offset = new Vector4(minX, minY, minZ, 1f); Debug.LogFormat("Scale={0} Offset={1}", scale, offset); var texWidth = LargerInPow2(vertexCount); var texHeight = LargerInPow2(verticesList.Count * 2); Debug.Log(string.Format("tex({0}x{1}), nVertices={2} nFrames={3}", texWidth, texHeight, vertexCount, verticesList.Count)); positionTex = new Texture2D(texWidth, texHeight, TextureFormat.RGB24, false, true); positionTex.filterMode = ANIM_TEX_FILTER; positionTex.wrapMode = TextureWrapMode.Clamp; normalTex = new Texture2D(texWidth, texHeight, TextureFormat.RGB24, false, true); normalTex.filterMode = ANIM_TEX_FILTER; normalTex.wrapMode = TextureWrapMode.Clamp; uv2 = new Vector2[vertexCount]; var texSize = new Vector2(1f / texWidth, 1f / texHeight); var halfTexOffset = 0.5f * texSize; for (int i = 0; i < uv2.Length; i++) { uv2 [i] = new Vector2((float)i * texSize.x, 0f) + halfTexOffset; } for (int y = 0; y < verticesList.Count; y++) { Vector3[] vertices = verticesList [y]; Vector3[] normals = normalsList [y]; for (int x = 0; x < vertices.Length; x++) { var pos = Normalize(vertices [x], offset, scale); Color c0, c1; Encode(pos, out c0, out c1); positionTex.SetPixel(x, y, c0); positionTex.SetPixel(x, y + (texHeight >> 1), c1); var normal = 0.5f * (normals [x].normalized + Vector3.one); Encode(normal, out c0, out c1); normalTex.SetPixel(x, y, c0); normalTex.SetPixel(x, y + (texHeight >> 1), c1); } } positionTex.Apply(); normalTex.Apply(); }
void OnEnable() { _sampler = new CombinedMeshSampler(target); _filter = GetComponent <MeshFilter>(); }