Difference between revisions of "Sprite:setBlendMode"

From GiderosMobile
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
+
'''Available since:''' Gideros 2011.6<br/>
'''<translate>Class</translate>:''' [[Special:MyLanguage/Sprite|Sprite]]<br/>
+
'''Class:''' [[Sprite]]<br/>
=== <translate>Description</translate> ===
+
 
<translate><br />
+
=== Description ===
Sets the blend mode of the sprite. This method can be called with 1 or 2 parameters.  
+
Sets the blend mode of the sprite.
If one parameter is passed it must be one of the following blending modes:<br />
+
<syntaxhighlight lang="lua">
<br />
+
Sprite:setBlendMode(blendMode or src,dst)
<ul><br />
+
</syntaxhighlight>
<li>Sprite.ALPHA = "alpha"</li><br />
+
 
<li>Sprite.NO_ALPHA = "noAlpha"</li><br />
+
This method can be called with 1 or 2 parameters.
<li>Sprite.ADD = "add"</li><br />
+
 
<li>Sprite.MULTIPLY = "multiply"</li><br />
+
If one parameter is passed it must be one of the following blending modes:
<li>Sprite.SCREEN = "screen"</li><br />
+
*Sprite.ALPHA = "alpha"
</ul><br />
+
*Sprite.NO_ALPHA = "noAlpha"
<br />
+
*Sprite.ADD = "add"
If a [[Special:MyLanguage/Sprite|Sprite]] object doesn't set any blending mode, it takes the blending mode from its parent sprite.<br />
+
*Sprite.MULTIPLY = "multiply"
<br />
+
*Sprite.SCREEN = "screen"
<ul><br />
+
If a '''Sprite''' object doesn't set any blending mode, it takes the blending mode from its parent sprite.
<li>*Note:** The following two lines are completely same:</li><br />
+
 
</ul><br />
+
'''Note''': the following two lines are completely the same:
<pre><code><br />
+
<syntaxhighlight lang="lua">
sprite:setBlendMode("add")<br />
+
sprite:setBlendMode("add")
sprite:setBlendMode(Sprite.ADD)<br />
+
sprite:setBlendMode(Sprite.ADD)
</code></pre><br />
+
</syntaxhighlight>
It's a matter of taste which one to choose.<br />
+
It's a matter of taste which one to choose.
<br />
+
 
<br />
+
 
If two parameters are passed to this method, then a source and destination blend can be set (in that order) and each take the values:
+
If two parameters are passed to this method, then a source and destination blend can be set (in that order) and each takes the values:
<br />
+
*Sprite.ZERO = "zero"
<ul><br />
+
*Sprite.ONE = "one"
<li>Sprite.ZERO = "zero"</li><br />
+
*Sprite.SRC_COLOR = "srcColor"
<li>Sprite.ONE = "one"</li><br />
+
*Sprite.ONE_MINUS_SRC_COLOR = "oneMinusSrcColor"
<li>Sprite.SRC_COLOR = "srcColor"</li><br />
+
*Sprite.DST_COLOR = "dstColor"
<li>Sprite.ONE_MINUS_SRC_COLOR = "oneMinusSrcColor"</li><br />
+
*Sprite.ONE_MINUS_DST_COLOR = "oneMinusDstColor"
<li>Sprite.DST_COLOR = "dstColor"</li><br />
+
*Sprite.SRC_ALPHA = "srcAlpha"
<li>Sprite.ONE_MINUS_DST_COLOR = "oneMinusDstColor"</li><br />
+
*Sprite.ONE_MINUS_SRC_ALPHA = "oneMinusSrcAlpha"
<li>Sprite.SRC_ALPHA = "srcAlpha"</li><br />
+
*Sprite.DST_ALPHA = "dstAlpha"
<li>Sprite.ONE_MINUS_SRC_ALPHA = "oneMinusSrcAlpha"</li><br />
+
*Sprite.ONE_MINUS_DST_ALPHA = "oneMinusDstAlpha"
<li>Sprite.DST_ALPHA = "dstAlpha"</li><br />
+
*Sprite.SRC_ALPHA_SATURATE = "srcAlphaSaturate"
<li>Sprite.ONE_MINUS_DST_ALPHA = "oneMinusDstAlpha"</li><br />
+
 
