Skip to content

Instantly share code, notes, and snippets.

@Megumi3
Last active October 9, 2015 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Megumi3/17ac611b5e3db993b8fc to your computer and use it in GitHub Desktop.
Save Megumi3/17ac611b5e3db993b8fc to your computer and use it in GitHub Desktop.
Shader "Custom/Shader" {
Properties {
// Inspectorから入力される値
_MaterialColor ("Color", Color) = (1,1,1,1)
_Sspec ("Specular Color", Color) = (1,1,1,1)
_Mgls ("Specular Index", Float) = 5
}
SubShader {
Pass {
Tags { "LightMode" = "ForwardBase" }
GLSLPROGRAM
#include "UnityCG.glslinc"
// 入射光の色
uniform vec4 _LightColor0;
// Propertiesで設定した値を受け取る
uniform vec4 _MaterialColor;
uniform vec4 _Sspec;
uniform float _Mgls;
// バーテックスシェーダーからフラッグメントシェーダーに渡す値を入れる
varying vec4 glVertexWorld;
varying vec3 surfaceNormal;
// バーテックスシェーダー
#ifdef VERTEX
void main() {
// 面法線
surfaceNormal = normalize((_Object2World * vec4(gl_Normal, 0.0)).xyz);
// 頂点情報ワールド空間に変換して入れておく
glVertexWorld = _Object2World * gl_Vertex;
// 座標変換
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#endif
// フラッグメントシェーダー
#ifdef FRAGMENT
void main() {
// 鏡面成分
// 入射光ベクトル
vec3 L = normalize(_WorldSpaceLightPos0.xyz);
// 視点方向ベクトル
vec3 V = normalize((vec4(_WorldSpaceCameraPos, 1.0) - glVertexWorld).xyz);
// Cspec = (V・R)^Mgls * Sspec * Mspec
vec3 specularReflection = pow(max(0.0, dot(reflect(-L, surfaceNormal), V)), _Mgls) * _LightColor0.xyz * _Sspec.xyz;
// 拡散成分
// Cdiff = (N・L)Sdiff * Mdiff
vec3 diffuseReflection = max(0.0, dot(surfaceNormal, L)) * _LightColor0.xyz * _MaterialColor.xyz;
// 環境成分
// Camb = Gamb * Mamb 
vec3 ambientLight = gl_LightModel.ambient.xyz * vec3(_MaterialColor);
// 色を出力
gl_FragColor = vec4(specularReflection + diffuseReflection + ambientLight, 1.0);
}
#endif
ENDGLSL
}
}
//FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment