Archived
Private
Public Access
1
0

Initial commit

This commit is contained in:
2022-09-04 12:45:01 +02:00
commit f4a01d6a69
11601 changed files with 4206660 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
( function () {
class BoxLineGeometry extends THREE.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 THREE.Float32BufferAttribute( vertices, 3 ) );
}
}
THREE.BoxLineGeometry = BoxLineGeometry;
} )();

View File

@@ -0,0 +1,48 @@
( function () {
class ConvexGeometry extends THREE.BufferGeometry {
constructor( points = [] ) {
super(); // buffers
const vertices = [];
const normals = [];
if ( THREE.ConvexHull === undefined ) {
console.error( 'THREE.ConvexBufferGeometry: ConvexBufferGeometry relies on THREE.ConvexHull' );
}
const convexHull = new THREE.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 THREE.Float32BufferAttribute( vertices, 3 ) );
this.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
}
}
THREE.ConvexGeometry = ConvexGeometry;
} )();

View File

@@ -0,0 +1,298 @@
( function () {
/**
* 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 THREE.BufferGeometry {
constructor( mesh, position, orientation, size ) {
super(); // buffers
const vertices = [];
const normals = [];
const uvs = []; // helpers
const plane = new THREE.Vector3(); // this matrix represents the transformation of the decal projector
const projectorMatrix = new THREE.Matrix4();
projectorMatrix.makeRotationFromEuler( orientation );
projectorMatrix.setPosition( position );
const projectorMatrixInverse = new THREE.Matrix4();
projectorMatrixInverse.copy( projectorMatrix ).invert(); // generate buffers
generate(); // build geometry
this.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
this.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
this.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
function generate() {
let decalVertices = [];
const vertex = new THREE.Vector3();
const normal = new THREE.Vector3(); // handle different geometry types
if ( mesh.geometry.isGeometry === true ) {
console.error( 'THREE.DecalGeometry no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
return;
}
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 THREE.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 THREE.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 THREE.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 THREE.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() );
}
}
THREE.DecalGeometry = DecalGeometry;
THREE.DecalVertex = DecalVertex;
} )();

View File

@@ -0,0 +1,874 @@
( function () {
/**
* @fileoverview LightningStrike object for creating lightning strikes and voltaic arcs.
*
*
* Usage
*
* var myRay = new LightningStrike( paramsObject );
* var myRayMesh = new THREE.Mesh( myRay, myMaterial );
* scene.add( myRayMesh );
* ...
* myRay.update( currentTime );
*
* The "currentTime" can vary its rate, go forwards, backwards or even jump, but it cannot be negative.
*
* You should normally leave the ray position to (0, 0, 0). You should control it by changing the sourceOffset and destOffset parameters.
*
*
* LightningStrike parameters
*
* The paramsObject can contain any of the following parameters.
*
* Legend:
* 'LightningStrike' (also called 'ray'): An independent voltaic arc with its ramifications and defined with a set of parameters.
* 'Subray': A ramification of the ray. It is not a LightningStrike object.
* 'Segment': A linear segment piece of a subray.
* 'Leaf segment': A ray segment which cannot be smaller.
*
*
* The following parameters can be changed any time and if they vary smoothly, the ray form will also change smoothly:
*
* @param {Vector3} sourceOffset The point where the ray starts.
*
* @param {Vector3} destOffset The point where the ray ends.
*
* @param {double} timeScale The rate at wich the ray form changes in time. Default: 1
*
* @param {double} roughness From 0 to 1. The higher the value, the more wrinkled is the ray. Default: 0.9
*
* @param {double} straightness From 0 to 1. The higher the value, the more straight will be a subray path. Default: 0.7
*
* @param {Vector3} up0 Ray 'up' direction at the ray starting point. Must be normalized. It should be perpendicular to the ray forward direction but it doesn't matter much.
*
* @param {Vector3} up1 Like the up0 parameter but at the end of the ray. Must be normalized.
*
* @param {double} radius0 Radius of the main ray trunk at the start point. Default: 1
*
* @param {double} radius1 Radius of the main ray trunk at the end point. Default: 1
*
* @param {double} radius0Factor The radius0 of a subray is this factor times the radius0 of its parent subray. Default: 0.5
*
* @param {double} radius1Factor The radius1 of a subray is this factor times the radius1 of its parent subray. Default: 0.2
*
* @param {minRadius} Minimum value a subray radius0 or radius1 can get. Default: 0.1
*
*
* The following parameters should not be changed after lightning creation. They can be changed but the ray will change its form abruptly:
*
* @param {boolean} isEternal If true the ray never extinguishes. Otherwise its life is controlled by the 'birthTime' and 'deathTime' parameters. Default: true if any of those two parameters is undefined.
*
* @param {double} birthTime The time at which the ray starts its life and begins propagating. Only if isEternal is false. Default: None.
*
* @param {double} deathTime The time at which the ray ends vanishing and its life. Only if isEternal is false. Default: None.
*
* @param {double} propagationTimeFactor From 0 to 1. Lifetime factor at which the ray ends propagating and enters the steady phase. For example, 0.1 means it is propagating 1/10 of its lifetime. Default: 0.1
*
* @param {double} vanishingTimeFactor From 0 to 1. Lifetime factor at which the ray ends the steady phase and begins vanishing. For example, 0.9 means it is vanishing 1/10 of its lifetime. Default: 0.9
*
* @param {double} subrayPeriod Subrays cycle periodically. This is their time period. Default: 4
*
* @param {double} subrayDutyCycle From 0 to 1. This is the fraction of time a subray is active. Default: 0.6
*
*
* These parameters cannot change after lightning creation:
*
* @param {integer} maxIterations: Greater than 0. The number of ray's leaf segments is 2**maxIterations. Default: 9
*
* @param {boolean} isStatic Set to true only for rays which won't change over time and are not attached to moving objects (Rare case). It is used to set the vertex buffers non-dynamic. You can omit calling update() for these rays.
*
* @param {integer} ramification Greater than 0. Maximum number of child subrays a subray can have. Default: 5
*
* @param {integer} maxSubrayRecursion Greater than 0. Maximum level of recursion (subray descendant generations). Default: 3
*
* @param {double} recursionProbability From 0 to 1. The lower the value, the less chance each new generation of subrays has to generate new subrays. Default: 0.6
*
* @param {boolean} generateUVs If true, the ray geometry will have uv coordinates generated. u runs along the ray, and v across its perimeter. Default: false.
*
* @param {Object} randomGenerator Set here your random number generator which will seed the THREE.SimplexNoise and other decisions during ray tree creation.
* It can be used to generate repeatable rays. For that, set also the noiseSeed parameter, and each ray created with that generator and seed pair will be identical in time.
* The randomGenerator parameter should be an object with a random() function similar to Math.random, but seedable.
* It must have also a getSeed() method, which returns the current seed, and a setSeed( seed ) method, which accepts as seed a fractional number from 0 to 1, as well as any other number.
* The default value is an internal generator for some uses and Math.random for others (It is non-repeatable even if noiseSeed is supplied)
*
* @param {double} noiseSeed Seed used to make repeatable rays (see the randomGenerator)
*
* @param {function} onDecideSubrayCreation Set this to change the callback which decides subray creation. You can look at the default callback in the code (createDefaultSubrayCreationCallbacks)for more info.
*
* @param {function} onSubrayCreation This is another callback, more simple than the previous one. It can be used to adapt the form of subrays or other parameters once a subray has been created and initialized. It is used in the examples to adapt subrays to a sphere or to a plane.
*
*
*/
class LightningStrike extends THREE.BufferGeometry {
constructor( rayParameters = {} ) {
super();
this.type = 'LightningStrike'; // Set parameters, and set undefined parameters to default values
this.init( LightningStrike.copyParameters( rayParameters, rayParameters ) ); // Creates and populates the mesh
this.createMesh();
}
static createRandomGenerator() {
const numSeeds = 2053;
const seeds = [];
for ( let i = 0; i < numSeeds; i ++ ) {
seeds.push( Math.random() );
}
const generator = {
currentSeed: 0,
random: function () {
const value = seeds[ generator.currentSeed ];
generator.currentSeed = ( generator.currentSeed + 1 ) % numSeeds;
return value;
},
getSeed: function () {
return generator.currentSeed / numSeeds;
},
setSeed: function ( seed ) {
generator.currentSeed = Math.floor( seed * numSeeds ) % numSeeds;
}
};
return generator;
}
static copyParameters( dest = {}, source = {} ) {
const vecCopy = function ( v ) {
if ( source === dest ) {
return v;
} else {
return v.clone();
}
};
dest.sourceOffset = source.sourceOffset !== undefined ? vecCopy( source.sourceOffset ) : new THREE.Vector3( 0, 100, 0 ), dest.destOffset = source.destOffset !== undefined ? vecCopy( source.destOffset ) : new THREE.Vector3( 0, 0, 0 ), dest.timeScale = source.timeScale !== undefined ? source.timeScale : 1, dest.roughness = source.roughness !== undefined ? source.roughness : 0.9, dest.straightness = source.straightness !== undefined ? source.straightness : 0.7, dest.up0 = source.up0 !== undefined ? vecCopy( source.up0 ) : new THREE.Vector3( 0, 0, 1 );
dest.up1 = source.up1 !== undefined ? vecCopy( source.up1 ) : new THREE.Vector3( 0, 0, 1 ), dest.radius0 = source.radius0 !== undefined ? source.radius0 : 1, dest.radius1 = source.radius1 !== undefined ? source.radius1 : 1, dest.radius0Factor = source.radius0Factor !== undefined ? source.radius0Factor : 0.5, dest.radius1Factor = source.radius1Factor !== undefined ? source.radius1Factor : 0.2, dest.minRadius = source.minRadius !== undefined ? source.minRadius : 0.2, // These parameters should not be changed after lightning creation. They can be changed but the ray will change its form abruptly:
dest.isEternal = source.isEternal !== undefined ? source.isEternal : source.birthTime === undefined || source.deathTime === undefined, dest.birthTime = source.birthTime, dest.deathTime = source.deathTime, dest.propagationTimeFactor = source.propagationTimeFactor !== undefined ? source.propagationTimeFactor : 0.1, dest.vanishingTimeFactor = source.vanishingTimeFactor !== undefined ? source.vanishingTimeFactor : 0.9, dest.subrayPeriod = source.subrayPeriod !== undefined ? source.subrayPeriod : 4, dest.subrayDutyCycle = source.subrayDutyCycle !== undefined ? source.subrayDutyCycle : 0.6; // These parameters cannot change after lightning creation:
dest.maxIterations = source.maxIterations !== undefined ? source.maxIterations : 9;
dest.isStatic = source.isStatic !== undefined ? source.isStatic : false;
dest.ramification = source.ramification !== undefined ? source.ramification : 5;
dest.maxSubrayRecursion = source.maxSubrayRecursion !== undefined ? source.maxSubrayRecursion : 3;
dest.recursionProbability = source.recursionProbability !== undefined ? source.recursionProbability : 0.6;
dest.generateUVs = source.generateUVs !== undefined ? source.generateUVs : false;
dest.randomGenerator = source.randomGenerator, dest.noiseSeed = source.noiseSeed, dest.onDecideSubrayCreation = source.onDecideSubrayCreation, dest.onSubrayCreation = source.onSubrayCreation;
return dest;
}
update( time ) {
if ( this.isStatic ) return;
if ( this.rayParameters.isEternal || this.rayParameters.birthTime <= time && time <= this.rayParameters.deathTime ) {
this.updateMesh( time );
if ( time < this.subrays[ 0 ].endPropagationTime ) {
this.state = LightningStrike.RAY_PROPAGATING;
} else if ( time > this.subrays[ 0 ].beginVanishingTime ) {
this.state = LightningStrike.RAY_VANISHING;
} else {
this.state = LightningStrike.RAY_STEADY;
}
this.visible = true;
} else {
this.visible = false;
if ( time < this.rayParameters.birthTime ) {
this.state = LightningStrike.RAY_UNBORN;
} else {
this.state = LightningStrike.RAY_EXTINGUISHED;
}
}
}
init( rayParameters ) {
// Init all the state from the parameters
this.rayParameters = rayParameters; // These parameters cannot change after lightning creation:
this.maxIterations = rayParameters.maxIterations !== undefined ? Math.floor( rayParameters.maxIterations ) : 9;
rayParameters.maxIterations = this.maxIterations;
this.isStatic = rayParameters.isStatic !== undefined ? rayParameters.isStatic : false;
rayParameters.isStatic = this.isStatic;
this.ramification = rayParameters.ramification !== undefined ? Math.floor( rayParameters.ramification ) : 5;
rayParameters.ramification = this.ramification;
this.maxSubrayRecursion = rayParameters.maxSubrayRecursion !== undefined ? Math.floor( rayParameters.maxSubrayRecursion ) : 3;
rayParameters.maxSubrayRecursion = this.maxSubrayRecursion;
this.recursionProbability = rayParameters.recursionProbability !== undefined ? rayParameters.recursionProbability : 0.6;
rayParameters.recursionProbability = this.recursionProbability;
this.generateUVs = rayParameters.generateUVs !== undefined ? rayParameters.generateUVs : false;
rayParameters.generateUVs = this.generateUVs; // Random generator
if ( rayParameters.randomGenerator !== undefined ) {
this.randomGenerator = rayParameters.randomGenerator;
this.seedGenerator = rayParameters.randomGenerator;
if ( rayParameters.noiseSeed !== undefined ) {
this.seedGenerator.setSeed( rayParameters.noiseSeed );
}
} else {
this.randomGenerator = LightningStrike.createRandomGenerator();
this.seedGenerator = Math;
} // Ray creation callbacks
if ( rayParameters.onDecideSubrayCreation !== undefined ) {
this.onDecideSubrayCreation = rayParameters.onDecideSubrayCreation;
} else {
this.createDefaultSubrayCreationCallbacks();
if ( rayParameters.onSubrayCreation !== undefined ) {
this.onSubrayCreation = rayParameters.onSubrayCreation;
}
} // Internal state
this.state = LightningStrike.RAY_INITIALIZED;
this.maxSubrays = Math.ceil( 1 + Math.pow( this.ramification, Math.max( 0, this.maxSubrayRecursion - 1 ) ) );
rayParameters.maxSubrays = this.maxSubrays;
this.maxRaySegments = 2 * ( 1 << this.maxIterations );
this.subrays = [];
for ( let i = 0; i < this.maxSubrays; i ++ ) {
this.subrays.push( this.createSubray() );
}
this.raySegments = [];
for ( let i = 0; i < this.maxRaySegments; i ++ ) {
this.raySegments.push( this.createSegment() );
}
this.time = 0;
this.timeFraction = 0;
this.currentSegmentCallback = null;
this.currentCreateTriangleVertices = this.generateUVs ? this.createTriangleVerticesWithUVs : this.createTriangleVerticesWithoutUVs;
this.numSubrays = 0;
this.currentSubray = null;
this.currentSegmentIndex = 0;
this.isInitialSegment = false;
this.subrayProbability = 0;
this.currentVertex = 0;
this.currentIndex = 0;
this.currentCoordinate = 0;
this.currentUVCoordinate = 0;
this.vertices = null;
this.uvs = null;
this.indices = null;
this.positionAttribute = null;
this.uvsAttribute = null;
this.simplexX = new THREE.SimplexNoise( this.seedGenerator );
this.simplexY = new THREE.SimplexNoise( this.seedGenerator );
this.simplexZ = new THREE.SimplexNoise( this.seedGenerator ); // Temp vectors
this.forwards = new THREE.Vector3();
this.forwardsFill = new THREE.Vector3();
this.side = new THREE.Vector3();
this.down = new THREE.Vector3();
this.middlePos = new THREE.Vector3();
this.middleLinPos = new THREE.Vector3();
this.newPos = new THREE.Vector3();
this.vPos = new THREE.Vector3();
this.cross1 = new THREE.Vector3();
}
createMesh() {
const maxDrawableSegmentsPerSubRay = 1 << this.maxIterations;
const maxVerts = 3 * ( maxDrawableSegmentsPerSubRay + 1 ) * this.maxSubrays;
const maxIndices = 18 * maxDrawableSegmentsPerSubRay * this.maxSubrays;
this.vertices = new Float32Array( maxVerts * 3 );
this.indices = new Uint32Array( maxIndices );
if ( this.generateUVs ) {
this.uvs = new Float32Array( maxVerts * 2 );
} // Populate the mesh
this.fillMesh( 0 );
this.setIndex( new THREE.Uint32BufferAttribute( this.indices, 1 ) );
this.positionAttribute = new THREE.Float32BufferAttribute( this.vertices, 3 );
this.setAttribute( 'position', this.positionAttribute );
if ( this.generateUVs ) {
this.uvsAttribute = new THREE.Float32BufferAttribute( new Float32Array( this.uvs ), 2 );
this.setAttribute( 'uv', this.uvsAttribute );
}
if ( ! this.isStatic ) {
this.index.usage = THREE.DynamicDrawUsage;
this.positionAttribute.usage = THREE.DynamicDrawUsage;
if ( this.generateUVs ) {
this.uvsAttribute.usage = THREE.DynamicDrawUsage;
}
} // Store buffers for later modification
this.vertices = this.positionAttribute.array;
this.indices = this.index.array;
if ( this.generateUVs ) {
this.uvs = this.uvsAttribute.array;
}
}
updateMesh( time ) {
this.fillMesh( time );
this.drawRange.count = this.currentIndex;
this.index.needsUpdate = true;
this.positionAttribute.needsUpdate = true;
if ( this.generateUVs ) {
this.uvsAttribute.needsUpdate = true;
}
}
fillMesh( time ) {
const scope = this;
this.currentVertex = 0;
this.currentIndex = 0;
this.currentCoordinate = 0;
this.currentUVCoordinate = 0;
this.fractalRay( time, function fillVertices( segment ) {
const subray = scope.currentSubray;
if ( time < subray.birthTime ) {
//&& ( ! this.rayParameters.isEternal || scope.currentSubray.recursion > 0 ) ) {
return;
} else if ( this.rayParameters.isEternal && scope.currentSubray.recursion == 0 ) {
// Eternal rays don't propagate nor vanish, but its subrays do
scope.createPrism( segment );
scope.onDecideSubrayCreation( segment, scope );
} else if ( time < subray.endPropagationTime ) {
if ( scope.timeFraction >= segment.fraction0 * subray.propagationTimeFactor ) {
// Ray propagation has arrived to this segment
scope.createPrism( segment );
scope.onDecideSubrayCreation( segment, scope );
}
} else if ( time < subray.beginVanishingTime ) {
// Ray is steady (nor propagating nor vanishing)
scope.createPrism( segment );
scope.onDecideSubrayCreation( segment, scope );
} else {
if ( scope.timeFraction <= subray.vanishingTimeFactor + segment.fraction1 * ( 1 - subray.vanishingTimeFactor ) ) {
// Segment has not yet vanished
scope.createPrism( segment );
}
scope.onDecideSubrayCreation( segment, scope );
}
} );
}
addNewSubray() {
return this.subrays[ this.numSubrays ++ ];
}
initSubray( subray, rayParameters ) {
subray.pos0.copy( rayParameters.sourceOffset );
subray.pos1.copy( rayParameters.destOffset );
subray.up0.copy( rayParameters.up0 );
subray.up1.copy( rayParameters.up1 );
subray.radius0 = rayParameters.radius0;
subray.radius1 = rayParameters.radius1;
subray.birthTime = rayParameters.birthTime;
subray.deathTime = rayParameters.deathTime;
subray.timeScale = rayParameters.timeScale;
subray.roughness = rayParameters.roughness;
subray.straightness = rayParameters.straightness;
subray.propagationTimeFactor = rayParameters.propagationTimeFactor;
subray.vanishingTimeFactor = rayParameters.vanishingTimeFactor;
subray.maxIterations = this.maxIterations;
subray.seed = rayParameters.noiseSeed !== undefined ? rayParameters.noiseSeed : 0;
subray.recursion = 0;
}
fractalRay( time, segmentCallback ) {
this.time = time;
this.currentSegmentCallback = segmentCallback;
this.numSubrays = 0; // Add the top level subray
this.initSubray( this.addNewSubray(), this.rayParameters ); // Process all subrays that are being generated until consuming all of them
for ( let subrayIndex = 0; subrayIndex < this.numSubrays; subrayIndex ++ ) {
const subray = this.subrays[ subrayIndex ];
this.currentSubray = subray;
this.randomGenerator.setSeed( subray.seed );
subray.endPropagationTime = THREE.MathUtils.lerp( subray.birthTime, subray.deathTime, subray.propagationTimeFactor );
subray.beginVanishingTime = THREE.MathUtils.lerp( subray.deathTime, subray.birthTime, 1 - subray.vanishingTimeFactor );
const random1 = this.randomGenerator.random;
subray.linPos0.set( random1(), random1(), random1() ).multiplyScalar( 1000 );
subray.linPos1.set( random1(), random1(), random1() ).multiplyScalar( 1000 );
this.timeFraction = ( time - subray.birthTime ) / ( subray.deathTime - subray.birthTime );
this.currentSegmentIndex = 0;
this.isInitialSegment = true;
const segment = this.getNewSegment();
segment.iteration = 0;
segment.pos0.copy( subray.pos0 );
segment.pos1.copy( subray.pos1 );
segment.linPos0.copy( subray.linPos0 );
segment.linPos1.copy( subray.linPos1 );
segment.up0.copy( subray.up0 );
segment.up1.copy( subray.up1 );
segment.radius0 = subray.radius0;
segment.radius1 = subray.radius1;
segment.fraction0 = 0;
segment.fraction1 = 1;
segment.positionVariationFactor = 1 - subray.straightness;
this.subrayProbability = this.ramification * Math.pow( this.recursionProbability, subray.recursion ) / ( 1 << subray.maxIterations );
this.fractalRayRecursive( segment );
}
this.currentSegmentCallback = null;
this.currentSubray = null;
}
fractalRayRecursive( segment ) {
// Leave recursion condition
if ( segment.iteration >= this.currentSubray.maxIterations ) {
this.currentSegmentCallback( segment );
return;
} // Interpolation
this.forwards.subVectors( segment.pos1, segment.pos0 );
let lForwards = this.forwards.length();
if ( lForwards < 0.000001 ) {
this.forwards.set( 0, 0, 0.01 );
lForwards = this.forwards.length();
}
const middleRadius = ( segment.radius0 + segment.radius1 ) * 0.5;
const middleFraction = ( segment.fraction0 + segment.fraction1 ) * 0.5;
const timeDimension = this.time * this.currentSubray.timeScale * Math.pow( 2, segment.iteration );
this.middlePos.lerpVectors( segment.pos0, segment.pos1, 0.5 );
this.middleLinPos.lerpVectors( segment.linPos0, segment.linPos1, 0.5 );
const p = this.middleLinPos; // Noise
this.newPos.set( this.simplexX.noise4d( p.x, p.y, p.z, timeDimension ), this.simplexY.noise4d( p.x, p.y, p.z, timeDimension ), this.simplexZ.noise4d( p.x, p.y, p.z, timeDimension ) );
this.newPos.multiplyScalar( segment.positionVariationFactor * lForwards );
this.newPos.add( this.middlePos ); // Recursion
const newSegment1 = this.getNewSegment();
newSegment1.pos0.copy( segment.pos0 );
newSegment1.pos1.copy( this.newPos );
newSegment1.linPos0.copy( segment.linPos0 );
newSegment1.linPos1.copy( this.middleLinPos );
newSegment1.up0.copy( segment.up0 );
newSegment1.up1.copy( segment.up1 );
newSegment1.radius0 = segment.radius0;
newSegment1.radius1 = middleRadius;
newSegment1.fraction0 = segment.fraction0;
newSegment1.fraction1 = middleFraction;
newSegment1.positionVariationFactor = segment.positionVariationFactor * this.currentSubray.roughness;
newSegment1.iteration = segment.iteration + 1;
const newSegment2 = this.getNewSegment();
newSegment2.pos0.copy( this.newPos );
newSegment2.pos1.copy( segment.pos1 );
newSegment2.linPos0.copy( this.middleLinPos );
newSegment2.linPos1.copy( segment.linPos1 );
this.cross1.crossVectors( segment.up0, this.forwards.normalize() );
newSegment2.up0.crossVectors( this.forwards, this.cross1 ).normalize();
newSegment2.up1.copy( segment.up1 );
newSegment2.radius0 = middleRadius;
newSegment2.radius1 = segment.radius1;
newSegment2.fraction0 = middleFraction;
newSegment2.fraction1 = segment.fraction1;
newSegment2.positionVariationFactor = segment.positionVariationFactor * this.currentSubray.roughness;
newSegment2.iteration = segment.iteration + 1;
this.fractalRayRecursive( newSegment1 );
this.fractalRayRecursive( newSegment2 );
}
createPrism( segment ) {
// Creates one triangular prism and its vertices at the segment
this.forwardsFill.subVectors( segment.pos1, segment.pos0 ).normalize();
if ( this.isInitialSegment ) {
this.currentCreateTriangleVertices( segment.pos0, segment.up0, this.forwardsFill, segment.radius0, 0 );
this.isInitialSegment = false;
}
this.currentCreateTriangleVertices( segment.pos1, segment.up0, this.forwardsFill, segment.radius1, segment.fraction1 );
this.createPrismFaces();
}
createTriangleVerticesWithoutUVs( pos, up, forwards, radius ) {
// Create an equilateral triangle (only vertices)
this.side.crossVectors( up, forwards ).multiplyScalar( radius * LightningStrike.COS30DEG );
this.down.copy( up ).multiplyScalar( - radius * LightningStrike.SIN30DEG );
const p = this.vPos;
const v = this.vertices;
p.copy( pos ).sub( this.side ).add( this.down );
v[ this.currentCoordinate ++ ] = p.x;
v[ this.currentCoordinate ++ ] = p.y;
v[ this.currentCoordinate ++ ] = p.z;
p.copy( pos ).add( this.side ).add( this.down );
v[ this.currentCoordinate ++ ] = p.x;
v[ this.currentCoordinate ++ ] = p.y;
v[ this.currentCoordinate ++ ] = p.z;
p.copy( up ).multiplyScalar( radius ).add( pos );
v[ this.currentCoordinate ++ ] = p.x;
v[ this.currentCoordinate ++ ] = p.y;
v[ this.currentCoordinate ++ ] = p.z;
this.currentVertex += 3;
}
createTriangleVerticesWithUVs( pos, up, forwards, radius, u ) {
// Create an equilateral triangle (only vertices)
this.side.crossVectors( up, forwards ).multiplyScalar( radius * LightningStrike.COS30DEG );
this.down.copy( up ).multiplyScalar( - radius * LightningStrike.SIN30DEG );
const p = this.vPos;
const v = this.vertices;
const uv = this.uvs;
p.copy( pos ).sub( this.side ).add( this.down );
v[ this.currentCoordinate ++ ] = p.x;
v[ this.currentCoordinate ++ ] = p.y;
v[ this.currentCoordinate ++ ] = p.z;
uv[ this.currentUVCoordinate ++ ] = u;
uv[ this.currentUVCoordinate ++ ] = 0;
p.copy( pos ).add( this.side ).add( this.down );
v[ this.currentCoordinate ++ ] = p.x;
v[ this.currentCoordinate ++ ] = p.y;
v[ this.currentCoordinate ++ ] = p.z;
uv[ this.currentUVCoordinate ++ ] = u;
uv[ this.currentUVCoordinate ++ ] = 0.5;
p.copy( up ).multiplyScalar( radius ).add( pos );
v[ this.currentCoordinate ++ ] = p.x;
v[ this.currentCoordinate ++ ] = p.y;
v[ this.currentCoordinate ++ ] = p.z;
uv[ this.currentUVCoordinate ++ ] = u;
uv[ this.currentUVCoordinate ++ ] = 1;
this.currentVertex += 3;
}
createPrismFaces( vertex
/*, index*/
) {
const indices = this.indices;
vertex = this.currentVertex - 6;
indices[ this.currentIndex ++ ] = vertex + 1;
indices[ this.currentIndex ++ ] = vertex + 2;
indices[ this.currentIndex ++ ] = vertex + 5;
indices[ this.currentIndex ++ ] = vertex + 1;
indices[ this.currentIndex ++ ] = vertex + 5;
indices[ this.currentIndex ++ ] = vertex + 4;
indices[ this.currentIndex ++ ] = vertex + 0;
indices[ this.currentIndex ++ ] = vertex + 1;
indices[ this.currentIndex ++ ] = vertex + 4;
indices[ this.currentIndex ++ ] = vertex + 0;
indices[ this.currentIndex ++ ] = vertex + 4;
indices[ this.currentIndex ++ ] = vertex + 3;
indices[ this.currentIndex ++ ] = vertex + 2;
indices[ this.currentIndex ++ ] = vertex + 0;
indices[ this.currentIndex ++ ] = vertex + 3;
indices[ this.currentIndex ++ ] = vertex + 2;
indices[ this.currentIndex ++ ] = vertex + 3;
indices[ this.currentIndex ++ ] = vertex + 5;
}
createDefaultSubrayCreationCallbacks() {
const random1 = this.randomGenerator.random;
this.onDecideSubrayCreation = function ( segment, lightningStrike ) {
// Decide subrays creation at parent (sub)ray segment
const subray = lightningStrike.currentSubray;
const period = lightningStrike.rayParameters.subrayPeriod;
const dutyCycle = lightningStrike.rayParameters.subrayDutyCycle;
const phase0 = lightningStrike.rayParameters.isEternal && subray.recursion == 0 ? - random1() * period : THREE.MathUtils.lerp( subray.birthTime, subray.endPropagationTime, segment.fraction0 ) - random1() * period;
const phase = lightningStrike.time - phase0;
const currentCycle = Math.floor( phase / period );
const childSubraySeed = random1() * ( currentCycle + 1 );
const isActive = phase % period <= dutyCycle * period;
let probability = 0;
if ( isActive ) {
probability = lightningStrike.subrayProbability; // Distribution test: probability *= segment.fraction0 > 0.5 && segment.fraction0 < 0.9 ? 1 / 0.4 : 0;
}
if ( subray.recursion < lightningStrike.maxSubrayRecursion && lightningStrike.numSubrays < lightningStrike.maxSubrays && random1() < probability ) {
const childSubray = lightningStrike.addNewSubray();
const parentSeed = lightningStrike.randomGenerator.getSeed();
childSubray.seed = childSubraySeed;
lightningStrike.randomGenerator.setSeed( childSubraySeed );
childSubray.recursion = subray.recursion + 1;
childSubray.maxIterations = Math.max( 1, subray.maxIterations - 1 );
childSubray.linPos0.set( random1(), random1(), random1() ).multiplyScalar( 1000 );
childSubray.linPos1.set( random1(), random1(), random1() ).multiplyScalar( 1000 );
childSubray.up0.copy( subray.up0 );
childSubray.up1.copy( subray.up1 );
childSubray.radius0 = segment.radius0 * lightningStrike.rayParameters.radius0Factor;
childSubray.radius1 = Math.min( lightningStrike.rayParameters.minRadius, segment.radius1 * lightningStrike.rayParameters.radius1Factor );
childSubray.birthTime = phase0 + currentCycle * period;
childSubray.deathTime = childSubray.birthTime + period * dutyCycle;
if ( ! lightningStrike.rayParameters.isEternal && subray.recursion == 0 ) {
childSubray.birthTime = Math.max( childSubray.birthTime, subray.birthTime );
childSubray.deathTime = Math.min( childSubray.deathTime, subray.deathTime );
}
childSubray.timeScale = subray.timeScale * 2;
childSubray.roughness = subray.roughness;
childSubray.straightness = subray.straightness;
childSubray.propagationTimeFactor = subray.propagationTimeFactor;
childSubray.vanishingTimeFactor = subray.vanishingTimeFactor;
lightningStrike.onSubrayCreation( segment, subray, childSubray, lightningStrike );
lightningStrike.randomGenerator.setSeed( parentSeed );
}
};
const vec1Pos = new THREE.Vector3();
const vec2Forward = new THREE.Vector3();
const vec3Side = new THREE.Vector3();
const vec4Up = new THREE.Vector3();
this.onSubrayCreation = function ( segment, parentSubray, childSubray, lightningStrike ) {
// Decide childSubray origin and destination positions (pos0 and pos1) and possibly other properties of childSubray
// Just use the default cone position generator
lightningStrike.subrayCylinderPosition( segment, parentSubray, childSubray, 0.5, 0.6, 0.2 );
};
this.subrayConePosition = function ( segment, parentSubray, childSubray, heightFactor, sideWidthFactor, minSideWidthFactor ) {
// Sets childSubray pos0 and pos1 in a cone
childSubray.pos0.copy( segment.pos0 );
vec1Pos.subVectors( parentSubray.pos1, parentSubray.pos0 );
vec2Forward.copy( vec1Pos ).normalize();
vec1Pos.multiplyScalar( segment.fraction0 + ( 1 - segment.fraction0 ) * ( random1() * heightFactor ) );
const length = vec1Pos.length();
vec3Side.crossVectors( parentSubray.up0, vec2Forward );
const angle = 2 * Math.PI * random1();
vec3Side.multiplyScalar( Math.cos( angle ) );
vec4Up.copy( parentSubray.up0 ).multiplyScalar( Math.sin( angle ) );
childSubray.pos1.copy( vec3Side ).add( vec4Up ).multiplyScalar( length * sideWidthFactor * ( minSideWidthFactor + random1() * ( 1 - minSideWidthFactor ) ) ).add( vec1Pos ).add( parentSubray.pos0 );
};
this.subrayCylinderPosition = function ( segment, parentSubray, childSubray, heightFactor, sideWidthFactor, minSideWidthFactor ) {
// Sets childSubray pos0 and pos1 in a cylinder
childSubray.pos0.copy( segment.pos0 );
vec1Pos.subVectors( parentSubray.pos1, parentSubray.pos0 );
vec2Forward.copy( vec1Pos ).normalize();
vec1Pos.multiplyScalar( segment.fraction0 + ( 1 - segment.fraction0 ) * ( ( 2 * random1() - 1 ) * heightFactor ) );
const length = vec1Pos.length();
vec3Side.crossVectors( parentSubray.up0, vec2Forward );
const angle = 2 * Math.PI * random1();
vec3Side.multiplyScalar( Math.cos( angle ) );
vec4Up.copy( parentSubray.up0 ).multiplyScalar( Math.sin( angle ) );
childSubray.pos1.copy( vec3Side ).add( vec4Up ).multiplyScalar( length * sideWidthFactor * ( minSideWidthFactor + random1() * ( 1 - minSideWidthFactor ) ) ).add( vec1Pos ).add( parentSubray.pos0 );
};
}
createSubray() {
return {
seed: 0,
maxIterations: 0,
recursion: 0,
pos0: new THREE.Vector3(),
pos1: new THREE.Vector3(),
linPos0: new THREE.Vector3(),
linPos1: new THREE.Vector3(),
up0: new THREE.Vector3(),
up1: new THREE.Vector3(),
radius0: 0,
radius1: 0,
birthTime: 0,
deathTime: 0,
timeScale: 0,
roughness: 0,
straightness: 0,
propagationTimeFactor: 0,
vanishingTimeFactor: 0,
endPropagationTime: 0,
beginVanishingTime: 0
};
}
createSegment() {
return {
iteration: 0,
pos0: new THREE.Vector3(),
pos1: new THREE.Vector3(),
linPos0: new THREE.Vector3(),
linPos1: new THREE.Vector3(),
up0: new THREE.Vector3(),
up1: new THREE.Vector3(),
radius0: 0,
radius1: 0,
fraction0: 0,
fraction1: 0,
positionVariationFactor: 0
};
}
getNewSegment() {
return this.raySegments[ this.currentSegmentIndex ++ ];
}
copy( source ) {
super.copy( source );
this.init( LightningStrike.copyParameters( {}, source.rayParameters ) );
return this;
}
clone() {
return new this.constructor( LightningStrike.copyParameters( {}, this.rayParameters ) );
}
}
LightningStrike.prototype.isLightningStrike = true; // Ray states
LightningStrike.RAY_INITIALIZED = 0;
LightningStrike.RAY_UNBORN = 1;
LightningStrike.RAY_PROPAGATING = 2;
LightningStrike.RAY_STEADY = 3;
LightningStrike.RAY_VANISHING = 4;
LightningStrike.RAY_EXTINGUISHED = 5;
LightningStrike.COS30DEG = Math.cos( 30 * Math.PI / 180 );
LightningStrike.SIN30DEG = Math.sin( 30 * Math.PI / 180 );
THREE.LightningStrike = LightningStrike;
} )();

