添加关照、全局等高线、修改图层问题

This commit is contained in:
2025-07-17 18:54:05 +08:00
parent c781d38c0c
commit b274b62671
4594 changed files with 791769 additions and 4921 deletions

View File

@ -0,0 +1,69 @@
import {
BufferGeometry,
Float32BufferAttribute
} from 'three';
class BoxLineGeometry extends BufferGeometry {
constructor( width = 1, height = 1, depth = 1, widthSegments = 1, heightSegments = 1, depthSegments = 1 ) {
super();
widthSegments = Math.floor( widthSegments );
heightSegments = Math.floor( heightSegments );
depthSegments = Math.floor( depthSegments );
const widthHalf = width / 2;
const heightHalf = height / 2;
const depthHalf = depth / 2;
const segmentWidth = width / widthSegments;
const segmentHeight = height / heightSegments;
const segmentDepth = depth / depthSegments;
const vertices = [];
let x = - widthHalf;
let y = - heightHalf;
let z = - depthHalf;
for ( let i = 0; i <= widthSegments; i ++ ) {
vertices.push( x, - heightHalf, - depthHalf, x, heightHalf, - depthHalf );
vertices.push( x, heightHalf, - depthHalf, x, heightHalf, depthHalf );
vertices.push( x, heightHalf, depthHalf, x, - heightHalf, depthHalf );
vertices.push( x, - heightHalf, depthHalf, x, - heightHalf, - depthHalf );
x += segmentWidth;
}
for ( let i = 0; i <= heightSegments; i ++ ) {
vertices.push( - widthHalf, y, - depthHalf, widthHalf, y, - depthHalf );
vertices.push( widthHalf, y, - depthHalf, widthHalf, y, depthHalf );
vertices.push( widthHalf, y, depthHalf, - widthHalf, y, depthHalf );
vertices.push( - widthHalf, y, depthHalf, - widthHalf, y, - depthHalf );
y += segmentHeight;
}
for ( let i = 0; i <= depthSegments; i ++ ) {
vertices.push( - widthHalf, - heightHalf, z, - widthHalf, heightHalf, z );
vertices.push( - widthHalf, heightHalf, z, widthHalf, heightHalf, z );
vertices.push( widthHalf, heightHalf, z, widthHalf, - heightHalf, z );
vertices.push( widthHalf, - heightHalf, z, - widthHalf, - heightHalf, z );
z += segmentDepth;
}
this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
}
}
export { BoxLineGeometry };

View File

@ -0,0 +1,53 @@
import {
BufferGeometry,
Float32BufferAttribute
} from 'three';
import { ConvexHull } from '../math/ConvexHull.js';
class ConvexGeometry extends BufferGeometry {
constructor( points = [] ) {
super();
// buffers
const vertices = [];
const normals = [];
const convexHull = new ConvexHull().setFromPoints( points );
// generate vertices and normals
const faces = convexHull.faces;
for ( let i = 0; i < faces.length; i ++ ) {
const face = faces[ i ];
let edge = face.edge;
// we move along a doubly-connected edge list to access all face points (see HalfEdge docs)
do {
const point = edge.head().point;
vertices.push( point.x, point.y, point.z );
normals.push( face.normal.x, face.normal.y, face.normal.z );
edge = edge.next;
} while ( edge !== face.edge );
}
// build geometry
this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
}
}
export { ConvexGeometry };

View File

