Texture.new: Difference between revisions
No edit summary |
No edit summary |
||
| (25 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
'''Available since:''' Gideros 2011.6<br/> | '''Available since:''' Gideros 2011.6<br/> | ||
'''Class:''' [[Texture]]<br/> | |||
=== Description === | === Description === | ||
Creates a new Texture object. | |||
Creates a new Texture object. | <syntaxhighlight lang="lua"> | ||
Texture.new(filename,filtering,options) | |||
< | </syntaxhighlight> | ||
</ | |||
=== Parameters === | === Parameters === | ||
'''filename''': (string) | '''filename''': (string) the name of the texture file to be loaded<br/> | ||
'''filtering''': (boolean | '''filtering''': (boolean) whether or not the texture is filtered, that is smoothing the image (''blurry'') ('''optional, default = false''')<br/> | ||
'''options''': (table | '''options''': (table) a table that specifies optional parameters ('''optional'''). The following options are supported:<br/> | ||
* '''transparentColor''': specify which color stands for transparent for formats that don't supply an alpha channel such as JPEG | |||
* '''wrap''': how to treat texels outside the texture. Possible values are TextureBase.CLAMP ('''default''') and TextureBase.REPEAT | |||
* '''format''': the GPU pixel format for the texture ('''default = RGBA8888''') see '''[[TextureBase]]''' | |||
* '''extend''': whether the texture should be extended to a power of two size ('''default = true''') | |||
* '''scale''': the scale at which this texture was made, if it cannot be determined by a suffix ('''default = 1''') | |||
* '''rawalpha''': disable alpha premultiplication. This can be helpful if one wants to store specific data in the alpha channel of a texture along with a dedicated shader ('''new 2022.3''') ('''default = false''') | |||
* '''mipmap''': enable mipmaping ('''new 2022.6''') ('''default = false'''), for more info see https://www.khronos.org/opengl/wiki/Texture#Mip_maps | |||
=== Examples === | === Examples === | ||
<syntaxhighlight lang="lua"> | |||
-- do not filter and make the color 0xff00ff transparent | |||
local texture = Texture.new( | local texture = Texture.new("image.jpg", false, {transparentColor = 0xff00ff}) | ||
-- enable filtering and repeat the texture | |||
local texture = Texture.new("image.png", true, {wrap = Texture.REPEAT}) | |||
</syntaxhighlight> | |||
---- | |||
__NOTOC__ | __NOTOC__ | ||
'''Available since:''' Gideros 2016.08<br/> | '''Available since:''' Gideros 2016.08<br/> | ||
'''Class:''' [[Texture]]<br/> | |||
=== Description === | === Description === | ||
Creates a texture from pixel data. | |||
< | <syntaxhighlight lang="lua"> | ||
Texture.new(pixels,width,height,filtering,options) | |||
</ | </syntaxhighlight> | ||
=== Parameters === | === Parameters === | ||
'''pixels''': (string) | '''pixels''': (string) a string containing actual R,G,B,A components of each pixel in the new texture. Each component is stored as a byte. You can pass nil if you don't need to initialize texture content<br/> | ||
'''width''': (number) width of the texture to create <br/> | '''width''': (number) width of the texture to create<br/> | ||
'''height''': (number) | '''height''': (number) height of the texture to create<br/> | ||
'''filtering''': (boolean | '''filtering''': (boolean) indicates that the texture should be filtered, that is smoothing the image (''blurry'') ('''optional, default = false)'''<br/> | ||
'''options''': (table | '''options''': (table) a table that specifies optional parameters ('''optional'''). The following options are supported:<br/> | ||
* '''transparentColor''': specify which color stands for transparent for formats that don't supply an alpha channel such as JPEG | |||
* '''wrap''': how to treat texels outside the texture. Possible values are TextureBase.CLAMP ('''default''') and TextureBase.REPEAT | |||
* '''format''': the GPU pixel format for the texture ('''default = RGBA8888''') see '''[[TextureBase]]''' | |||
* '''extend''': whether the texture should be extended to a power of two size ('''default = true''') | |||
* '''scale''': the scale at which this texture was made, if it cannot be determined by a suffix ('''default = 1''') | |||
* '''rawalpha''': disable alpha premultiplication. This can be helpful if one wants to store specific data in the alpha channel of a texture along with a dedicated shader ('''new 2022.3''') ('''default = false''') | |||
* '''mipmap''': enable mipmaping ('''new 2022.6''') ('''default = false''') | |||
=== Examples === | === Examples === | ||
<syntaxhighlight lang="lua"> | |||
-- Create a 300x400 empty texture. Prevent gideros from extending the texture to the next power of two size | |||
local texture = Texture.new(nil, 300, 400, false, {extend=false}) | |||
</syntaxhighlight> | |||
'''Generating a Dynamic Checkerboard Pattern''' AI | |||
<syntaxhighlight lang="lua"> | |||
-- 1. Define dimensions | |||
local width = 4 | |||
local height = 4 | |||
-- 2. Build a table of raw byte characters (RGBA format) | |||
local pixelData = {} | |||
for y = 1, height do | |||
for x = 1, width do | |||
-- Alternate between colors based on grid coordinates | |||
if (x + y) % 2 == 0 then | |||
-- Pure White Pixel: R=255, G=255, B=255, A=255 | |||
table.insert(pixelData, string.char(255, 255, 255, 255)) | |||
else | |||
-- Pure Black Pixel: R=0, G=0, B=0, A=255 | |||
table.insert(pixelData, string.char(0, 0, 0, 255)) | |||
end | |||
end | |||
end | |||
-- 3. Collapse the table into a single continuous raw byte string | |||
local binaryPixels = table.concat(pixelData) | |||
-- 4. Create the texture directly from the memory buffer | |||
-- We pass 'false' to keep pixel-perfect sharp edges (nearest neighbor) | |||
local dynamicTexture = Texture.new( | |||
binaryPixels, | |||
width, | |||
height, | |||
false, | |||
{ wrap = Texture.REPEAT } | |||
) | |||
-- 5. Pass it to a Bitmap to display it on screen | |||
local bg = Bitmap.new(dynamicTexture) | |||
-- 6. Scale it up so you can clearly see the tiny 4x4 pixel grid! | |||
bg:setScale(50) | |||
bg:setPosition(100, 100) | |||
stage:addChild(bg) | |||
</syntaxhighlight> | |||
=== See also === | |||
'''[[Buffer.new]]''' | |||
{{Texture}} | |||
Latest revision as of 20:23, 9 October 2026
Available since: Gideros 2011.6
Class: Texture
Description
Creates a new Texture object.
Texture.new(filename,filtering,options)
Parameters
filename: (string) the name of the texture file to be loaded
filtering: (boolean) whether or not the texture is filtered, that is smoothing the image (blurry) (optional, default = false)
options: (table) a table that specifies optional parameters (optional). The following options are supported:
- transparentColor: specify which color stands for transparent for formats that don't supply an alpha channel such as JPEG
- wrap: how to treat texels outside the texture. Possible values are TextureBase.CLAMP (default) and TextureBase.REPEAT
- format: the GPU pixel format for the texture (default = RGBA8888) see TextureBase
- extend: whether the texture should be extended to a power of two size (default = true)
- scale: the scale at which this texture was made, if it cannot be determined by a suffix (default = 1)
- rawalpha: disable alpha premultiplication. This can be helpful if one wants to store specific data in the alpha channel of a texture along with a dedicated shader (new 2022.3) (default = false)
- mipmap: enable mipmaping (new 2022.6) (default = false), for more info see https://www.khronos.org/opengl/wiki/Texture#Mip_maps
Examples
-- do not filter and make the color 0xff00ff transparent
local texture = Texture.new("image.jpg", false, {transparentColor = 0xff00ff})
-- enable filtering and repeat the texture
local texture = Texture.new("image.png", true, {wrap = Texture.REPEAT})
Available since: Gideros 2016.08
Class: Texture
Description
Creates a texture from pixel data.
Texture.new(pixels,width,height,filtering,options)
Parameters
pixels: (string) a string containing actual R,G,B,A components of each pixel in the new texture. Each component is stored as a byte. You can pass nil if you don't need to initialize texture content
width: (number) width of the texture to create
height: (number) height of the texture to create
filtering: (boolean) indicates that the texture should be filtered, that is smoothing the image (blurry) (optional, default = false)
options: (table) a table that specifies optional parameters (optional). The following options are supported:
- transparentColor: specify which color stands for transparent for formats that don't supply an alpha channel such as JPEG
- wrap: how to treat texels outside the texture. Possible values are TextureBase.CLAMP (default) and TextureBase.REPEAT
- format: the GPU pixel format for the texture (default = RGBA8888) see TextureBase
- extend: whether the texture should be extended to a power of two size (default = true)
- scale: the scale at which this texture was made, if it cannot be determined by a suffix (default = 1)
- rawalpha: disable alpha premultiplication. This can be helpful if one wants to store specific data in the alpha channel of a texture along with a dedicated shader (new 2022.3) (default = false)
- mipmap: enable mipmaping (new 2022.6) (default = false)
Examples
-- Create a 300x400 empty texture. Prevent gideros from extending the texture to the next power of two size
local texture = Texture.new(nil, 300, 400, false, {extend=false})
Generating a Dynamic Checkerboard Pattern AI
-- 1. Define dimensions
local width = 4
local height = 4
-- 2. Build a table of raw byte characters (RGBA format)
local pixelData = {}
for y = 1, height do
for x = 1, width do
-- Alternate between colors based on grid coordinates
if (x + y) % 2 == 0 then
-- Pure White Pixel: R=255, G=255, B=255, A=255
table.insert(pixelData, string.char(255, 255, 255, 255))
else
-- Pure Black Pixel: R=0, G=0, B=0, A=255
table.insert(pixelData, string.char(0, 0, 0, 255))
end
end
end
-- 3. Collapse the table into a single continuous raw byte string
local binaryPixels = table.concat(pixelData)
-- 4. Create the texture directly from the memory buffer
-- We pass 'false' to keep pixel-perfect sharp edges (nearest neighbor)
local dynamicTexture = Texture.new(
binaryPixels,
width,
height,
false,
{ wrap = Texture.REPEAT }
)
-- 5. Pass it to a Bitmap to display it on screen
local bg = Bitmap.new(dynamicTexture)
-- 6. Scale it up so you can clearly see the tiny 4x4 pixel grid!
bg:setScale(50)
bg:setPosition(100, 100)
stage:addChild(bg)
See also