From 72b0bb309a463aa1ecbcf94ee2404850f64145cd Mon Sep 17 00:00:00 2001 From: liteng <930372551@qq.com> Date: Mon, 26 Aug 2019 12:24:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=95=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/weekend/src/Application.js | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/ShadowEditor.Web/test/weekend/src/Application.js b/ShadowEditor.Web/test/weekend/src/Application.js index 06e14e51..24adc698 100644 --- a/ShadowEditor.Web/test/weekend/src/Application.js +++ b/ShadowEditor.Web/test/weekend/src/Application.js @@ -71,21 +71,39 @@ Application.prototype.hitSphere = function () { let b = temp.copy(oc).dot(r.direction) * 2; let c = temp.copy(oc).dot(oc) - radius * radius; let discriminant = b * b - 4 * a * c; + + if (discriminant < 0) { + return -1; + } else { + return (-b - Math.sqrt(discriminant)) / (2 * a); + } + return discriminant > 0; }; }(); Application.prototype.calculateColor = function () { let mz = new THREE.Vector3(0, 0, -1); + let target = new THREE.Vector3(); + let one = new THREE.Vector3(1, 1, 1); return function (ray, color) { - if (this.hitSphere(mz, 0.5, ray)) { - color.setRGB(1, 0, 0); + let t = this.hitSphere(mz, 0.5, ray); + + if (t > 0) { + ray.at(t, target); + let n = target.sub(mz).normalize(); + + let rgb = n.add(one).multiplyScalar(0.5); + + color.setRGB(rgb.x, rgb.y, rgb.z); return; } let direction = ray.direction; - let t = 0.5 * (direction.y + 1); + + t = 0.5 * (direction.y + 1); + color.setRGB( (1 - t) * 1 + t * 0.5, (1 - t) * 1 + t * 0.7,