EditableImage

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建

EditableImage 允许运行时创建和操作图像。

要创建一个空白 EditableImage ,请使用 AssetService:CreateEditableImage() 。要从现有图像中创建一个 EditableImage ,请使用 AssetService:CreateEditableImageAsync()

EditableImage 可以用于任何带有图像的 Content 属性,例如 ImageLabel.ImageContentMeshPart.TextureContent .这可以通过将内容属性设置为 Content.fromObject(editableImage) 来实现。

EditableImage 坐标系是相对于图像的左上角:

  • 顶部左侧:(0, 0)
  • 右下:(Size.X - 1, Size.Y - 1)

当你使用 AssetService:PromptCreateAssetAsync() 来发布一个具有 Content 属性的对象,该属性引用一个 EditableImage 时,编辑图像将发布为图像,属性将设置为新的资产ID。

更新限制

仅在显示侧每帧更新一次 EditableImage 。例如,如果你更新了三个正在显示的 EditableImage 对象,那么为它们全部更新将需要三个框架。

启用可编辑图像对已发布体验

为了安全目的,默认情况下,使用 EditableImage 对发布的体验失败。要启用使用 EditableImage ,您必须年满 13 岁并身份验证通过。验证通过后,打开 工作室 。选择 文件 > 游戏设置 > 安全 并启用 允许网格和图像 API 切换。记得在启用切换之前查看使用条款。

权限

为了防止滥用,AssetService:CreateEditableImageAsync()仅允许您加载和编辑图像资产:

  • 这些是由体验的创建者拥有的(如果体验由个人拥有)。
  • 这些是由群组拥有的(如果体验由群组拥有)。
  • 这些是由登录的 Studio 用户拥有的(如果地点文件尚未保存或发布到 Roblox)。

如果使用 API 来加载不满足上述条件的资产,它们将抛出错误。

概要

属性

方法

属性

只读
未复制
读取并联

像素内的 EditableImage 的大小。最大尺寸为 1024×1024。一个 EditableImage 无法调整大小;该属性为读写不可变。为了调整图像大小或裁剪图像,创建一个新的 EditableImage 并使用 DrawImageTransformed() 来转移内容;然后调用 Destroy()

方法

Destroy

()

摧毁图像的内容,立即回收使用的内存。


返回

()

DrawCircle

()

EditableImage 上的指定位置绘制圆圈。如果圆圈是半透明的,它将使用源过滤与它后面的像素混合。

参数

center: Vector2

圆圈中心,相对于左上角的 EditableImage 。允许位置超出画布边界。

默认值:""
radius: number

圆圈的半径以像素计。

默认值:""
color: Color3

圆圈的颜色。

默认值:""
transparency: number

圆圈的透明度,0完全不透明,1完全透明。

默认值:""

源图像的像素与添加的图像的像素如何融合。

默认值:""

返回

()

DrawImage

()

在指定位置绘制另一个 EditableImage 到这个 EditableImage 。允许超出画布边界的位置,只绘制新图像的一部分。

参数

position: Vector2

添加的图像顶左角的位置将被绘制。

默认值:""

EditableImage 绘制到这个 EditableImage

默认值:""

源图像的像素应如何与添加的图像的像素融合。

默认值:""

返回

()

DrawImageProjected

()

将另一个 EditableImage 项目转换为 EditableMesh 并将结果存储在这个 EditableImage 上,使用指定的投影和画笔配置进行转换。

参数

用于投射的 EditableMesh

默认值:""
projection: Dictionary

包括以下键值对的投影配置词典:

  • DirectionVector3) 在投射器面向的地方。
  • Position ( Vector3 ) 作为对网格的本地空间位置。
  • Size ( Vector3 ) 作为投影机的尺寸。
  • UpVector3)作为投射到本地空间的项目器对网格的上向向量。
默认值:""
brushConfig: Dictionary

