LoginSignup
16
11

More than 5 years have passed since last update.

【Unity】すごくゆるいGLSL to HLSL変換

Last updated at Posted at 2017-06-23

glsl(glslsandboxのコードなど)から、hlslに変換し、Unity用で、フラグメントシェーダーのみのコードに変換し、shaderファイルを出力させるツールです。

ソースコード

環境

Mac Sierra 10.12
python2.7
Unity5.3.5f1

使い方

  1. ソースコード、DLもしくは、クローンする。
  2. Assets/Scripts/Shader/shader.txtを開き、変換したい、GLSLのシェーダーコードをコピペし、保存する。
  3. Macのターミナルで、「cd [DL/クローンしたディレクトリ]/Assets/Scripts/Shader/」 に移動する。
  4. 「python GLSLToHLSLConverter.py ファイル名(任意)」 で、shaderファイルを出力。
  5. ファイル名を記述した場合は、「Assets/Scripts/GLSLShader.cs」で設定している、シェーダー名を適切変更する。
  6. Main.unityを開き、動作を確認。

※glslsandboxのソースコードは、結構、入れ子になった書き方もあったりで、変換が難しいところは、特に手は入れていないので、shaderファイルを出力し、UnityEditor上で確認した際に、コンソールにエラーが吐かれると思いますが、最低限は変換してあるので、エラーが出た箇所を修正していけば、大抵は、動きますが、中には、エラーは出なくなったけど、真っ黒とか、真っ白とか、というのもあります、その際は、ソースコード、見直して、調整していけば、うまく、動くようになるかとは、思います。

検証したコード

以下は、実際に変換してみて、特にエラーなどでず、動作もだいたいGLSLで動いていたもので、一緒だったものです。

2Dエフェクト的なもの:
http://glslsandbox.com/e#31193.2
http://glslsandbox.com/e#40943.0
http://glslsandbox.com/e#40431.3
http://glslsandbox.com/e#40357.0
http://glslsandbox.com/e#29562.0
http://glslsandbox.com/e#22867.0
http://glslsandbox.com/e#22728.0

「ベクトル x 行列」の置換まで、対応できていないので、レイトレ的なものは、以下のような、回転行列のない、ものは、エラーなく、すぐUNITY側で、動作確認できます。

レイトレ:
http://glslsandbox.com/e#36565.0
http://glslsandbox.com/e#31380.0
http://glslsandbox.com/e#29454.9

HLSLコード変換の参考

https://alastaira.wordpress.com/2015/08/07/unity-shadertoys-a-k-a-converting-glsl-shaders-to-cghlsl/
https://docs.microsoft.com/ja-jp/windows/uwp/gaming/glsl-to-hlsl-reference
http://dench.flatlib.jp/opengl/glsl_hlsl

16
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
11