Last active
June 2, 2021 11:28
-
-
Save watab0shi/8a905759d3f70ac877bc5c844fbba0ce to your computer and use it in GitHub Desktop.
openFrameworks ofShader with live reload
This file contains hidden or 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 "AutoReloadShader.h" | |
| //-------------------------------------------------------------- | |
| bool AutoReloadShader::load(const std::filesystem::path& _vertName, const std::filesystem::path& _fragName) { | |
| vertFile.open(_vertName); | |
| fragFile.open(_fragName); | |
| vertLastWriteTime = ""; | |
| fragLastWriteTime = ""; | |
| return shader.load(_vertName, _fragName); | |
| } | |
| //-------------------------------------------------------------- | |
| bool AutoReloadShader::needsUpdate() { | |
| bool needsUpdate = false; | |
| if (vertFile.exists()) { | |
| std::string vertWriteTime = ofToString(std::filesystem::last_write_time(vertFile.getAbsolutePath())); | |
| if (vertWriteTime != vertLastWriteTime) { | |
| needsUpdate = true; | |
| vertLastWriteTime = vertWriteTime; | |
| } | |
| } | |
| if (fragFile.exists()) { | |
| std::string fragWriteTime = ofToString(std::filesystem::last_write_time(fragFile.getAbsolutePath())); | |
| if (fragWriteTime != fragLastWriteTime) { | |
| needsUpdate = true; | |
| fragLastWriteTime = fragWriteTime; | |
| } | |
| } | |
| return needsUpdate; | |
| } | |
| //-------------------------------------------------------------- | |
| void AutoReloadShader::update() { | |
| if (needsUpdate()) { | |
| std::string vertPath = vertFile.path(); | |
| std::string fragPath = fragFile.path(); | |
| shader.load(vertPath, fragPath); | |
| ofLog() << "[AutoReloadShader::reload]" << vertPath << " " << fragPath; | |
| } | |
| } | |
| //-------------------------------------------------------------- | |
| void AutoReloadShader::begin() { | |
| shader.begin(); | |
| } | |
| //-------------------------------------------------------------- | |
| void AutoReloadShader::end() { | |
| shader.end(); | |
| } | |
| //-------------------------------------------------------------- | |
| ofShader& AutoReloadShader::getShader() { | |
| return shader; | |
| } |
This file contains hidden or 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 AutoReloadShader { | |
| public: | |
| bool load(const std::filesystem::path& vertName, const std::filesystem::path& fragName); | |
| bool needsUpdate(); | |
| void update(); | |
| void begin(); | |
| void end(); | |
| ofShader& getShader(); | |
| ofFile vertFile, fragFile; | |
| std::string vertLastWriteTime, fragLastWriteTime; | |
| ofShader shader; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment