private void handleVertex( Vector3[] tan1, Vector3[] tan2, Float32Array normals, Float32Array tangents, int v ) { Vector3 tmp = new Vector3(), tmp2 = new Vector3(); Vector3 n = new Vector3(), n2 = new Vector3(); n.fromArray( normals, v * 3 ); n2.copy( n ); Vector3 t = tan1[ v ]; // Gram-Schmidt orthogonalize tmp.copy( t ); tmp.sub( n.multiply( n.dot( t ) ) ).normalize(); // Calculate handedness tmp2.cross( n2, t ); double test = tmp2.dot( tan2[ v ] ); double w = ( test < 0.0 ) ? - 1.0 : 1.0; tangents.set( v * 4 , tmp.getX()); tangents.set( v * 4 + 1 , tmp.getY()); tangents.set( v * 4 + 2 , tmp.getZ()); tangents.set( v * 4 + 3 , w); }
public void setDirection( Vector3 dir ) { Vector3 axis = new Vector3(); double radians; // dir is assumed to be normalized if ( dir.getY() > 0.99999 ) { this.quaternion.set( 0, 0, 0, 1 ); } else if ( dir.getY() < - 0.99999 ) { this.quaternion.set( 1, 0, 0, 0 ); } else { axis.set( dir.getZ(), 0, - dir.getX() ).normalize(); radians = Math.acos( dir.getY() ); this.quaternion.setFromAxisAngle( axis, radians ); } }
/** * Translate an object by distance along an axis in object space. The axis is assumed to be normalized. * @param axis A normalized vector in object space. * @param distance The distance to translate. * @return */ public Object3D translateOnAxis ( Vector3 axis, double distance ) { Vector3 v1 = new Vector3(); v1.copy( axis ).apply( this.quaternion ); this.position.add( v1.multiply( distance ) ); return this; }
private void updateMovementVector() { int forward = ( this.moveState.forward || ( this.isAutoForward && !this.moveState.back ) ) ? 1 : 0; this.moveVector.setX( - (this.moveState.left ? 1D : 0D) + (this.moveState.right ? 1D : 0D) ); this.moveVector.setY( - (this.moveState.down ? 1D : 0D) + (this.moveState.up ? 1D : 0D) ); this.moveVector.setZ( - (double)forward + (this.moveState.back ? 1D : 0D) ); }
@Override public Vector3 getPoint(double t) { double tx = ShapeUtils.b2(t, this.v0.getX(), this.v1.getX(), this.v2.getX()); double ty = ShapeUtils.b2(t, this.v0.getY(), this.v1.getY(), this.v2.getY()); double tz = ShapeUtils.b2(t, this.v0.getZ(), this.v1.getZ(), this.v2.getZ()); return new Vector3(tx, ty, tz); } }
@Override public Vector3 getPoint(double t) { Vector3 v = new Vector3(); double point = ( points.size() - 1.0 ) * t; int intPoint = (int) Math.floor( point ); double weight = point - intPoint; int c0 = intPoint == 0 ? intPoint : intPoint - 1; int c1 = intPoint; int c2 = intPoint > points.size() - 2 ? points.size() -1 : intPoint + 1; int c3 = intPoint > points.size() - 3 ? points.size() -1 : intPoint + 2; v.setX( CurveUtils.interpolate( points.get(c0).getX(), points.get(c1).getX(), points.get(c2).getX(), points.get(c3).getX(), weight) ); v.setY( CurveUtils.interpolate( points.get(c0).getY(), points.get(c1).getY(), points.get(c2).getY(), points.get(c3).getY(), weight) ); v.setZ( CurveUtils.interpolate( points.get(c0).getZ(), points.get(c1).getZ(), points.get(c2).getZ(), points.get(c3).getZ(), weight) ); return v; } }
private Vector3 getMouseProjectionOnBall ( int clientX, int clientY ) { Vector3 mouseOnBall = new Vector3( ( clientX - getContext().getWidth() * 0.5) / this.radius, ( getContext().getHeight() * 0.5 - clientY ) / this.radius, 0.0 ); double length = mouseOnBall.length(); if ( length > 1.0 ) mouseOnBall.normalize(); else mouseOnBall.setZ( Math.sqrt( 1.0 - length * length ) ); eye.copy( getObject().getPosition() ).sub( this.target ); Vector3 projection = getObject().getUp().clone().setLength( mouseOnBall.getY() ); projection.add( getObject().getUp().clone().cross( eye ).setLength( mouseOnBall.getX() ) ); projection.add( eye.setLength( mouseOnBall.getZ() ) ); return projection; }
this.min.set( Double.POSITIVE_INFINITY ); this.max.set( Double.NEGATIVE_INFINITY ); p.copy( pointsFrustum.get( i ) ); p.unproject( camera ); p.apply( shadowCamera.getMatrixWorldInverse() ); if ( p.getX() < this.min.getX() ) { this.min.setX( p.getX() ); if ( p.getX() > this.max.getX() ) { this.max.setX( p.getX() ); if ( p.getY() < this.min.getY() ) { this.min.setY( p.getY() ); if ( p.getY() > this.max.getY() ) { this.max.setY( p.getY() ); if ( p.getZ() < this.min.getZ() ) { this.min.setZ( p.getZ() ); if ( p.getZ() > this.max.getZ() ) { this.max.setZ( p.getZ() ); shadowCamera.setLeft( this.min.getX() ); shadowCamera.setRight( this.max.getX() ); shadowCamera.setTop( this.max.getY() );
Vector3 tang = new Vector3(); Vector3 n = new Vector3(); Vector3 bitan = new Vector3(); Vector3 p1 = getPos( u, q, p, radius, heightScale ); Vector3 p2 = getPos( u + 0.01, q, p, radius, heightScale ); tang.sub( p2, p1 ); n.add( p2, p1 ); bitan.cross( tang, n ); n.cross( bitan, tang ); bitan.normalize(); n.normalize(); double cy = tube * Math.sin( v ); Vector3 pos = new Vector3(); pos.setX( p1.getX() + cx * n.getX() + cy * bitan.getX() ); pos.setY( p1.getY() + cx * n.getY() + cy * bitan.getY() ); pos.setZ( p1.getZ() + cx * n.getZ() + cy * bitan.getZ() );
localPoint.copy( point ).apply( _object.inverseMatrix ); eyeVector.sub( raycaster.getRay().getOrigin(), point ).normalize(); rayLight.getOrigin().copy( point ); lightVector.setFromMatrixPosition(light.getMatrixWorld()); lightVector.sub(point); rayLight.getDirection().copy(lightVector).normalize(); lightVector.setFromMatrixPosition(light.getMatrixWorld()); lightVector.sub(point); rayLight.getDirection().copy(lightVector).normalize(); normalVector.apply(_object.normalMatrix).normalize(); attenuation = lightVector.length(); attenuation = 1.0 / (attenuation * attenuation); lightVector.normalize(); double dot = Math.max(normalVector.dot(lightVector), 0); double diffuseIntensity = dot * ((HasIntensity) light).getIntensity(); halfVector.add(lightVector, eyeVector).normalize(); double dotNormalHalf = Math.max(normalVector.dot(halfVector), 0.0); double specularIntensity = Math.max(Math.pow(dotNormalHalf, ((MeshPhongMaterial) material).getShininess()), 0.0) * diffuseIntensity;
private void initialNormal2() { // This uses the Frenet-Serret formula for deriving binormal Vector3 t2 = (Vector3) path.getTangentAt( epsilon ); normals.add( 0, new Vector3().sub( t2, tangents.get( 0 ) ).normalize() ); binormals.add( 0, new Vector3().cross( tangents.get( 0 ), normals.get( 0 ) ) ); normals.get( 0 ).cross( binormals.get( 0 ), tangents.get( 0 ) ).normalize(); // last binormal x tangent binormals.get( 0 ).cross( tangents.get( 0 ), normals.get( 0 ) ).normalize(); }
Vector3 center = new Vector3(); Vector2 centerUV = new Vector2( 0.5, 0.5 ); List<Vector2> uvs = new ArrayList<Vector2>(); Vector3 vertex = new Vector3(); vertex.setX(radius * Math.cos( thetaStart + (double)i / segments * thetaLength )); vertex.setY(radius * Math.sin( thetaStart + (double)i / segments * thetaLength )); uvs.add( new Vector2( ( vertex.getX() / radius + 1.0 ) / 2.0, - ( vertex.getY() / radius + 1.0 ) / 2.0 + 1.0 ) ); Vector3 n = new Vector3( 0, 0, -1 );
Vector3 center = new Vector3(); List<Vector2> uvs = new ArrayList<Vector2>(); List<Vector3> normals = new ArrayList<Vector3>(); double v = j / (double)segmentsR * Math.PI * 2.0; center.setX(radius * Math.cos( u )); center.setY(radius * Math.sin( u )); Vector3 vertex = new Vector3(); vertex.setX(( radius + tube * Math.cos( v ) ) * Math.cos( u )); vertex.setY(( radius + tube * Math.cos( v ) ) * Math.sin( u )); vertex.setZ(tube * Math.sin( v )); normals.add( vertex.clone().sub( center ).normalize() ); int d = ( segmentsT + 1 ) * j + i; Face3 face = new Face3( a, b, d, Arrays.asList( normals.get( a ).clone(), normals.get( b ).clone(), normals.get( d ).clone() ) ); getFaces().add( face ); getFaceVertexUvs().get( 0 ).add( Arrays.asList( uvs.get( a ).clone(), uvs.get( b ).clone(), uvs.get( d ).clone() ) ); face = new Face3( b, c, d, Arrays.asList( normals.get( b ).clone(), normals.get( c ).clone(), normals.get( d ).clone() ) ); getFaces().add( face ); getFaceVertexUvs().get( 0 ).add( Arrays.asList( uvs.get( b ).clone(), uvs.get( c ).clone(), uvs.get( d ).clone() ) );
double v = y / (double)heightSegments; Vector3 vertex = new Vector3(); vertex.setX(- radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength )); vertex.setY(radius * Math.cos( thetaStart + v * thetaLength )); vertex.setZ(radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength )); int v4 = vertices.get( y + 1 ).get( x + 1 ); Vector3 n1 = getVertices().get( v1 ).clone().normalize(); Vector3 n2 = getVertices().get( v2 ).clone().normalize(); Vector3 n3 = getVertices().get( v3 ).clone().normalize(); Vector3 n4 = getVertices().get( v4 ).clone().normalize(); Vector2 uv4 = uvs.get( y + 1 ).get( x + 1 ).clone(); if ( Math.abs( getVertices().get( v1 ).getY() ) == radius ) getFaceVertexUvs().get( 0 ).add( Arrays.asList( uv1, uv3, uv4 ) ); else if ( Math.abs( getVertices().get( v3 ).getY() ) == radius ) getFaceVertexUvs().get( 0 ).add( Arrays.asList( uv1, uv2, uv4 ) ); getFaces().add( new Face3( v2, v3, v4, Arrays.asList( n2.clone(), n3, n4.clone() ) ) ); getFaceVertexUvs().get( 0 ).add( Arrays.asList( uv2.clone(), uv3, uv4.clone() ) ); setBoundingSphere( new Sphere(new Vector3(), radius) );
double segment_width = width / (double)gridX; double segment_height = height / (double)gridY; Vector3 normal = new Vector3(); normal.setX(normalValue); else if(w.equals("y")) normal.setY(normalValue); else if(w.equals("z")) normal.setZ(normalValue); Vector3 vector = new Vector3(); vector.setX(u1); else if(u.equals("y")) vector.setY(u1); else if(u.equals("z")) vector.setZ(u1); vector.setX(v1); else if(v.equals("y")) vector.setY(v1); else if(v.equals("z")) vector.setZ(v1); vector.setX(depth); else if(w.equals("y")) vector.setY(depth); else if(w.equals("z")) vector.setZ(depth);
/** * Modifies the current matrix by looking at target on defined eye. * * @param eye the Eye vector * @param target the Target vector * @param up the Up vector * * @return the current matrix */ public Matrix4 lookAt(Vector3 eye, Vector3 target, Vector3 up) { Float32Array te = this.elements; _z.sub( eye, target ).normalize(); if ( _z.length() == 0 ) { _z.z = 1.0; } _x.cross( up, _z ).normalize(); if ( _x.length() == 0 ) { _z.x += 0.0001; _x.cross( up, _z ).normalize(); } _y.cross( _z, _x ); te.set(0, _x.getX()); te.set(4, _y.getX()); te.set(8, _z.getX()); te.set(1, _x.getY()); te.set(5, _y.getY()); te.set(9, _z.getY()); te.set(2, _x.getZ()); te.set(6, _y.getZ()); te.set(10, _z.getZ()); return this; }
public Vector3 clampPoint( Vector3 point, Vector3 optionalTarget ) { double deltaLengthSq = this.center.distanceToSquared( point ); optionalTarget.copy( point ); if ( deltaLengthSq > ( this.radius * this.radius ) ) { optionalTarget.sub( this.center ).normalize(); optionalTarget.multiply( this.radius ).add( this.center ); } return optionalTarget; }
public Vector3 center() { this.computeBoundingBox(); Vector3 offset = new Vector3(); offset.add( this.boundingBox.getMin(), this.boundingBox.getMax() ); offset.multiply( - 0.5 ); this.applyMatrix( new Matrix4().makeTranslation( offset.getX(), offset.getY(), offset.getZ() ) ); this.computeBoundingBox(); return offset; }
public static Vector3 normal( Vector3 a, Vector3 b, Vector3 c, Vector3 optionalTarget ) { optionalTarget.sub( c, b ); _v0.sub( a, b ); optionalTarget.cross( _v0 ); double resultLengthSq = optionalTarget.lengthSq(); if ( resultLengthSq > 0 ) { return optionalTarget.multiply( 1.0 / Math.sqrt( resultLengthSq ) ); } return optionalTarget.set( 0, 0, 0 ); }