View source
class SpotShadow
package h3d.shader
extends Shader
@:directlyUsed@:src({ @const var enable:Bool; @const var USE_ESM:Bool; @param var shadowPower:Float; @const var USE_PCF:Bool; @const var pcfQuality:Int; @param var pcfScale:Float; @param var shadowRes:Vec2; @param var shadowMap:Channel; @param var shadowProj:Mat4; @param var shadowBias:Float; var transformedPosition:Vec3; var shadow:Float; @param var poissonDiskLow:Array<Vec4,4>; @param var poissonDiskHigh:Array<Vec4,12>; @param var poissonDiskVeryHigh:Array<Vec4,64>; function rand(v:Float):Float { var dp = dot(vec4(v), vec4(12.9898, 78.233, 45.164, 94.673)); return fract(sin(dp) * 43758.5453); }; function fragment() { if (enable) { if (USE_PCF) { shadow = 1.0; var texelSize = 1.0 / shadowRes; var shadowPos = vec4(transformedPosition, 1.0) * shadowProj; shadowPos.xyz = shadowPos.xyz / shadowPos.w; var shadowUv = screenToUv(shadowPos.xy); var zMax = shadowPos.z; var rot = rand(transformedPosition.x + transformedPosition.y + transformedPosition.z) * 3.14 * 2; switch (pcfQuality) { case 1: var sampleStrength = 1.0 / 4.0; for (i in 0 ... 4) { var offset = poissonDiskLow[i].xy * texelSize * pcfScale; offset = vec2(cos(rot) * offset.x - sin(rot) * offset.y, cos(rot) * offset.y + sin(rot) * offset.x); var depth = shadowMap.getLod(shadowUv + offset, 0); if (zMax - shadowBias > depth) shadow -= sampleStrength; }; case 2: var sampleStrength = 1.0 / 12.0; for (i in 0 ... 12) { var offset = poissonDiskHigh[i].xy * texelSize * pcfScale; offset = vec2(cos(rot) * offset.x - sin(rot) * offset.y, cos(rot) * offset.y + sin(rot) * offset.x); var depth = shadowMap.getLod(shadowUv + offset, 0); if (zMax - shadowBias > depth) shadow -= sampleStrength; }; case 3: var sampleStrength = 1.0 / 64.0; for (i in 0 ... 64) { var offset = poissonDiskHigh[i].xy * texelSize * pcfScale; offset = vec2(cos(rot) * offset.x - sin(rot) * offset.y, cos(rot) * offset.y + sin(rot) * offset.x); var depth = shadowMap.getLod(shadowUv + offset, 0); if (zMax - shadowBias > depth) shadow -= sampleStrength; }; }; } else if (USE_ESM) { var shadowPos = vec4(transformedPosition, 1.0) * shadowProj; var shadowScreenPos = shadowPos.xyz / shadowPos.w; var depth = shadowMap.get(screenToUv(shadowScreenPos.xy)); var zMax = shadowScreenPos.z.saturate(); var delta = (depth + shadowBias).min(zMax) - zMax; shadow = exp(shadowPower * delta).saturate(); } else { var shadowPos = vec4(transformedPosition, 1.0) * shadowProj; shadowPos.xyz = shadowPos.xyz / shadowPos.w; var shadowUv = screenToUv(shadowPos.xy); var depth = shadowMap.get(shadowUv.xy); shadow = shadowPos.z - shadowBias > depth ? 0 : 1; }; }; }; })@:build(hxsl.Macros.buildShader())@:autoBuild(hxsl.Macros.buildShader())Constructor
Variables
Methods
Inherited Variables
Inherited Methods
Defined by Shader
setPriority(v:Int):Void
Shader priority should only be changed before the shader is added to a material.