View File

@@ -0,0 +1,215 @@
( function () {
/**
* 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 THREE.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 THREE.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 THREE.Curve {
getPoint( t, optionalTarget = new THREE.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 THREE.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 THREE.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 );
}
};
THREE.ParametricGeometries = ParametricGeometries;
} )();

View File

@@ -0,0 +1,115 @@
( function () {
/**
* Parametric Surfaces Geometry
* based on the brilliant article by @prideout https://prideout.net/blog/old/blog/index.html@p=44.html
*/
class ParametricGeometry extends THREE.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 THREE.Vector3();
const p0 = new THREE.Vector3(),
p1 = new THREE.Vector3();
const pu = new THREE.Vector3(),
pv = new THREE.Vector3();
if ( func.length < 3 ) {
console.error( 'THREE.ParametricGeometry: Function must now modify a THREE.Vector3 as third parameter.' );
} // 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 THREE.Float32BufferAttribute( vertices, 3 ) );
this.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
this.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
}
}
THREE.ParametricGeometry = ParametricGeometry;
} )();

View File

@@ -0,0 +1,140 @@
( function () {
const _tempNormal = new THREE.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 THREE.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 THREE.Vector3();
const normal = new THREE.Vector3();
const box = new THREE.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 THREE.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;
}
}
}
}
THREE.RoundedBoxGeometry = RoundedBoxGeometry;
} )();

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,51 @@
( function () {
/**
* Text = 3D Text
*
* parameters = {
* font: <THREE.Font>, // font
*
* size: <float>, // size of the text
* height: <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
* }
*/
class TextGeometry extends THREE.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 THREE.ExtrudeGeometry API
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';
}
}
THREE.TextGeometry = TextGeometry;
} )();