1/22

日記

2026/01/22

Three.js

JSで法線の計算 manual-HTML要素を3Dに揃える

this.normalMatrix.getNormalMatrix(this.camera.instance.matrixWorldInverse);

matrixWorldInverseはカメラのビュー行列。glslでいうとviewMatrix

getNormalMatrix(matrixWorldInverse) は、渡した4x4行列(ここでは カメラのビュー行列 = matrixWorldInverse)から 法線ベクトルを正しく変換するための 3x3 行列(normal matrix)を作って this.normalMatrix に入れます。

this.tempV.copy(position);
this.tempV.applyMatrix3(this.normalMatrix);

positionは単位球上の位置なので、そのまま法線として使う。
法線をカメラ空間に変換している。

this.cameraToPoint.copy(position);
this.cameraToPoint
 .applyMatrix4(this.camera.instance.matrixWorldInverse)
 .normalize();

position(国のワールド座標ベクトル)を cameraToPoint にコピー
それを カメラのビュー行列 matrixWorldInverse(mat4)で変換して、カメラ空間(view space)に持っていく

const dot = this.tempV.dot(this.cameraToPoint);

所感

サイゼリヤのワインすごく安いのにすごく酔えて良い

Saizeriya's wine is super cheap but get you really drunk. - it is great.

Saizeriya's wine is great because super drunk Although It is cheap.