Guest User

Untitled

a guest
Oct 28th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.30 KB | None | 0 0
  1. #include "Uniforms.glsl"
  2. #include "Samplers.glsl"
  3. #include "Transform.glsl"
  4. #include "ScreenPos.glsl"
  5. #include "Lighting.glsl"
  6. #include "Fog.glsl"
  7.  
  8. #ifdef NORMALMAP
  9.     varying vec4 vTexCoord;
  10.     varying vec4 vTangent;
  11. #else
  12.     varying vec2 vTexCoord;
  13. #endif
  14. varying vec3 vNormal;
  15. varying vec4 vWorldPos;
  16. #ifdef VERTEXCOLOR
  17.     varying vec4 vColor;
  18. #endif
  19. #ifdef PERPIXEL
  20.     #ifdef SHADOW
  21.         #ifndef GL_ES
  22.             varying vec4 vShadowPos[NUMCASCADES];
  23.         #else
  24.             varying highp vec4 vShadowPos[NUMCASCADES];
  25.         #endif
  26.     #endif
  27.     #ifdef SPOTLIGHT
  28.         varying vec4 vSpotPos;
  29.     #endif
  30.     #ifdef POINTLIGHT
  31.         varying vec3 vCubeMaskVec;
  32.     #endif
  33. #else
  34.     varying vec3 vVertexLight;
  35.     varying vec4 vScreenPos;
  36.     #ifdef ENVCUBEMAP
  37.         varying vec3 vReflectionVec;
  38.     #endif
  39.     #if defined(LIGHTMAP) || defined(AO)
  40.         varying vec2 vTexCoord2;
  41.     #endif
  42. #endif
  43.  
  44. uniform vec4 cHeightData;  // terrain width, terrain height, spacing.x, spacing.y
  45. uniform sampler2D sHeightMap1;
  46. uniform sampler2D sCoverMap2;
  47.  
  48. void VS()
  49. {
  50.     mat4 modelMatrix = iModelMatrix;
  51.     vec3 worldPos = GetWorldPos(modelMatrix);
  52.  
  53.     // Convert world coords to UV coords
  54.     float tu=worldPos.x / cHeightData.z;
  55.     float tv=worldPos.z / cHeightData.z;
  56.  
  57.     vec2 htuv=vec2((tu/cHeightData.x)+0.5, 1.0-((tv/cHeightData.y)+0.5));
  58.     vec4 htt=textureLod(sHeightMap1, htuv, 0.0);
  59.     vec4 cov=textureLod(sCoverMap2, htuv, 0.0);
  60.  
  61.     float vx=cov.r*2.0-1.0;
  62.     float vz=cov.b*2.0-1.0;
  63.     worldPos.x=worldPos.x+vx*cHeightData.w*0.5;
  64.     worldPos.z=worldPos.z+vz*cHeightData.w*0.5;
  65.     float htscale=cHeightData.w*255.0;
  66.     float ht=htt.r*htscale + htt.g*cHeightData.w;
  67.  
  68.     float dx=worldPos.x - cCameraPos.x;
  69.     float dz=worldPos.z - cCameraPos.z;
  70.     float dist=sqrt(dx*dx+dz*dz);
  71.     dist=(dist-30.0)/(0.7*30.0-30.0);
  72.     dist=clamp(dist,0.0,1.0);
  73.     worldPos.y=worldPos.y*dist*cov.g + ht;
  74.  
  75.     gl_Position = GetClipPos(worldPos);
  76.     vNormal = GetWorldNormal(modelMatrix);
  77.     vWorldPos = vec4(worldPos, GetDepth(gl_Position));
  78.  
  79.     #ifdef VERTEXCOLOR
  80.         vColor = iColor;
  81.     #endif
  82.  
  83.     #ifdef NORMALMAP
  84.         vec4 tangent = GetWorldTangent(modelMatrix);
  85.         vec3 bitangent = cross(tangent.xyz, vNormal) * tangent.w;
  86.         vTexCoord = vec4(GetTexCoord(iTexCoord), bitangent.xy);
  87.         vTangent = vec4(tangent.xyz, bitangent.z);
  88.     #else
  89.         vTexCoord = GetTexCoord(iTexCoord);
  90.     #endif
  91.  
  92.     #ifdef PERPIXEL
  93.         // Per-pixel forward lighting
  94.         vec4 projWorldPos = vec4(worldPos, 1.0);
  95.  
  96.         #ifdef SHADOW
  97.             // Shadow projection: transform from world space to shadow space
  98.             for (int i = 0; i < NUMCASCADES; i++)
  99.                 vShadowPos[i] = GetShadowPos(i, vNormal, projWorldPos);
  100.         #endif
  101.  
  102.         #ifdef SPOTLIGHT
  103.             // Spotlight projection: transform from world space to projector texture coordinates
  104.             vSpotPos = projWorldPos * cLightMatrices[0];
  105.         #endif
  106.  
  107.         #ifdef POINTLIGHT
  108.             vCubeMaskVec = (worldPos - cLightPos.xyz) * mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz);
  109.         #endif
  110.     #else
  111.         // Ambient & per-vertex lighting
  112.         #if defined(LIGHTMAP) || defined(AO)
  113.             // If using lightmap, disregard zone ambient light
  114.             // If using AO, calculate ambient in the PS
  115.             vVertexLight = vec3(0.0, 0.0, 0.0);
  116.             vTexCoord2 = iTexCoord1;
  117.         #else
  118.             vVertexLight = GetAmbient(GetZonePos(worldPos));
  119.         #endif
  120.  
  121.         #ifdef NUMVERTEXLIGHTS
  122.             for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  123.                 vVertexLight += GetVertexLight(i, worldPos, vNormal) * cVertexLights[i * 3].rgb;
  124.         #endif
  125.  
  126.         vScreenPos = GetScreenPos(gl_Position);
  127.  
  128.         #ifdef ENVCUBEMAP
  129.             vReflectionVec = worldPos - cCameraPos;
  130.         #endif
  131.     #endif
  132. }
  133.  
  134. void PS()
  135. {
  136.     float tu=vWorldPos.x / cHeightData.z;
  137.     float tv=vWorldPos.z / cHeightData.z;
  138.     //vec2 htuv=vec2(worldPos.x/(cHeightData.x)+0.5, 1.0-(worldPos.z/(cHeightData.y)+0.5));
  139.     vec2 htuv=vec2((tu/cHeightData.x)+0.5, 1.0-((tv/cHeightData.y)+0.5));
  140.     vec4 htt=texture2D(sHeightMap1, htuv);
  141.  
  142.     //htuv=vec2(floor(tu)/cHeightData.x+0.5, 1.0-(floor(tv)/cHeightData.y+0.5));
  143.     vec4 cov=texture2D(sCoverMap2, htuv);
  144.  
  145.     // Get material diffuse albedo
  146.     vec4 diffInput = texture2D(sDiffMap, vTexCoord.xy);
  147.  
  148.     //diffInput=cov;
  149.  
  150.     if (diffInput.a < 0.5) discard;
  151.     vec4 diffColor = cMatDiffColor * diffInput;
  152.  
  153.     #ifdef VERTEXCOLOR
  154.         diffColor *= vColor;
  155.     #endif
  156.  
  157.     // Get material specular albedo
  158.     #ifdef SPECMAP
  159.         vec3 specColor = cMatSpecColor.rgb * texture2D(sSpecMap, vTexCoord.xy).rgb;
  160.     #else
  161.         vec3 specColor = cMatSpecColor.rgb;
  162.     #endif
  163.  
  164.     // Get normal
  165.     #ifdef NORMALMAP
  166.         mat3 tbn = mat3(vTangent.xyz, vec3(vTexCoord.zw, vTangent.w), vNormal);
  167.         vec3 normal = normalize(tbn * DecodeNormal(texture2D(sNormalMap, vTexCoord.xy)));
  168.     #else
  169.         vec3 normal = normalize(vNormal);
  170.     #endif
  171.  
  172.     // Get fog factor
  173.     #ifdef HEIGHTFOG
  174.         float fogFactor = GetHeightFogFactor(vWorldPos.w, vWorldPos.y);
  175.     #else
  176.         float fogFactor = GetFogFactor(vWorldPos.w);
  177.     #endif
  178.  
  179.     #if defined(PERPIXEL)
  180.         // Per-pixel forward lighting
  181.         vec3 lightColor;
  182.         vec3 lightDir;
  183.         vec3 finalColor;
  184.  
  185.         float diff = GetDiffuse(normal, vWorldPos.xyz, lightDir);
  186.  
  187.         #ifdef SHADOW
  188.             diff *= GetShadow(vShadowPos, vWorldPos.w);
  189.         #endif
  190.  
  191.         #if defined(SPOTLIGHT)
  192.             lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  193.         #elif defined(CUBEMASK)
  194.             lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  195.         #else
  196.             lightColor = cLightColor.rgb;
  197.         #endif
  198.  
  199.         #ifdef SPECULAR
  200.             float spec = GetSpecular(normal, cCameraPosPS - vWorldPos.xyz, lightDir, cMatSpecColor.a);
  201.             finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  202.         #else
  203.             finalColor = diff * lightColor * diffColor.rgb;
  204.         #endif
  205.  
  206.         #ifdef AMBIENT
  207.             finalColor += cAmbientColor.rgb * diffColor.rgb;
  208.             finalColor += cMatEmissiveColor;
  209.             gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  210.         #else
  211.             gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  212.         #endif
  213.     #elif defined(PREPASS)
  214.         // Fill light pre-pass G-Buffer
  215.         float specPower = cMatSpecColor.a / 255.0;
  216.  
  217.         gl_FragData[0] = vec4(normal * 0.5 + 0.5, specPower);
  218.         gl_FragData[1] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  219.     #elif defined(DEFERRED)
  220.         // Fill deferred G-buffer
  221.         float specIntensity = specColor.g;
  222.         float specPower = cMatSpecColor.a / 255.0;
  223.  
  224.         vec3 finalColor = vVertexLight * diffColor.rgb;
  225.         #ifdef AO
  226.             // If using AO, the vertex light ambient is black, calculate occluded ambient here
  227.             finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor.rgb * diffColor.rgb;
  228.         #endif
  229.  
  230.         #ifdef ENVCUBEMAP
  231.             finalColor += cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  232.         #endif
  233.         #ifdef LIGHTMAP
  234.             finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  235.         #endif
  236.         #ifdef EMISSIVEMAP
  237.             finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord.xy).rgb;
  238.         #else
  239.             finalColor += cMatEmissiveColor;
  240.         #endif
  241.  
  242.         gl_FragData[0] = vec4(GetFog(finalColor, fogFactor), 1.0);
  243.         gl_FragData[1] = fogFactor * vec4(diffColor.rgb, specIntensity);
  244.         gl_FragData[2] = vec4(normal * 0.5 + 0.5, specPower);
  245.         gl_FragData[3] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  246.     #else
  247.         // Ambient & per-vertex lighting
  248.         vec3 finalColor = vVertexLight * diffColor.rgb;
  249.         #ifdef AO
  250.             // If using AO, the vertex light ambient is black, calculate occluded ambient here
  251.             finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor.rgb * diffColor.rgb;
  252.         #endif
  253.  
  254.         #ifdef MATERIAL
  255.             // Add light pre-pass accumulation result
  256.             // Lights are accumulated at half intensity. Bring back to full intensity now
  257.             vec4 lightInput = 2.0 * texture2DProj(sLightBuffer, vScreenPos);
  258.             vec3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  259.  
  260.             finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  261.         #endif
  262.  
  263.         #ifdef ENVCUBEMAP
  264.             finalColor += cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  265.         #endif
  266.         #ifdef LIGHTMAP
  267.             finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  268.         #endif
  269.         #ifdef EMISSIVEMAP
  270.             finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord.xy).rgb;
  271.         #else
  272.             finalColor += cMatEmissiveColor;
  273.         #endif
  274.  
  275.         gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  276.     #endif
  277.     //gl_FragColor=htt;
  278. }
Advertisement
Add Comment
Please, Sign In to add comment