@ -0,0 +1,356 @@
import {
BufferGeometry,
Float32BufferAttribute,
Matrix4,
Vector3
} from 'three';
/**
* You can use this geometry to create a decal mesh, that serves different kinds of purposes.
* e.g. adding unique details to models, performing dynamic visual environmental changes or covering seams.
*
* Constructor parameter:
*
* mesh — Any mesh object
* position — Position of the decal projector
* orientation — Orientation of the decal projector
* size — Size of the decal projector
*
* reference: http://blog.wolfire.com/2009/06/how-to-project-decals/
*
*/
class DecalGeometry extends BufferGeometry {
constructor( mesh, position, orientation, size ) {
super();
// buffers
const vertices = [];
const normals = [];
const uvs = [];
// helpers
const plane = new Vector3();
// this matrix represents the transformation of the decal projector
const projectorMatrix = new Matrix4();
projectorMatrix.makeRotationFromEuler( orientation );
projectorMatrix.setPosition( position );
const projectorMatrixInverse = new Matrix4();
projectorMatrixInverse.copy( projectorMatrix ).invert();
// generate buffers
generate();
// build geometry
this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
function generate() {
let decalVertices = [];
const vertex = new Vector3();
const normal = new Vector3();
// handle different geometry types
const geometry = mesh.geometry;
const positionAttribute = geometry.attributes.position;
const normalAttribute = geometry.attributes.normal;
// first, create an array of 'DecalVertex' objects
// three consecutive 'DecalVertex' objects represent a single face
//
// this data structure will be later used to perform the clipping
if ( geometry.index !== null ) {
// indexed BufferGeometry
const index = geometry.index;
for ( let i = 0; i < index.count; i ++ ) {
vertex.fromBufferAttribute( positionAttribute, index.getX( i ) );
normal.fromBufferAttribute( normalAttribute, index.getX( i ) );
pushDecalVertex( decalVertices, vertex, normal );
}
} else {
// non-indexed BufferGeometry
for ( let i = 0; i < positionAttribute.count; i ++ ) {
vertex.fromBufferAttribute( positionAttribute, i );
normal.fromBufferAttribute( normalAttribute, i );
pushDecalVertex( decalVertices, vertex, normal );
}
}
// second, clip the geometry so that it doesn't extend out from the projector
decalVertices = clipGeometry( decalVertices, plane.set( 1, 0, 0 ) );
decalVertices = clipGeometry( decalVertices, plane.set( - 1, 0, 0 ) );
decalVertices = clipGeometry( decalVertices, plane.set( 0, 1, 0 ) );
decalVertices = clipGeometry( decalVertices, plane.set( 0, - 1, 0 ) );
decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, 1 ) );
decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, - 1 ) );
// third, generate final vertices, normals and uvs
for ( let i = 0; i < decalVertices.length; i ++ ) {
const decalVertex = decalVertices[ i ];
// create texture coordinates (we are still in projector space)
uvs.push(
0.5 + ( decalVertex.position.x / size.x ),
0.5 + ( decalVertex.position.y / size.y )
);
// transform the vertex back to world space
decalVertex.position.applyMatrix4( projectorMatrix );
// now create vertex and normal buffer data
vertices.push( decalVertex.position.x, decalVertex.position.y, decalVertex.position.z );
normals.push( decalVertex.normal.x, decalVertex.normal.y, decalVertex.normal.z );
}
}
function pushDecalVertex( decalVertices, vertex, normal ) {
// transform the vertex to world space, then to projector space
vertex.applyMatrix4( mesh.matrixWorld );
vertex.applyMatrix4( projectorMatrixInverse );
normal.transformDirection( mesh.matrixWorld );
decalVertices.push( new DecalVertex( vertex.clone(), normal.clone() ) );
}
function clipGeometry( inVertices, plane ) {
const outVertices = [];
const s = 0.5 * Math.abs( size.dot( plane ) );
// a single iteration clips one face,
// which consists of three consecutive 'DecalVertex' objects
for ( let i = 0; i < inVertices.length; i += 3 ) {
let total = 0;
let nV1;
let nV2;
let nV3;
let nV4;
const d1 = inVertices[ i + 0 ].position.dot( plane ) - s;
const d2 = inVertices[ i + 1 ].position.dot( plane ) - s;
const d3 = inVertices[ i + 2 ].position.dot( plane ) - s;
const v1Out = d1 > 0;
const v2Out = d2 > 0;
const v3Out = d3 > 0;
// calculate, how many vertices of the face lie outside of the clipping plane
total = ( v1Out ? 1 : 0 ) + ( v2Out ? 1 : 0 ) + ( v3Out ? 1 : 0 );
switch ( total ) {
case 0: {
// the entire face lies inside of the plane, no clipping needed
outVertices.push( inVertices[ i ] );
outVertices.push( inVertices[ i + 1 ] );
outVertices.push( inVertices[ i + 2 ] );
break;
}
case 1: {
// one vertex lies outside of the plane, perform clipping
if ( v1Out ) {
nV1 = inVertices[ i + 1 ];
nV2 = inVertices[ i + 2 ];
nV3 = clip( inVertices[ i ], nV1, plane, s );
nV4 = clip( inVertices[ i ], nV2, plane, s );
}
if ( v2Out ) {
nV1 = inVertices[ i ];
nV2 = inVertices[ i + 2 ];
nV3 = clip( inVertices[ i + 1 ], nV1, plane, s );
nV4 = clip( inVertices[ i + 1 ], nV2, plane, s );
outVertices.push( nV3 );
outVertices.push( nV2.clone() );
outVertices.push( nV1.clone() );
outVertices.push( nV2.clone() );
outVertices.push( nV3.clone() );
outVertices.push( nV4 );
break;
}
if ( v3Out ) {
nV1 = inVertices[ i ];
nV2 = inVertices[ i + 1 ];
nV3 = clip( inVertices[ i + 2 ], nV1, plane, s );
nV4 = clip( inVertices[ i + 2 ], nV2, plane, s );
}
outVertices.push( nV1.clone() );
outVertices.push( nV2.clone() );
outVertices.push( nV3 );
outVertices.push( nV4 );
outVertices.push( nV3.clone() );
outVertices.push( nV2.clone() );
break;
}
case 2: {
// two vertices lies outside of the plane, perform clipping
if ( ! v1Out ) {
nV1 = inVertices[ i ].clone();
nV2 = clip( nV1, inVertices[ i + 1 ], plane, s );
nV3 = clip( nV1, inVertices[ i + 2 ], plane, s );
outVertices.push( nV1 );
outVertices.push( nV2 );
outVertices.push( nV3 );
}
if ( ! v2Out ) {
nV1 = inVertices[ i + 1 ].clone();
nV2 = clip( nV1, inVertices[ i + 2 ], plane, s );
nV3 = clip( nV1, inVertices[ i ], plane, s );
outVertices.push( nV1 );
outVertices.push( nV2 );
outVertices.push( nV3 );
}
if ( ! v3Out ) {
nV1 = inVertices[ i + 2 ].clone();
nV2 = clip( nV1, inVertices[ i ], plane, s );
nV3 = clip( nV1, inVertices[ i + 1 ], plane, s );
outVertices.push( nV1 );
outVertices.push( nV2 );
outVertices.push( nV3 );
}
break;
}
case 3: {
// the entire face lies outside of the plane, so let's discard the corresponding vertices
break;
}
}
}
return outVertices;
}
function clip( v0, v1, p, s ) {
const d0 = v0.position.dot( p ) - s;
const d1 = v1.position.dot( p ) - s;
const s0 = d0 / ( d0 - d1 );
const v = new DecalVertex(
new Vector3(
v0.position.x + s0 * ( v1.position.x - v0.position.x ),
v0.position.y + s0 * ( v1.position.y - v0.position.y ),
v0.position.z + s0 * ( v1.position.z - v0.position.z )
),
new Vector3(
v0.normal.x + s0 * ( v1.normal.x - v0.normal.x ),
v0.normal.y + s0 * ( v1.normal.y - v0.normal.y ),
v0.normal.z + s0 * ( v1.normal.z - v0.normal.z )
)
);
// need to clip more values (texture coordinates)? do it this way:
// intersectpoint.value = a.value + s * ( b.value - a.value );
return v;
}
}
}
// helper
class DecalVertex {
constructor( position, normal ) {
this.position = position;
this.normal = normal;
}
clone() {
return new this.constructor( this.position.clone(), this.normal.clone() );
}
}
export { DecalGeometry, DecalVertex };

View File

@ -0,0 +1,174 @@
import {
Box3,
Float32BufferAttribute,
InstancedBufferGeometry,
InstancedBufferAttribute,
Sphere,
Vector3
} from 'three';
const _vector = new Vector3();
class InstancedPointsGeometry extends InstancedBufferGeometry {
constructor() {
super();
this.isInstancedPointsGeometry = true;
this.type = 'InstancedPointsGeometry';
const positions = [ - 1, 1, 0, 1, 1, 0, - 1, - 1, 0, 1, - 1, 0 ];
const uvs = [ - 1, 1, 1, 1, - 1, - 1, 1, - 1 ];
const index = [ 0, 2, 1, 2, 3, 1 ];
this.setIndex( index );
this.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
}
applyMatrix4( matrix ) {
const pos = this.attributes.instancePosition;
if ( pos !== undefined ) {
pos.applyMatrix4( matrix );
pos.needsUpdate = true;
}
if ( this.boundingBox !== null ) {
this.computeBoundingBox();
}
if ( this.boundingSphere !== null ) {
this.computeBoundingSphere();
}
return this;
}
setPositions( array ) {
let points;
if ( array instanceof Float32Array ) {
points = array;
} else if ( Array.isArray( array ) ) {
points = new Float32Array( array );
}
this.setAttribute( 'instancePosition', new InstancedBufferAttribute( points, 3 ) ); // xyz
//
this.computeBoundingBox();
this.computeBoundingSphere();
return this;
}
setColors( array ) {
let colors;
if ( array instanceof Float32Array ) {
colors = array;
} else if ( Array.isArray( array ) ) {
colors = new Float32Array( array );
}
this.setAttribute( 'instanceColor', new InstancedBufferAttribute( colors, 3 ) ); // rgb
return this;
}
computeBoundingBox() {
if ( this.boundingBox === null ) {
this.boundingBox = new Box3();
}
const pos = this.attributes.instancePosition;
if ( pos !== undefined ) {
this.boundingBox.setFromBufferAttribute( pos );
}
}
computeBoundingSphere() {
if ( this.boundingSphere === null ) {
this.boundingSphere = new Sphere();
}
if ( this.boundingBox === null ) {
this.computeBoundingBox();
}
const pos = this.attributes.instancePosition;
if ( pos !== undefined ) {
const center = this.boundingSphere.center;
this.boundingBox.getCenter( center );
let maxRadiusSq = 0;
for ( let i = 0, il = pos.count; i < il; i ++ ) {
_vector.fromBufferAttribute( pos, i );
maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
}
this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
if ( isNaN( this.boundingSphere.radius ) ) {
console.error( 'THREE.InstancedPointsGeometry.computeBoundingSphere(): Computed radius is NaN. The instanced position data is likely to have NaN values.', this );
}
}
}
toJSON() {
// todo
}
}
export default InstancedPointsGeometry;

View File

@ -0,0 +1,254 @@
import {
Curve,
Vector3
} from 'three';
import { ParametricGeometry } from './ParametricGeometry.js';
/**
* Experimenting of primitive geometry creation using Surface Parametric equations
*/
const ParametricGeometries = {
klein: function ( v, u, target ) {
u *= Math.PI;
v *= 2 * Math.PI;
u = u * 2;
let x, z;
if ( u < Math.PI ) {
x = 3 * Math.cos( u ) * ( 1 + Math.sin( u ) ) + ( 2 * ( 1 - Math.cos( u ) / 2 ) ) * Math.cos( u ) * Math.cos( v );
z = - 8 * Math.sin( u ) - 2 * ( 1 - Math.cos( u ) / 2 ) * Math.sin( u ) * Math.cos( v );
} else {
x = 3 * Math.cos( u ) * ( 1 + Math.sin( u ) ) + ( 2 * ( 1 - Math.cos( u ) / 2 ) ) * Math.cos( v + Math.PI );
z = - 8 * Math.sin( u );
}
const y = - 2 * ( 1 - Math.cos( u ) / 2 ) * Math.sin( v );
target.set( x, y, z );
},
plane: function ( width, height ) {
return function ( u, v, target ) {
const x = u * width;
const y = 0;
const z = v * height;
target.set( x, y, z );
};
},
mobius: function ( u, t, target ) {
// flat mobius strip
// http://www.wolframalpha.com/input/?i=M%C3%B6bius+strip+parametric+equations&lk=1&a=ClashPrefs_*Surface.MoebiusStrip.SurfaceProperty.ParametricEquations-
u = u - 0.5;
const v = 2 * Math.PI * t;
const a = 2;
const x = Math.cos( v ) * ( a + u * Math.cos( v / 2 ) );
const y = Math.sin( v ) * ( a + u * Math.cos( v / 2 ) );
const z = u * Math.sin( v / 2 );
target.set( x, y, z );
},
mobius3d: function ( u, t, target ) {
// volumetric mobius strip
u *= Math.PI;
t *= 2 * Math.PI;
u = u * 2;
const phi = u / 2;
const major = 2.25, a = 0.125, b = 0.65;
let x = a * Math.cos( t ) * Math.cos( phi ) - b * Math.sin( t ) * Math.sin( phi );
const z = a * Math.cos( t ) * Math.sin( phi ) + b * Math.sin( t ) * Math.cos( phi );
const y = ( major + x ) * Math.sin( u );
x = ( major + x ) * Math.cos( u );
target.set( x, y, z );
}
};
/*********************************************
*
* Parametric Replacement for TubeGeometry
*
*********************************************/
ParametricGeometries.TubeGeometry = class TubeGeometry extends ParametricGeometry {
constructor( path, segments = 64, radius = 1, segmentsRadius = 8, closed = false ) {
const numpoints = segments + 1;
const frames = path.computeFrenetFrames( segments, closed ),
tangents = frames.tangents,
normals = frames.normals,
binormals = frames.binormals;
const position = new Vector3();
function ParametricTube( u, v, target ) {
v *= 2 * Math.PI;
const i = Math.floor( u * ( numpoints - 1 ) );
path.getPointAt( u, position );
const normal = normals[ i ];
const binormal = binormals[ i ];
const cx = - radius * Math.cos( v ); // TODO: Hack: Negating it so it faces outside.
const cy = radius * Math.sin( v );
position.x += cx * normal.x + cy * binormal.x;
position.y += cx * normal.y + cy * binormal.y;
position.z += cx * normal.z + cy * binormal.z;
target.copy( position );
}
super( ParametricTube, segments, segmentsRadius );
// proxy internals
this.tangents = tangents;
this.normals = normals;
this.binormals = binormals;
this.path = path;
this.segments = segments;
this.radius = radius;
this.segmentsRadius = segmentsRadius;
this.closed = closed;
}
};
/*********************************************
*
* Parametric Replacement for TorusKnotGeometry
*
*********************************************/
ParametricGeometries.TorusKnotGeometry = class TorusKnotGeometry extends ParametricGeometries.TubeGeometry {
constructor( radius = 200, tube = 40, segmentsT = 64, segmentsR = 8, p = 2, q = 3 ) {
class TorusKnotCurve extends Curve {
getPoint( t, optionalTarget = new Vector3() ) {
const point = optionalTarget;
t *= Math.PI * 2;
const r = 0.5;
const x = ( 1 + r * Math.cos( q * t ) ) * Math.cos( p * t );
const y = ( 1 + r * Math.cos( q * t ) ) * Math.sin( p * t );
const z = r * Math.sin( q * t );
return point.set( x, y, z ).multiplyScalar( radius );
}
}
const segments = segmentsT;
const radiusSegments = segmentsR;
const extrudePath = new TorusKnotCurve();
super( extrudePath, segments, tube, radiusSegments, true, false );
this.radius = radius;
this.tube = tube;
this.segmentsT = segmentsT;
this.segmentsR = segmentsR;
this.p = p;
this.q = q;
}
};
/*********************************************
*
* Parametric Replacement for SphereGeometry
*
*********************************************/
ParametricGeometries.SphereGeometry = class SphereGeometry extends ParametricGeometry {
constructor( size, u, v ) {
function sphere( u, v, target ) {
u *= Math.PI;
v *= 2 * Math.PI;
const x = size * Math.sin( u ) * Math.cos( v );
const y = size * Math.sin( u ) * Math.sin( v );
const z = size * Math.cos( u );
target.set( x, y, z );
}
super( sphere, u, v );
}
};
/*********************************************
*
* Parametric Replacement for PlaneGeometry
*
*********************************************/
ParametricGeometries.PlaneGeometry = class PlaneGeometry extends ParametricGeometry {
constructor( width, depth, segmentsWidth, segmentsDepth ) {
function plane( u, v, target ) {
const x = u * width;
const y = 0;
const z = v * depth;
target.set( x, y, z );
}
super( plane, segmentsWidth, segmentsDepth );
}
};
export { ParametricGeometries };

View File

@ -0,0 +1,139 @@
/**
* Parametric Surfaces Geometry
* based on the brilliant article by @prideout https://prideout.net/blog/old/blog/index.html@p=44.html
*/
import {
BufferGeometry,
Float32BufferAttribute,
Vector3
} from 'three';
class ParametricGeometry extends BufferGeometry {
constructor( func = ( u, v, target ) => target.set( u, v, Math.cos( u ) * Math.sin( v ) ), slices = 8, stacks = 8 ) {
super();
this.type = 'ParametricGeometry';
this.parameters = {
func: func,
slices: slices,
stacks: stacks
};
// buffers
const indices = [];
const vertices = [];
const normals = [];
const uvs = [];
const EPS = 0.00001;
const normal = new Vector3();
const p0 = new Vector3(), p1 = new Vector3();
const pu = new Vector3(), pv = new Vector3();
// generate vertices, normals and uvs
const sliceCount = slices + 1;
for ( let i = 0; i <= stacks; i ++ ) {
const v = i / stacks;
for ( let j = 0; j <= slices; j ++ ) {
const u = j / slices;
// vertex
func( u, v, p0 );
vertices.push( p0.x, p0.y, p0.z );
// normal
// approximate tangent vectors via finite differences
if ( u - EPS >= 0 ) {
func( u - EPS, v, p1 );
pu.subVectors( p0, p1 );
} else {
func( u + EPS, v, p1 );
pu.subVectors( p1, p0 );
}
if ( v - EPS >= 0 ) {
func( u, v - EPS, p1 );
pv.subVectors( p0, p1 );
} else {
func( u, v + EPS, p1 );
pv.subVectors( p1, p0 );
}
// cross product of tangent vectors returns surface normal
normal.crossVectors( pu, pv ).normalize();
normals.push( normal.x, normal.y, normal.z );
// uv
uvs.push( u, v );
}
}
// generate indices
for ( let i = 0; i < stacks; i ++ ) {
for ( let j = 0; j < slices; j ++ ) {
const a = i * sliceCount + j;
const b = i * sliceCount + j + 1;
const c = ( i + 1 ) * sliceCount + j + 1;
const d = ( i + 1 ) * sliceCount + j;
// faces one and two
indices.push( a, b, d );
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;
}
}
export { ParametricGeometry };

View File

@ -0,0 +1,155 @@
import {
BoxGeometry,
Vector3
} from 'three';
const _tempNormal = new Vector3();
function getUv( faceDirVector, normal, uvAxis, projectionAxis, radius, sideLength ) {
const totArcLength = 2 * Math.PI * radius / 4;
// length of the planes between the arcs on each axis
const centerLength = Math.max( sideLength - 2 * radius, 0 );
const halfArc = Math.PI / 4;
// Get the vector projected onto the Y plane
_tempNormal.copy( normal );
_tempNormal[ projectionAxis ] = 0;
_tempNormal.normalize();
// total amount of UV space alloted to a single arc
const arcUvRatio = 0.5 * totArcLength / ( totArcLength + centerLength );
// the distance along one arc the point is at
const arcAngleRatio = 1.0 - ( _tempNormal.angleTo( faceDirVector ) / halfArc );
if ( Math.sign( _tempNormal[ uvAxis ] ) === 1 ) {
return arcAngleRatio * arcUvRatio;
} else {
// total amount of UV space alloted to the plane between the arcs
const lenUv = centerLength / ( totArcLength + centerLength );
return lenUv + arcUvRatio + arcUvRatio * ( 1.0 - arcAngleRatio );
}
}
class RoundedBoxGeometry extends BoxGeometry {
constructor( width = 1, height = 1, depth = 1, segments = 2, radius = 0.1 ) {
// ensure segments is odd so we have a plane connecting the rounded corners
segments = segments * 2 + 1;
// ensure radius isn't bigger than shortest side
radius = Math.min( width / 2, height / 2, depth / 2, radius );
super( 1, 1, 1, segments, segments, segments );
// if we just have one segment we're the same as a regular box
if ( segments === 1 ) return;
const geometry2 = this.toNonIndexed();
this.index = null;
this.attributes.position = geometry2.attributes.position;
this.attributes.normal = geometry2.attributes.normal;
this.attributes.uv = geometry2.attributes.uv;
//
const position = new Vector3();
const normal = new Vector3();
const box = new Vector3( width, height, depth ).divideScalar( 2 ).subScalar( radius );
const positions = this.attributes.position.array;
const normals = this.attributes.normal.array;
const uvs = this.attributes.uv.array;
const faceTris = positions.length / 6;
const faceDirVector = new Vector3();
const halfSegmentSize = 0.5 / segments;
for ( let i = 0, j = 0; i < positions.length; i += 3, j += 2 ) {
position.fromArray( positions, i );
normal.copy( position );
normal.x -= Math.sign( normal.x ) * halfSegmentSize;
normal.y -= Math.sign( normal.y ) * halfSegmentSize;
normal.z -= Math.sign( normal.z ) * halfSegmentSize;
normal.normalize();
positions[ i + 0 ] = box.x * Math.sign( position.x ) + normal.x * radius;
positions[ i + 1 ] = box.y * Math.sign( position.y ) + normal.y * radius;
positions[ i + 2 ] = box.z * Math.sign( position.z ) + normal.z * radius;
normals[ i + 0 ] = normal.x;
normals[ i + 1 ] = normal.y;
normals[ i + 2 ] = normal.z;
const side = Math.floor( i / faceTris );
switch ( side ) {
case 0: // right
// generate UVs along Z then Y
faceDirVector.set( 1, 0, 0 );
uvs[ j + 0 ] = getUv( faceDirVector, normal, 'z', 'y', radius, depth );
uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'z', radius, height );
break;
case 1: // left
// generate UVs along Z then Y
faceDirVector.set( - 1, 0, 0 );
uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'z', 'y', radius, depth );
uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'z', radius, height );
break;
case 2: // top
// generate UVs along X then Z
faceDirVector.set( 0, 1, 0 );
uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'x', 'z', radius, width );
uvs[ j + 1 ] = getUv( faceDirVector, normal, 'z', 'x', radius, depth );
break;
case 3: // bottom
// generate UVs along X then Z
faceDirVector.set( 0, - 1, 0 );
uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'x', 'z', radius, width );
uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'z', 'x', radius, depth );
break;
case 4: // front
// generate UVs along X then Y
faceDirVector.set( 0, 0, 1 );
uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'x', 'y', radius, width );
uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'x', radius, height );
break;
case 5: // back
// generate UVs along X then Y
faceDirVector.set( 0, 0, - 1 );
uvs[ j + 0 ] = getUv( faceDirVector, normal, 'x', 'y', radius, width );
uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'x', radius, height );
break;
}
}
}
}
export { RoundedBoxGeometry };

View File

@ -0,0 +1,144 @@
/**
* @author santiago / @glitch_life
* wrapper of https://www.npmjs.com/package/isosurface by https://github.com/mikolalysenko
*
* Returns BufferGeometry from SDF
*/
import {
BufferAttribute,
BufferGeometry,
FloatType,
Mesh,
OrthographicCamera,
PlaneGeometry,
Scene,
ShaderMaterial,
Vector2,
WebGLRenderTarget
} from 'three';
import { surfaceNet } from './../libs/surfaceNet.js';
class SDFGeometryGenerator {
constructor( renderer ) {
this.renderer = renderer;
}
generate( res = 64, distFunc = 'float dist( vec3 p ){ return length(p) - 0.5; }', bounds = 1 ) {
let w, h;
if ( res == 8 ) [ w, h ] = [ 32, 16 ];
else if ( res == 16 ) [ w, h ] = [ 64, 64 ];
else if ( res == 32 ) [ w, h ] = [ 256, 128 ];
else if ( res == 64 ) [ w, h ] = [ 512, 512 ];
else if ( res == 128 ) [ w, h ] = [ 2048, 1024 ];
else if ( res == 256 ) [ w, h ] = [ 4096, 4096 ];
else if ( res == 512 ) [ w, h ] = [ 16384, 8096 ];
else if ( res == 1024 ) [ w, h ] = [ 32768, 32768 ];
else throw new Error( 'THREE.SDFGeometryGenerator: Resolution must be in range 8 < res < 1024 and must be ^2' );
const maxTexSize = this.renderer.capabilities.maxTextureSize;
if ( w > maxTexSize || h > maxTexSize ) throw new Error( 'THREE.SDFGeometryGenerator: Your device does not support this resolution ( ' + res + ' ), decrease [res] param.' );
const [ tilesX, tilesY ] = [ ( w / res ), ( h / res ) ];
const sdfCompute = `
varying vec2 vUv;
uniform float tileNum;
uniform float bounds;
[#dist#]
void main() { gl_FragColor=vec4( ( dist( vec3( vUv, tileNum ) * 2.0 * bounds - vec3( bounds ) ) < 0.00001 ) ? 1.0 : 0.0 ); }
`;
const sdfRT = this.computeSDF( w, h, tilesX, tilesY, bounds, sdfCompute.replace( '[#dist#]', distFunc ) );
const read = new Float32Array( w * h * 4 );
this.renderer.readRenderTargetPixels( sdfRT, 0, 0, w, h, read );
sdfRT.dispose();
//
const mesh = surfaceNet( [ res, res, res ], ( x, y, z ) => {
x = ( x + bounds ) * ( res / ( bounds * 2 ) );
y = ( y + bounds ) * ( res / ( bounds * 2 ) );
z = ( z + bounds ) * ( res / ( bounds * 2 ) );
let p = ( x + ( z % tilesX ) * res ) + y * w + ( Math.floor( z / tilesX ) * res * w );
p *= 4;
return ( read[ p + 3 ] > 0 ) ? - 0.000000001 : 1;
}, [[ - bounds, - bounds, - bounds ], [ bounds, bounds, bounds ]] );
const ps = [], ids = [];
const geometry = new BufferGeometry();
mesh.positions.forEach( p => {
ps.push( p[ 0 ], p[ 1 ], p[ 2 ] );
} );
mesh.cells.forEach( p => ids.push( p[ 0 ], p[ 1 ], p[ 2 ] ) );
geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( ps ), 3 ) );
geometry.setIndex( ids );
return geometry;
}
computeSDF( width, height, tilesX, tilesY, bounds, shader ) {
const rt = new WebGLRenderTarget( width, height, { type: FloatType } );
const scn = new Scene();
const cam = new OrthographicCamera();
const tiles = tilesX * tilesY;
let currentTile = 0;
Object.assign( cam, { left: width / - 2, right: width / 2, top: height / 2, bottom: height / - 2 } ).updateProjectionMatrix();
cam.position.z = 2;
const tileSize = width / tilesX;
const geometry = new PlaneGeometry( tileSize, tileSize );
while ( currentTile ++ < tiles ) {
const c = currentTile - 1;
const [ px, py ] = [ ( tileSize ) / 2 + ( c % tilesX ) * ( tileSize ) - width / 2, ( tileSize ) / 2 + Math.floor( c / tilesX ) * ( tileSize ) - height / 2 ];
const compPlane = new Mesh( geometry, new ShaderMaterial( {
uniforms: {
res: { value: new Vector2( width, height ) },
tileNum: { value: c / ( tilesX * tilesY - 1 ) },
bounds: { value: bounds }
},
vertexShader: 'varying vec2 vUv;void main(){vUv=uv;gl_Position=projectionMatrix*modelViewMatrix*vec4(position,1.0);}',
fragmentShader: shader
} ) );
compPlane.position.set( px, py, 0 );
scn.add( compPlane );
}
this.renderer.setRenderTarget( rt );
this.renderer.render( scn, cam );
this.renderer.setRenderTarget( null );
//
geometry.dispose();
scn.traverse( function ( object ) {
if ( object.material !== undefined ) object.material.dispose();
} );
return rt;
}
}
export { SDFGeometryGenerator };

View File

@ -0,0 +1,704 @@
import {
BufferAttribute,
BufferGeometry,
Matrix4,
Vector3,
Vector4
} from 'three';
/**
* Tessellates the famous Utah teapot database by Martin Newell into triangles.
*
* Parameters: size = 50, segments = 10, bottom = true, lid = true, body = true,
* fitLid = false, blinn = true
*
* size is a relative scale: I've scaled the teapot to fit vertically between -1 and 1.
* Think of it as a "radius".
* segments - number of line segments to subdivide each patch edge;
* 1 is possible but gives degenerates, so two is the real minimum.
* bottom - boolean, if true (default) then the bottom patches are added. Some consider
* adding the bottom heresy, so set this to "false" to adhere to the One True Way.
* lid - to remove the lid and look inside, set to true.
* body - to remove the body and leave the lid, set this and "bottom" to false.
* fitLid - the lid is a tad small in the original. This stretches it a bit so you can't
* see the teapot's insides through the gap.
* blinn - Jim Blinn scaled the original data vertically by dividing by about 1.3 to look
* nicer. If you want to see the original teapot, similar to the real-world model, set
* this to false. True by default.
* See http://en.wikipedia.org/wiki/File:Original_Utah_Teapot.jpg for the original
* real-world teapot (from http://en.wikipedia.org/wiki/Utah_teapot).
*
* Note that the bottom (the last four patches) is not flat - blame Frank Crow, not me.
*
* The teapot should normally be rendered as a double sided object, since for some
* patches both sides can be seen, e.g., the gap around the lid and inside the spout.
*
* Segments 'n' determines the number of triangles output.
* Total triangles = 32*2*n*n - 8*n [degenerates at the top and bottom cusps are deleted]
*
* size_factor # triangles
* 1 56
* 2 240
* 3 552
* 4 992
*
* 10 6320
* 20 25440
* 30 57360
*
* Code converted from my ancient SPD software, http://tog.acm.org/resources/SPD/
* Created for the Udacity course "Interactive Rendering", http://bit.ly/ericity
* YouTube video on teapot history: https://www.youtube.com/watch?v=DxMfblPzFNc
*
* See https://en.wikipedia.org/wiki/Utah_teapot for the history of the teapot
*
*/
class TeapotGeometry extends BufferGeometry {
constructor( size = 50, segments = 10, bottom = true, lid = true, body = true, fitLid = true, blinn = true ) {
// 32 * 4 * 4 Bezier spline patches
const teapotPatches = [
/*rim*/
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
3, 16, 17, 18, 7, 19, 20, 21, 11, 22, 23, 24, 15, 25, 26, 27,
18, 28, 29, 30, 21, 31, 32, 33, 24, 34, 35, 36, 27, 37, 38, 39,
30, 40, 41, 0, 33, 42, 43, 4, 36, 44, 45, 8, 39, 46, 47, 12,
/*body*/
12, 13, 14, 15, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
15, 25, 26, 27, 51, 60, 61, 62, 55, 63, 64, 65, 59, 66, 67, 68,
27, 37, 38, 39, 62, 69, 70, 71, 65, 72, 73, 74, 68, 75, 76, 77,
39, 46, 47, 12, 71, 78, 79, 48, 74, 80, 81, 52, 77, 82, 83, 56,
56, 57, 58, 59, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
59, 66, 67, 68, 87, 96, 97, 98, 91, 99, 100, 101, 95, 102, 103, 104,
68, 75, 76, 77, 98, 105, 106, 107, 101, 108, 109, 110, 104, 111, 112, 113,
77, 82, 83, 56, 107, 114, 115, 84, 110, 116, 117, 88, 113, 118, 119, 92,
/*handle*/
120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
123, 136, 137, 120, 127, 138, 139, 124, 131, 140, 141, 128, 135, 142, 143, 132,
132, 133, 134, 135, 144, 145, 146, 147, 148, 149, 150, 151, 68, 152, 153, 154,
135, 142, 143, 132, 147, 155, 156, 144, 151, 157, 158, 148, 154, 159, 160, 68,
/*spout*/
161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176,
164, 177, 178, 161, 168, 179, 180, 165, 172, 181, 182, 169, 176, 183, 184, 173,
173, 174, 175, 176, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
176, 183, 184, 173, 188, 197, 198, 185, 192, 199, 200, 189, 196, 201, 202, 193,
/*lid*/
203, 203, 203, 203, 204, 205, 206, 207, 208, 208, 208, 208, 209, 210, 211, 212,
203, 203, 203, 203, 207, 213, 214, 215, 208, 208, 208, 208, 212, 216, 217, 218,
203, 203, 203, 203, 215, 219, 220, 221, 208, 208, 208, 208, 218, 222, 223, 224,
203, 203, 203, 203, 221, 225, 226, 204, 208, 208, 208, 208, 224, 227, 228, 209,
209, 210, 211, 212, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
212, 216, 217, 218, 232, 241, 242, 243, 236, 244, 245, 246, 240, 247, 248, 249,
218, 222, 223, 224, 243, 250, 251, 252, 246, 253, 254, 255, 249, 256, 257, 258,
224, 227, 228, 209, 252, 259, 260, 229, 255, 261, 262, 233, 258, 263, 264, 237,
/*bottom*/
265, 265, 265, 265, 266, 267, 268, 269, 270, 271, 272, 273, 92, 119, 118, 113,
265, 265, 265, 265, 269, 274, 275, 276, 273, 277, 278, 279, 113, 112, 111, 104,
265, 265, 265, 265, 276, 280, 281, 282, 279, 283, 284, 285, 104, 103, 102, 95,
265, 265, 265, 265, 282, 286, 287, 266, 285, 288, 289, 270, 95, 94, 93, 92
];
const teapotVertices = [
1.4, 0, 2.4,
1.4, - 0.784, 2.4,
0.784, - 1.4, 2.4,
0, - 1.4, 2.4,
1.3375, 0, 2.53125,
1.3375, - 0.749, 2.53125,
0.749, - 1.3375, 2.53125,
0, - 1.3375, 2.53125,
1.4375, 0, 2.53125,
1.4375, - 0.805, 2.53125,
0.805, - 1.4375, 2.53125,
0, - 1.4375, 2.53125,
1.5, 0, 2.4,
1.5, - 0.84, 2.4,
0.84, - 1.5, 2.4,
0, - 1.5, 2.4,
- 0.784, - 1.4, 2.4,
- 1.4, - 0.784, 2.4,
- 1.4, 0, 2.4,
- 0.749, - 1.3375, 2.53125,
- 1.3375, - 0.749, 2.53125,
- 1.3375, 0, 2.53125,
- 0.805, - 1.4375, 2.53125,
- 1.4375, - 0.805, 2.53125,
- 1.4375, 0, 2.53125,
- 0.84, - 1.5, 2.4,
- 1.5, - 0.84, 2.4,
- 1.5, 0, 2.4,
- 1.4, 0.784, 2.4,
- 0.784, 1.4, 2.4,
0, 1.4, 2.4,
- 1.3375, 0.749, 2.53125,
- 0.749, 1.3375, 2.53125,
0, 1.3375, 2.53125,
- 1.4375, 0.805, 2.53125,
- 0.805, 1.4375, 2.53125,
0, 1.4375, 2.53125,
- 1.5, 0.84, 2.4,
- 0.84, 1.5, 2.4,
0, 1.5, 2.4,
0.784, 1.4, 2.4,
1.4, 0.784, 2.4,
0.749, 1.3375, 2.53125,
1.3375, 0.749, 2.53125,
0.805, 1.4375, 2.53125,
1.4375, 0.805, 2.53125,
0.84, 1.5, 2.4,
1.5, 0.84, 2.4,
1.75, 0, 1.875,
1.75, - 0.98, 1.875,
0.98, - 1.75, 1.875,
0, - 1.75, 1.875,
2, 0, 1.35,
2, - 1.12, 1.35,
1.12, - 2, 1.35,
0, - 2, 1.35,
2, 0, 0.9,
2, - 1.12, 0.9,
1.12, - 2, 0.9,
0, - 2, 0.9,
- 0.98, - 1.75, 1.875,
- 1.75, - 0.98, 1.875,
- 1.75, 0, 1.875,
- 1.12, - 2, 1.35,
- 2, - 1.12, 1.35,
- 2, 0, 1.35,
- 1.12, - 2, 0.9,
- 2, - 1.12, 0.9,
- 2, 0, 0.9,
- 1.75, 0.98, 1.875,
- 0.98, 1.75, 1.875,
0, 1.75, 1.875,
- 2, 1.12, 1.35,
- 1.12, 2, 1.35,
0, 2, 1.35,
- 2, 1.12, 0.9,
- 1.12, 2, 0.9,
0, 2, 0.9,
0.98, 1.75, 1.875,
1.75, 0.98, 1.875,
1.12, 2, 1.35,
2, 1.12, 1.35,
1.12, 2, 0.9,
2, 1.12, 0.9,
2, 0, 0.45,
2, - 1.12, 0.45,
1.12, - 2, 0.45,
0, - 2, 0.45,
1.5, 0, 0.225,
1.5, - 0.84, 0.225,
0.84, - 1.5, 0.225,
0, - 1.5, 0.225,
1.5, 0, 0.15,
1.5, - 0.84, 0.15,
0.84, - 1.5, 0.15,
0, - 1.5, 0.15,
- 1.12, - 2, 0.45,
- 2, - 1.12, 0.45,
- 2, 0, 0.45,
- 0.84, - 1.5, 0.225,
- 1.5, - 0.84, 0.225,
- 1.5, 0, 0.225,
- 0.84, - 1.5, 0.15,
- 1.5, - 0.84, 0.15,
- 1.5, 0, 0.15,
- 2, 1.12, 0.45,
- 1.12, 2, 0.45,
0, 2, 0.45,
- 1.5, 0.84, 0.225,
- 0.84, 1.5, 0.225,
0, 1.5, 0.225,
- 1.5, 0.84, 0.15,
- 0.84, 1.5, 0.15,
0, 1.5, 0.15,
1.12, 2, 0.45,
2, 1.12, 0.45,
0.84, 1.5, 0.225,
1.5, 0.84, 0.225,
0.84, 1.5, 0.15,
1.5, 0.84, 0.15,
- 1.6, 0, 2.025,
- 1.6, - 0.3, 2.025,
- 1.5, - 0.3, 2.25,
- 1.5, 0, 2.25,
- 2.3, 0, 2.025,
- 2.3, - 0.3, 2.025,
- 2.5, - 0.3, 2.25,
- 2.5, 0, 2.25,
- 2.7, 0, 2.025,
- 2.7, - 0.3, 2.025,
- 3, - 0.3, 2.25,
- 3, 0, 2.25,
- 2.7, 0, 1.8,
- 2.7, - 0.3, 1.8,
- 3, - 0.3, 1.8,
- 3, 0, 1.8,
- 1.5, 0.3, 2.25,
- 1.6, 0.3, 2.025,
- 2.5, 0.3, 2.25,
- 2.3, 0.3, 2.025,
- 3, 0.3, 2.25,
- 2.7, 0.3, 2.025,
- 3, 0.3, 1.8,
- 2.7, 0.3, 1.8,
- 2.7, 0, 1.575,
- 2.7, - 0.3, 1.575,
- 3, - 0.3, 1.35,
- 3, 0, 1.35,
- 2.5, 0, 1.125,
- 2.5, - 0.3, 1.125,
- 2.65, - 0.3, 0.9375,
- 2.65, 0, 0.9375,
- 2, - 0.3, 0.9,
- 1.9, - 0.3, 0.6,
- 1.9, 0, 0.6,
- 3, 0.3, 1.35,
- 2.7, 0.3, 1.575,
- 2.65, 0.3, 0.9375,
- 2.5, 0.3, 1.125,
- 1.9, 0.3, 0.6,
- 2, 0.3, 0.9,
1.7, 0, 1.425,
1.7, - 0.66, 1.425,
1.7, - 0.66, 0.6,
1.7, 0, 0.6,
2.6, 0, 1.425,
2.6, - 0.66, 1.425,
3.1, - 0.66, 0.825,
3.1, 0, 0.825,
2.3, 0, 2.1,
2.3, - 0.25, 2.1,
2.4, - 0.25, 2.025,
2.4, 0, 2.025,
2.7, 0, 2.4,
2.7, - 0.25, 2.4,
3.3, - 0.25, 2.4,
3.3, 0, 2.4,
1.7, 0.66, 0.6,
1.7, 0.66, 1.425,
3.1, 0.66, 0.825,
2.6, 0.66, 1.425,
2.4, 0.25, 2.025,
2.3, 0.25, 2.1,
3.3, 0.25, 2.4,
2.7, 0.25, 2.4,
2.8, 0, 2.475,
2.8, - 0.25, 2.475,
3.525, - 0.25, 2.49375,
3.525, 0, 2.49375,
2.9, 0, 2.475,
2.9, - 0.15, 2.475,
3.45, - 0.15, 2.5125,
3.45, 0, 2.5125,
2.8, 0, 2.4,
2.8, - 0.15, 2.4,
3.2, - 0.15, 2.4,
3.2, 0, 2.4,
3.525, 0.25, 2.49375,
2.8, 0.25, 2.475,
3.45, 0.15, 2.5125,
2.9, 0.15, 2.475,
3.2, 0.15, 2.4,
2.8, 0.15, 2.4,
0, 0, 3.15,
0.8, 0, 3.15,
0.8, - 0.45, 3.15,
0.45, - 0.8, 3.15,
0, - 0.8, 3.15,
0, 0, 2.85,
0.2, 0, 2.7,
0.2, - 0.112, 2.7,
0.112, - 0.2, 2.7,
0, - 0.2, 2.7,
- 0.45, - 0.8, 3.15,
- 0.8, - 0.45, 3.15,
- 0.8, 0, 3.15,
- 0.112, - 0.2, 2.7,
- 0.2, - 0.112, 2.7,
- 0.2, 0, 2.7,
- 0.8, 0.45, 3.15,
- 0.45, 0.8, 3.15,
0, 0.8, 3.15,
- 0.2, 0.112, 2.7,
- 0.112, 0.2, 2.7,
0, 0.2, 2.7,
0.45, 0.8, 3.15,
0.8, 0.45, 3.15,
0.112, 0.2, 2.7,
0.2, 0.112, 2.7,
0.4, 0, 2.55,
0.4, - 0.224, 2.55,
0.224, - 0.4, 2.55,
0, - 0.4, 2.55,
1.3, 0, 2.55,
1.3, - 0.728, 2.55,
0.728, - 1.3, 2.55,
0, - 1.3, 2.55,
1.3, 0, 2.4,
1.3, - 0.728, 2.4,
0.728, - 1.3, 2.4,
0, - 1.3, 2.4,
- 0.224, - 0.4, 2.55,
- 0.4, - 0.224, 2.55,
- 0.4, 0, 2.55,
- 0.728, - 1.3, 2.55,
- 1.3, - 0.728, 2.55,
- 1.3, 0, 2.55,
- 0.728, - 1.3, 2.4,
- 1.3, - 0.728, 2.4,
- 1.3, 0, 2.4,
- 0.4, 0.224, 2.55,
- 0.224, 0.4, 2.55,
0, 0.4, 2.55,
- 1.3, 0.728, 2.55,
- 0.728, 1.3, 2.55,
0, 1.3, 2.55,
- 1.3, 0.728, 2.4,
- 0.728, 1.3, 2.4,
0, 1.3, 2.4,
0.224, 0.4, 2.55,
0.4, 0.224, 2.55,
0.728, 1.3, 2.55,
1.3, 0.728, 2.55,
0.728, 1.3, 2.4,
1.3, 0.728, 2.4,
0, 0, 0,
1.425, 0, 0,
1.425, 0.798, 0,
0.798, 1.425, 0,
0, 1.425, 0,
1.5, 0, 0.075,
1.5, 0.84, 0.075,
0.84, 1.5, 0.075,
0, 1.5, 0.075,
- 0.798, 1.425, 0,
- 1.425, 0.798, 0,
- 1.425, 0, 0,
- 0.84, 1.5, 0.075,
- 1.5, 0.84, 0.075,
- 1.5, 0, 0.075,
- 1.425, - 0.798, 0,
- 0.798, - 1.425, 0,
0, - 1.425, 0,
- 1.5, - 0.84, 0.075,
- 0.84, - 1.5, 0.075,
0, - 1.5, 0.075,
0.798, - 1.425, 0,
1.425, - 0.798, 0,
0.84, - 1.5, 0.075,
1.5, - 0.84, 0.075
];
super();
// number of segments per patch
segments = Math.max( 2, Math.floor( segments ) );
// Jim Blinn scaled the teapot down in size by about 1.3 for
// some rendering tests. He liked the new proportions that he kept
// the data in this form. The model was distributed with these new
// proportions and became the norm. Trivia: comparing images of the
// real teapot and the computer model, the ratio for the bowl of the
// real teapot is more like 1.25, but since 1.3 is the traditional
// value given, we use it here.
const blinnScale = 1.3;
// scale the size to be the real scaling factor
const maxHeight = 3.15 * ( blinn ? 1 : blinnScale );
const maxHeight2 = maxHeight / 2;
const trueSize = size / maxHeight2;
// Number of elements depends on what is needed. Subtract degenerate
// triangles at tip of bottom and lid out in advance.
let numTriangles = bottom ? ( 8 * segments - 4 ) * segments : 0;
numTriangles += lid ? ( 16 * segments - 4 ) * segments : 0;
numTriangles += body ? 40 * segments * segments : 0;
const indices = new Uint32Array( numTriangles * 3 );
let numVertices = bottom ? 4 : 0;
numVertices += lid ? 8 : 0;
numVertices += body ? 20 : 0;
numVertices *= ( segments + 1 ) * ( segments + 1 );
const vertices = new Float32Array( numVertices * 3 );
const normals = new Float32Array( numVertices * 3 );
const uvs = new Float32Array( numVertices * 2 );
// Bezier form
const ms = new Matrix4();
ms.set(
- 1.0, 3.0, - 3.0, 1.0,
3.0, - 6.0, 3.0, 0.0,
- 3.0, 3.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0 );
const g = [];
const sp = [];
const tp = [];
const dsp = [];
const dtp = [];
// M * G * M matrix, sort of see
// http://www.cs.helsinki.fi/group/goa/mallinnus/curves/surfaces.html
const mgm = [];
const vert = [];
const sdir = [];
const tdir = [];
const norm = new Vector3();
let tcoord;
let sval;
let tval;
let p;
let dsval = 0;
let dtval = 0;
const normOut = new Vector3();
const gmx = new Matrix4();
const tmtx = new Matrix4();
const vsp = new Vector4();
const vtp = new Vector4();
const vdsp = new Vector4();
const vdtp = new Vector4();
const vsdir = new Vector3();
const vtdir = new Vector3();
const mst = ms.clone();
mst.transpose();
// internal function: test if triangle has any matching vertices;
// if so, don't save triangle, since it won't display anything.
const notDegenerate = ( vtx1, vtx2, vtx3 ) => // if any vertex matches, return false
! ( ( ( vertices[ vtx1 * 3 ] === vertices[ vtx2 * 3 ] ) &&
( vertices[ vtx1 * 3 + 1 ] === vertices[ vtx2 * 3 + 1 ] ) &&
( vertices[ vtx1 * 3 + 2 ] === vertices[ vtx2 * 3 + 2 ] ) ) ||
( ( vertices[ vtx1 * 3 ] === vertices[ vtx3 * 3 ] ) &&
( vertices[ vtx1 * 3 + 1 ] === vertices[ vtx3 * 3 + 1 ] ) &&
( vertices[ vtx1 * 3 + 2 ] === vertices[ vtx3 * 3 + 2 ] ) ) || ( vertices[ vtx2 * 3 ] === vertices[ vtx3 * 3 ] ) &&
( vertices[ vtx2 * 3 + 1 ] === vertices[ vtx3 * 3 + 1 ] ) &&
( vertices[ vtx2 * 3 + 2 ] === vertices[ vtx3 * 3 + 2 ] ) );
for ( let i = 0; i < 3; i ++ ) {
mgm[ i ] = new Matrix4();
}
const minPatches = body ? 0 : 20;
const maxPatches = bottom ? 32 : 28;
const vertPerRow = segments + 1;
let surfCount = 0;
let vertCount = 0;
let normCount = 0;
let uvCount = 0;
let indexCount = 0;
for ( let surf = minPatches; surf < maxPatches; surf ++ ) {
// lid is in the middle of the data, patches 20-27,
// so ignore it for this part of the loop if the lid is not desired
if ( lid || ( surf < 20 || surf >= 28 ) ) {
// get M * G * M matrix for x,y,z
for ( let i = 0; i < 3; i ++ ) {
// get control patches
for ( let r = 0; r < 4; r ++ ) {
for ( let c = 0; c < 4; c ++ ) {
// transposed
g[ c * 4 + r ] = teapotVertices[ teapotPatches[ surf * 16 + r * 4 + c ] * 3 + i ];
// is the lid to be made larger, and is this a point on the lid
// that is X or Y?
if ( fitLid && ( surf >= 20 && surf < 28 ) && ( i !== 2 ) ) {
// increase XY size by 7.7%, found empirically. I don't
// increase Z so that the teapot will continue to fit in the
// space -1 to 1 for Y (Y is up for the final model).
g[ c * 4 + r ] *= 1.077;
}
// Blinn "fixed" the teapot by dividing Z by blinnScale, and that's the
// data we now use. The original teapot is taller. Fix it:
if ( ! blinn && ( i === 2 ) ) {
g[ c * 4 + r ] *= blinnScale;
}
}
}
gmx.set( g[ 0 ], g[ 1 ], g[ 2 ], g[ 3 ], g[ 4 ], g[ 5 ], g[ 6 ], g[ 7 ], g[ 8 ], g[ 9 ], g[ 10 ], g[ 11 ], g[ 12 ], g[ 13 ], g[ 14 ], g[ 15 ] );
tmtx.multiplyMatrices( gmx, ms );
mgm[ i ].multiplyMatrices( mst, tmtx );
}
// step along, get points, and output
for ( let sstep = 0; sstep <= segments; sstep ++ ) {
const s = sstep / segments;
for ( let tstep = 0; tstep <= segments; tstep ++ ) {
const t = tstep / segments;
// point from basis
// get power vectors and their derivatives
for ( p = 4, sval = tval = 1.0; p --; ) {
sp[ p ] = sval;
tp[ p ] = tval;
sval *= s;
tval *= t;
if ( p === 3 ) {
dsp[ p ] = dtp[ p ] = 0.0;
dsval = dtval = 1.0;
} else {
dsp[ p ] = dsval * ( 3 - p );
dtp[ p ] = dtval * ( 3 - p );
dsval *= s;
dtval *= t;
}
}
vsp.fromArray( sp );
vtp.fromArray( tp );
vdsp.fromArray( dsp );
vdtp.fromArray( dtp );
// do for x,y,z
for ( let i = 0; i < 3; i ++ ) {
// multiply power vectors times matrix to get value
tcoord = vsp.clone();
tcoord.applyMatrix4( mgm[ i ] );
vert[ i ] = tcoord.dot( vtp );
// get s and t tangent vectors
tcoord = vdsp.clone();
tcoord.applyMatrix4( mgm[ i ] );
sdir[ i ] = tcoord.dot( vtp );
tcoord = vsp.clone();
tcoord.applyMatrix4( mgm[ i ] );
tdir[ i ] = tcoord.dot( vdtp );
}
// find normal
vsdir.fromArray( sdir );
vtdir.fromArray( tdir );
norm.crossVectors( vtdir, vsdir );
norm.normalize();
// if X and Z length is 0, at the cusp, so point the normal up or down, depending on patch number
if ( vert[ 0 ] === 0 && vert[ 1 ] === 0 ) {
// if above the middle of the teapot, normal points up, else down
normOut.set( 0, vert[ 2 ] > maxHeight2 ? 1 : - 1, 0 );
} else {
// standard output: rotate on X axis
normOut.set( norm.x, norm.z, - norm.y );
}
// store it all
vertices[ vertCount ++ ] = trueSize * vert[ 0 ];
vertices[ vertCount ++ ] = trueSize * ( vert[ 2 ] - maxHeight2 );
vertices[ vertCount ++ ] = - trueSize * vert[ 1 ];
normals[ normCount ++ ] = normOut.x;
normals[ normCount ++ ] = normOut.y;
normals[ normCount ++ ] = normOut.z;
uvs[ uvCount ++ ] = 1 - t;
uvs[ uvCount ++ ] = 1 - s;
}
}
// save the faces
for ( let sstep = 0; sstep < segments; sstep ++ ) {
for ( let tstep = 0; tstep < segments; tstep ++ ) {
const v1 = surfCount * vertPerRow * vertPerRow + sstep * vertPerRow + tstep;
const v2 = v1 + 1;
const v3 = v2 + vertPerRow;
const v4 = v1 + vertPerRow;
// Normals and UVs cannot be shared. Without clone(), you can see the consequences
// of sharing if you call geometry.applyMatrix4( matrix ).
if ( notDegenerate( v1, v2, v3 ) ) {
indices[ indexCount ++ ] = v1;
indices[ indexCount ++ ] = v2;
indices[ indexCount ++ ] = v3;
}
if ( notDegenerate( v1, v3, v4 ) ) {
indices[ indexCount ++ ] = v1;
indices[ indexCount ++ ] = v3;
indices[ indexCount ++ ] = v4;
}
}
}
// increment only if a surface was used
surfCount ++;
}
}
this.setIndex( new BufferAttribute( indices, 1 ) );
this.setAttribute( 'position', new BufferAttribute( vertices, 3 ) );
this.setAttribute( 'normal', new BufferAttribute( normals, 3 ) );
this.setAttribute( 'uv', new BufferAttribute( uvs, 2 ) );
this.computeBoundingSphere();
}
}
export { TeapotGeometry };

