diff --git a/ShadowEditor.Web/src/event/GPUPickEvent.js b/ShadowEditor.Web/src/event/GPUPickEvent.js index 9bf9c16f..227b78e9 100644 --- a/ShadowEditor.Web/src/event/GPUPickEvent.js +++ b/ShadowEditor.Web/src/event/GPUPickEvent.js @@ -163,7 +163,7 @@ GPUPickEvent.prototype.onAfterRender = function () { hex = -hex; } - let depth = hex * (camera.far - camera.near) + camera.near; + let depth = hex * camera.far; this.world.set( this.offsetX / width * 2 - 1, @@ -172,7 +172,7 @@ GPUPickEvent.prototype.onAfterRender = function () { ); this.world.unproject(camera); - console.log(`${hex},${depth}, ${this.world.x}, ${this.world.y}, ${this.world.z}`); + console.log(`(${this.pixel[0]},${this.pixel[1]},${this.pixel[2]}),${depth}, ${this.world.x}, ${this.world.y}, ${this.world.z}`); // 还原原来的属性 scene.background = oldBackground; diff --git a/ShadowEditor.Web/src/event/shader/depth_fragment.glsl b/ShadowEditor.Web/src/event/shader/depth_fragment.glsl index 36c02cc6..c189a852 100644 --- a/ShadowEditor.Web/src/event/shader/depth_fragment.glsl +++ b/ShadowEditor.Web/src/event/shader/depth_fragment.glsl @@ -1,20 +1,18 @@ precision highp float; -uniform float near; -uniform float far; +varying float depth; void main() { - // 参考1:https://stackoverflow.com/questions/6408851/draw-the-depth-value-in-opengl-using-shaders - // 参考2:https://gamedev.stackexchange.com/questions/93055/getting-the-real-fragment-depth-in-glsl - float ndcDepth = (gl_FragCoord.z - near) / (far - near); - float clipDepth = ndcDepth / gl_FragCoord.w; + // 参考:https://gamedev.stackexchange.com/questions/93055/getting-the-real-fragment-depth-in-glsl + // float ndcDepth = (gl_FragCoord.z - near) / (far - near); + // float clipDepth = ndcDepth / gl_FragCoord.w; - float hex = abs(clamp(clipDepth, -1.0, 1.0)) * 16777215.0; // 0xffffff + float hex = abs(clamp(depth, -1.0, 1.0)) * 16777215.0; // 0xffffff float r = floor(hex / 65535.0); float g = floor((hex - r * 65535.0) / 255.0); float b = floor(hex - r * 65535.0 - g * 255.0); - float a = sign(clipDepth) >= 0.0 ? 1.0 : 0.0; // depth大于等于0,为1.0;小于0,为0.0。 + float a = sign(depth) >= 0.0 ? 1.0 : 0.0; // depth大于等于0,为1.0;小于0,为0.0。 gl_FragColor = vec4(r / 255.0, g / 255.0, b / 255.0, a); } \ No newline at end of file diff --git a/ShadowEditor.Web/src/event/shader/depth_vertex.glsl b/ShadowEditor.Web/src/event/shader/depth_vertex.glsl index d2119de5..9db9d12d 100644 --- a/ShadowEditor.Web/src/event/shader/depth_vertex.glsl +++ b/ShadowEditor.Web/src/event/shader/depth_vertex.glsl @@ -1,5 +1,11 @@ precision highp float; +uniform float far; + +varying float depth; + void main() { + // 参考:https://stackoverflow.com/questions/6408851/draw-the-depth-value-in-opengl-using-shaders gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); + depth = gl_Position.z / far; } \ No newline at end of file