New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Geometry] Add geometry for superquadric #28036
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import { BufferGeometry, Vector3, Float32BufferAttribute } from 'three'; | ||
|
||
// avoid floating point precision errors | ||
const tolerance = 1e-15; | ||
|
||
function signed_pow( x, y ) { | ||
|
||
// round to next integer | ||
if ( Math.abs( Math.round( x ) - x ) < tolerance ) x = Math.round( x ); | ||
|
||
// avoid NaN (0 ** y if y < 0 ) | ||
if ( x === 0 ) return 0; | ||
|
||
return Math.sign( x ) * Math.pow( Math.abs( x ), y ); | ||
|
||
} | ||
|
||
class SuperquadricGeometry extends BufferGeometry { | ||
|
||
constructor( | ||
epsilon_1 = 1.0, | ||
epsilon_2 = 1.0, | ||
widthSegments = 32, | ||
heightSegments = 16, | ||
phiStart = 0, | ||
phiLength = Math.PI * 2, | ||
thetaStart = 0, | ||
thetaLength = Math.PI, | ||
) { | ||
|
||
super(); | ||
|
||
this.type = 'SuperquadricGeometry'; | ||
|
||
this.parameters = { | ||
epsilon_1: epsilon_1, | ||
epsilon_2: epsilon_2, | ||
widthSegments: widthSegments, | ||
heightSegments: heightSegments, | ||
phiStart: phiStart, | ||
phiLength: phiLength, | ||
thetaStart: thetaStart, | ||
thetaLength: thetaLength, | ||
}; | ||
|
||
// round to the next power of 2 | ||
widthSegments = Math.max( 3, Math.floor( widthSegments ) ); | ||
heightSegments = Math.max( 2, Math.floor( heightSegments ) ); | ||
|
||
|
||
const thetaEnd = Math.min( thetaStart + thetaLength, Math.PI ); | ||
|
||
let index = 0; | ||
const grid = []; | ||
|
||
const vertex = new Vector3(); | ||
const normal = new Vector3(); | ||
|
||
// buffers | ||
|
||
const indices = []; | ||
const vertices = []; | ||
const normals = []; | ||
const uvs = []; | ||
|
||
// generate vertices, normals and uvs | ||
|
||
for ( let iy = 0; iy <= heightSegments; iy ++ ) { | ||
|
||
const verticesRow = []; | ||
|
||
const eta = thetaStart + ( iy / heightSegments ) * thetaLength; | ||
|
||
for ( let ix = 0; ix <= widthSegments; ix ++ ) { | ||
|
||
const omega = phiStart + ( ix / widthSegments ) * phiLength; | ||
|
||
// vertex | ||
|
||
vertex.x = - signed_pow( Math.sin( eta ), epsilon_1 ) * signed_pow( Math.cos( omega ), epsilon_2 ); | ||
vertex.y = signed_pow( Math.cos( eta ), epsilon_1 ); | ||
vertex.z = signed_pow( Math.sin( eta ), epsilon_1 ) * signed_pow( Math.sin( omega ), epsilon_2 ); | ||
|
||
vertices.push( vertex.x, vertex.y, vertex.z ); | ||
|
||
// normal | ||
|
||
normal.x = - signed_pow( Math.sin( eta ), 2 - epsilon_1 ) * signed_pow( Math.cos( omega ), 2 - epsilon_2 ); | ||
normal.y = signed_pow( Math.cos( eta ), 2 - epsilon_1 ); | ||
normal.z = signed_pow( Math.sin( eta ), 2 - epsilon_1 ) * signed_pow( Math.sin( omega ), 2 - epsilon_2 ); | ||
|
||
normal.normalize(); | ||
normals.push( normal.x, normal.y, normal.z ); | ||
|
||
// uv (calculate uv from coordinates / mapped sphere angles) | ||
|
||
const latitude = Math.atan2( vertex.z, vertex.x ); | ||
const longitude = Math.acos( vertex.y / vertex.length() ); | ||
|
||
let u = 1 - ( 0.5 * ( ( latitude / Math.PI ) + 1 ) ); | ||
const v = 1 - ( longitude / Math.PI ); | ||
|
||
// edge cases (poles and dateline) | ||
if ( iy == 0.0 || iy == heightSegments || ix == widthSegments ) { | ||
|
||
u = omega / ( 2 * Math.PI ); | ||
|
||
} | ||
|
||
uvs.push( u, v ); | ||
|
||
verticesRow.push( index ++ ); | ||
|
||
} | ||
|
||
grid.push( verticesRow ); | ||
|
||
} | ||
|
||
// indices | ||
|
||
for ( let iy = 0; iy < heightSegments; iy ++ ) { | ||
|
||
for ( let ix = 0; ix < widthSegments; ix ++ ) { | ||
|
||
const a = grid[ iy ][ ix + 1 ]; | ||
const b = grid[ iy ][ ix ]; | ||
const c = grid[ iy + 1 ][ ix ]; | ||
const d = grid[ iy + 1 ][ ix + 1 ]; | ||
|
||
if ( iy !== 0 || thetaStart > 0 ) indices.push( a, b, d ); | ||
if ( iy !== heightSegments - 1 || thetaEnd < Math.PI ) indices.push( b, c, d ); | ||
|
||
} | ||
|
||
} | ||
|
||
// build geometry | ||
|
||
this.setIndex( indices ); | ||
this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); | ||
this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); | ||
this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); | ||
|
||
} | ||
|
||
copy( source ) { | ||
|
||
super.copy( source ); | ||
|
||
this.parameters = Object.assign( {}, source.parameters ); | ||
|
||
return this; | ||
|
||
} | ||
|
||
static fromJSON( data ) { | ||
|
||
return new SuperquadricGeometry( | ||
data.epsilon_1, | ||
data.epsilon_2, | ||
data.widthSegments, | ||
data.heightSegments, | ||
data.phiStart, | ||
data.phiLength, | ||
data.thetaStart, | ||
data.thetaLength, | ||
); | ||
|
||
} | ||
|
||
} | ||
|
||
export { SuperquadricGeometry }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we decide to add
SuperquadricGeometry
, I would prefer to add it as an addon so it should be placed in theexamples/jsm/geometries
directory.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the meanwhile I suggest a different approach. Please host
SuperquadricGeometry
as a separatenpm
package and then create a new example that uses your library as an external dependency. We use this approach for other demos as well e.g. https://threejs.org/examples/?q=subdivision#webgl_modifier_subdivisionThe import of the subvision library looks like so:
Your import could look like so:
Later in the code, the module is imported like so: