Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Chroma key / green screen — Gideros Forum

Chroma key / green screen

totebototebo Member
edited January 2017 in General questions
How feasible would chroma key be in Gideros? Ideally in real-time using camera. Second best take a photo with camera and key out a colour afterwards.
My Gideros games: www.totebo.com

Comments

  • hgy29hgy29 Maintainer
    Accepted Answer
    Possible using the camera plugin and a shader :)

    Likes: totebo

    +1 -1 (+1 / -0 )Share on Facebook
  • Nice one, I should have guessed!

    Likes: pie

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    edited January 2017 Accepted Answer
    For the record:
    require "camera"
     
    local ChromaKeyVShader=
    [[
    attribute vec4 POSITION0;
    attribute vec2 TEXCOORD0;
     
    uniform mat4 g_MVPMatrix;
     
    varying mediump vec2 texCoord;
     
    void main()
    {
    	gl_Position = g_MVPMatrix * POSITION0;
    	texCoord = TEXCOORD0;
    }
    ]]
     
    local ChromaKeyFShader=[[
    uniform lowp sampler2D gCam;
    uniform lowp sampler2D gTexture;
    varying mediump vec2 texCoord;
     
    #ifdef GLES2
    precision mediump float;
    #endif
     
    mediump vec3 rgb2hsv(vec3 c)
    {
        mediump vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
        mediump vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
        mediump vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
     
        mediump float d = q.x - min(q.w, q.y);
        mediump float e = 1.0e-10;
        return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
    }
     
    void main()
    {
    	lowp vec4 colorTex = texture2D(gTexture, texCoord);
    	lowp vec4 colorCam = texture2D(gCam, texCoord);
    	vec3 hsv=rgb2hsv(colorCam.rgb);
    	gl_FragColor=colorTex;
    	if ((hsv.x>0.7)||(hsv.x<0.5)||(hsv.y<0.5)||(hsv.z<0.1)) //Filter-out non blue-ish colors
    		gl_FragColor=colorCam;
    }
    ]]
     
    local ChromaKeyShaderAttrs=
    {
    {name="POSITION0",type=Shader.DFLOAT,mult=3,slot=0,offset=0},
    {name="vColor",type=Shader.DUBYTE,mult=0,slot=1,offset=0},
    {name="TEXCOORD0",type=Shader.DFLOAT,mult=2,slot=2,offset=0}
    }
     
    local ChromaKeyShaderConstants={
    {name="g_MVPMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WVP, vertex=true},
    {name="gCam",type=Shader.CTEXTURE,mult=1,vertex=false},
    {name="gTexture",type=Shader.CTEXTURE,mult=1,vertex=false},
    }
     
     
    local ChromaKeyShader= Shader.new(
    ChromaKeyVShader,ChromaKeyFShader,
    Shader.FLAG_FROM_CODE,ChromaKeyShaderConstants,ChromaKeyShaderAttrs) 
     
    local camTex=Texture.new(nil,1024,1024)
    Camera.start(camTex,application:getDeviceHeight(),application:getDeviceWidth())
    local scrw,scrh=application:getContentWidth(),application:getContentHeight()
    local camview=Mesh.new()
    camview:setVertexArray(0,0,scrw,0,scrw,scrh,0,scrh)
    camview:setTextureCoordinateArray(0,0,1024,0,1024,1024,0,1024)
    camview:setIndexArray(1,2,3,1,3,4)
    camview:setTexture(camTex)
    camview:setTexture(Texture.new("back.jpg",true),1)
    camview:setShader(ChromaKeyShader)
    stage:addChild(camview)
    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.