Skip to content

Commit

Permalink
Merge pull request #18962 from donmccurdy/feat-sh-addScaledSH
Browse files Browse the repository at this point in the history
SphericalHarmonics3: Add .addScaledSH method.
  • Loading branch information
mrdoob authored Mar 25, 2020
2 parents 902fa09 + d70daf2 commit 11e81cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/math/SphericalHarmonics3.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class SphericalHarmonics3 {
set ( coefficients: Vector3[] ): SphericalHarmonics3;
zero(): SphericalHarmonics3;
add( sh: SphericalHarmonics3 ): SphericalHarmonics3;
addScaledSH( sh: SphericalHarmonics3, s: number ): SphericalHarmonics3;
scale( s: number ): SphericalHarmonics3;
lerp( sh: SphericalHarmonics3, alpha: number ): SphericalHarmonics3;
equals( sh: SphericalHarmonics3 ): boolean;
Expand Down
43 changes: 27 additions & 16 deletions src/math/SphericalHarmonics3.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ Object.assign( SphericalHarmonics3.prototype, {
target.copy( coeff[ 0 ] ).multiplyScalar( 0.282095 );

// band 1
target.addScale( coeff[ 1 ], 0.488603 * y );
target.addScale( coeff[ 2 ], 0.488603 * z );
target.addScale( coeff[ 3 ], 0.488603 * x );
target.addScaledVector( coeff[ 1 ], 0.488603 * y );
target.addScaledVector( coeff[ 2 ], 0.488603 * z );
target.addScaledVector( coeff[ 3 ], 0.488603 * x );

// band 2
target.addScale( coeff[ 4 ], 1.092548 * ( x * y ) );
target.addScale( coeff[ 5 ], 1.092548 * ( y * z ) );
target.addScale( coeff[ 6 ], 0.315392 * ( 3.0 * z * z - 1.0 ) );
target.addScale( coeff[ 7 ], 1.092548 * ( x * z ) );
target.addScale( coeff[ 8 ], 0.546274 * ( x * x - y * y ) );
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;

Expand All @@ -97,16 +97,16 @@ Object.assign( SphericalHarmonics3.prototype, {
target.copy( coeff[ 0 ] ).multiplyScalar( 0.886227 ); // π * 0.282095

// band 1
target.addScale( coeff[ 1 ], 2.0 * 0.511664 * y ); // ( 2 * π / 3 ) * 0.488603
target.addScale( coeff[ 2 ], 2.0 * 0.511664 * z );
target.addScale( coeff[ 3 ], 2.0 * 0.511664 * x );
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.addScale( coeff[ 4 ], 2.0 * 0.429043 * x * y ); // ( π / 4 ) * 1.092548
target.addScale( coeff[ 5 ], 2.0 * 0.429043 * y * z );
target.addScale( coeff[ 6 ], 0.743125 * z * z - 0.247708 ); // ( π / 4 ) * 0.315392 * 3
target.addScale( coeff[ 7 ], 2.0 * 0.429043 * x * z );
target.addScale( coeff[ 8 ], 0.429043 * ( x * x - y * y ) ); // ( π / 4 ) * 0.546274
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;

Expand All @@ -124,6 +124,17 @@ Object.assign( SphericalHarmonics3.prototype, {

},

addScaledSH: function ( sh, s ) {

for ( var i = 0; i < 9; i ++ ) {

this.coefficients[ i ].addScaledVector( sh.coefficients[ i ], s );

}

return this;

},

scale: function ( s ) {

Expand Down

0 comments on commit 11e81cb

Please sign in to comment.