Skip to content

Commit

Permalink
Optimized globals.js
Browse files Browse the repository at this point in the history
  • Loading branch information
boytchev committed Aug 9, 2024
1 parent 6130eb2 commit 38648d8
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 43 deletions.
8 changes: 4 additions & 4 deletions docs/userguide-bg.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ var kid = new Child();

Библиотеката mannequin.js дефинира следните фукции и класове:

* `getVersion()` – функция, връща текущата версия на mannequin.js като число; напр. 5.2
* `getPostureVersion()` – функция, връща текущата версия на формата на данните, описващи поза
* `getGroundLevel()` – функция, връща вертикалното положение на земята в метри

* `getVersion()` – функция, текущата версия на mannequin.js като число; напр. 5.2
* `getPostureVersion()` – функция, текущата версия на формата на данните, описващи поза
* `getGroundLevel()` – функция, вертикалното положение на земята в метри
* `blend(p,q,k)` – функция, смесена поза на пози *p* и *q* с коефициент *k*



Expand Down
8 changes: 5 additions & 3 deletions docs/userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ male and female posture.

The library mannequin.js defines the following functions and classes:

* `getVersion()` – function, returns the current version of mannequin.js as a number; e.g. 5.2
* `getPostureVersion()` – function, returns the current version of posture data format
* `getGroundLevel()` – function, returns the vertical position of the ground in meters
* `getVersion()` – function, current version of mannequin.js as a number; e.g. 5.2
* `getPostureVersion()` – function, current version of posture data format
* `getGroundLevel()` – function, vertical position of the ground in meters
* `blend(p,q,k)` – function, blended posture of postures *p* and *q* with coefficient *k*
* `addLabel(...)` – function, attaches a 3D label to a joint



Expand Down
45 changes: 24 additions & 21 deletions src/globals.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import * as THREE from "three";


const VERSION = 5.2;
const VERSION_POSTURE = 7;



const GROUND_LEVEL = -0.7;



const BODY_COLORS = {
HEAD: 'antiquewhite', // 0
SHOES: 'gray', // 1
PELVIS: 'antiquewhite', // 2
JOINTS: 'burlywood', // 3
LIMBS: 'antiquewhite', // 4
TORSO: 'bisque', // 5
NAILS: 'burlywood', // 6
HEAD: 'antiquewhite',
SHOES: 'gray',
PELVIS: 'antiquewhite',
JOINTS: 'burlywood',
LIMBS: 'antiquewhite',
TORSO: 'bisque',
NAILS: 'burlywood',
};