包括以下键值对的刷子配置词典:

  • AlphaBlendTypeEnum.ImageAlphaType)决定这个投影将如何融合 alpha 值。
  • ColorBlendTypeEnum.ImageCombineType)决定这个投影将如何融合颜色值。
  • DecalEditableImage)用于投影的图像。
  • FadeAngle (数字)作为投影边落关闭的角度以度。
  • BlendIntensity (数字)作为在 01 之间的值,控制投影是否融入到最终图像中的多少。
默认值:""

返回

()

DrawImageTransformed

()

这种方法可以让你将 EditableImage 绘制到这个 EditableImage 中,应用变形,例如缩放和旋转。位置参数指定源图的旋转点将在变形后放置在这个图像上的位置。允许超出画布边界的位置,只绘制新图像的一部分。

参数

position: Vector2

位于像素中的位置,在这个图像上将放置来源图像的旋转点。

默认值:""
scale: Vector2

沿 X 和 Y 轴的源图像的缩放因子。

默认值:""
rotation: number

应用于来源图像的旋转角度,以度为单位,环绕其旋转点。

默认值:""

EditableImage 将被绘制到这个图像中。

默认值:""
options: Dictionary

可选词典用于额外配置:

  • CombineType : 指定源图像的像素与目标像素融合的方式。默认值为 Enum.ImageCombineType.AlphaBlend
  • SamplingMode : 指定采样方法 (例如 Default 对于二线或 Pixelated 对于最近邻居)。默认值为 Enum.ResamplerMode.Default
  • PivotPoint : 指定源图像内的旋转点以进行缩放和旋转。默认是源图像的中心(即 Image.Size / 2 )。
默认值:""

返回

()

代码示例

The following code draws a rotated and scaled image onto another.

EditableImage:DrawImageTransformed()

local AssetService = game:GetService("AssetService")
-- Example of drawing a rotated and scaled image onto another EditableImage
local srcImage = AssetService:CreateEditableImage({ Size = Vector2.new(256, 256) })
local dstImage = AssetService:CreateEditableImage({ Size = Vector2.new(512, 512) })
-- Drawing with a rotation of 45 degrees, scaling by 2x, and placing at (100, 100)
dstImage:DrawImageTransformed(
Vector2.new(100, 100), -- Position
Vector2.new(2, 2), -- Scale
45, -- Rotation (degrees)
srcImage, -- Source image
{
CombineType = Enum.ImageCombineType.AlphaBlend, -- Optional, default is AlphaBlend
SamplingMode = Enum.ResamplerMode.Default, -- Optional, default is Default
PivotPoint = srcImage.Size / 2, -- Optional, default is center of the source image
}
)

The following code shows how cropping an image can be done using the EditableImage:DrawImageTransformed() method.

EditableImage:DrawImageTransformed() - Crop

local AssetService = game:GetService("AssetService")
-- Source image
local srcImage = AssetService:CreateEditableImageAsync(Content.fromUri(assetUri))
-- Crop area defined by offset and size
local cropOffset = Vector2.new(50, 50)
local cropSize = Vector2.new(100, 100)
-- Destination image with size of the crop area
local dstImage = AssetService:CreateEditableImage({ Size = cropSize })
-- Position (top-left corner)
local position = Vector2.new(0, 0)
-- Scale factors (no scaling)
local scale = Vector2.new(1, 1)
-- Rotation angle (no rotation)
local rotation = 0
-- Draw the source image onto the destination image with adjusted pivot to crop the image
dstImage:DrawImageTransformed(position, scale, rotation, srcImage, {
CombineType = Enum.ImageCombineType.Overwrite,
PivotPoint = cropOffset, -- Set pivot point to cropOffset to start drawing from there
})

DrawLine

()

在两个提供的点之间绘制一条反折线,厚度为一像素 EditableImage

参数

线的起始点。

默认值:""

线的末端。

默认值:""
color: Color3

线的颜色。

默认值:""
transparency: number

线的透明度。

默认值:""

源图像的像素与添加的图像的像素如何融合。

默认值:""

返回

()

DrawRectangle

()

在给定的左上角位置绘制一个长方形在给定的尺寸EditableImage

参数

position: Vector2

长方形的左上角位置。与其他绘画方法不同,这不能超出 EditableImage 画布边界。

默认值:""
size: Vector2

