使用 Overlay Canvas 上的 RectTransform 在 WorldSpace Canvas 上裁剪图像纹理

ManIp

几天来,我一直在尝试使用 Overlay Canvas 上的 RectTransform(代码示例中的 CropArea -cropArea)在 WorldSpace Canvas 上裁剪图像纹理(代码示例中的Board - originalImage )。

在此处输入图像描述

问题是我在原始图像上找不到正确的cropArea坐标。

我试过这个:

        Texture2D croppedTexture = new Texture2D((int)cropArea.rectTransform.rect.width, (int)cropArea.rectTransform.rect.height);
        Texture2D originalTexture = (Texture2D) originalImage.mainTexture;
        
        croppedTexture.SetPixels(originalTexture.GetPixels((int)cropArea.rectTransform.anchoredPosition.x, (int)cropArea.rectTransform.anchoredPosition.y, (int)cropArea.rectTransform.rect.width, (int)cropArea.rectTransform.rect.height));
        croppedTexture.Apply();
        resultImage.texture = croppedTexture;

但结果图像未正确裁剪。它向左一点,向下一点。

在此处输入图像描述

有谁知道我怎样才能做到这一点?

脖子

我发现我必须考虑很多变量。这是一个简化版本。

需要一个新字段:worldCanvas

var cropRectTrans = cropArea.rectTransform;
var origRectTrans = originalImage.rectTransform;
var origRectSize = origRectTrans.sizeDelta;
var pivot = origRectTrans.pivot;
Texture2D originalTexture = (Texture2D)originalImage.mainTexture;

// Scale pivot to pixel unit.

pivot.Scale(origRectSize);

// Get corners of the overlay rectangle in world space.
// The canvas is "Screen Space Overlay", so these positions are
// also the screen positions.

var cropCorners = new Vector3[4];
cropRectTrans.GetWorldCorners(cropCorners);

// Transform the left-bottom and right-top corners to the space
// of the original image. The translated position needs to be added
// with the scaled pivot, so that we can obtain the coordinates
// relative to the left-bottom corner of the image.

var cam = worldCanvas.worldCamera;
RectTransformUtility.ScreenPointToLocalPointInRectangle(
    origRectTrans, cropCorners[0], cam, out Vector2 lb);
RectTransformUtility.ScreenPointToLocalPointInRectangle(
    origRectTrans, cropCorners[2], cam, out Vector2 tr);

var point = lb + pivot;
var size = tr - lb;

// Scale the position and size if the image is scaled.

var scale = new Vector2(
    originalTexture.width / origRectSize.x,
    originalTexture.height / origRectSize.y
);
point.Scale(scale);
size.Scale(scale);

// Finally we get the correct position and size in the original image space.

Texture2D croppedTexture = new Texture2D((int)size.x, (int)size.y);
croppedTexture.SetPixels(originalTexture.GetPixels(
    (int)point.x, (int)point.y, (int)size.x, (int)size.y));
croppedTexture.Apply();
resultImage.texture = croppedTexture;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用 HTML Canvas 在图像上绘制?

如何使用 Javascript 在 Canvas 上绘制 SVG?

如何使用JavaScript和Canvas在图像上添加文本

如何在不使用Canvas的图像控件上绘制椭圆?

使用drawImage()在Canvas上绘制多个图像的问题

在Canvas SwiftUI上崩溃

如何在画布上使用Canvas在移动对象上用自定义图像填充对象?

在 html Canvas 上添加 QR

如何在Canvas ..上设置图像?然后在图像上绘制

如何在 Jupyterhub 上使用 ipycanvas 显示 Canvas?

在Android上使用canvas描边圆imageview

使用react-konva在Canvas上制作GIF动画

如何使用:JavaScript Canvas中的多个对象上的globalCompositeOperation

javascript canvas drawimage 可以在 iPhone 上使用吗?

使用 MVVM 在 WPF Canvas 上绘制动态圆

如何使用SharedPreference在Canvas上绘制字符串?

如何使用 Tkinter 在 Python Canvas 上引用绘制事件?

如何使用JavaFX在Canvas上检测鼠标拖动事件

如何在tkinter中的Canvas上使用叠加Vkeyboard?

使用移动设备拖动在Canvas上渲染的图像会导致该图像消失

WPF Canvas上没有Canvas.ZIndex或SetZIndex()属性

在Java SWT中使用ImageData在Canvas上显示图像的WPF等效项是什么?

Android在Canvas上处理图像-使用触摸,移动,缩放-放大/缩小,缩放

phantomjs使用src图像在canvas.toDataURL上抛出DOM异常18

如何使用canvas js使我的图像上的多边形可点击?

我无法在Tkinter上使用canvas和create_image打开图像

canvas.drawImage裁剪图像

使用<canvas>中的<canvas>中的MediaRecorder()使用canvas.captureStream()对<video>录制的流进行回放在Firefox,Crem上呈现不同

使用 tkinter Canvas 显示图像,然后使用 cv2.circle/cv2.line 在图像上绘制