Created
February 25, 2016 04:20
-
-
Save tado/c1c8f7c80e0a1c85338b to your computer and use it in GitHub Desktop.
openFrameworks + GLSLでクロマキー合成 ref: http://qiita.com/yoppa/items/602f8b5a48a3cc6777cd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 120 | |
uniform sampler2DRect tex0; // ソースのテクスチャ | |
uniform float threshold; // 閾値 | |
uniform vec3 chromaKeyColor; // キーの色 | |
varying vec2 texCoordVarying; | |
void main() | |
{ | |
// テクスチャーの色を取得 | |
vec4 texel0 = texture2DRect(tex0, texCoordVarying); | |
// キーの色との差分を計算 | |
float diff = length(chromaKeyColor - texel0.rgb); | |
if(diff < threshold){ | |
// もしキーの色より差分が少なかったら描画しない | |
discard; | |
}else{ | |
// キーの色より差分が多かったら、そのまま描画 | |
gl_FragColor = texel0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
class ofApp : public ofBaseApp{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
ofShader shader; // クロマキー合成Shader | |
ofImage image; // ソースのイメージ | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
void ofApp::setup(){ | |
image.load("source.jpg"); // ソース画像 | |
shader.load("shader/chromaKey"); // クロマキー合成GLSL | |
} | |
void ofApp::update(){ | |
} | |
void ofApp::draw(){ | |
ofBackgroundGradient(ofColor(128), ofColor(31), OF_GRADIENT_LINEAR); | |
// マウスのX座標で閾値を設定 | |
float threshold = ofMap(mouseX, 0, ofGetWidth(), 0.0, 2.0); | |
shader.begin(); | |
// キーの色を設定(green) | |
shader.setUniform3f("chromaKeyColor", ofVec3f(0.0, 1.0, 0.0)); | |
// 閾値設定 | |
shader.setUniform1f("threshold", threshold); | |
// クロマキー合成する画像を描画 | |
image.draw(0, 0); | |
shader.end(); | |
ofDrawBitmapString("difference = " + ofToString(difference), 20, 20); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
class ofApp : public ofBaseApp{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
ofShader shader; | |
ofVideoPlayer video; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
void ofApp::setup(){ | |
video.load("source.mp4"); | |
video.play(); | |
shader.load("shader/chromaKey"); | |
} | |
void ofApp::update(){ | |
video.update(); | |
} | |
void ofApp::draw(){ | |
ofBackgroundGradient(ofColor(128), ofColor(31), OF_GRADIENT_LINEAR); | |
float threshold = ofMap(mouseX, 0, ofGetWidth(), 0.0, 2.0); | |
shader.begin(); | |
shader.setUniform3f("chromaKeyColor", ofVec3f(0.0, 1.0, 0.0)); | |
shader.setUniform1f("threshold", threshold); | |
video.draw(0, 0); | |
shader.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment