Skip to content

Commit

Permalink
初版shadowmap有点问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MuseumMage committed May 27, 2024
1 parent 8e20efe commit bd18112
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions Assignment1/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
height: 100%;
}
</style>
<link rel="preload" href="assets/mary/MC003_Kozakura_Mari.png" as="image" type="image/png" crossorigin />
<script src="lib/three.js" defer></script>
<script src="lib/OrbitControls.js" defer></script>
<script src="lib/gl-matrix-min.js" defer></script>
Expand Down
12 changes: 11 additions & 1 deletion Assignment1/src/lights/DirectionalLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ class DirectionalLight {
let projectionMatrix = mat4.create();

// Model transform
mat4.translate(modelMatrix, modelMatrix, translate);
mat4.scale(modelMatrix, modelMatrix, scale);

// View transform

mat4.lookAt(viewMatrix, this.lightPos, this.focalPoint, this.lightUp);

// Projection transform
var r = 100;
var l = -r;
var t = 100;
var b = -t;
var n = 0.01;
var f = 200;
mat4.ortho(projectionMatrix, l, r, b, t, n, f);

mat4.multiply(lightMVP, projectionMatrix, viewMatrix);
mat4.multiply(lightMVP, lightMVP, modelMatrix);
Expand Down
15 changes: 11 additions & 4 deletions Assignment1/src/shaders/phongShader/phongFragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ float PCSS(sampler2D shadowMap, vec4 coords){


float useShadowMap(sampler2D shadowMap, vec4 shadowCoord){
return 1.0;
float closestDepth = unpack(texture2D(shadowMap, shadowCoord.xy));
float currentDepth = shadowCoord.z;
float visibility = currentDepth > (closestDepth + EPS) ? 0.0 : 1.0;
return visibility;
}

vec3 blinnPhong() {
Expand Down Expand Up @@ -134,12 +137,16 @@ vec3 blinnPhong() {
void main(void) {

float visibility;
//visibility = useShadowMap(uShadowMap, vec4(shadowCoord, 1.0));
// 参考https://learnopengl-cn.github.io/05%20Advanced%20Lighting/03%20Shadows/01%20Shadow%20Mapping/ 的ShadowCalculation函数
vec3 shadowCoord = vPositionFromLight.xyz / vPositionFromLight.w;
shadowCoord = shadowCoord * 0.5 + 0.5;

visibility = useShadowMap(uShadowMap, vec4(shadowCoord, 1.0));
//visibility = PCF(uShadowMap, vec4(shadowCoord, 1.0));
//visibility = PCSS(uShadowMap, vec4(shadowCoord, 1.0));

vec3 phongColor = blinnPhong();

//gl_FragColor = vec4(phongColor * visibility, 1.0);
gl_FragColor = vec4(phongColor, 1.0);
gl_FragColor = vec4(phongColor * visibility, 1.0);
// gl_FragColor = vec4(phongColor, 1.0);
}

0 comments on commit bd18112

Please sign in to comment.