Excuse my first post, it is literally that. I've been banging my head for three days trying to get squares to align properly on a sphere. Does anyone know the parametric equations for the tangents of a plane? See tangents X, Y and Z in the code below...
Here is the output for reference. My stupid face is there, as a bonus, for you all to hate on
The code is a part of a much larger library on multiple pages. It is kind of a distraction from the question. I have scoured math sites and StackOverflow for the answer. I am baking the positional and rotational data into Float32 arrays before sending it to the renderer. So, lookAt and other THREE and WebGL functions won't work. I need the pure math solution.
this.sphere = function()
{
var args = arguments[ 0 ];
var offsets = offset( args.parameters.offsets );
var arrays =
{
unique: [],
position: [],
rotation: []
};
var phi, theta;
var x, y, z;
var tangentX, tangentY, tangentZ;
var pos = [];
var d360 = Math.PI * 2;
var d180 = Math.PI;
var d90 = Math.PI / 2;
var cap = false;
for ( var p = 0; p <= args.parameters.stacks; p++ )
{
// stacks
phi = d180 * p / args.parameters.stacks;
for ( var t = 0; t < args.parameters.slices; t++ )
{
var predicate = true;
// slices
theta = d360 * t / args.parameters.slices;
x = precision( Math.cos( theta ) * Math.sin( phi ), 3 ) * args.parameters.scale.x + offsets.position.x;
y = precision( Math.cos( phi ), 3 ) * args.parameters.scale.y + offsets.position.y;
z = precision( Math.sin( theta ) * Math.sin( phi ), 3 ) * args.parameters.scale.z + offsets.position.z;
pos = [ x, y, z ];
// caps - both sin and cos are 0
cap = !( precision( Math.sin( phi ), 0 ) || precision( Math.cos( theta ), 0 ) );
/* This is my problem area ************************************/
tangentX = - Math.sign( Math.sin( theta ) ) * Math.tan( y );
tangentY = d90 - theta;
tangentZ = 0;
//if ( predicate ) console.log( p, t, degrees( theta ), degrees( tangentY ), Math.sign( Math.sin( phi ) ) );
// add only once
if ( !hash( arrays.unique, pos ) && predicate )
{
arrays.unique.push( pos );
arrays.position.push( x, y, z );
arrays.rotation.push( tangentX, tangentY, tangentZ );
}
}
}
return { position: new Float32Array( arrays.position ), rotation: new Float32Array( arrays.rotation ) };
};