Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.66 KB | None | 0 0
  1. <jittershader name="jit.gl.material.jxs">
  2. <description>
  3. material's system code generated JXS file
  4. </description>
  5. <param name="heightmap" type="int" default="0"/>
  6. <param name="diffuse" type="int" default="0"/>
  7. <param name="jit_Tangent" type="vec3" state="TANGENT" />
  8.  
  9. <language name="glsl" version="1.0">
  10. <bind param="heightmap" program="vs" />
  11. <bind param="diffuse" program="fs" />
  12. <bind param="jit_Tangent" program="vs" />
  13. <program name="vs" type="vertex">
  14. <![CDATA[
  15. #version 120
  16. vec3 filter_normal(vec2 uv, float texelsize, sampler2D tex) {
  17. float h0 = texture2D(tex, uv + texelsize * vec2( 0,-1)).r;
  18. float h1 = texture2D(tex, uv + texelsize * vec2(-1, 0)).r;
  19. float h2 = texture2D(tex, uv + texelsize * vec2( 1, 0)).r;
  20. float h3 = texture2D(tex, uv + texelsize * vec2( 0, 1)).r;
  21. vec2 step = vec2(1.0, 0.0);
  22. vec3 va = normalize( vec3(step.xy, h1 - h0) );
  23. vec3 vb = normalize( vec3(step.yx, h3 - h2) );
  24. return cross(va,vb);
  25. }
  26. vec4 sample_vertex(vec2 uv, sampler2D tex, vec3 normal) {
  27. float height = texture2D(tex, uv).r;
  28. vec4 vert = gl_ModelViewMatrix * gl_Vertex;
  29. return vert + normalize(vec4(normal, 1.0)) * height;
  30. }
  31.  
  32.  
  33. attribute vec3 jit_Tangent;
  34. uniform sampler2D heightmap;
  35. varying vec4 jit_Surface_position;
  36. varying vec3 jit_Surface_tangent;
  37. varying vec2 jit_Surface_texcoord0;
  38. varying vec2 jit_Surface_texcoord1;
  39. varying vec3 jit_Surface_normal;
  40. varying vec3 jit_Surface_filtered_normal;
  41. varying vec3 jit_Surface_bitangent;
  42.  
  43. void main() {
  44. jit_Surface_normal = gl_NormalMatrix*gl_Normal;
  45. jit_Surface_texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);
  46. jit_Surface_texcoord1 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0);
  47. jit_Surface_tangent = gl_NormalMatrix*jit_Tangent;
  48. jit_Surface_filtered_normal = filter_normal(jit_Surface_texcoord1, 0.001, heightmap);
  49. jit_Surface_bitangent = cross(jit_Surface_normal, jit_Surface_tangent);
  50. vec4 pos = sample_vertex(jit_Surface_texcoord1, heightmap, jit_Surface_normal);
  51. gl_Position = gl_ProjectionMatrix * pos;
  52. jit_Surface_position = gl_ModelViewMatrix * pos;
  53. }
  54. ]]>
  55. </program>
  56. <program name="fs" type="fragment">
  57. <![CDATA[
  58.  
  59. #version 120
  60. #define PI (3.1415926535898)
  61. #define jit_LightModel gl_LightModel
  62. struct Material{
  63. vec4 color;
  64. };
  65. struct Light{
  66. vec4 ambient;
  67. vec4 diffuse;
  68. vec4 specular;
  69. vec3 toEyePosition;
  70. };
  71.  
  72.  
  73. ///////////////////////////////////
  74. // Diffuse Shading Models
  75.  
  76. // aka Lambertian
  77. float lambertian(vec3 Nn, vec3 L) {
  78. return max(dot(Nn, L), 0.);
  79. }
  80. float oren_nayer(vec3 Vn, vec3 Nn, vec3 L, float roughness) {
  81. float roughness2 = roughness*roughness;
  82. float A = 1. - (0.5*roughness2)/(roughness2+0.33);
  83. float B = (0.45*roughness2)/(roughness2+0.09);
  84. float VdotN = dot(Vn, Nn);
  85. // reflected (between vertex normal and view direction)
  86. float LdotN = dot(L, Nn);
  87. // incident (between vertex normal and light direction
  88. float irradiance = max(0., LdotN);
  89. // clamp incident
  90. float cos_delta_angle = max(0., dot(normalize(Vn-Nn*VdotN), normalize(L-Nn*LdotN)));
  91. float theta_r = acos(VdotN);
  92. float theta_i = acos(LdotN);
  93. float alpha = max(theta_i, theta_r);
  94. float beta = min(theta_i, theta_r);
  95. float profile = sin(alpha) * tan(beta);
  96. return (A+B*cos_delta_angle*profile)*irradiance;
  97. }
  98. float oren_nayer_lut(sampler2D ONtex, vec3 Vn, vec3 Nn, vec3 L, float roughness) {
  99. float roughness2 = roughness*roughness;
  100. float A = 1. - (0.5*roughness2)/(roughness2+0.33);
  101. float B = (0.45*roughness2)/(roughness2+0.09);
  102. float VdotN = dot(Vn, Nn);
  103. // reflected (between vertex normal and view direction)
  104. float LdotN = dot(L, Nn);
  105. // incident (between vertex normal and light direction
  106. float irradiance = max(0., LdotN);
  107. // clamp incident
  108. float cos_delta_angle = max(0., dot(normalize(Vn-Nn*VdotN), normalize(L-Nn*LdotN)));
  109. float profile = texture2D(ONtex, vec2(VdotN, LdotN)*0.5+0.5).r;
  110. return (A+B*cos_delta_angle*profile)*irradiance;
  111. }
  112. float toon(vec3 Nn, vec3 L, float size, float smoothfactor) {
  113. float v = dot(Nn, L);
  114. float thresh = 1.-size;
  115. return smoothstep(thresh-0.5*smoothfactor, thresh+0.5*smoothfactor, v);
  116. }
  117. float minnaert(vec3 Vn, vec3 Nn, vec3 L, float darkness) {
  118. float NL = max(dot(Nn, L), 0.);
  119. float NV = max(dot(Nn, Vn), 0.);
  120. return pow(NL, darkness+1.)*pow(1.-NV, 1.-darkness);
  121. }
  122.  
  123.  
  124. ///////////////////////////////////
  125. // Specular Shading Models
  126.  
  127. // aka Blinn (really Blinn-Phong)
  128. float blinn(vec3 Vn, vec3 Nn, vec3 L, float Ns) {
  129. vec3 H = normalize(L + Vn);
  130. return pow(max(dot(Nn, H), 0.), Ns);
  131. }
  132.  
  133. // aka Phong
  134. float specular_phong(vec3 Vn, vec3 Nn, vec3 L, float Ns) {
  135. vec3 R = reflect(-L, Nn);
  136. return pow(max(dot(R, Vn), 0.), Ns);
  137. }
  138. float toonspecular(vec3 Vn, vec3 Nn, vec3 L, float Ns, float size, float smoothfactor) {
  139. vec3 H = normalize(L + Vn);
  140. float v = pow(max(dot(Nn, H), 0.), Ns);
  141. float thresh = 1.-size;
  142. return smoothstep(thresh-0.5*smoothfactor, thresh+0.5*smoothfactor, v);
  143. }
  144.  
  145. // Ward Isotropic model
  146. float ward(vec3 Vn, vec3 Nn, vec3 L, float rms) {
  147. vec3 H = normalize(Vn + L);
  148. float NH = max(dot(Nn, H), 0.);
  149. float NV = max(dot(Nn, Vn), 0.);
  150. float NL = max(dot(Nn, L), 0.);
  151. float alpha = max(rms, 0.001);
  152. // prevent divide-by 0
  153. float angle = acos(NH);
  154. float angle2 = angle*angle;
  155. float alpha2 = alpha*alpha;
  156. return NL * 0.25 * (1.0/alpha2) * exp(-2.*angle2/alpha2);
  157. }
  158. float cook_torrance(vec3 Vn, vec3 Nn, vec3 L, float rms) {
  159. vec3 H = normalize(Vn + L);
  160. float NH = max(dot(Nn, H), 0.);
  161. float VH = max(dot(Vn, H), 0.);
  162. float NV = max(dot(Nn, Vn), 0.);
  163. float NL = max(dot(Nn, L), 0.);
  164. float factor = min(NV, NL);
  165. float G = min(1., 2.*NH*factor/VH);
  166. float alpha = acos(NH);
  167. float exponent = alpha/rms;
  168. float D = exp(-exponent*exponent);
  169. return D*G/(0.001+NV);
  170. }
  171. vec2 sphere_map(vec3 v) {
  172. float m = 2.*sqrt(v.x*v.x + v.y*v.y + (v.z+1.)*(v.z+1.));
  173. return vec2(v)/m+0.5;
  174. }
  175. vec4 fog_linear(vec4 color, float dist) {
  176. float fogfactor = (gl_Fog.end-dist)*gl_Fog.scale;
  177. fogfactor = clamp(fogfactor, 0., 1.);
  178. return mix(gl_Fog.color, color, fogfactor);
  179. }
  180. vec4 fog_exp(vec4 color, float dist) {
  181. float fogfactor = exp(-gl_Fog.density * dist);
  182. fogfactor = clamp(fogfactor, 0., 1.);
  183. return mix(gl_Fog.color, color, fogfactor);
  184. }
  185. vec4 fog_exp2(vec4 color, float dist) {
  186. float fogfactor = exp(-gl_Fog.density*gl_Fog.density * dist*dist);
  187. fogfactor = clamp(fogfactor, 0., 1.);
  188. return mix(gl_Fog.color, color, fogfactor);
  189. }
  190.  
  191. // Shadows
  192. vec4 btex2D(sampler2D map, vec2 uv, float radius, float steps) {
  193. float stepSize = 2.0 * radius / steps;
  194. uv.xy -= vec2(radius,radius);
  195. vec4 total = vec4(0, 0, 0, 0);
  196. for (int x = 0; x < steps; ++x)
  197. for (int y = 0; y < steps; ++y)
  198. total += texture2D(map, vec2(uv.xy + vec2(x * stepSize, y * stepSize)));
  199. return total / (steps * steps);
  200. }
  201. float compute_shadow(sampler2D shadowMap, vec4 shadowMapPos, float ourDepth, vec3 param) {
  202. // param.x = radius
  203. // param.y = epsilon
  204. // param.z = min clamp
  205. vec2 suv = (shadowMapPos.xy / shadowMapPos.w) * 0.5 + 0.5;
  206. vec2 moments = btex2D(shadowMap, suv, param.x, 4).xy;
  207. float litFactor = (ourDepth <= moments.x ? 1 : 0);
  208. float variance = clamp((moments.y - (moments.x*moments.x)), param.y, 1.0);
  209. float m_d = moments.x - ourDepth;
  210. float p = variance / (variance + m_d * m_d);
  211. return smoothstep(param.z, 1.0, max(litFactor, p));
  212. }
  213.  
  214. Material jit_Material;
  215. Light jit_Light[8];
  216.  
  217.  
  218. uniform samplerJit0 diffuse;
  219. varying vec4 jit_Surface_position;
  220. varying vec3 jit_Surface_tangent;
  221. varying vec2 jit_Surface_texcoord0;
  222. varying vec2 jit_Surface_texcoord1;
  223. varying vec3 jit_Surface_normal;
  224. varying vec3 jit_Surface_filtered_normal;
  225. varying vec3 jit_Surface_bitangent;
  226.  
  227. void main() {
  228. jit_Material.color = vec4(0., 0., 0., 0.);
  229. vec3 jit_Surface_view = -vec3(jit_Surface_position);
  230. vec3 Vn = normalize(jit_Surface_view);
  231. vec3 Tb = normalize(jit_Surface_tangent);
  232. vec3 Bb = normalize(jit_Surface_bitangent);
  233. vec3 Nb = normalize(jit_Surface_normal);
  234. mat3 local = mat3(Tb, Bb, Nb);
  235. vec3 Nn = normalize(jit_Surface_filtered_normal);
  236. Nn = local*Nn;
  237. vec4 diffuse_tex = textureJit0(diffuse, jit_Surface_texcoord0);
  238. jit_Light[0].toEyePosition = normalize(jit_Surface_position.xyz - gl_LightSource[0].position.xyz);
  239. float d6 = length(jit_Surface_position.xyz-gl_LightSource[0].position.xyz);
  240. float atten7 = gl_LightSource[0].constantAttenuation+gl_LightSource[0].linearAttenuation*d6+gl_LightSource[0].quadraticAttenuation*d6*d6;
  241. atten7 = 1.0/atten7;
  242. jit_Light[0].ambient = gl_LightSource[0].ambient*atten7;
  243. jit_Light[0].diffuse = gl_LightSource[0].diffuse*atten7;
  244. jit_Light[0].specular = gl_LightSource[0].specular*atten7;
  245. vec3 jit_Surface_toLight1 = -jit_Light[0].toEyePosition;
  246. vec3 L2 = normalize(jit_Surface_toLight1);
  247. float shininess3 = gl_FrontMaterial.shininess;
  248. float mat_diffuse4 = lambertian(Nn, L2);
  249. float mat_specular5 = blinn(Vn, Nn, L2, shininess3);
  250. jit_Material.color += gl_FrontMaterial.emission;
  251. jit_Material.color += gl_FrontMaterial.ambient*(jit_Light[0].ambient+jit_LightModel.ambient)*diffuse_tex;
  252. jit_Material.color += gl_FrontMaterial.diffuse*jit_Light[0].diffuse*mat_diffuse4*diffuse_tex;
  253. jit_Material.color += gl_FrontMaterial.specular*jit_Light[0].specular*mat_specular5;
  254. gl_FragColor = jit_Material.color;
  255. }
  256. ]]>
  257. </program>
  258. </language>
  259. </jittershader>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement