From 38648d8665096b7d3d54d806ea7ef9067c83be0d Mon Sep 17 00:00:00 2001 From: boytchev <70705048+boytchev@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:04:24 +0300 Subject: [PATCH] Optimized globals.js --- docs/userguide-bg.md | 8 +++---- docs/userguide.md | 8 ++++--- src/globals.js | 45 +++++++++++++++++++---------------- src/mannequin.js | 4 ++-- src/organs/Phalange.js | 9 +++++-- src/shapes/LimbShape.js | 4 ++-- src/shapes/ParametricShape.js | 10 ++++++-- src/shapes/PelvisShape.js | 4 ++-- src/shapes/ShoeShape.js | 6 ++--- src/shapes/TorsoShape.js | 4 ++-- 10 files changed, 59 insertions(+), 43 deletions(-) diff --git a/docs/userguide-bg.md b/docs/userguide-bg.md index bf5162d..0707033 100644 --- a/docs/userguide-bg.md +++ b/docs/userguide-bg.md @@ -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* diff --git a/docs/userguide.md b/docs/userguide.md index 2f4f73e..c05ace1 100644 --- a/docs/userguide.md +++ b/docs/userguide.md @@ -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 diff --git a/src/globals.js b/src/globals.js index 3842b72..bffd29c 100644 --- a/src/globals.js +++ b/src/globals.js @@ -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', }; @@ -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 ) ); @@ -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 ) @@ -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 }; diff --git a/src/mannequin.js b/src/mannequin.js index 437882f..89c802b 100644 --- a/src/mannequin.js +++ b/src/mannequin.js @@ -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'; diff --git a/src/organs/Phalange.js b/src/organs/Phalange.js index d45fbd7..4458aa1 100644 --- a/src/organs/Phalange.js +++ b/src/organs/Phalange.js @@ -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 ) { @@ -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, diff --git a/src/shapes/LimbShape.js b/src/shapes/LimbShape.js index 3af1768..09d568e 100644 --- a/src/shapes/LimbShape.js +++ b/src/shapes/LimbShape.js @@ -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'; @@ -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 ); diff --git a/src/shapes/ParametricShape.js b/src/shapes/ParametricShape.js index 6823de4..76fc45b 100644 --- a/src/shapes/ParametricShape.js +++ b/src/shapes/ParametricShape.js @@ -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( @@ -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, diff --git a/src/shapes/PelvisShape.js b/src/shapes/PelvisShape.js index 19e03d3..2031e1c 100644 --- a/src/shapes/PelvisShape.js +++ b/src/shapes/PelvisShape.js @@ -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'; @@ -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 ], diff --git a/src/shapes/ShoeShape.js b/src/shapes/ShoeShape.js index 4a43ce1..73c5e4b 100644 --- a/src/shapes/ShoeShape.js +++ b/src/shapes/ShoeShape.js @@ -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'; @@ -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 ], @@ -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 ] diff --git a/src/shapes/TorsoShape.js b/src/shapes/TorsoShape.js index bd4909c..7932909 100644 --- a/src/shapes/TorsoShape.js +++ b/src/shapes/TorsoShape.js @@ -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'; @@ -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, [