aboutsummaryrefslogtreecommitdiff
path: root/demo/d3d/shaders/pointPS.hlsl
diff options
context:
space:
mode:
authorMiles Macklin <[email protected]>2017-06-09 13:41:15 +1200
committerMiles Macklin <[email protected]>2017-06-09 13:41:15 +1200
commit688b5f42e9bfe498d7af7075d4d8f4429867f3a3 (patch)
tree7e0d0e7c95298f0418723abd92f61ac6e16b055e /demo/d3d/shaders/pointPS.hlsl
parentUpdate README.md (diff)
downloadflex-688b5f42e9bfe498d7af7075d4d8f4429867f3a3.tar.xz
flex-688b5f42e9bfe498d7af7075d4d8f4429867f3a3.zip
1.2.0.beta.11.2.0.beta.1
Diffstat (limited to 'demo/d3d/shaders/pointPS.hlsl')
-rw-r--r--demo/d3d/shaders/pointPS.hlsl81
1 files changed, 81 insertions, 0 deletions
diff --git a/demo/d3d/shaders/pointPS.hlsl b/demo/d3d/shaders/pointPS.hlsl
new file mode 100644
index 0000000..62c8ab4
--- /dev/null
+++ b/demo/d3d/shaders/pointPS.hlsl
@@ -0,0 +1,81 @@
+#include "shaderCommon.h"
+
+cbuffer constBuf : register(b0)
+{
+ PointShaderConst gParams;
+};
+
+Texture2D<float> shadowTexture : register(t0); // shadow map
+
+SamplerComparisonState shadowSampler : register(s0); // texture sample used to sample depth from shadow texture in this sample
+
+float sqr(float x) { return x * x; }
+
+float shadowSample(float4 lightOffsetPosition)
+{
+ float3 pos = float3(lightOffsetPosition.xyz / lightOffsetPosition.w);
+ //float3 uvw = (pos.xyz * 0.5) + vec3(0.5);
+ float3 uvw = (pos.xyz * float3(0.5, 0.5, 1.0)) + float3(0.5, 0.5, 0.0);
+
+ // user clip
+ if (uvw.x < 0.0 || uvw.x > 1.0)
+ return 1.0;
+ if (uvw.y < 0.0 || uvw.y > 1.0)
+ return 1.0;
+
+ float s = 0.0;
+ float radius = 0.002;
+
+ // flip uv y-coordinate
+ uvw.y = 1.0f - uvw.y;
+
+ [unroll]
+ for (int i = 0; i < 8; i++)
+ {
+ float2 shadowTaps = gParams.shadowTaps[i].xy;
+ shadowTaps.y = 1.0f - shadowTaps.y;
+
+ //s += shadow2D(shadowTex, vec3(uvw.xy + shadowTaps[i] * radius, uvw.z)).r;
+ s += shadowTexture.SampleCmp(shadowSampler, uvw.xy + shadowTaps * radius, uvw.z);
+ }
+ s /= 8.0;
+
+ return s;
+}
+
+float4 pointPS(PointGeoOut input) : SV_TARGET
+{
+ const float spotMin = gParams.spotMin;
+ const float spotMax = gParams.spotMax;
+
+ // calculate normal from texture coordinates
+ float3 normal;
+ normal.xy = input.texCoord * float2(2.0, -2.0) + float2(-1.0, 1.0);
+ float mag = dot(normal.xy, normal.xy);
+ if (mag > 1.0)
+ {
+ discard; // kill pixels outside circle
+ }
+
+ normal.z = sqrt(1.0 - mag);
+
+ if (gParams.mode == 2)
+ {
+ float alpha = normal.z * input.reflectance.w;
+ return float4(input.reflectance.xyz * alpha, alpha);
+ }
+
+ // calculate lighting
+ float shadow = shadowSample(input.lightOffsetPosition);
+
+ float3 lPos = float3(input.lightOffsetPosition.xyz / input.lightOffsetPosition.w);
+ float attenuation = max(smoothstep(spotMax, spotMin, dot(lPos.xy, lPos.xy)), 0.05);
+
+ float3 diffuse = float3(0.9, 0.9, 0.9);
+ float3 reflectance = input.reflectance.xyz;
+
+ float3 lo = diffuse * reflectance * max(0.0, sqr(-dot(input.viewLightDir, normal) * 0.5 + 0.5)) * max(0.2, shadow) * attenuation;
+
+ const float tmp = 1.0 / 2.2;
+ return float4(pow(abs(lo), float3(tmp, tmp, tmp)), 1.0);
+}