public void Init() { f4 = (float4 *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <float4>() * 10000, UnsafeUtility.AlignOf <float4>(), Allocator.Persistent); for (int i = 0; i < 10000; ++i) { f4[i] = new float4(1.0f, 2.0f, 3.0f, 4.0f); } h4 = (half4 *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <half4>() * 10000, UnsafeUtility.AlignOf <half4>(), Allocator.Persistent); for (int i = 0; i < 10000; ++i) { h4[i] = new half4(new float4(-1.0f, -2.0f, -3.0f, -4.0f)); } }
public Dictionary <Material, int> GetMaterialsData(MeshRenderer[] allRenderers, string textureSavePath) { float3 ColorToVector(Color c) { return(float3(c.r, c.b, c.g)); } float2 GetVector2(float4 vec) { return(float2(vec.x, vec.y)); } var dict = new Dictionary <Material, int>(allRenderers.Length); allProperties = new List <MaterialProperties>(allRenderers.Length); var albedoTexs = new List <Texture>(allRenderers.Length); var normalTexs = new List <Texture>(allRenderers.Length); var smoTexs = new List <Texture>(allRenderers.Length); var emissionTex = new List <Texture>(allRenderers.Length); var heightTex = new List <Texture>(allRenderers.Length); var secondAlbedoTex = new List <Texture>(allRenderers.Length); var secondBumpTex = new List <Texture>(allRenderers.Length); var secondSpecTex = new List <Texture>(allRenderers.Length); var albedoDict = new Dictionary <Texture, int>(allRenderers.Length); var normalDict = new Dictionary <Texture, int>(allRenderers.Length); var smoDict = new Dictionary <Texture, int>(allRenderers.Length); var emissionDict = new Dictionary <Texture, int>(allRenderers.Length); var heightDict = new Dictionary <Texture, int>(allRenderers.Length); var secondAlbedoDict = new Dictionary <Texture, int>(allRenderers.Length); var secondBumpDict = new Dictionary <Texture, int>(allRenderers.Length); var secondSpecDict = new Dictionary <Texture, int>(allRenderers.Length); int len = 0; int GetTextureIndex(List <Texture> lst, Dictionary <Texture, int> texDict, Texture tex) { int ind = -1; if (tex) { if (!texDict.TryGetValue(tex, out ind)) { ind = lst.Count; lst.Add(tex); texDict.Add(tex, ind); } } return(ind); } foreach (var r in allRenderers) { var ms = r.sharedMaterials; foreach (var m in ms) { if (!m) { throw new System.Exception(r.name + " Has Null Mat"); } if (!dict.ContainsKey(m)) { dict.Add(m, len); Texture albedo = m.GetTexture("_MainTex"); Texture normal = m.GetTexture("_BumpMap"); Texture smo = m.GetTexture("_SpecularMap"); Texture emission = m.GetTexture("_EmissionMap"); Texture height = m.GetTexture("_HeightMap"); Texture secondBump = m.GetTexture("_SecondaryBumpMap"); Texture secondAlbedo = m.GetTexture("_SecondaryMainTex"); Texture secondSpec = m.GetTexture("_SecondarySpecularMap"); int albedoIndex = GetTextureIndex(albedoTexs, albedoDict, albedo); int normalIndex = GetTextureIndex(normalTexs, normalDict, normal); int smoIndex = GetTextureIndex(smoTexs, smoDict, smo); int emissionIndex = GetTextureIndex(emissionTex, emissionDict, emission); int heightIndex = GetTextureIndex(heightTex, heightDict, height); int secondBumpIndex = GetTextureIndex(secondBumpTex, secondBumpDict, secondBump); int secondAlbedoIndex = GetTextureIndex(secondAlbedoTex, secondAlbedoDict, secondAlbedo); int secondSpecIndex = GetTextureIndex(secondSpecTex, secondSpecDict, secondSpec); allProperties.Add(new MaterialProperties { _Color = ColorToVector(m.GetColor("_Color")), _Glossiness = m.GetFloat("_Glossiness"), _DecalLayer = (uint)m.GetInt("_DecalLayer"), _EmissionColor = ColorToVector(m.GetColor("_EmissionColor") * m.GetFloat("_EmissionMultiplier")), _MetallicIntensity = m.GetFloat("_MetallicIntensity"), _SpecularIntensity = m.GetFloat("_SpecularIntensity"), _Occlusion = m.GetFloat("_Occlusion"), _NormalIntensity = GetVector2(m.GetVector("_NormalIntensity")), _TileOffset = m.GetVector("_TileOffset"), _BumpMap = normalIndex, _EmissionMap = emissionIndex, _MainTex = albedoIndex, _SpecularMap = smoIndex, _HeightMap = heightIndex, _HeightMapIntensity = m.GetFloat("_HeightmapIntensity"), _SecondaryBumpMap = secondBumpIndex, _SecondaryMainTex = secondAlbedoIndex, _SecondarySpecularMap = secondSpecIndex, _SecondaryTileOffset = m.GetVector("_SecondaryTileOffset") }); len++; } } } ComputeShader readRTDataShader = Resources.Load <ComputeShader>("ReadRTData"); void GetGUIDs(out string[] strs, List <Texture> texs, int typeIndex) { strs = new string[texs.Count]; for (int i = 0; i < texs.Count; ++i) { strs[i] = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(texs[i])); ComputeBuffer floatBuffer = new ComputeBuffer(texs[i].width * texs[i].height, sizeof(float4)); int pass = typeIndex == 1 ? 2 : 0; readRTDataShader.SetBuffer(pass, "_TextureDatas", floatBuffer); readRTDataShader.SetTexture(pass, "_TargetTexture", texs[i]); readRTDataShader.SetInt("_Width", texs[i].width); readRTDataShader.SetInt("_Height", texs[i].height); readRTDataShader.SetInt("_Offset", 0); readRTDataShader.Dispatch(pass, texs[i].width / 8, texs[i].height / 8, 1); float4[] resultDatas = new float4[floatBuffer.count]; floatBuffer.GetData(resultDatas); string savePath = textureSavePath + strs[i] + ".mtex"; switch (typeIndex) { case 0: byte[] albedoColors = new byte[resultDatas.Length * sizeof(Color32)]; Color32 *albedoColorsPtr = (Color32 *)albedoColors.Ptr(); for (int j = 0; j < resultDatas.Length; ++j) { float4 v = resultDatas[j]; albedoColorsPtr[j] = new Color32((byte)(v.x * 255), (byte)(v.y * 255), (byte)(v.z * 255), (byte)(v.w * 255)); } System.IO.File.WriteAllBytes(savePath, albedoColors); break; case 1: byte[] normalColors = new byte[resultDatas.Length * sizeof(half2)]; half2 *normalColorsPtr = (half2 *)normalColors.Ptr(); for (int j = 0; j < resultDatas.Length; ++j) { float4 v = resultDatas[j]; normalColorsPtr[j] = (half2)v.xy; } System.IO.File.WriteAllBytes(savePath, normalColors); break; case 2: byte[] hdrColors = new byte[resultDatas.Length * sizeof(half4)]; half4 *hdrColorsPtr = (half4 *)hdrColors.Ptr(); for (int j = 0; j < resultDatas.Length; ++j) { float4 v = resultDatas[j]; hdrColorsPtr[j] = (half4)v; } System.IO.File.WriteAllBytes(savePath, hdrColors); break; case 3: byte[] heightColors = new byte[resultDatas.Length]; for (int j = 0; j < resultDatas.Length; ++j) { float4 v = resultDatas[j]; heightColors[j] = (byte)(v.x * 255); } System.IO.File.WriteAllBytes(savePath, heightColors); break; } floatBuffer.Dispose(); } } GetGUIDs(out albedoGUIDs, albedoTexs, 0); GetGUIDs(out secondAlbedoGUIDs, secondAlbedoTex, 0); GetGUIDs(out normalGUIDs, normalTexs, 1); GetGUIDs(out secondNormalGUIDs, secondBumpTex, 1); GetGUIDs(out smoGUIDs, smoTexs, 0); GetGUIDs(out secondSpecGUIDs, secondSpecTex, 0); GetGUIDs(out emissionGUIDs, emissionTex, 2); GetGUIDs(out heightGUIDs, heightTex, 3); return(dict); }