<li>Sprite.SRC_ALPHA_SATURATE = "srcAlphaSaturate"</li><br />
+
=== Parameters ===
</ul><br /></translate>
+
'''blendMode or src''': (String) if one parameter is used, this is the blend mode. If two parameters are used this is the source blend specification<br/>
<source lang="lua">
+
'''dst''': (String) destination blend mode '''optional'''<br/>
Sprite:setBlendMode(blendMode or src,dst)
+
 
</source>
+
=== Examples ===
=== <translate>Parameters</translate> ===
+
'''All blend modes'''
'''blendMode or src''': (String) <translate>If one parameter is used, this is the blend mode. If two parameters are used this is the source blend specification.</translate> <br/>
+
<syntaxhighlight lang="lua">
'''dst''': (string) <translate>Destination blend mode</translate> '''optional'''<br/>
+
for x = 1, 11 do
 +
for y = 1, 11 do
 +
local top = Pixel.new(topTex, s, s)
 +
local btm = Pixel.new(btmTex, s, s)
 +
stage:addChild(btm)
 +
btm:addChild(top)
 +
btm:setBlendMode(x, y)
 +
btm:setPosition(lh + s * (x-1), lh + s * (y-1))
 +
end
 +
end
 +
</syntaxhighlight>
 +
 
 +
'''Spot light'''
 +
'''note: you have to provide your own asset'''
 +
<syntaxhighlight lang="lua">
 +
--[[
 +
    Script:  spotlight
 +
Description: Show how to create an animated spot light of the Gideros logo
 +
Author:  Michael Hartlef
 +
Contact: mike@fantomgl.com
 +
--]]
 +
 
 +
-- Load the mask images (can be a gradient or a plain image)
 +
--local mask = Bitmap.new(Texture.new("images/maskCircle2.png")) -- semi-transparent
 +
local mask = Bitmap.new(Texture.new("images/carmo.png")) -- plain image
 +
-- Set its anchor point in the center
 +
mask:setAnchorPoint(0.5, 0.5)
 +
-- Scale it up by a factor of 3
 +
mask:setScale(3)
 +
-- change alpha
 +
mask:setAlpha(2)
 +
-- Position the mask in the middle of the simulator
 +
mask:setPosition(application:getLogicalHeight()/2,application:getLogicalWidth()/2)
 +
-- Set its speed and direction values
 +
mask.speedX = 2
 +
mask.speedY = 1
 +
mask.dirX=1
 +
mask.dirY=1
 +
--Add the mask to the stage
 +
stage:addChild(mask)
 +
 
 +
--Now load the logo image
 +
local logo = Bitmap.new(Texture.new("images/carmo.png"))
 +
-- Set its anchor point to the center
 +
logo:setAnchorPoint(0.5, 0.5)
 +
-- Scale it down to a half
 +
logo:setScale(10)
 +
-- Position the logo in the middle of the simulator
 +
logo:setPosition(application:getLogicalHeight()/2,application:getLogicalWidth()/2)
 +
--Now set the blend mode
 +
logo:setBlendMode(Sprite.MULTIPLY)
 +
-- Add the logo to the stage
 +
stage:addChild(logo)
 +
 
 +
-- Set the background color of the stage to black (you can experiment)
 +
stage:setBackgroundColor(0.07, 0.07, 0.07) -- r, g, b
 +
 
 +
-- Define the onEnterFrame event function. This deals with the positioning of the mask
 +
function onEnterFrame(event)
 +
-- Get the position of the maks
 +
local x, y = mask:getPosition()
 +
-- Calculate the new position
 +
x += (mask.speedX * mask.dirX)
 +
y += (mask.speedY * mask.dirY)
 +
-- Check if the mask reaches the edges of the canvas and then mirror its movement.
 +
if x < 0 then mask.dirX = 1 end
 +
if x > application:getLogicalHeight() then mask.dirX = -1 end
 +
if y < 0 then mask.dirY = 1 end
 +
if y > application:getLogicalWidth() then mask.dirY = -1 end
 +
-- Set the new position of the mask
 +
mask:setPosition(x,y)
 +
end
 +
 
 +
-- Register an enterFrame event handler function
 +
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
 +
</syntaxhighlight>
 +
 
 +
{{Sprite}}

Latest revision as of 00:34, 2 December 2023

Available since: Gideros 2011.6
Class: Sprite

Description

Sets the blend mode of the sprite.

Sprite:setBlendMode(blendMode or src,dst)

