Spaces:
Sleeping
Sleeping
File size: 4,258 Bytes
2b7aae2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | import { Vector3 } from './Vector3.js';
/**
* Primary reference:
* https://graphics.stanford.edu/papers/envmap/envmap.pdf
*
* Secondary reference:
* https://www.ppsloan.org/publications/StupidSH36.pdf
*/
// 3-band SH defined by 9 coefficients
class SphericalHarmonics3 {
constructor() {
this.coefficients = [];
for (let i = 0; i < 9; i++) {
this.coefficients.push(new Vector3());
}
}
set(coefficients) {
for (let i = 0; i < 9; i++) {
this.coefficients[i].copy(coefficients[i]);
}
return this;
}
zero() {
for (let i = 0; i < 9; i++) {
this.coefficients[i].set(0, 0, 0);
}
return this;
}
// get the radiance in the direction of the normal
// target is a Vector3
getAt(normal, target) {
// normal is assumed to be unit length
const x = normal.x,
y = normal.y,
z = normal.z;
const coeff = this.coefficients;
// band 0
target.copy(coeff[0]).multiplyScalar(0.282095);
// band 1
target.addScaledVector(coeff[1], 0.488603 * y);
target.addScaledVector(coeff[2], 0.488603 * z);
target.addScaledVector(coeff[3], 0.488603 * x);
// band 2
target.addScaledVector(coeff[4], 1.092548 * (x * y));
target.addScaledVector(coeff[5], 1.092548 * (y * z));
target.addScaledVector(coeff[6], 0.315392 * (3.0 * z * z - 1.0));
target.addScaledVector(coeff[7], 1.092548 * (x * z));
target.addScaledVector(coeff[8], 0.546274 * (x * x - y * y));
return target;
}
// get the irradiance (radiance convolved with cosine lobe) in the direction of the normal
// target is a Vector3
// https://graphics.stanford.edu/papers/envmap/envmap.pdf
getIrradianceAt(normal, target) {
// normal is assumed to be unit length
const x = normal.x,
y = normal.y,
z = normal.z;
const coeff = this.coefficients;
// band 0
target.copy(coeff[0]).multiplyScalar(0.886227); // π * 0.282095
// band 1
target.addScaledVector(coeff[1], 2.0 * 0.511664 * y); // ( 2 * π / 3 ) * 0.488603
target.addScaledVector(coeff[2], 2.0 * 0.511664 * z);
target.addScaledVector(coeff[3], 2.0 * 0.511664 * x);
// band 2
target.addScaledVector(coeff[4], 2.0 * 0.429043 * x * y); // ( π / 4 ) * 1.092548
target.addScaledVector(coeff[5], 2.0 * 0.429043 * y * z);
target.addScaledVector(coeff[6], 0.743125 * z * z - 0.247708); // ( π / 4 ) * 0.315392 * 3
target.addScaledVector(coeff[7], 2.0 * 0.429043 * x * z);
target.addScaledVector(coeff[8], 0.429043 * (x * x - y * y)); // ( π / 4 ) * 0.546274
return target;
}
add(sh) {
for (let i = 0; i < 9; i++) {
this.coefficients[i].add(sh.coefficients[i]);
}
return this;
}
addScaledSH(sh, s) {
for (let i = 0; i < 9; i++) {
this.coefficients[i].addScaledVector(sh.coefficients[i], s);
}
return this;
}
scale(s) {
for (let i = 0; i < 9; i++) {
this.coefficients[i].multiplyScalar(s);
}
return this;
}
lerp(sh, alpha) {
for (let i = 0; i < 9; i++) {
this.coefficients[i].lerp(sh.coefficients[i], alpha);
}
return this;
}
equals(sh) {
for (let i = 0; i < 9; i++) {
if (!this.coefficients[i].equals(sh.coefficients[i])) {
return false;
}
}
return true;
}
copy(sh) {
return this.set(sh.coefficients);
}
clone() {
return new this.constructor().copy(this);
}
fromArray(array, offset = 0) {
const coefficients = this.coefficients;
for (let i = 0; i < 9; i++) {
coefficients[i].fromArray(array, offset + i * 3);
}
return this;
}
toArray(array = [], offset = 0) {
const coefficients = this.coefficients;
for (let i = 0; i < 9; i++) {
coefficients[i].toArray(array, offset + i * 3);
}
return array;
}
// evaluate the basis functions
// shBasis is an Array[ 9 ]
static getBasisAt(normal, shBasis) {
// normal is assumed to be unit length
const x = normal.x,
y = normal.y,
z = normal.z;
// band 0
shBasis[0] = 0.282095;
// band 1
shBasis[1] = 0.488603 * y;
shBasis[2] = 0.488603 * z;
shBasis[3] = 0.488603 * x;
// band 2
shBasis[4] = 1.092548 * x * y;
shBasis[5] = 1.092548 * y * z;
shBasis[6] = 0.315392 * (3 * z * z - 1);
shBasis[7] = 1.092548 * x * z;
shBasis[8] = 0.546274 * (x * x - y * y);
}
}
SphericalHarmonics3.prototype.isSphericalHarmonics3 = true;
export { SphericalHarmonics3 };
|