Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <jittershader name="jit.gl.material.jxs">
- <description>
- material's system code generated JXS file
- </description>
- <param name="heightmap" type="int" default="0"/>
- <param name="diffuse" type="int" default="0"/>
- <param name="jit_Tangent" type="vec3" state="TANGENT" />
- <language name="glsl" version="1.0">
- <bind param="heightmap" program="vs" />
- <bind param="diffuse" program="fs" />
- <bind param="jit_Tangent" program="vs" />
- <program name="vs" type="vertex">
- <![CDATA[
- #version 120
- vec3 filter_normal(vec2 uv, float texelsize, sampler2D tex) {
- float h0 = texture2D(tex, uv + texelsize * vec2( 0,-1)).r;
- float h1 = texture2D(tex, uv + texelsize * vec2(-1, 0)).r;
- float h2 = texture2D(tex, uv + texelsize * vec2( 1, 0)).r;
- float h3 = texture2D(tex, uv + texelsize * vec2( 0, 1)).r;
- vec2 step = vec2(1.0, 0.0);
- vec3 va = normalize( vec3(step.xy, h1 - h0) );
- vec3 vb = normalize( vec3(step.yx, h3 - h2) );
- return cross(va,vb);
- }
- vec4 sample_vertex(vec2 uv, sampler2D tex, vec3 normal) {
- float height = texture2D(tex, uv).r;
- vec4 vert = gl_ModelViewMatrix * gl_Vertex;
- return vert + normalize(vec4(normal, 1.0)) * height;
- }
- attribute vec3 jit_Tangent;
- uniform sampler2D heightmap;
- varying vec4 jit_Surface_position;
- varying vec3 jit_Surface_tangent;
- varying vec2 jit_Surface_texcoord0;
- varying vec2 jit_Surface_texcoord1;
- varying vec3 jit_Surface_normal;
- varying vec3 jit_Surface_filtered_normal;
- varying vec3 jit_Surface_bitangent;
- void main() {
- jit_Surface_normal = gl_NormalMatrix*gl_Normal;
- jit_Surface_texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);
- jit_Surface_texcoord1 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0);
- jit_Surface_tangent = gl_NormalMatrix*jit_Tangent;
- jit_Surface_filtered_normal = filter_normal(jit_Surface_texcoord1, 0.001, heightmap);
- jit_Surface_bitangent = cross(jit_Surface_normal, jit_Surface_tangent);
- vec4 pos = sample_vertex(jit_Surface_texcoord1, heightmap, jit_Surface_normal);
- gl_Position = gl_ProjectionMatrix * pos;
- jit_Surface_position = gl_ModelViewMatrix * pos;
- }
- ]]>
- </program>
- <program name="fs" type="fragment">
- <![CDATA[
- #version 120
- #define PI (3.1415926535898)
- #define jit_LightModel gl_LightModel
- struct Material{
- vec4 color;
- };
- struct Light{
- vec4 ambient;
- vec4 diffuse;
- vec4 specular;
- vec3 toEyePosition;
- };
- ///////////////////////////////////
- // Diffuse Shading Models
- // aka Lambertian
- float lambertian(vec3 Nn, vec3 L) {
- return max(dot(Nn, L), 0.);
- }
- float oren_nayer(vec3 Vn, vec3 Nn, vec3 L, float roughness) {
- float roughness2 = roughness*roughness;
- float A = 1. - (0.5*roughness2)/(roughness2+0.33);
- float B = (0.45*roughness2)/(roughness2+0.09);
- float VdotN = dot(Vn, Nn);
- // reflected (between vertex normal and view direction)
- float LdotN = dot(L, Nn);
- // incident (between vertex normal and light direction
- float irradiance = max(0., LdotN);
- // clamp incident
- float cos_delta_angle = max(0., dot(normalize(Vn-Nn*VdotN), normalize(L-Nn*LdotN)));
- float theta_r = acos(VdotN);
- float theta_i = acos(LdotN);
- float alpha = max(theta_i, theta_r);
- float beta = min(theta_i, theta_r);
- float profile = sin(alpha) * tan(beta);
- return (A+B*cos_delta_angle*profile)*irradiance;
- }
- float oren_nayer_lut(sampler2D ONtex, vec3 Vn, vec3 Nn, vec3 L, float roughness) {
- float roughness2 = roughness*roughness;
- float A = 1. - (0.5*roughness2)/(roughness2+0.33);
- float B = (0.45*roughness2)/(roughness2+0.09);
- float VdotN = dot(Vn, Nn);
- // reflected (between vertex normal and view direction)
- float LdotN = dot(L, Nn);
- // incident (between vertex normal and light direction
- float irradiance = max(0., LdotN);
- // clamp incident
- float cos_delta_angle = max(0., dot(normalize(Vn-Nn*VdotN), normalize(L-Nn*LdotN)));
- float profile = texture2D(ONtex, vec2(VdotN, LdotN)*0.5+0.5).r;
- return (A+B*cos_delta_angle*profile)*irradiance;
- }
- float toon(vec3 Nn, vec3 L, float size, float smoothfactor) {
- float v = dot(Nn, L);
- float thresh = 1.-size;
- return smoothstep(thresh-0.5*smoothfactor, thresh+0.5*smoothfactor, v);
- }
- float minnaert(vec3 Vn, vec3 Nn, vec3 L, float darkness) {
- float NL = max(dot(Nn, L), 0.);
- float NV = max(dot(Nn, Vn), 0.);
- return pow(NL, darkness+1.)*pow(1.-NV, 1.-darkness);
- }
- ///////////////////////////////////
- // Specular Shading Models
- // aka Blinn (really Blinn-Phong)
- float blinn(vec3 Vn, vec3 Nn, vec3 L, float Ns) {
- vec3 H = normalize(L + Vn);
- return pow(max(dot(Nn, H), 0.), Ns);
- }
- // aka Phong
- float specular_phong(vec3 Vn, vec3 Nn, vec3 L, float Ns) {
- vec3 R = reflect(-L, Nn);
- return pow(max(dot(R, Vn), 0.), Ns);
- }
- float toonspecular(vec3 Vn, vec3 Nn, vec3 L, float Ns, float size, float smoothfactor) {
- vec3 H = normalize(L + Vn);
- float v = pow(max(dot(Nn, H), 0.), Ns);
- float thresh = 1.-size;
- return smoothstep(thresh-0.5*smoothfactor, thresh+0.5*smoothfactor, v);
- }
- // Ward Isotropic model
- float ward(vec3 Vn, vec3 Nn, vec3 L, float rms) {
- vec3 H = normalize(Vn + L);
- float NH = max(dot(Nn, H), 0.);
- float NV = max(dot(Nn, Vn), 0.);
- float NL = max(dot(Nn, L), 0.);
- float alpha = max(rms, 0.001);
- // prevent divide-by 0
- float angle = acos(NH);
- float angle2 = angle*angle;
- float alpha2 = alpha*alpha;
- return NL * 0.25 * (1.0/alpha2) * exp(-2.*angle2/alpha2);
- }
- float cook_torrance(vec3 Vn, vec3 Nn, vec3 L, float rms) {
- vec3 H = normalize(Vn + L);
- float NH = max(dot(Nn, H), 0.);
- float VH = max(dot(Vn, H), 0.);
- float NV = max(dot(Nn, Vn), 0.);
- float NL = max(dot(Nn, L), 0.);
- float factor = min(NV, NL);
- float G = min(1., 2.*NH*factor/VH);
- float alpha = acos(NH);
- float exponent = alpha/rms;
- float D = exp(-exponent*exponent);
- return D*G/(0.001+NV);
- }
- vec2 sphere_map(vec3 v) {
- float m = 2.*sqrt(v.x*v.x + v.y*v.y + (v.z+1.)*(v.z+1.));
- return vec2(v)/m+0.5;
- }
- vec4 fog_linear(vec4 color, float dist) {
- float fogfactor = (gl_Fog.end-dist)*gl_Fog.scale;
- fogfactor = clamp(fogfactor, 0., 1.);
- return mix(gl_Fog.color, color, fogfactor);
- }
- vec4 fog_exp(vec4 color, float dist) {
- float fogfactor = exp(-gl_Fog.density * dist);
- fogfactor = clamp(fogfactor, 0., 1.);
- return mix(gl_Fog.color, color, fogfactor);
- }
- vec4 fog_exp2(vec4 color, float dist) {
- float fogfactor = exp(-gl_Fog.density*gl_Fog.density * dist*dist);
- fogfactor = clamp(fogfactor, 0., 1.);
- return mix(gl_Fog.color, color, fogfactor);
- }
- // Shadows
- vec4 btex2D(sampler2D map, vec2 uv, float radius, float steps) {
- float stepSize = 2.0 * radius / steps;
- uv.xy -= vec2(radius,radius);
- vec4 total = vec4(0, 0, 0, 0);
- for (int x = 0; x < steps; ++x)
- for (int y = 0; y < steps; ++y)
- total += texture2D(map, vec2(uv.xy + vec2(x * stepSize, y * stepSize)));
- return total / (steps * steps);
- }
- float compute_shadow(sampler2D shadowMap, vec4 shadowMapPos, float ourDepth, vec3 param) {
- // param.x = radius
- // param.y = epsilon
- // param.z = min clamp
- vec2 suv = (shadowMapPos.xy / shadowMapPos.w) * 0.5 + 0.5;
- vec2 moments = btex2D(shadowMap, suv, param.x, 4).xy;
- float litFactor = (ourDepth <= moments.x ? 1 : 0);
- float variance = clamp((moments.y - (moments.x*moments.x)), param.y, 1.0);
- float m_d = moments.x - ourDepth;
- float p = variance / (variance + m_d * m_d);
- return smoothstep(param.z, 1.0, max(litFactor, p));
- }
- Material jit_Material;
- Light jit_Light[8];
- uniform samplerJit0 diffuse;
- varying vec4 jit_Surface_position;
- varying vec3 jit_Surface_tangent;
- varying vec2 jit_Surface_texcoord0;
- varying vec2 jit_Surface_texcoord1;
- varying vec3 jit_Surface_normal;
- varying vec3 jit_Surface_filtered_normal;
- varying vec3 jit_Surface_bitangent;
- void main() {
- jit_Material.color = vec4(0., 0., 0., 0.);
- vec3 jit_Surface_view = -vec3(jit_Surface_position);
- vec3 Vn = normalize(jit_Surface_view);
- vec3 Tb = normalize(jit_Surface_tangent);
- vec3 Bb = normalize(jit_Surface_bitangent);
- vec3 Nb = normalize(jit_Surface_normal);
- mat3 local = mat3(Tb, Bb, Nb);
- vec3 Nn = normalize(jit_Surface_filtered_normal);
- Nn = local*Nn;
- vec4 diffuse_tex = textureJit0(diffuse, jit_Surface_texcoord0);
- jit_Light[0].toEyePosition = normalize(jit_Surface_position.xyz - gl_LightSource[0].position.xyz);
- float d6 = length(jit_Surface_position.xyz-gl_LightSource[0].position.xyz);
- float atten7 = gl_LightSource[0].constantAttenuation+gl_LightSource[0].linearAttenuation*d6+gl_LightSource[0].quadraticAttenuation*d6*d6;
- atten7 = 1.0/atten7;
- jit_Light[0].ambient = gl_LightSource[0].ambient*atten7;
- jit_Light[0].diffuse = gl_LightSource[0].diffuse*atten7;
- jit_Light[0].specular = gl_LightSource[0].specular*atten7;
- vec3 jit_Surface_toLight1 = -jit_Light[0].toEyePosition;
- vec3 L2 = normalize(jit_Surface_toLight1);
- float shininess3 = gl_FrontMaterial.shininess;
- float mat_diffuse4 = lambertian(Nn, L2);
- float mat_specular5 = blinn(Vn, Nn, L2, shininess3);
- jit_Material.color += gl_FrontMaterial.emission;
- jit_Material.color += gl_FrontMaterial.ambient*(jit_Light[0].ambient+jit_LightModel.ambient)*diffuse_tex;
- jit_Material.color += gl_FrontMaterial.diffuse*jit_Light[0].diffuse*mat_diffuse4*diffuse_tex;
- jit_Material.color += gl_FrontMaterial.specular*jit_Light[0].specular*mat_specular5;
- gl_FragColor = jit_Material.color;
- }
- ]]>
- </program>
- </language>
- </jittershader>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement