Tag: texture2d

将Sprite对象数组合成一个Sprite – Unity

我在Unity中有一组Sprite对象。 它们的大小取决于加载的图像。 我想将它们像平铺地图并排组合成一个图像。 我希望它们的布局就像你正在形成一系列图像,一个接一个。 (注意:没有一个在另一个之上)我怎么能这样做? 我正在组合的原因(仅适用于那些想知道的人)是因为我正在使用polygon2D Collider。 由于当我并排使用多个碰撞器时发生了一些奇怪的行为,我决定在添加一个大的多边形碰撞器之前组合图像。 请注意,这些事情发生在运行时。 我不能只创建一个大图像并加载,因为图像的顺序只在运行时确定。 我希望能得到一些帮助。 谢谢。

有没有从XNA中的Bitmap对象创建Texture2D的快速替代方法?

我已经环顾了很多,我发现从Bitmap创建Texture2D的唯一方法是: using (MemoryStream s = new MemoryStream()) { bmp.Save(s, System.Drawing.Imaging.ImageFormat.Png); s.Seek(0, SeekOrigin.Begin); Texture2D tx = Texture2D.FromFile(device, s); } 和 Texture2D tx = new Texture2D(device, bmp.Width, bmp.Height, 0, TextureUsage.None, SurfaceFormat.Color); tx.SetData(rgbValues, 0, rgbValues.Length, SetDataOptions.NoOverwrite); 其中rgbValues是一个字节数组,包含32位ARGB格式的位图像素数据。 我的问题是,我可以尝试更快的方法吗? 我正在编写一个地图编辑器,它必须读取自定义格式图像(地图图块)并将它们转换为Texture2D纹理才能显示。 以前版本的编辑器是一个C ++实现,它首先将图像转换为位图,然后转换为使用DirectX绘制的纹理。 我在这里尝试了相同的方法,但是上述两种方法都非常慢。 要加载到内存中,映射所需的所有纹理都需要第一个方法~250秒,对于合理的规格计算机,第二个方法需要~110秒(为了比较,C ++代码大约需要5秒)。 如果有一种方法可以直接编辑纹理数据(例如使用Bitmap类的LockBits方法),那么我就可以将自定义格式图像直接转换为Texture2D,并希望节省处理时间。 任何帮助将非常感谢。 谢谢

写入RenderTarget后,如何有效地克隆输出?

XNA noob在这里,每天都在学习。 我刚刚研究了如何使用RenderTarget2D将多个纹理合成为一个。 然而,虽然我可以将RenderTarget2D用作Texture2D用于大多数目的,但是存在一个重要的区别:当后缓冲区resize时这些渲染的纹理会丢失(毫无疑问,在其他情况下,如图形设备内存不足)。 目前,我只是将完成的RenderTarget2D复制到一个新的非易失性Texture2D对象中。 不过,我这样做的代码非常难看。 有没有更优雅的方式来做到这一点? 也许我只是累了,但我无法在Google或SO上找到答案。 略有简化: public static Texture2D MergeTextures(int width, int height, IEnumerable<Tuple> textures) { RenderTarget2D buffer = new RenderTarget2D(_device, width, height); _device.SetRenderTarget(buffer); _device.Clear(Color.Transparent); SpriteBatch spriteBatch = new SpriteBatch(_device); spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied); // Paint each texture over the one before, in the appropriate color Rectangle rectangle = new Rectangle(0, 0, width, height); […]

将SurfaceTexture渲染为Unity Texture2D

我之前提出过simillar问题,但是他们并没有很好地澄清,现在我想建议我在我的代码中做错了什么。 所以我要做的是将SurfaceTexture从Android插件渲染到Unity Texture2D。 这是我的Unity代码: public class AndroidHandler : MonoBehaviour { [SerializeField] private RawImage _rawImage; private Texture2D _inputTexture; private AndroidJavaObject androidStreamerObj; private System.IntPtr _nativePtr; void Start () { _rawImage.material.SetTextureScale(“_MainTex”, new Vector2(-1, -1)); InitAndroidStreamerObject(); } private void InitAndroidStreamerObject() { androidStreamerObj = new AndroidJavaObject(“makeitbetter.figazzz.com.vitamiousing7.AndroidStreamer”); Int32 texPtr = androidStreamerObj.Call (“GetTexturePtr”); Debug.Log(“texture pointer? ” + texPtr); Texture2D nativeTexture = Texture2D.CreateExternalTexture […]

如何通过脚本使Texture2D可读

我想让用户能够解码从图库加载的QR图像,我找到了一个插件来探索并加载图像作为texture2D,但要解码那个QR码,Texture2D必须是可读/可写的,我检查插件,对于Android,它正在使用jar进行探索和加载,在IOS平台中,它使用的是打包的库,因此我无法访问lib的代码, 我已经搜索了答案,最大的解决方案是在Unity检查器中更改纹理的导入设置,但由于这是由代码加载的纹理,因此没有可用的检查器设置,所以我的问题是: 有没有办法让这个加载的纹理可以通过代码读/写? 无需访问lib代码? 谢谢 这是可以通过此插件获取纹理的代码 void OnImageLoad(string imgPath, Texture2D tex, ImageAndVideoPicker.ImageOrientation imgOrientation) { Debug.Log(“Image Location : ” + imgPath); Debug.Log(“Image Loaded : ” + imgPath); texture = tex; Texture2D readableText = new Texture2D(tex.width, tex.height); readableText.LoadImage(tex.GetRawTextureData()); string url = QRCodeDecodeController.DecodeByStaticPic(readableText); StartCoroutine(GetSceneAndLoadLevel(url)); } 正如你所看到的,我试过这个答案,但没有运气。 以下是Android显示的错误: 06-23 21:47:32.853: I/Unity(10557): (Filename: D Line: 0) 06-23 21:47:33.784: E/Unity(10557): Texture […]