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