This method can be called with 1 or 2 parameters.

If one parameter is passed it must be one of the following blending modes:

  • Sprite.ALPHA = "alpha"
  • Sprite.NO_ALPHA = "noAlpha"
  • Sprite.ADD = "add"
  • Sprite.MULTIPLY = "multiply"
  • Sprite.SCREEN = "screen"

If a Sprite object doesn't set any blending mode, it takes the blending mode from its parent sprite.

Note: the following two lines are completely the same:

sprite:setBlendMode("add")
sprite:setBlendMode(Sprite.ADD)

It's a matter of taste which one to choose.


If two parameters are passed to this method, then a source and destination blend can be set (in that order) and each takes the values:

  • Sprite.ZERO = "zero"
  • Sprite.ONE = "one"
  • Sprite.SRC_COLOR = "srcColor"
  • Sprite.ONE_MINUS_SRC_COLOR = "oneMinusSrcColor"
  • Sprite.DST_COLOR = "dstColor"
  • Sprite.ONE_MINUS_DST_COLOR = "oneMinusDstColor"
  • Sprite.SRC_ALPHA = "srcAlpha"
  • Sprite.ONE_MINUS_SRC_ALPHA = "oneMinusSrcAlpha"
  • Sprite.DST_ALPHA = "dstAlpha"
  • Sprite.ONE_MINUS_DST_ALPHA = "oneMinusDstAlpha"
  • Sprite.SRC_ALPHA_SATURATE = "srcAlphaSaturate"

Parameters

blendMode or src: (String) if one parameter is used, this is the blend mode. If two parameters are used this is the source blend specification
dst: (String) destination blend mode optional

Examples

All blend modes

for x = 1, 11 do
	for y = 1, 11 do
		local top = Pixel.new(topTex, s, s)
		local btm = Pixel.new(btmTex, s, s)
		stage:addChild(btm)
		btm:addChild(top)
		btm:setBlendMode(x, y)
		btm:setPosition(lh + s * (x-1), lh + s * (y-1))
	end
end

Spot light

note: you have to provide your own asset
--[[
    Script:  spotlight
	Description: Show how to create an animated spot light of the Gideros logo
	Author:  Michael Hartlef
	Contact: mike@fantomgl.com
--]]

-- Load the mask images (can be a gradient or a plain image)
--local mask = Bitmap.new(Texture.new("images/maskCircle2.png")) -- semi-transparent
local mask = Bitmap.new(Texture.new("images/carmo.png")) -- plain image
-- Set its anchor point in the center
mask:setAnchorPoint(0.5, 0.5)
-- Scale it up by a factor of 3
mask:setScale(3)
-- change alpha
mask:setAlpha(2)
-- Position the mask in the middle of the simulator
mask:setPosition(application:getLogicalHeight()/2,application:getLogicalWidth()/2)
-- Set its speed and direction values
mask.speedX = 2
mask.speedY = 1
mask.dirX=1
mask.dirY=1
--Add the mask to the stage
stage:addChild(mask)

--Now load the logo image
local logo = Bitmap.new(Texture.new("images/carmo.png"))
-- Set its anchor point to the center
logo:setAnchorPoint(0.5, 0.5)
-- Scale it down to a half
logo:setScale(10)
-- Position the logo in the middle of the simulator
logo:setPosition(application:getLogicalHeight()/2,application:getLogicalWidth()/2)
--Now set the blend mode
logo:setBlendMode(Sprite.MULTIPLY)
-- Add the logo to the stage
stage:addChild(logo)

-- Set the background color of the stage to black (you can experiment)
stage:setBackgroundColor(0.07, 0.07, 0.07) -- r, g, b

-- Define the onEnterFrame event function. This deals with the positioning of the mask
function onEnterFrame(event)
	-- Get the position of the maks
	local x, y = mask:getPosition()
	-- Calculate the new position
	x += (mask.speedX * mask.dirX)
	y += (mask.speedY * mask.dirY)
	-- Check if the mask reaches the edges of the canvas and then mirror its movement.
	if x < 0 then mask.dirX = 1 end
	if x > application:getLogicalHeight() then mask.dirX = -1 end
	if y < 0 then mask.dirY = 1 end
	if y > application:getLogicalWidth() then mask.dirY = -1 end
	-- Set the new position of the mask
	mask:setPosition(x,y)
end

-- Register an enterFrame event handler function
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)