internal static void RenderColoredTextures(List <renderColoredTextureProperties> texturesToRender) { if (texturesToRender.Count == 0) { return; } if (!m_initialized) { Init(); } const int RENDER_TEXTURE_RESOLUTION = 512; RC.DeviceContext.OutputMerger.BlendState = null; RC.SetIL(null); RC.SetPS(m_ps); RC.SetCB(0, MyCommon.FrameConstants); RC.SetCB(1, m_cb); Dictionary <Vector2I, MyRenderTarget> createdRenderTextureTargets = new Dictionary <Vector2I, MyRenderTarget>(); foreach (var texture in texturesToRender) { TexId texId = MyTextures.GetTexture(texture.TextureName, MyTextureEnum.COLOR_METAL, true); if (texId == TexId.NULL) { continue; } Vector2 texSize = MyTextures.GetSize(texId); Vector2I renderTargetResolution = new Vector2I(RENDER_TEXTURE_RESOLUTION, RENDER_TEXTURE_RESOLUTION); if (texSize.Y > 0) { if (texSize.Y < RENDER_TEXTURE_RESOLUTION) { renderTargetResolution.X = (int)texSize.X; renderTargetResolution.Y = (int)texSize.Y; } else { renderTargetResolution.X *= (int)(texSize.X / texSize.Y); } } MyViewport viewport = new MyViewport(renderTargetResolution.X, renderTargetResolution.Y); MyRenderTarget renderTexture = null; if (!createdRenderTextureTargets.TryGetValue(renderTargetResolution, out renderTexture)) { renderTexture = new MyRenderTarget(renderTargetResolution.X, renderTargetResolution.Y, SharpDX.DXGI.Format.R8G8B8A8_UNorm_SRgb, 1, 0); createdRenderTextureTargets[renderTargetResolution] = renderTexture; } RC.BindDepthRT(null, DepthStencilAccess.ReadWrite, renderTexture); // Set color var mapping = MyMapping.MapDiscard(m_cb); Vector4 color = new Vector4(texture.ColorMaskHSV, 1); mapping.WriteAndPosition(ref color); mapping.Unmap(); // Set texture RC.DeviceContext.PixelShader.SetShaderResource(0, MyTextures.GetView(texId)); // Draw MyScreenPass.DrawFullscreenQuad(viewport); // Save to file MyTextureData.ToFile(renderTexture.GetHWResource(), texture.PathToSave, ImageFileFormat.Png); } texturesToRender.Clear(); foreach (var texture in createdRenderTextureTargets) { texture.Value.Release(); } createdRenderTextureTargets.Clear(); RC.BindDepthRT(null, DepthStencilAccess.ReadWrite, null); RC.BindGBufferForRead(0, MyGBuffer.Main); }
internal static void RenderColoredTextures(List <renderColoredTextureProperties> texturesToRender) { if (texturesToRender.Count == 0) { return; } if (!m_initialized) { Init(); } const int RENDER_TEXTURE_RESOLUTION = 512; RC.SetBlendState(null); RC.SetInputLayout(null); RC.PixelShader.Set(m_ps); RC.AllShaderStages.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.AllShaderStages.SetConstantBuffer(1, m_cb); MyBorrowedRwTextureManager rwTexManager = MyManagers.RwTexturesPool; MyFileTextureManager fileTexManager = MyManagers.FileTextures; foreach (var texture in texturesToRender) { ISrvBindable tex = fileTexManager.GetTexture(texture.TextureName, MyFileTextureEnum.COLOR_METAL, true); if (tex == null) { continue; } Vector2 texSize = tex.Size; Vector2I renderTargetResolution = new Vector2I(RENDER_TEXTURE_RESOLUTION, RENDER_TEXTURE_RESOLUTION); if (texSize.Y > 0) { if (texSize.Y < RENDER_TEXTURE_RESOLUTION) { renderTargetResolution.X = (int)texSize.X; renderTargetResolution.Y = (int)texSize.Y; } else { renderTargetResolution.X *= (int)(texSize.X / texSize.Y); } } MyViewport viewport = new MyViewport(renderTargetResolution.X, renderTargetResolution.Y); IBorrowedRtvTexture renderTexture = rwTexManager.BorrowRtv("MySaveExportedTextures.RenderColoredTextures", renderTargetResolution.X, renderTargetResolution.Y, SharpDX.DXGI.Format.R8G8B8A8_UNorm_SRgb, 1, 0); RC.SetRtv(renderTexture); // Set color var mapping = MyMapping.MapDiscard(m_cb); Vector4 color = new Vector4(texture.ColorMaskHSV, 1); mapping.WriteAndPosition(ref color); mapping.Unmap(); // Set texture RC.PixelShader.SetSrv(0, tex); // Draw MyScreenPass.DrawFullscreenQuad(viewport); // Save to file MyTextureData.ToFile(renderTexture, texture.PathToSave, ImageFileFormat.Png); renderTexture.Release(); } texturesToRender.Clear(); RC.SetRtv(null); RC.PixelShader.SetSrvs(0, MyGBuffer.Main); }
public override void OnInspectorGUI() { LinSpriteAtlas textureData = (LinSpriteAtlas)target; base.OnInspectorGUI(); if (GUILayout.Button("生成")) { string texturePath = AssetDatabase.GetAssetPath(target); if (textureData.texture != null) { texturePath = AssetDatabase.GetAssetPath(textureData.texture); } else { texturePath = texturePath.Substring(0, texturePath.Length - 5) + "png"; } HashSet <string> paths = new HashSet <string>(); foreach (var _folder in textureData.folders) { if (null != _folder && _folder.Path.Length > 0) { { string[] _paths = System.IO.Directory.GetFiles(_folder.Path, "*.png", SearchOption.AllDirectories); foreach (var s in _paths) { paths.Add(s.Replace('\\', '/')); } } { string[] _paths = System.IO.Directory.GetFiles(_folder.Path, "*.tga", SearchOption.AllDirectories); foreach (var s in _paths) { paths.Add(s.Replace('\\', '/')); } } { string[] _paths = System.IO.Directory.GetFiles(_folder.Path, "*.jpg", SearchOption.AllDirectories); foreach (var s in _paths) { paths.Add(s.Replace('\\', '/')); } } } } Texture2D png = new Texture2D(2048, 2048, TextureFormat.RGBA32, false); for (int i = 0; i < png.width; i++) { for (int j = 0; j < png.height; j++) { png.SetPixel(i, j, new Color(0, 0, 0, 0)); } } png.Apply(); //RenderTexture rt = new RenderTexture(2048, 2048, 16,RenderTextureFormat.ARGB32); Pack2d.Packer packer = new Pack2d.Packer(); //List<Pack2d.Box> boxes = new List<Pack2d.Box>(); foreach (var path in paths) { Object[] datas = AssetDatabase.LoadAllAssetsAtPath(path); Texture2D baseTex = null; foreach (Object o in datas) { if (o is UnityEngine.Texture2D) { baseTex = (Texture2D)o; } } if (null != baseTex) { string p0 = AssetDatabase.GetAssetPath(baseTex); TextureImporter _importer = (TextureImporter)TextureImporter.GetAtPath(p0); if (_importer.textureType == TextureImporterType.Sprite) { Pack2d.Box box = new Pack2d.Box { width = baseTex.width + 2, height = baseTex.height + 2 }; MyTextureData _data = new MyTextureData(); _data.tex = baseTex; _data.path = p0; _data.spritesheet = new SpriteMetaData[_importer.spritesheet.Length]; for (int i = 0; i < _importer.spritesheet.Length; i++) { _data.spritesheet[i] = _importer.spritesheet[i]; } box.userData = _data; packer.Add(box); } } } //var err = packer.Pack(2048); var err = packer.Pack2(2048); if (err.Count > 0) { EditorUtility.DisplayDialog("", err.Count.ToString() + "张贴图贴图放不下", "确定"); foreach (var b in err) { MyTextureData ud = (MyTextureData)b.userData; Debug.LogError(" " + b.width + "," + b.height + " " + ud.path); } } foreach (Pack2d.Box box in packer.GetBoxs()) { MyTextureData _data = (MyTextureData)box.userData; _data.offset = new Vector2(box.position.pos_x + 1, box.position.pos_y + 1); var _tex = _data.tex; _tex = ReadTexture(_tex); for (int i = 0; i < _tex.width; i++) { for (int j = 0; j < _tex.height; j++) { Color c = _tex.GetPixel(i, j); png.SetPixel(i + (int)(_data.offset.x), j + (int)(_data.offset.y), _tex.GetPixel(i, j)); } } GameObject.DestroyImmediate(_tex); } byte[] bytes = png.EncodeToPNG(); System.IO.File.WriteAllBytes(texturePath, bytes); AssetDatabase.ImportAsset(texturePath); textureData.texture = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePath); TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(texturePath); importer.alphaIsTransparency = true; importer.textureType = TextureImporterType.Sprite; importer.spriteImportMode = SpriteImportMode.Multiple; List <SpriteMetaData> spritesheetMeta = new List <SpriteMetaData>(); foreach (Pack2d.Box box in packer.GetBoxs()) { MyTextureData _data = (MyTextureData)box.userData; for (int i = 0; i < _data.spritesheet.Length; i++) { SpriteMetaData currentMeta = _data.spritesheet[i]; Rect _rect = currentMeta.rect; _rect.x += _data.offset.x; _rect.y += _data.offset.y; currentMeta.rect = _rect; spritesheetMeta.Add(currentMeta); } } importer.spritesheet = spritesheetMeta.ToArray(); importer.SaveAndReimport(); } }