gpt4 book ai didi

javascript - 测量三 Angular 形面对光线的程度

转载 作者:行者123 更新时间:2023-11-30 16:42:50 24 4
gpt4 key购买 nike

我有 3 个顶点组成一个三 Angular 形:

var a = [0, 0, 0];
var b = [1, 0, 0];
var c = [1, 1, 0];

还有一盏灯:

var d = [0, 1, 1];

enter image description here

如何计算三 Angular 形是否面向光线,以及多少?

最佳答案

我猜你想计算整个三 Angular 形上的点的强度。从概念上讲,代码可能如下所示:

var vector1 = Vector(c, a); // a vector from a -> c
var vector2 = Vector(b, a); // a vector from a -> c
var normal = cross(vector1, vector2);

// normalize the vector, i.e length of the normal vector is 1
normal = normalize(normal);

var vectorToLightSource = Vector(lightSourceVector, a); // Any point could be taken. The result would vary some.
vectorToLightSource = normalize(vectorToLightSource);

// This will be a value between -1 and 1, but light can't be negative
var intensity = dotProduct(normal, vectorToLightSource);
intensity = intensity > 0 ? intensity : 0;

现在您可以将三 Angular 形的颜色设为 color = lightSourceColor*intensity

上面的代码很伪;需要构建crossnormalize 等函数。为此,我建议使用几何库,或者如果你喜欢线性代数,你可以自己构建它们:)

供引用:

交叉:https://en.wikipedia.org/wiki/Cross_product

点积:https://en.wikipedia.org/wiki/Dot_product

归一化:https://en.wikipedia.org/wiki/Unit_vector

关于javascript - 测量三 Angular 形面对光线的程度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31681428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com