graphics.newTexture()

类型 函数
graphics.*
返回值 TextureResource
修订 版本 2024.3703
关键词 纹理,性能优化,纹理内存,图像
另请参阅 graphics.releaseTextures()
texture:releaseSelf()
纹理加载/管理 (指南)
TextureResource

概述

创建一个 TextureResource 对象,允许您访问和管理纹理。这可以用于预加载纹理/图像,并防止在没有显示对象使用它时将其处置。

创建 TextureResource 对象后,您可以在纹理相关实例中使用它,方法是指定 filenamebaseDir 属性。

重要

在使用 TextureResource 对象之前,您应该了解几个重要的细微差别 — 有关详细信息,请参阅下一节。

陷阱

语法

graphics.newTexture( params )
params (必填)

包含 TextureResource 对象所需参数的表 — 有关详细信息,请参阅下一节。

参数参考

type (必填)

字符串 要创建的纹理的 类型。根据此值,其他键值参数将被考虑在内(仔细检查以下内容)。

local imageTexture = graphics.newTexture( { type="image", filename="icon.png", baseDir=system.ResourceDirectory } )
local canvasTexture = graphics.newTexture( { type="canvas", width=128, height=128 } )
  • "maskCanvas" — 创建类型为 TextureResourceCanvasTextureResource 对象。此纹理资源是内存中的纹理,可以通过将显示对象渲染到它来修改它。

    此类型专门用于遮罩。特别是,画布将是提供给 graphics.newMask 的图像,因此其尺寸必须遵守相同的约束。

    当对象绘制到画布上时,将使用其“红色”结果,并忽略其余组件。红色值为 0 会生成黑色像素,值为 1 会生成白色像素。中间值会导致相应的灰色阴影。

    (**陷阱**:这仅确认在 Windows 和 Android 上有效。其他平台可能仍需要小的修复。)

local maskCanvasTexture = graphics.newTexture( { type="maskCanvas", width=128, height=128 } )
filename (必填)

字符串 仅在 type"image" 时适用。指示要加载的图像文件的名称,相对于 baseDir(默认为 system.ResourceDirectory)。

baseDir (可选)

常量 仅在 type"image" 时适用。指定 filename 所在的基本目录。选项包括 system.ResourceDirectorysystem.DocumentsDirectorysystem.ApplicationSupportDirectorysystem.TemporaryDirectorysystem.CachesDirectory。默认为 system.ResourceDirectory

width (必填)

数字 仅在 type"canvas""maskCanvas" 时适用。指定可以在 TextureResourceCanvas 中渲染对象的宽度。

height (必填)

数字 仅在 type"canvas"“maskCanvas” 时适用。指定可以在 TextureResourceCanvas 中渲染对象的高度。

pixelWidth (可选)

数字 仅在 type"canvas""maskCanvas" 时适用。指定画布资源渲染到的纹理的水平像素尺寸。

pixelHeight (可选)

数字 仅在 type"canvas""maskCanvas" 时适用。指定画布资源渲染到的纹理的垂直像素尺寸。

示例

预加载图像
-- Create "TextureResource" object of type "TextureResourceBitmap"
local backgroundTexture = graphics.newTexture( { type="image", filename="background.png" } )

-- Create display object with the pre-loaded texture
local background = display.newImageRect(
    backgroundTexture.filename,  -- "filename" property required
    backgroundTexture.baseDir,   -- "baseDir" property required
    display.contentWidth,
    display.contentHeight
)
background.x = display.contentCenterX
background.y = display.contentCenterY

-- If you no longer need the texture, release it to prevent memory leaks
backgroundTexture:releaseSelf()

-- Alternatively, release all texture objects of a specific type
graphics.releaseTextures( { type="image" } )
渲染到画布资源
local tex = graphics.newTexture( { type="canvas", width=128, height=128 } )

-- Create display object with texture as contents
local rect = display.newImageRect(
    tex.filename,  -- "filename" property required
    tex.baseDir,   -- "baseDir" property required
    display.contentWidth,
    display.contentHeight
)
rect.x = display.contentCenterX
rect.y = display.contentCenterY

-- Create a circle and draw/render it to the texture
local circ = display.newCircle( 0, 0, 64 )
circ:setFillColor( { type="gradient", color1={0,0.2,1}, color2={0.8,0.8,0.8}, direction="down" } )
tex:draw( circ )

-- Schedule texture objects to be rendered to texture before next frame
tex:invalidate()
渲染到遮罩画布资源
local tex = graphics.newTexture{ type = "canvas", width = 128, height = 128 }
local back = display.newRect(0, 0, 128, 128)

back:setFillColor(1, 0, 0)

local rect = display.newRect(0, 0, 32, 64)

rect:setFillColor(0, 0, 1)

tex:draw(back)
tex:draw(rect)
tex:invalidate()

-- Create a renderable mask and draw an all-white rect into it.
local mask = graphics.newTexture{ type = "maskCanvas", width = 512, height = 512 }
local mask_back = display.newRect(0, 0, 512 - 6, 512 - 6)

mask:draw(mask_back)

-- Add some circles in their initial locations. We'll make these black and
-- periodically draw them into the mask, above the white rect.
local circles = {}

for i = 1, 10 do
    circles[i] = display.newCircle(0, 0, math.random(10, 35))

    circles[i]:setFillColor(0)

    circles[i].radius = math.random(i * 10, 170)
    circles[i].speed = math.random(1, 7) * .05

    mask:draw(circles[i])
end

-- Add a background beneath the masked parts.
local background = display.newRect(250, 250, mask.width, mask.height)

background.fill = { type = "image", filename = tex.filename, baseDir = tex.baseDir }

background:setStrokeColor(0, 0, 1)

background.alpha = .7
background.strokeWidth = 3

-- These three rects will be affected by the mask.
local masked_group = display.newGroup()

local r1 = display.newRect(masked_group, 250, 350, 150, 200)
local r2 = display.newRect(masked_group, 450, 100, 200, 100)
local r3 = display.newRect(masked_group, 150, 150, 100, 200)

masked_group:setMask(graphics.newMask(mask.filename, mask.baseDir))

masked_group.maskX, masked_group.maskY = background.x, background.y

local x0 = background.x

-- Move the background back and forth...
timer.performWithDelay(50, function(event)
    background.x = x0 + math.sin(event.time / 800) * 50
end, 0)

-- ...and update the circle arcs along with it.
timer.performWithDelay(150, function(event)
    for i, circ in ipairs(circles) do
        local angle = (event.time * circ.speed) * .0035

        circ.x, circ.y = math.cos(angle) * circ.radius, math.sin(angle) * circ.radius
    end

    mask:invalidate("cache")
end, 0)