Difference between revisions of "Sprite:setShaderConstant"
From GiderosMobile
m (Text replacement - "</source>" to "</syntaxhighlight>") |
|||
Line 16: | Line 16: | ||
'''program type''': (int) the type of program this constant applies to '''optional'''<br/> | '''program type''': (int) the type of program this constant applies to '''optional'''<br/> | ||
'''program variant''': (int) the variant of program this constant applies to '''optional'''<br/> | '''program variant''': (int) the variant of program this constant applies to '''optional'''<br/> | ||
+ | |||
+ | === Example === | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | spriteOutline = {} | ||
+ | |||
+ | spriteOutline.VS_GL = [[ | ||
+ | |||
+ | uniform highp mat4 vMatrix; | ||
+ | |||
+ | attribute highp vec3 vVertex; | ||
+ | attribute mediump vec2 vTexCoord; | ||
+ | |||
+ | varying mediump vec2 fTexCoord; | ||
+ | |||
+ | void main() { | ||
+ | vec4 vertex = vec4(vVertex, 1.0); | ||
+ | gl_Position = vMatrix*vertex; | ||
+ | fTexCoord = vTexCoord; | ||
+ | } | ||
+ | ]] | ||
+ | |||
+ | spriteOutline.FS_GL = [[ | ||
+ | |||
+ | #ifdef GL_ES | ||
+ | precision highp float; | ||
+ | #endif | ||
+ | |||
+ | const float offseta = 0.010; | ||
+ | |||
+ | uniform lowp vec4 fColor; | ||
+ | uniform mediump vec4 fTexSize; | ||
+ | uniform lowp sampler2D fTexture; | ||
+ | |||
+ | uniform lowp int fSwitch; | ||
+ | |||
+ | varying mediump vec2 fTexCoord; | ||
+ | |||
+ | vec4 getSample(float x, float y, float offset, float num) | ||
+ | { | ||
+ | vec4 result = vec4(0); | ||
+ | float size = 360.0 / num; | ||
+ | |||
+ | for (float a; a <= 360.0; a += size) | ||
+ | { | ||
+ | vec2 coord = vec2(cos(a) * offset + x, sin(a) * offset + y); | ||
+ | |||
+ | result += texture2D(fTexture, coord); | ||
+ | } | ||
+ | |||
+ | return result; | ||
+ | } | ||
+ | |||
+ | void main() | ||
+ | { | ||
+ | vec4 col = texture2D(fTexture, fTexCoord); | ||
+ | |||
+ | float nums = 32.0; | ||
+ | |||
+ | if (fSwitch == 44) // fSwitch can be changed in code, 44 is some random value for documentation purposes | ||
+ | { | ||
+ | vec4 nb = getSample(fTexCoord.x, fTexCoord.y, 0.020, nums); | ||
+ | |||
+ | if (col.a > 0. && nb.a < nums) | ||
+ | { | ||
+ | col.a = nb.a / nums; | ||
+ | col.r = 0.0; | ||
+ | col.g = 0.0; | ||
+ | col.b = 0.0; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | gl_FragColor = col; | ||
+ | } | ||
+ | ]] | ||
+ | |||
+ | spriteOutline.Shader=Shader.new(spriteOutline.VS_GL, spriteOutline.FS_GL, Shader.FLAG_FROM_CODE, | ||
+ | { | ||
+ | {name="vMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WVP,vertex=true}, | ||
+ | {name="fColor",type=Shader.CFLOAT4,sys=Shader.SYS_COLOR,vertex=false}, | ||
+ | {name="fTexture",type=Shader.CTEXTURE,vertex=false}, | ||
+ | {name="fSwitch",type=Shader.CINT,vertex=false}, | ||
+ | }, | ||
+ | { | ||
+ | {name="vVertex",type=Shader.DFLOAT,mult=3,slot=0,offset=0}, | ||
+ | {name="vColor",type=Shader.DUBYTE,mult=4,slot=1,offset=0}, | ||
+ | {name="vTexCoord",type=Shader.DFLOAT,mult=2,slot=2,offset=0}, | ||
+ | } | ||
+ | ) | ||
+ | |||
+ | local sprite = Bitmap.new(Texture.new("gfx/test.png")) | ||
+ | sprite:setShader(spriteOutline.Shader) | ||
+ | sprite:setShaderConstant("fSwitch", Shader.CINT, 1, 44) -- set fSwitch value to be 44 | ||
+ | stage:addChild(sprite) | ||
+ | </syntaxhighlight> | ||
{{Sprite}} | {{Sprite}} |
Latest revision as of 04:03, 6 November 2023
Available since: Gideros 2017.4
Class: Sprite
Description
Changes the value of a uniform from lua.
Sprite:setShaderConstant(uniformName,dataType,mult,data,programType,programVariant)
Parameters
uniform name: (string) the uniform name to change
data type: (int) the type of data to set (one of the Shader.Cxxx constants)
mult: (number) number of elements of the given type to set
data: (varies) and the actual data to set, either as a table or as multiple arguments
program type: (int) the type of program this constant applies to optional
program variant: (int) the variant of program this constant applies to optional
Example
spriteOutline = {}
spriteOutline.VS_GL = [[
uniform highp mat4 vMatrix;
attribute highp vec3 vVertex;
attribute mediump vec2 vTexCoord;
varying mediump vec2 fTexCoord;
void main() {
vec4 vertex = vec4(vVertex, 1.0);
gl_Position = vMatrix*vertex;
fTexCoord = vTexCoord;
}
]]
spriteOutline.FS_GL = [[
#ifdef GL_ES
precision highp float;
#endif
const float offseta = 0.010;
uniform lowp vec4 fColor;
uniform mediump vec4 fTexSize;
uniform lowp sampler2D fTexture;
uniform lowp int fSwitch;
varying mediump vec2 fTexCoord;
vec4 getSample(float x, float y, float offset, float num)
{
vec4 result = vec4(0);
float size = 360.0 / num;
for (float a; a <= 360.0; a += size)
{
vec2 coord = vec2(cos(a) * offset + x, sin(a) * offset + y);
result += texture2D(fTexture, coord);
}
return result;
}
void main()
{
vec4 col = texture2D(fTexture, fTexCoord);
float nums = 32.0;
if (fSwitch == 44) // fSwitch can be changed in code, 44 is some random value for documentation purposes
{
vec4 nb = getSample(fTexCoord.x, fTexCoord.y, 0.020, nums);
if (col.a > 0. && nb.a < nums)
{
col.a = nb.a / nums;
col.r = 0.0;
col.g = 0.0;
col.b = 0.0;
}
}
gl_FragColor = col;
}
]]
spriteOutline.Shader=Shader.new(spriteOutline.VS_GL, spriteOutline.FS_GL, Shader.FLAG_FROM_CODE,
{
{name="vMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WVP,vertex=true},
{name="fColor",type=Shader.CFLOAT4,sys=Shader.SYS_COLOR,vertex=false},
{name="fTexture",type=Shader.CTEXTURE,vertex=false},
{name="fSwitch",type=Shader.CINT,vertex=false},
},
{
{name="vVertex",type=Shader.DFLOAT,mult=3,slot=0,offset=0},
{name="vColor",type=Shader.DUBYTE,mult=4,slot=1,offset=0},
{name="vTexCoord",type=Shader.DFLOAT,mult=2,slot=2,offset=0},
}
)
local sprite = Bitmap.new(Texture.new("gfx/test.png"))
sprite:setShader(spriteOutline.Shader)
sprite:setShaderConstant("fSwitch", Shader.CINT, 1, 44) -- set fSwitch value to be 44
stage:addChild(sprite)
- Sprite:addChild
- Sprite:addChildAt
- Sprite:clearBlendMode
- Sprite:clone
- Sprite:contains
- Sprite:get
- Sprite:getAlpha
- Sprite:getAnchorPoint
- Sprite:getAnchorPosition
- Sprite:getBounds
- Sprite:getChildAt
- Sprite:getChildIndex
- Sprite:getChildrenAtPoint
- Sprite:getClip
- Sprite:getColorTransform
- Sprite:getDrawCount
- Sprite:getHeight
- Sprite:getLayoutConstraints
- Sprite:getLayoutInfo
- Sprite:getLayoutParameters
- Sprite:getMatrix
- Sprite:getNumChildren
- Sprite:getParent
- Sprite:getPosition
- Sprite:getRotation
- Sprite:getRotationX
- Sprite:getRotationY
- Sprite:getScale
- Sprite:getScaleX
- Sprite:getScaleY
- Sprite:getScaleZ
- Sprite:getSize
- Sprite:getSkew
- Sprite:getSkewX
- Sprite:getSkewY
- Sprite:getWidth
- Sprite:getX
- Sprite:getY
- Sprite:getZ
- Sprite:globalToLocal
- Sprite:hitTestPoint
- Sprite:isVisible
- Sprite:localToGlobal
- Sprite:redrawEffects
- Sprite:removeChild
- Sprite:removeChildAt
- Sprite:removeFromParent
- Sprite:set
- Sprite:setAlpha
- Sprite:setAnchorPoint
- Sprite:setAnchorPosition
- Sprite:setBlendMode
- Sprite:setClip
- Sprite:setColorTransform
- Sprite:setEffectConstant
- Sprite:setEffectStack
- Sprite:setGhosts
- Sprite:setLayoutConstraints
- Sprite:setLayoutParameters
- Sprite:setMatrix
- Sprite:setPosition
- Sprite:setRotation
- Sprite:setRotationX
- Sprite:setRotationY
- Sprite:setScale
- Sprite:setScaleX
- Sprite:setScaleY
- Sprite:setScaleZ
- Sprite:setShader
- Sprite:setShaderConstant
- Sprite:setSkew
- Sprite:setSkewX
- Sprite:setSkewY
- Sprite:setStencilOperation
- Sprite:setStopEventPropagation
- Sprite:setVisible
- Sprite:setX
- Sprite:setY
- Sprite:setZ
- Sprite:spriteToLocal
- Sprite:swapChildren
- Sprite:swapChildrenAt
- Sprite.ADD
- Sprite.ALPHA
- Sprite.EFFECT MODE AUTOMATIC
- Sprite.EFFECT MODE CONTINUOUS
- Sprite.EFFECT MODE TRIGGERED
- Sprite.LAYOUT ANCHOR CENTER
- Sprite.LAYOUT ANCHOR EAST
- Sprite.LAYOUT ANCHOR NORTH
- Sprite.LAYOUT ANCHOR NORTHEAST
- Sprite.LAYOUT ANCHOR NORTHWEST
- Sprite.LAYOUT ANCHOR SOUTH
- Sprite.LAYOUT ANCHOR SOUTHEAST
- Sprite.LAYOUT ANCHOR SOUTHWEST
- Sprite.LAYOUT ANCHOR WEST
- Sprite.LAYOUT FILL BOTH
- Sprite.LAYOUT FILL HORIZONTAL
- Sprite.LAYOUT FILL NONE
- Sprite.LAYOUT FILL VERTICAL
- Sprite.MULTIPLY
- Sprite.NO ALPHA
- Sprite.SCREEN
- Sprite.new
- Event.ADDED_TO_STAGE
- Event.ENTER_FRAME
- Event.KEY_CHAR
- Event.KEY_DOWN
- Event.KEY_UP
- Event.LAYOUT_RESIZED
- Event.MOUSE_DOWN
- Event.MOUSE_ENTER
- Event.MOUSE_HOVER
- Event.MOUSE_LEAVE
- Event.MOUSE_MOVE
- Event.MOUSE_UP
- Event.MOUSE_WHEEL
- Event.REMOVED_FROM_STAGE
- Event.TOUCHES_BEGIN
- Event.TOUCHES_CANCEL
- Event.TOUCHES_END
- Event.TOUCHES_MOVE