LoginSignup
41
29

More than 5 years have passed since last update.

【Unityシェーダー】よく使うシェーダー関数はcgincを作ると便利

Last updated at Posted at 2016-02-24

※この記事で使っているUnityのバージョンは Unity5.2.1p3 です。 (OSはWindowsです)

はじめに

シェーダーを書いてると.shaderがついつい長くなってしまいます.
そこで、外部ファイルにシェーダー関数を定義して.shaderからこれを呼ぶ方法はないか調べてみました.

1 .cgincファイルを作る

Unity上の右クリックメニューからは.cgincを作ることができないみたいなのでエクスプローラー上で適当なファイルを作って拡張子をcgincに変更します.

image

hoge.cginc
float hogefunc()
{
    return 0.7;
}

2 .shaderで#includeする

.shaderの頭で #include"hoge.cginc" と記述します.
#include"UnityCG.cginc" の下に書いておけば大丈夫だと思います
image

3 完成

これでhoge.cgincの中で定義した関数が使えるようになりました.
.shaderの中でhogefunc()を呼ぶと正しく動きます.

参考

How can I make my own shader include file (.cginc or .glslinc)?
http://answers.unity3d.com/questions/7595/how-can-i-make-my-own-shader-include-file-cginc-or.html

41
29
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
41
29