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
Compute ParametricGeometry normals from tangent vectors #11056
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ ParametricGeometry.prototype.constructor = ParametricGeometry; | |
|
||
import { BufferGeometry } from '../core/BufferGeometry'; | ||
import { Float32BufferAttribute } from '../core/BufferAttribute'; | ||
import { Vector3 } from '../math/Vector3'; | ||
|
||
function ParametricBufferGeometry( func, slices, stacks ) { | ||
|
||
|
@@ -53,11 +54,15 @@ function ParametricBufferGeometry( func, slices, stacks ) { | |
|
||
var indices = []; | ||
var vertices = []; | ||
var normals = []; | ||
var uvs = []; | ||
|
||
var EPS = 0.00001; | ||
var pu = new Vector3(), pv = new Vector3(), normal = new Vector3(); | ||
|
||
var i, j; | ||
|
||
// generate vertices and uvs | ||
// generate vertices, normals and uvs | ||
|
||
var sliceCount = slices + 1; | ||
|
||
|
@@ -72,6 +77,16 @@ function ParametricBufferGeometry( func, slices, stacks ) { | |
var p = func( u, v ); | ||
vertices.push( p.x, p.y, p.z ); | ||
|
||
// approximate tangent plane vectors via central difference | ||
|
||
pu.subVectors( func( u + EPS, v ), func( u - EPS, v ) ); | ||
pv.subVectors( func( u, v + EPS ), func( u, v - EPS ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Mugen87 Actually, there should be no reason to instantiate var result = optionalTarget || new THREE.Vector3();
...
return result.set( x, y, z ); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds reasonable. We can also adjust some entities in @jaxry Are you interested in doing this 😉 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Separate PR. |
||
|
||
// cross product of tangent plane vectors returns surface normal | ||
|
||
normal.crossVectors( pu, pv ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. normal.crossVectors( pu, pv ).normalize(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
normals.push( normal.x, normal.y, normal.z ); | ||
|
||
uvs.push( u, v ); | ||
|
||
} | ||
|
@@ -102,11 +117,10 @@ function ParametricBufferGeometry( func, slices, stacks ) { | |
|
||
this.setIndex( indices ); | ||
this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); | ||
this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); | ||
this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); | ||
|
||
// generate normals | ||
|
||
this.computeVertexNormals(); | ||
this.normalizeNormals(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed |
||
|
||
} | ||
|
||
|
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.
Is
func( u, v )
guaranteed to be well-defined for values ofu, v
outside [ 0, 1 ] ?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.
That's a good point! According to the implementation of
func()
, values outside [ 0, 1 ] can be problematic.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.
Then, use non-centered differences with a special case for the edge. By reusing
p
, only two additional calls are needed.