View File

@ -0,0 +1,65 @@
/**
* Text = 3D Text
*
* parameters = {
* font: <THREE.Font>, // font
*
* size: <float>, // size of the text
* depth: <float>, // thickness to extrude text
* curveSegments: <int>, // number of points on the curves
*
* bevelEnabled: <bool>, // turn on bevel
* bevelThickness: <float>, // how deep into text bevel goes
* bevelSize: <float>, // how far from text outline (including bevelOffset) is bevel
* bevelOffset: <float> // how far from text outline does bevel start
* }
*/
import {
ExtrudeGeometry
} from '../../three.module.min.js';
class TextGeometry extends ExtrudeGeometry {
constructor( text, parameters = {} ) {
const font = parameters.font;
if ( font === undefined ) {
super(); // generate default extrude geometry
} else {
const shapes = font.generateShapes( text, parameters.size );
// translate parameters to ExtrudeGeometry API
if ( parameters.depth === undefined && parameters.height !== undefined ) {
console.warn( 'THREE.TextGeometry: .height is now depreciated. Please use .depth instead' ); // @deprecated, r163
}
parameters.depth = parameters.depth !== undefined ?
parameters.depth : parameters.height !== undefined ?
parameters.height : 50;
// defaults
if ( parameters.bevelThickness === undefined ) parameters.bevelThickness = 10;
if ( parameters.bevelSize === undefined ) parameters.bevelSize = 8;
if ( parameters.bevelEnabled === undefined ) parameters.bevelEnabled = false;
super( shapes, parameters );
}
this.type = 'TextGeometry';
}
}
export { TextGeometry };