绘制长方形的大小,以像素计算。

默认值:""
color: Color3

长方形的颜色。

默认值:""
transparency: number

矩形的透明度。

默认值:""

源图像的像素与添加的图像的像素如何融合。

默认值:""

返回

()

ReadPixelsBuffer

写入并联

EditableImage 读取一个长方形区域的像素,并将其返回为缓冲区。缓冲区中的每个数字都是单个字节,像素存储在四个字节顺序(红色、绿色、蓝色和 Alpha)中。

请注意,此方法使用 alpha 而不是透明,与 EditableImage 绘制方法不同。

参数

position: Vector2

已读取像素长方形区域的左上角。

默认值:""
size: Vector2

要阅已读的像素长方区域的大小。

默认值:""

返回

每个像素由四个字节表示的缓冲区(红色、绿色、蓝色和 Alpha 分别)。缓冲区的长度可以计算为 Size.X * Size.Y * 4 字节。

代码示例

The following code reads two pixels from a EditableImage and creates a part with the average color between them.

EditableImage:ReadPixelsBuffer()

local AssetService = game:GetService("AssetService")
local options = { Size = Vector2.new(32, 32) }
local editableImage = AssetService:CreateEditableImage(options)
local pixelsBuffer = editableImage:ReadPixelsBuffer(Vector2.zero, Vector2.new(2, 1))
local color1 =
Color3.fromRGB(buffer.readu8(pixelsBuffer, 0), buffer.readu8(pixelsBuffer, 1), buffer.readu8(pixelsBuffer, 2))
local transparency1 = (255 - buffer.readu8(pixelsBuffer, 3)) / 255
local color2 =
Color3.fromRGB(buffer.readu8(pixelsBuffer, 4), buffer.readu8(pixelsBuffer, 5), buffer.readu8(pixelsBuffer, 6))
local transparency2 = (255 - buffer.readu8(pixelsBuffer, 7)) / 255
local averageColor = color1:Lerp(color2, 0.5)
local averageTransparency = (transparency1 + transparency2) / 2
local part = Instance.new("Part")
part.Color = averageColor
part.Transparency = averageTransparency
part.Parent = workspace

WritePixelsBuffer

()

从缓冲区中写一个长方形区域的像素到 EditableImage 。缓冲区中的每个数字都是单个字节,像素存储在四个字节顺序(红色、绿色、蓝色和 Alpha)中。

请注意,此方法使用 alpha 而不是透明,与 EditableImage 绘制方法不同。

参数

position: Vector2

长方形区域的左上角,用于将像素绘制进来。

默认值:""
size: Vector2

要写入的像素长方区域的大小。

默认值:""
buffer: buffer

每个像素由四个字节(分别为红色、绿色、蓝色和 Alpha)表示的缓冲区。缓冲区的长度应为 Size.X * Size.Y * 4 字节。

默认值:""

返回

()

代码示例

The following code reads the pixels of a EditableImage and writes back the inverted color values of those pixels.

EditableImage:WritePixelsBuffer()

local AssetService = game:GetService("AssetService")
local options = { Size = Vector2.new(32, 32) }
local editableImage = AssetService:CreateEditableImage(options)
local pixelsBuffer = editableImage:ReadPixelsBuffer(Vector2.zero, editableImage.Size)
for i = 1, editableImage.Size.X * editableImage.Size.Y do
local pixelIndex = (i - 1) * 4
buffer.writeu8(pixelsBuffer, pixelIndex, 255 - buffer.readu8(pixelsBuffer, pixelIndex))
buffer.writeu8(pixelsBuffer, pixelIndex + 1, 255 - buffer.readu8(pixelsBuffer, pixelIndex + 1))
buffer.writeu8(pixelsBuffer, pixelIndex + 2, 255 - buffer.readu8(pixelsBuffer, pixelIndex + 2))
-- Set alpha to 255 to make all pixels fully opaque.
buffer.writeu8(pixelsBuffer, pixelIndex + 3, 255)
end
editableImage:WritePixelsBuffer(Vector2.zero, editableImage.Size, pixelsBuffer)

活动