Expand Down Expand Up @@ -45,33 +47,34 @@ function getGroundLevel( ) {



// limb and body texture
var limbTexture = new THREE.TextureLoader().load( "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQMAAACQp+OdAAAABlBMVEX////Ly8vsgL9iAAAAHElEQVQoz2OgEPyHAjgDjxoKGWTaRRkYDR/8AAAU9d8hJ6+ZxgAAAABJRU5ErkJggg==" );


// joint object-template
var jointGeometry = new THREE.IcosahedronGeometry( 1, 2 );


// helper functions working with degrees
// helper function degrees->radians
function rad( x ) {

return x * Math.PI / 180;

}



// helper function radians->degrees with rounding
function grad( x ) {

return Number( ( x * 180 / Math.PI ).toFixed( 1 ) );

}



// helper function sine of degrees
function sin( x ) {

return Math.sin( rad( x ) );

}



// helper function cosine of degrees
function cos( x ) {

return Math.cos( rad( x ) );
Expand Down Expand Up @@ -102,7 +105,7 @@ function cossers( u, v, params ) {




// blends two postures and returns the resulting posture
function blend( posture0, posture1, k ) {

if ( posture0.version != posture1.version )
Expand Down Expand Up @@ -136,10 +139,10 @@ function blend( posture0, posture1, k ) {

// public exports

export { getVersion, getPostureVersion, getGroundLevel };
export { getVersion, getPostureVersion, getGroundLevel, blend };



// system exports

export { BODY_COLORS, jointGeometry, cossers, rad, grad, sin, cos, blend, limbTexture };
export { BODY_COLORS, cossers, rad, grad, sin, cos };
4 changes: 2 additions & 2 deletions src/mannequin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export * from './bodies/Mannequin.js';
export * from './bodies/Female.js';
export * from './bodies/Male.js';
export * from './bodies/Child.js';
export * from './globals.js';
export { getVersion, getPostureVersion, getGroundLevel, blend } from './globals.js';
export * from './scene.js';
export * from './label.js';
export { addLabel } from './label.js';
9 changes: 7 additions & 2 deletions src/organs/Phalange.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import * as THREE from "three";
import { BODY_COLORS, jointGeometry } from '../globals.js';
import { BODY_COLORS } from '../globals.js';
import { LimbShape } from '../shapes/LimbShape.js';
import { Joint } from "./Joint.js";



var NAIL_GEOMETRY = new THREE.IcosahedronGeometry( 1, 2 );



class Phalange extends Joint {

constructor( parentJoint, params, nailSize ) {
Expand All @@ -15,7 +20,7 @@ class Phalange extends Joint {

if ( nailSize > 0 ) {

this.nail = new THREE.Mesh( jointGeometry,
this.nail = new THREE.Mesh( NAIL_GEOMETRY,
new THREE.MeshStandardMaterial(
{
color: BODY_COLORS.NAILS,
Expand Down
4 changes: 2 additions & 2 deletions src/shapes/LimbShape.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as THREE from "three";

import { BODY_COLORS, cos, limbTexture, sin } from '../globals.js';
import { BODY_COLORS, cos, sin } from '../globals.js';
import { ParametricShape } from './ParametricShape.js';


Expand All @@ -17,7 +17,7 @@ class LimbShape extends ParametricShape {
offset = params[ 5 ],
scale = params[ 6 ],
rad = params[ 7 ];
super( limbTexture, BODY_COLORS.LIMBS, function ( u, v, target ) {
super( null, BODY_COLORS.LIMBS, function ( u, v, target ) {

v = 360 * v;
var r = offset + scale * cos( alpha + dAlpha * u );
Expand Down
10 changes: 8 additions & 2 deletions src/shapes/ParametricShape.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import * as THREE from "three";
import { ParametricGeometry } from 'three/addons/geometries/ParametricGeometry.js';
import { BODY_COLORS } from "../globals.js";

import { BODY_COLORS, jointGeometry } from "../globals.js";


var JOINT_GEOMETRY = new THREE.IcosahedronGeometry( 1, 2 );

var LIMB_TEXTURE = new THREE.TextureLoader().load( "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQMAAACQp+OdAAAABlBMVEX////Ly8vsgL9iAAAAHElEQVQoz2OgEPyHAjgDjxoKGWTaRRkYDR/8AAAU9d8hJ6+ZxgAAAABJRU5ErkJggg==" );


// parametric surface
class ParametricShape extends THREE.Mesh {

constructor( texture, color, func, uDivisions = 3, vDivisions = 3 ) {

texture = texture ?? LIMB_TEXTURE;

super(
new ParametricGeometry( func, uDivisions, vDivisions ),
new THREE.MeshStandardMaterial(
Expand All @@ -29,7 +35,7 @@ class ParametricShape extends THREE.Mesh {

addSphere( radius, y, x = 0, z = 0 ) {

var s = new THREE.Mesh( jointGeometry,
var s = new THREE.Mesh( JOINT_GEOMETRY,
new THREE.MeshStandardMaterial(
{
color: BODY_COLORS.JOINTS,
Expand Down
4 changes: 2 additions & 2 deletions src/shapes/PelvisShape.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BODY_COLORS, cos, cossers, limbTexture, sin } from '../globals.js';
import { BODY_COLORS, cos, cossers, sin } from '../globals.js';
import { ParametricShape } from './ParametricShape.js';


Expand All @@ -7,7 +7,7 @@ class PelvisShape extends ParametricShape {

constructor( feminine, params ) {

super( limbTexture, BODY_COLORS.PELVIS, function ( u, v, target ) {
super( null, BODY_COLORS.PELVIS, function ( u, v, target ) {

var r = cossers( u, v, [
[ 0.6, 0.95, 0, 1, 4 ],
Expand Down
6 changes: 3 additions & 3 deletions src/shapes/ShoeShape.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BODY_COLORS, cos, cossers, limbTexture, sin } from '../globals.js';
import { BODY_COLORS, cos, cossers, sin } from '../globals.js';
import { ParametricShape } from './ParametricShape.js';


Expand All @@ -7,7 +7,7 @@ class ShoeShape extends ParametricShape {

constructor( feminine, params ) {

super( limbTexture, BODY_COLORS.SHOES, function ( u, v, target ) {
super( null, BODY_COLORS.SHOES, function ( u, v, target ) {

var r = cossers( u, v, [
[ 0.6, 1.1, 0.05, 0.95, 1 ],
Expand All @@ -24,7 +24,7 @@ class ShoeShape extends ParametricShape {

if ( feminine ) {

this.add( new ParametricShape( limbTexture, BODY_COLORS.LIMBS, function ( u, v, target ) {
this.add( new ParametricShape( null, BODY_COLORS.LIMBS, function ( u, v, target ) {

var r = cossers( u, v, [
[ 0.6, 1.1, 0.05, 0.95, 1 / 2 ]
Expand Down
4 changes: 2 additions & 2 deletions src/shapes/TorsoShape.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BODY_COLORS, cos, cossers, limbTexture, sin } from '../globals.js';
import { BODY_COLORS, cos, cossers, sin } from '../globals.js';
import { ParametricShape } from './ParametricShape.js';


Expand All @@ -14,7 +14,7 @@ class TorsoShape extends ParametricShape {
dAlpha = params[ 4 ],
offset = params[ 5 ],
scale = params[ 6 ];
super( limbTexture, BODY_COLORS.TORSO, function ( u, v, target ) {
super( null, BODY_COLORS.TORSO, function ( u, v, target ) {

var r = offset + scale * cos( alpha + dAlpha * u );
if ( feminine ) r += cossers( u, v, [
Expand Down

0 comments on commit 38648d8

Please sign in to comment.