OSDN Git Service

add Savable support
[mikumikustudio/MikuMikuStudio.git] / src / MatDefs / pmd / pmd.vert
1 // #import "MatDefs/pmd/Skinning.glsllib"\r
2 #ifdef USE_HWSKINNING\r
3 uniform mat4 m_BoneMatrices[NUM_BONES];\r
4 #endif\r
5 #define ATTENUATION\r
6 // #define HQ_ATTENUATION\r
7 \r
8 uniform mat4 g_WorldViewProjectionMatrix;\r
9 uniform mat4 g_WorldViewMatrix;\r
10 uniform mat3 g_NormalMatrix;\r
11 uniform mat4 g_ViewMatrix;\r
12 \r
13 uniform vec4 m_Ambient;\r
14 uniform vec4 m_Diffuse;\r
15 uniform vec4 m_Specular;\r
16 uniform float m_Shininess;\r
17 \r
18 uniform vec4 g_LightColor;\r
19 uniform vec4 g_LightPosition;\r
20 uniform vec4 g_AmbientLightColor;\r
21 \r
22 varying vec2 texCoord;\r
23 \r
24 varying vec4 AmbientSum;\r
25 varying vec4 DiffuseSum;\r
26 varying vec4 SpecularSum;\r
27 \r
28 attribute vec3 inPosition;\r
29 attribute vec2 inTexCoord;\r
30 attribute vec3 inNormal;\r
31 \r
32 // uniform Sampler2D m_BoneParameter;\r
33 // uniform sampler2D m_BoneParameter;\r
34 \r
35 #ifdef HQ_ATTENUATION\r
36   varying vec3 lightVec;\r
37 #endif\r
38 \r
39 #ifdef VERTEX_COLOR\r
40   attribute vec4 inColor;\r
41 #endif\r
42 \r
43 #ifndef VERTEX_LIGHTING\r
44   attribute vec4 inTangent;\r
45 \r
46   #ifndef NORMALMAP\r
47     varying vec3 vNormal;\r
48   #endif\r
49   varying vec3 vPosition;\r
50   varying vec3 vViewDir;\r
51   varying vec4 vLightDir;\r
52 #endif\r
53 \r
54 // #ifdef USE_REFLECTION\r
55     uniform vec3 g_CameraPosition;\r
56     uniform mat4 g_WorldMatrix;\r
57 \r
58     uniform vec3 m_FresnelParams;\r
59     varying vec4 refVec;\r
60 \r
61 \r
62     /**\r
63      * Input:\r
64      * attribute inPosition\r
65      * attribute inNormal\r
66      * uniform g_WorldMatrix\r
67      * uniform g_CameraPosition\r
68      *\r
69      * Output:\r
70      * varying refVec\r
71      */\r
72     void computeRef(in vec4 position, in vec4 normal){\r
73         vec3 worldPos = (g_WorldMatrix * vec4(normalize(position.xyz),1.0)).xyz;\r
74 \r
75         vec3 I = normalize( g_CameraPosition - worldPos  ).xyz;\r
76         vec3 N = normalize( (g_WorldMatrix * vec4(normalize(normal.xyz), 0.0)).xyz );\r
77 \r
78         refVec.xyz = reflect(I, N);\r
79         refVec.w   = 1.0;//m_FresnelParams.x + m_FresnelParams.y * pow(1.0 + dot(I, N), m_FresnelParams.z);\r
80     }\r
81 // #endif\r
82 \r
83 // JME3 lights in world space\r
84 void lightComputeDir(in vec3 worldPos, in vec4 color, in vec4 position, out vec4 lightDir){\r
85     float posLight = step(0.5, color.w);\r
86     vec3 tempVec = position.xyz * sign(posLight - 0.5) - (worldPos * posLight);\r
87     #ifdef ATTENUATION\r
88      float dist = length(tempVec);\r
89      lightDir.w = clamp(1.0 - position.w * dist * posLight, 0.0, 1.0);\r
90      lightDir.xyz = tempVec / vec3(dist);\r
91      #ifdef HQ_ATTENUATION\r
92        lightVec = tempVec;\r
93      #endif\r
94     #else\r
95      lightDir = vec4(normalize(tempVec), 1.0);\r
96     #endif\r
97 }\r
98 \r
99 #ifdef VERTEX_LIGHTING\r
100   float lightComputeDiffuse(in vec3 norm, in vec3 lightdir){\r
101       return max(0.0, dot(norm, lightdir));\r
102   }\r
103 \r
104   float lightComputeSpecular(in vec3 norm, in vec3 viewdir, in vec3 lightdir, in float shiny){\r
105       #ifndef LOW_QUALITY\r
106         vec3 H = (viewdir + lightdir) * vec3(0.5);\r
107         return pow(max(dot(H, norm), 0.0), shiny);\r
108       #else\r
109         return 0.0;\r
110       #endif\r
111   }\r
112 \r
113 vec2 computeLighting(in vec3 wvPos, in vec3 wvNorm, in vec3 wvViewDir, in vec4 wvLightPos){\r
114      vec4 lightDir;\r
115      lightComputeDir(wvPos, g_LightColor, wvLightPos, lightDir);\r
116 \r
117      float diffuseFactor = lightComputeDiffuse(wvNorm, lightDir.xyz);\r
118      float specularFactor = lightComputeSpecular(wvNorm, wvViewDir, lightDir.xyz, m_Shininess);\r
119      //specularFactor *= step(0.01, diffuseFactor);\r
120      return vec2(diffuseFactor, specularFactor) * vec2(lightDir.w);\r
121   }\r
122 #endif\r
123 attribute vec4 inBoneWeight;\r
124 attribute vec4 inBoneIndices;\r
125 attribute vec4 inBoneIndex;\r
126 #ifdef USE_HWSKINNING\r
127 void Skinning_Compute(inout vec4 position, inout vec4 normal){\r
128 //    vec4 index  = inBoneIndices;\r
129     vec4 index  = inBoneIndex;\r
130     vec4 weight = inBoneWeight;\r
131 \r
132     vec4 newPos    = vec4(0.0,0.0,0.0,0.0);\r
133     vec4 newNormal = vec4(0.0,0.0,0.0,0.0);\r
134 \r
135     //for (float i = 1.0; i < 2.0; i += 1.0){\r
136         mat4 skinMat = m_BoneMatrices[int(index.x)];\r
137         newPos    = weight.x * (skinMat * position);\r
138         newNormal = weight.x * (skinMat * normal);\r
139         //index = index.yzwx;\r
140         //weight = weight.yzwx;\r
141         skinMat = m_BoneMatrices[int(index.y)];\r
142         newPos    = newPos + weight.y * (skinMat * position);\r
143         newNormal = newNormal + weight.y * (skinMat * normal);\r
144     //}\r
145 \r
146     position = newPos;\r
147     normal = newNormal;\r
148 }\r
149 #endif\r
150 void main(){\r
151    vec4 pos = vec4(inPosition, 1.0);\r
152    vec4 normal = vec4(inNormal,0.0);\r
153 #ifdef USE_HWSKINNING\r
154    Skinning_Compute(pos, normal);\r
155 #endif\r
156 //    pos = m_BoneMatrices[0] * pos;\r
157    gl_Position = g_WorldViewProjectionMatrix * pos;\r
158    texCoord = inTexCoord;\r
159 \r
160    vec3 wvPosition = (g_WorldViewMatrix * pos).xyz;\r
161    vec3 wvNormal  = normalize(g_NormalMatrix * normal.xyz);\r
162 \r
163 //   vec3 wvPosition = (g_WorldViewMatrix * pos).xyz;\r
164 //   vec3 wvNormal  = normalize(g_NormalMatrix * inNormal);\r
165 \r
166    vec3 viewDir = normalize(-wvPosition);\r
167 \r
168        //vec4 lightColor = g_LightColor[gl_InstanceID];\r
169        //vec4 lightPos   = g_LightPosition[gl_InstanceID];\r
170        //vec4 wvLightPos = (g_ViewMatrix * vec4(lightPos.xyz, lightColor.w));\r
171        //wvLightPos.w = lightPos.w;\r
172 \r
173    vec4 wvLightPos = (g_ViewMatrix * vec4(g_LightPosition.xyz, g_LightColor.w));\r
174    wvLightPos.w = g_LightPosition.w;\r
175    vec4 lightColor = g_LightColor;\r
176 \r
177    #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)\r
178      vec3 wvTangent = normalize(g_NormalMatrix * inTangent.xyz);\r
179      vec3 wvBinormal = cross(wvNormal, wvTangent);\r
180 \r
181      mat3 tbnMat = mat3(wvTangent, wvBinormal * -inTangent.w,wvNormal);\r
182      \r
183      vPosition = wvPosition * tbnMat;\r
184      vViewDir  = viewDir * tbnMat;\r
185      lightComputeDir(wvPosition, lightColor, wvLightPos, vLightDir);\r
186      vLightDir.xyz = (vLightDir.xyz * tbnMat).xyz;\r
187    #elif !defined(VERTEX_LIGHTING)\r
188      vNormal = wvNormal;\r
189 \r
190      vPosition = wvPosition;\r
191      vViewDir = viewDir;\r
192 \r
193      lightComputeDir(wvPosition, lightColor, wvLightPos, vLightDir);\r
194 \r
195      #ifdef V_TANGENT\r
196         vNormal = normalize(g_NormalMatrix * inTangent.xyz);\r
197         vNormal = -cross(cross(vLightDir.xyz, vNormal), vNormal);\r
198      #endif\r
199    #endif\r
200 \r
201    lightColor.w = 1.0;\r
202    #ifdef MATERIAL_COLORS\r
203       AmbientSum  = m_Ambient  * g_AmbientLightColor;\r
204       DiffuseSum  = m_Diffuse  * lightColor;\r
205       SpecularSum = m_Specular * lightColor;\r
206     #else\r
207       AmbientSum  = vec4(0.2, 0.2, 0.2, 1.0) * g_AmbientLightColor; // Default: ambient color is dark gray\r
208       DiffuseSum  = lightColor;\r
209       SpecularSum = lightColor;\r
210     #endif\r
211 \r
212     #ifdef VERTEX_COLOR\r
213       AmbientSum *= inColor;\r
214       DiffuseSum *= inColor;\r
215     #endif\r
216 \r
217     #ifdef VERTEX_LIGHTING\r
218        vec2 light = computeLighting(wvPosition, wvNormal, viewDir, wvLightPos);\r
219 \r
220        AmbientSum.a  = light.x;\r
221        SpecularSum.a = light.y * 0.3;\r
222     #endif\r
223 \r
224     #if defined(USE_REFLECTION) || defined(SPHERE_MAP_A) || defined(SPHERE_MAP_H)\r
225         computeRef(pos,normal);\r
226     #endif \r
227 }\r