cuju-render 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cuju/render/__init__.py +759 -0
- cuju/render/__main__.py +1065 -0
- cuju/render/_export/__init__.py +3 -0
- cuju/render/_export/feed.py +542 -0
- cuju/render/_export/sizes.py +157 -0
- cuju/render/_export/video.py +1618 -0
- cuju/render/_export/video_jobs.py +408 -0
- cuju/render/_format/__init__.py +3 -0
- cuju/render/_format/annotations.py +235 -0
- cuju/render/_format/codec.py +307 -0
- cuju/render/_format/keep.py +121 -0
- cuju/render/_format/recording.py +860 -0
- cuju/render/_format/writer.py +54 -0
- cuju/render/_guide/__init__.py +3 -0
- cuju/render/_guide/doctor.py +523 -0
- cuju/render/_guide/help.py +62 -0
- cuju/render/_guide/tutorial.py +254 -0
- cuju/render/_http/__init__.py +3 -0
- cuju/render/_http/commands.py +57 -0
- cuju/render/_http/headers.py +103 -0
- cuju/render/_http/launch.py +500 -0
- cuju/render/_http/launchers.py +408 -0
- cuju/render/_http/pwa.py +128 -0
- cuju/render/_http/server.py +2105 -0
- cuju/render/_http/window.py +360 -0
- cuju/render/_moments/__init__.py +3 -0
- cuju/render/_moments/captions.py +284 -0
- cuju/render/_moments/events.py +1123 -0
- cuju/render/_moments/search.py +694 -0
- cuju/render/_packages/__init__.py +3 -0
- cuju/render/_packages/glsl.py +746 -0
- cuju/render/_packages/loudness.py +334 -0
- cuju/render/_packages/pack.py +1959 -0
- cuju/render/_packages/presentation.py +1376 -0
- cuju/render/_packages/vocabulary.py +609 -0
- cuju/render/_playing/__init__.py +3 -0
- cuju/render/_playing/achievements.py +463 -0
- cuju/render/_playing/bench.py +609 -0
- cuju/render/_playing/challenges.py +1813 -0
- cuju/render/_playing/demo.py +323 -0
- cuju/render/_playing/draw.py +211 -0
- cuju/render/_playing/errors.py +20 -0
- cuju/render/_playing/game.py +645 -0
- cuju/render/_playing/limits.py +339 -0
- cuju/render/_playing/play.py +926 -0
- cuju/render/_playing/policy_worker.py +250 -0
- cuju/render/_playing/strict.py +810 -0
- cuju/render/_registry/__init__.py +3 -0
- cuju/render/_registry/clips.py +394 -0
- cuju/render/_registry/joints.py +108 -0
- cuju/render/_registry/teams.py +2058 -0
- cuju/render/_store/__init__.py +3 -0
- cuju/render/_store/home.py +70 -0
- cuju/render/_store/library.py +604 -0
- cuju/render/_store/samples.py +163 -0
- cuju/render/_text/__init__.py +3 -0
- cuju/render/_text/languages.py +637 -0
- cuju/render/_text/messages.py +1396 -0
- cuju/render/_tournaments/__init__.py +3 -0
- cuju/render/_tournaments/results.py +547 -0
- cuju/render/_tournaments/tournament.py +2742 -0
- cuju/render/_tournaments/tournament_cli.py +587 -0
- cuju/render/_tournaments/tournament_runner.py +1975 -0
- cuju/render/_version.py +6 -0
- cuju/render/_watch/__init__.py +3 -0
- cuju/render/_watch/grid.py +265 -0
- cuju/render/_watch/live.py +664 -0
- cuju/render/_watch/notebook.py +79 -0
- cuju/render/_watch/show.py +171 -0
- cuju/render/samples/sample-11v11.cjr +0 -0
- cuju/render/samples/sample-5v5.cjr +0 -0
- cuju/render/samples/teams/starter/main.py +33 -0
- cuju/render/scenarios/benchmark_full_game_11_vs_11_easy.py +46 -0
- cuju/render/scenarios/benchmark_full_game_11_vs_11_hard.py +46 -0
- cuju/render/scenarios/benchmark_full_game_11_vs_11_medium.py +46 -0
- cuju/render/scenarios/benchmark_full_game_5_vs_5_easy.py +34 -0
- cuju/render/scenarios/benchmark_full_game_5_vs_5_hard.py +34 -0
- cuju/render/scenarios/benchmark_full_game_5_vs_5_medium.py +34 -0
- cuju/render/web/assets/adboard_polygon.webp +0 -0
- cuju/render/web/assets/adboard_your_ad_here.webp +0 -0
- cuju/render/web/assets/animations/kaykit-rig-medium-combatmelee.glb +0 -0
- cuju/render/web/assets/animations/kaykit-rig-medium-general.glb +0 -0
- cuju/render/web/assets/animations/kaykit-rig-medium-movementadvanced.glb +0 -0
- cuju/render/web/assets/animations/kaykit-rig-medium-movementbasic.glb +0 -0
- cuju/render/web/assets/animations/kaykit-rig-medium-simulation.glb +0 -0
- cuju/render/web/assets/animations/kaykit-rig-medium-special.glb +0 -0
- cuju/render/web/assets/ball.glb +0 -0
- cuju/render/web/assets/challenges/3_vs_1_with_keeper.mp4 +0 -0
- cuju/render/web/assets/challenges/3_vs_1_with_keeper.webp +0 -0
- cuju/render/web/assets/challenges/corner.mp4 +0 -0
- cuju/render/web/assets/challenges/corner.webp +0 -0
- cuju/render/web/assets/challenges/counterattack_easy.mp4 +0 -0
- cuju/render/web/assets/challenges/counterattack_easy.webp +0 -0
- cuju/render/web/assets/challenges/counterattack_hard.mp4 +0 -0
- cuju/render/web/assets/challenges/counterattack_hard.webp +0 -0
- cuju/render/web/assets/challenges/empty_goal.mp4 +0 -0
- cuju/render/web/assets/challenges/empty_goal.webp +0 -0
- cuju/render/web/assets/challenges/empty_goal_close.mp4 +0 -0
- cuju/render/web/assets/challenges/empty_goal_close.webp +0 -0
- cuju/render/web/assets/challenges/manifest.json +375 -0
- cuju/render/web/assets/challenges/one_on_one.mp4 +0 -0
- cuju/render/web/assets/challenges/one_on_one.webp +0 -0
- cuju/render/web/assets/challenges/one_on_one_hard.mp4 +0 -0
- cuju/render/web/assets/challenges/one_on_one_hard.webp +0 -0
- cuju/render/web/assets/challenges/pass_and_shoot_with_keeper.mp4 +0 -0
- cuju/render/web/assets/challenges/pass_and_shoot_with_keeper.webp +0 -0
- cuju/render/web/assets/challenges/run_pass_and_shoot_with_keeper.mp4 +0 -0
- cuju/render/web/assets/challenges/run_pass_and_shoot_with_keeper.webp +0 -0
- cuju/render/web/assets/challenges/run_to_score.mp4 +0 -0
- cuju/render/web/assets/challenges/run_to_score.webp +0 -0
- cuju/render/web/assets/challenges/run_to_score_with_keeper.mp4 +0 -0
- cuju/render/web/assets/challenges/run_to_score_with_keeper.webp +0 -0
- cuju/render/web/assets/challenges/single_goal_versus_lazy.mp4 +0 -0
- cuju/render/web/assets/challenges/single_goal_versus_lazy.webp +0 -0
- cuju/render/web/assets/crowd.webp +0 -0
- cuju/render/web/assets/crowd_normal.webp +0 -0
- cuju/render/web/assets/cuju-logo-384.png +0 -0
- cuju/render/web/assets/cuju-logo.png +0 -0
- cuju/render/web/assets/cuju-logo.svg +46 -0
- cuju/render/web/assets/cuju-mark-light.svg +33 -0
- cuju/render/web/assets/cuju-mark.svg +33 -0
- cuju/render/web/assets/goal.glb +0 -0
- cuju/render/web/assets/grass.webp +0 -0
- cuju/render/web/assets/hair_black.png +0 -0
- cuju/render/web/assets/hair_blonde.png +0 -0
- cuju/render/web/assets/hair_brown.png +0 -0
- cuju/render/web/assets/hair_darkblonde.png +0 -0
- cuju/render/web/assets/hair_long01.glb +0 -0
- cuju/render/web/assets/hair_long02.glb +0 -0
- cuju/render/web/assets/hair_medium01.glb +0 -0
- cuju/render/web/assets/hair_red.png +0 -0
- cuju/render/web/assets/hair_short01.glb +0 -0
- cuju/render/web/assets/hair_short02.glb +0 -0
- cuju/render/web/assets/icons/cuju-192.png +0 -0
- cuju/render/web/assets/icons/cuju-512.png +0 -0
- cuju/render/web/assets/icons/cuju-maskable-512.png +0 -0
- cuju/render/web/assets/kit_goalkeeper.webp +0 -0
- cuju/render/web/assets/kit_mask.png +0 -0
- cuju/render/web/assets/kit_normal.webp +0 -0
- cuju/render/web/assets/manifest.json +601 -0
- cuju/render/web/assets/models/kaykit-barbarian.glb +0 -0
- cuju/render/web/assets/models/kaykit-barbarian.json +23 -0
- cuju/render/web/assets/models/kaykit-knight.glb +0 -0
- cuju/render/web/assets/models/kaykit-knight.json +23 -0
- cuju/render/web/assets/models/kaykit-mage.glb +0 -0
- cuju/render/web/assets/models/kaykit-mage.json +23 -0
- cuju/render/web/assets/models/kaykit-mannequin-medium.glb +0 -0
- cuju/render/web/assets/models/kaykit-mannequin-medium.json +23 -0
- cuju/render/web/assets/models/kaykit-ranger.glb +0 -0
- cuju/render/web/assets/models/kaykit-ranger.json +23 -0
- cuju/render/web/assets/models/kaykit-rogue-hooded.glb +0 -0
- cuju/render/web/assets/models/kaykit-rogue-hooded.json +23 -0
- cuju/render/web/assets/models/kaykit-rogue.glb +0 -0
- cuju/render/web/assets/models/kaykit-rogue.json +23 -0
- cuju/render/web/assets/models/kaykit-skeleton-mage.glb +0 -0
- cuju/render/web/assets/models/kaykit-skeleton-mage.json +23 -0
- cuju/render/web/assets/models/kaykit-skeleton-minion.glb +0 -0
- cuju/render/web/assets/models/kaykit-skeleton-minion.json +23 -0
- cuju/render/web/assets/models/kaykit-skeleton-rogue.glb +0 -0
- cuju/render/web/assets/models/kaykit-skeleton-rogue.json +23 -0
- cuju/render/web/assets/models/kaykit-skeleton-warrior.glb +0 -0
- cuju/render/web/assets/models/kaykit-skeleton-warrior.json +23 -0
- cuju/render/web/assets/pitch.webp +0 -0
- cuju/render/web/assets/pitch_normal.webp +0 -0
- cuju/render/web/assets/player.glb +0 -0
- cuju/render/web/assets/shoe.webp +0 -0
- cuju/render/web/assets/shoe_sole.png +0 -0
- cuju/render/web/assets/skin_01.png +0 -0
- cuju/render/web/assets/skin_02.png +0 -0
- cuju/render/web/assets/skin_03.png +0 -0
- cuju/render/web/assets/skin_04.png +0 -0
- cuju/render/web/assets/sounds/applause.ogg +0 -0
- cuju/render/web/assets/sounds/beach-wave-1.ogg +0 -0
- cuju/render/web/assets/sounds/beach-wave-2.ogg +0 -0
- cuju/render/web/assets/sounds/beach-wave-3.ogg +0 -0
- cuju/render/web/assets/sounds/beach-wave-4.ogg +0 -0
- cuju/render/web/assets/sounds/city-horn.ogg +0 -0
- cuju/render/web/assets/sounds/city-loop.ogg +0 -0
- cuju/render/web/assets/sounds/city-siren.ogg +0 -0
- cuju/render/web/assets/sounds/crowd-cheer.ogg +0 -0
- cuju/render/web/assets/sounds/crowd-loop.ogg +0 -0
- cuju/render/web/assets/sounds/crowd-ooh.ogg +0 -0
- cuju/render/web/assets/sounds/gulls.ogg +0 -0
- cuju/render/web/assets/sounds/neon-sting.ogg +0 -0
- cuju/render/web/assets/sounds/rain-loop.ogg +0 -0
- cuju/render/web/assets/sounds/sounds.json +46 -0
- cuju/render/web/assets/sounds/space-beep.ogg +0 -0
- cuju/render/web/assets/sounds/space-hum.ogg +0 -0
- cuju/render/web/assets/sounds/space-radio.ogg +0 -0
- cuju/render/web/assets/sounds/whistle-long.ogg +0 -0
- cuju/render/web/assets/sounds/whistle-short.ogg +0 -0
- cuju/render/web/assets/sounds/wind-loop.ogg +0 -0
- cuju/render/web/easy.css +357 -0
- cuju/render/web/export.html +23 -0
- cuju/render/web/feed.css +262 -0
- cuju/render/web/feed.html +28 -0
- cuju/render/web/flat.css +120 -0
- cuju/render/web/home.css +837 -0
- cuju/render/web/index.html +373 -0
- cuju/render/web/js/boot.js +50 -0
- cuju/render/web/js/broadcast/audio.js +2141 -0
- cuju/render/web/js/broadcast/celebrate.js +1238 -0
- cuju/render/web/js/broadcast/decisions.js +609 -0
- cuju/render/web/js/broadcast/hud.js +352 -0
- cuju/render/web/js/broadcast/show.js +3614 -0
- cuju/render/web/js/broadcast/transition.js +2231 -0
- cuju/render/web/js/broadcast/voices.js +153 -0
- cuju/render/web/js/capture.js +101 -0
- cuju/render/web/js/core/accessibility.js +90 -0
- cuju/render/web/js/core/api.js +429 -0
- cuju/render/web/js/core/comfort.js +135 -0
- cuju/render/web/js/core/i18n.js +705 -0
- cuju/render/web/js/core/icons.js +167 -0
- cuju/render/web/js/core/input-settings.js +156 -0
- cuju/render/web/js/core/keys.js +277 -0
- cuju/render/web/js/core/language-ui.js +125 -0
- cuju/render/web/js/core/menu.js +853 -0
- cuju/render/web/js/core/names.js +113 -0
- cuju/render/web/js/core/page-structure.js +110 -0
- cuju/render/web/js/core/scenarios.js +109 -0
- cuju/render/web/js/core/spoilers.js +236 -0
- cuju/render/web/js/core/typeahead.js +38 -0
- cuju/render/web/js/export/dom-picture.js +330 -0
- cuju/render/web/js/export/export-host.js +97 -0
- cuju/render/web/js/export/feed-dialog.js +191 -0
- cuju/render/web/js/export/feed-params.js +266 -0
- cuju/render/web/js/export/feed.js +363 -0
- cuju/render/web/js/export/mp4-mux.js +295 -0
- cuju/render/web/js/export/photo-core.js +313 -0
- cuju/render/web/js/export/photo.js +1314 -0
- cuju/render/web/js/export/safe-frame.js +59 -0
- cuju/render/web/js/export/size-fields.js +103 -0
- cuju/render/web/js/export/sizes.js +178 -0
- cuju/render/web/js/export/video-boot.js +55 -0
- cuju/render/web/js/export/video-browser.js +325 -0
- cuju/render/web/js/export/video-export.js +417 -0
- cuju/render/web/js/export/video-plan.js +207 -0
- cuju/render/web/js/export/video-range.js +185 -0
- cuju/render/web/js/export/webm-mux.js +198 -0
- cuju/render/web/js/feed-boot.js +151 -0
- cuju/render/web/js/feed-frame.js +101 -0
- cuju/render/web/js/help/doctor.js +488 -0
- cuju/render/web/js/help/firstrun.js +379 -0
- cuju/render/web/js/help/help-center.js +438 -0
- cuju/render/web/js/help/install-offer.js +164 -0
- cuju/render/web/js/help/install-state.js +104 -0
- cuju/render/web/js/help/lessons-core.js +207 -0
- cuju/render/web/js/help/lessons.js +463 -0
- cuju/render/web/js/help/palette.js +584 -0
- cuju/render/web/js/help/pwa.js +257 -0
- cuju/render/web/js/help/tour-core.js +241 -0
- cuju/render/web/js/help/tour.js +461 -0
- cuju/render/web/js/main.js +2513 -0
- cuju/render/web/js/pages/draw.js +188 -0
- cuju/render/web/js/pages/fieldpage.js +530 -0
- cuju/render/web/js/pages/home.js +521 -0
- cuju/render/web/js/pages/matches.js +779 -0
- cuju/render/web/js/pages/moments-ui.js +359 -0
- cuju/render/web/js/pages/queue-filters.js +63 -0
- cuju/render/web/js/pages/recording-groups.js +80 -0
- cuju/render/web/js/pages/recordings.js +1069 -0
- cuju/render/web/js/play/bench.js +347 -0
- cuju/render/web/js/play/challenges.js +1302 -0
- cuju/render/web/js/play/medals.js +127 -0
- cuju/render/web/js/play/play.js +1953 -0
- cuju/render/web/js/play/teasers.js +196 -0
- cuju/render/web/js/play/touch.js +350 -0
- cuju/render/web/js/play/tournament-layout.js +487 -0
- cuju/render/web/js/play/tournament-results.js +441 -0
- cuju/render/web/js/play/tournament-strict.js +560 -0
- cuju/render/web/js/play/tournament.js +2585 -0
- cuju/render/web/js/players/animate.js +972 -0
- cuju/render/web/js/players/clips.js +224 -0
- cuju/render/web/js/players/kaykit.js +162 -0
- cuju/render/web/js/players/kits.js +676 -0
- cuju/render/web/js/players/number-patch.js +82 -0
- cuju/render/web/js/players/players.js +2694 -0
- cuju/render/web/js/players/recolor.js +893 -0
- cuju/render/web/js/players/retarget.js +325 -0
- cuju/render/web/js/players/rig-numbers.js +174 -0
- cuju/render/web/js/presentation/animator.js +231 -0
- cuju/render/web/js/presentation/assign.js +85 -0
- cuju/render/web/js/presentation/behaviors.js +440 -0
- cuju/render/web/js/presentation/budget.js +209 -0
- cuju/render/web/js/presentation/catalog.js +1674 -0
- cuju/render/web/js/presentation/clocks.js +72 -0
- cuju/render/web/js/presentation/commands.js +301 -0
- cuju/render/web/js/presentation/contract.js +240 -0
- cuju/render/web/js/presentation/effects.js +904 -0
- cuju/render/web/js/presentation/flash.js +605 -0
- cuju/render/web/js/presentation/freeze-worklet.js +22 -0
- cuju/render/web/js/presentation/glsl.js +464 -0
- cuju/render/web/js/presentation/host.js +323 -0
- cuju/render/web/js/presentation/index.js +2066 -0
- cuju/render/web/js/presentation/loudness-worklet.js +235 -0
- cuju/render/web/js/presentation/loudness.js +740 -0
- cuju/render/web/js/presentation/moments.js +536 -0
- cuju/render/web/js/presentation/post.js +327 -0
- cuju/render/web/js/presentation/preview.js +180 -0
- cuju/render/web/js/presentation/protocol.js +174 -0
- cuju/render/web/js/presentation/reach.js +158 -0
- cuju/render/web/js/presentation/rigging.js +130 -0
- cuju/render/web/js/presentation/sandbox.js +157 -0
- cuju/render/web/js/presentation/shaders.js +235 -0
- cuju/render/web/js/presentation/stage.js +402 -0
- cuju/render/web/js/presentation/teamaudio.js +722 -0
- cuju/render/web/js/presentation/tracks.js +197 -0
- cuju/render/web/js/presentation/values.js +81 -0
- cuju/render/web/js/recording/annotation-data.js +203 -0
- cuju/render/web/js/recording/captions.js +491 -0
- cuju/render/web/js/recording/codec.js +185 -0
- cuju/render/web/js/recording/decode-worker.js +70 -0
- cuju/render/web/js/recording/decode.js +94 -0
- cuju/render/web/js/recording/episodes.js +46 -0
- cuju/render/web/js/recording/format.js +461 -0
- cuju/render/web/js/recording/live.js +348 -0
- cuju/render/web/js/recording/moments-query.js +59 -0
- cuju/render/web/js/recording/moments.js +149 -0
- cuju/render/web/js/recording/pose.js +86 -0
- cuju/render/web/js/recording/timeline.js +275 -0
- cuju/render/web/js/teams/art.js +50 -0
- cuju/render/web/js/teams/clip-editor.js +293 -0
- cuju/render/web/js/teams/editor.js +1979 -0
- cuju/render/web/js/teams/library.js +344 -0
- cuju/render/web/js/teams/painter.js +633 -0
- cuju/render/web/js/teams/surprise-ui.js +195 -0
- cuju/render/web/js/teams/surprise.js +497 -0
- cuju/render/web/js/teams/team-check.js +127 -0
- cuju/render/web/js/watch/annotations.js +270 -0
- cuju/render/web/js/watch/flat.js +366 -0
- cuju/render/web/js/watch/grid.js +436 -0
- cuju/render/web/js/watch/pitch2d.js +289 -0
- cuju/render/web/js/watch/player-tools.js +192 -0
- cuju/render/web/js/watch/presets.js +201 -0
- cuju/render/web/js/world/arenas.js +997 -0
- cuju/render/web/js/world/ball.js +204 -0
- cuju/render/web/js/world/cameras.js +982 -0
- cuju/render/web/js/world/crowd.js +376 -0
- cuju/render/web/js/world/field.js +227 -0
- cuju/render/web/js/world/framing.js +145 -0
- cuju/render/web/js/world/occlusion.js +48 -0
- cuju/render/web/js/world/pitch.js +703 -0
- cuju/render/web/js/world/scene.js +395 -0
- cuju/render/web/js/world/sky.js +325 -0
- cuju/render/web/js/world/stadium.js +1262 -0
- cuju/render/web/js/world/vendor-sky.js +367 -0
- cuju/render/web/js/world/weather.js +264 -0
- cuju/render/web/languages/en.json +4750 -0
- cuju/render/web/lessons.json +137 -0
- cuju/render/web/manifest.webmanifest +23 -0
- cuju/render/web/style.css +8399 -0
- cuju/render/web/sw.js +235 -0
- cuju/render/web/vendor/fabric/LICENSE +16 -0
- cuju/render/web/vendor/fabric/fabric.min.mjs +1 -0
- cuju/render/web/vendor/lucide/LICENSE +43 -0
- cuju/render/web/vendor/lucide/icons.js +141 -0
- cuju/render/web/vendor/three/LICENSE +21 -0
- cuju/render/web/vendor/three/build/three.core.js +60586 -0
- cuju/render/web/vendor/three/build/three.module.js +19719 -0
- cuju/render/web/vendor/three/examples/jsm/loaders/GLTFLoader.js +4925 -0
- cuju/render/web/vendor/three/examples/jsm/postprocessing/EffectComposer.js +365 -0
- cuju/render/web/vendor/three/examples/jsm/postprocessing/MaskPass.js +195 -0
- cuju/render/web/vendor/three/examples/jsm/postprocessing/OutputPass.js +148 -0
- cuju/render/web/vendor/three/examples/jsm/postprocessing/Pass.js +191 -0
- cuju/render/web/vendor/three/examples/jsm/postprocessing/RenderPass.js +193 -0
- cuju/render/web/vendor/three/examples/jsm/postprocessing/ShaderPass.js +135 -0
- cuju/render/web/vendor/three/examples/jsm/postprocessing/UnrealBloomPass.js +541 -0
- cuju/render/web/vendor/three/examples/jsm/shaders/CopyShader.js +52 -0
- cuju/render/web/vendor/three/examples/jsm/shaders/LuminosityHighPassShader.js +68 -0
- cuju/render/web/vendor/three/examples/jsm/shaders/OutputShader.js +103 -0
- cuju/render/web/vendor/three/examples/jsm/utils/BufferGeometryUtils.js +1501 -0
- cuju/render/web/vendor/three/examples/jsm/utils/SkeletonUtils.js +496 -0
- cuju_render-0.1.0.dist-info/METADATA +116 -0
- cuju_render-0.1.0.dist-info/RECORD +382 -0
- cuju_render-0.1.0.dist-info/WHEEL +4 -0
- cuju_render-0.1.0.dist-info/licenses/LICENSE +202 -0
- cuju_render-0.1.0.dist-info/licenses/LICENSES/CC-BY-4.0.txt +396 -0
- cuju_render-0.1.0.dist-info/licenses/LICENSES/CC0-1.0.txt +127 -0
- cuju_render-0.1.0.dist-info/licenses/LICENSES/ISC.txt +16 -0
- cuju_render-0.1.0.dist-info/licenses/LICENSES/MIT.txt +24 -0
- cuju_render-0.1.0.dist-info/licenses/LICENSES/Unlicense.txt +13 -0
- cuju_render-0.1.0.dist-info/licenses/NOTICE +35 -0
|
@@ -0,0 +1,2694 @@
|
|
|
1
|
+
// Copyright 2026 The cuju Authors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Player figures. Skinned glTF models are retargeted onto the 14 nodes saved in each frame, the
|
|
5
|
+
// default assets/player.glb or a team's own model. Without a model, a figure of capsules and
|
|
6
|
+
// spheres rides the same nodes.
|
|
7
|
+
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
|
|
10
|
+
import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js';
|
|
11
|
+
import { NODE_COUNT, NODE_NAMES, NODE_PARENTS, PLAYER_FLOATS } from '../recording/format.js';
|
|
12
|
+
import {
|
|
13
|
+
ciede2000, hex, kitTexture, numberColor, paintKitBase, playerLook, shirtTexture, threeColor,
|
|
14
|
+
} from './kits.js';
|
|
15
|
+
import { Recolor, kitPairs, shownColors } from './recolor.js';
|
|
16
|
+
import { fallbackCelebration, has as hasAnimation, pickCelebration } from './animate.js';
|
|
17
|
+
import { attachKayKitClips } from './kaykit.js';
|
|
18
|
+
import { RigNumbers } from './rig-numbers.js';
|
|
19
|
+
import { t } from '../core/i18n.js';
|
|
20
|
+
|
|
21
|
+
// The largest team model read, the server takes at most 30 MB so this only stops a runaway file.
|
|
22
|
+
const MAX_MODEL_BYTES = 64 * 1024 * 1024;
|
|
23
|
+
|
|
24
|
+
// Local rest positions from GRF's player.object. GRF's animations replace every local rotation,
|
|
25
|
+
// so the rest pose, the bind pose of player.glb, has identity rotations everywhere.
|
|
26
|
+
const REST_LOCAL = [
|
|
27
|
+
[0, 0, 0], [0, 0, 0.96], [0, 0, 0.15], [0, -0.03, 0.5], [0.16, -0.01, 0.48], [-0.01, 0, -0.33],
|
|
28
|
+
[-0.16, -0.01, 0.48], [0.01, 0, -0.33], [0.087, 0, -0.01], [0, 0, -0.42], [0, -0.04, -0.44],
|
|
29
|
+
[-0.087, 0, -0.01], [0, 0, -0.42], [0, -0.04, -0.44],
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
const ONE = new THREE.Vector3(1, 1, 1);
|
|
33
|
+
const ZERO_SCALE = new THREE.Matrix4().makeScale(0, 0, 0);
|
|
34
|
+
const tmpPos = new THREE.Vector3();
|
|
35
|
+
const tmpQuat = new THREE.Quaternion();
|
|
36
|
+
const tmpMat = new THREE.Matrix4();
|
|
37
|
+
const blendMat = new THREE.Matrix4();
|
|
38
|
+
const blendMat2 = new THREE.Matrix4();
|
|
39
|
+
const blendPa = new THREE.Vector3();
|
|
40
|
+
const blendPb = new THREE.Vector3();
|
|
41
|
+
const blendQa = new THREE.Quaternion();
|
|
42
|
+
const blendQb = new THREE.Quaternion();
|
|
43
|
+
const blendSa = new THREE.Vector3();
|
|
44
|
+
const blendSb = new THREE.Vector3();
|
|
45
|
+
|
|
46
|
+
// Rest world transforms in the engine frame, as matrices.
|
|
47
|
+
function restWorldEngine() {
|
|
48
|
+
const world = [];
|
|
49
|
+
for (let i = 0; i < NODE_COUNT; i++) {
|
|
50
|
+
const local = new THREE.Matrix4().makeTranslation(...REST_LOCAL[i]);
|
|
51
|
+
world.push(NODE_PARENTS[i] < 0 ? local : world[NODE_PARENTS[i]].clone().multiply(local));
|
|
52
|
+
}
|
|
53
|
+
return world;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// GRF's reference height and the tallest a model is drawn, 1.25 times that.
|
|
57
|
+
const GRF_HEIGHT = 1.92;
|
|
58
|
+
const MAX_MODEL_HEIGHT = 1.25 * GRF_HEIGHT;
|
|
59
|
+
|
|
60
|
+
const ENGINE_TO_THREE = new THREE.Matrix4().makeRotationX(-Math.PI / 2);
|
|
61
|
+
const THREE_TO_ENGINE = ENGINE_TO_THREE.clone().invert();
|
|
62
|
+
const REST_ENGINE = restWorldEngine();
|
|
63
|
+
// Rest rotations in three's frame, the G of the retargeting.
|
|
64
|
+
const REST_THREE_QUAT = REST_ENGINE.map((w) => {
|
|
65
|
+
const m = ENGINE_TO_THREE.clone().multiply(w).multiply(THREE_TO_ENGINE);
|
|
66
|
+
const q = new THREE.Quaternion();
|
|
67
|
+
m.decompose(new THREE.Vector3(), q, new THREE.Vector3());
|
|
68
|
+
return q;
|
|
69
|
+
});
|
|
70
|
+
const REST_BODY_HEIGHT = new THREE.Vector3().setFromMatrixPosition(REST_ENGINE[1]).z;
|
|
71
|
+
|
|
72
|
+
// A pose buffer of one player in the rest pose, at the origin facing engine minus y.
|
|
73
|
+
export function restPoseNodes(heading = 0, out = new Float32Array(PLAYER_FLOATS)) {
|
|
74
|
+
const yaw = new THREE.Matrix4().makeRotationZ(heading);
|
|
75
|
+
const p = new THREE.Vector3();
|
|
76
|
+
const q = new THREE.Quaternion();
|
|
77
|
+
const s = new THREE.Vector3();
|
|
78
|
+
for (let i = 0; i < NODE_COUNT; i++) {
|
|
79
|
+
yaw.clone().multiply(REST_ENGINE[i]).decompose(p, q, s);
|
|
80
|
+
out.set([p.x, p.y, p.z, q.x, q.y, q.z, q.w], i * 7);
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// A procedural walk cycle in the engine frame for the team editor's preview. Every joint turns
|
|
86
|
+
// about its own x axis and the stride grows with speed up to 6 meters per second.
|
|
87
|
+
export function walkPoseNodes(phase, speed, heading = 0, out = new Float32Array(PLAYER_FLOATS)) {
|
|
88
|
+
const stride = Math.min(1, speed / 6);
|
|
89
|
+
const s = Math.sin(phase);
|
|
90
|
+
const c = Math.cos(phase);
|
|
91
|
+
const bob = 0.03 * stride * Math.abs(c);
|
|
92
|
+
const lean = -0.12 * stride;
|
|
93
|
+
const angles = [0, lean, lean * 0.5, -lean, 0.5 * stride * s, -0.5 - 0.4 * stride,
|
|
94
|
+
-0.5 * stride * s, -0.5 - 0.4 * stride, -0.55 * stride * s,
|
|
95
|
+
0.15 + 0.9 * stride * Math.max(0, -c), -0.2 * stride * s, 0.55 * stride * s,
|
|
96
|
+
0.15 + 0.9 * stride * Math.max(0, c), 0.2 * stride * s];
|
|
97
|
+
const world = [];
|
|
98
|
+
const xAxis = new THREE.Vector3(1, 0, 0);
|
|
99
|
+
for (let i = 0; i < NODE_COUNT; i++) {
|
|
100
|
+
const q = new THREE.Quaternion().setFromAxisAngle(xAxis, angles[i]);
|
|
101
|
+
const pos = new THREE.Vector3(...REST_LOCAL[i]);
|
|
102
|
+
if (i === 1) {
|
|
103
|
+
pos.z -= bob;
|
|
104
|
+
}
|
|
105
|
+
const local = new THREE.Matrix4().compose(pos, q, ONE);
|
|
106
|
+
if (i === 0) {
|
|
107
|
+
local.makeRotationZ(heading);
|
|
108
|
+
}
|
|
109
|
+
world.push(NODE_PARENTS[i] < 0 ? local : world[NODE_PARENTS[i]].clone().multiply(local));
|
|
110
|
+
}
|
|
111
|
+
const v = new THREE.Vector3();
|
|
112
|
+
const r = new THREE.Quaternion();
|
|
113
|
+
const sc = new THREE.Vector3();
|
|
114
|
+
for (let i = 0; i < NODE_COUNT; i++) {
|
|
115
|
+
world[i].decompose(v, r, sc);
|
|
116
|
+
out.set([v.x, v.y, v.z, r.x, r.y, r.z, r.w], i * 7);
|
|
117
|
+
}
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Three.js world matrix of one node from the pose buffer.
|
|
122
|
+
function nodeMatrix(nodes, offset, out) {
|
|
123
|
+
tmpPos.set(nodes[offset], nodes[offset + 2], -nodes[offset + 1]);
|
|
124
|
+
nodeQuaternion(nodes, offset, tmpQuat);
|
|
125
|
+
return out.compose(tmpPos, tmpQuat, ONE);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function nodeQuaternion(nodes, offset, out) {
|
|
129
|
+
out.set(nodes[offset + 3], nodes[offset + 5], -nodes[offset + 4], nodes[offset + 6]);
|
|
130
|
+
if (out.lengthSq() < 1e-8) {
|
|
131
|
+
out.identity();
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// One part of the simple figure, with its node, geometry, colour slot and local transform in
|
|
137
|
+
// the node's three.js axes, x to the player's left, y up the body or limb and z forward.
|
|
138
|
+
function figureParts() {
|
|
139
|
+
const capsule = (r, len) => new THREE.CapsuleGeometry(r, len, 4, 12);
|
|
140
|
+
const sphere = (r) => new THREE.SphereGeometry(r, 16, 12);
|
|
141
|
+
const part = (node, geometry, pos, slot, scale = [1, 1, 1], extra = {}) => ({
|
|
142
|
+
node, geometry, slot, ...extra,
|
|
143
|
+
local: new THREE.Matrix4().compose(new THREE.Vector3(...pos), new THREE.Quaternion(), new THREE.Vector3(...scale)),
|
|
144
|
+
});
|
|
145
|
+
const numberPlane = new THREE.PlaneGeometry(0.17, 0.17);
|
|
146
|
+
numberPlane.rotateY(Math.PI);
|
|
147
|
+
const parts = [
|
|
148
|
+
part(1, sphere(0.15), [0, 0.02, 0], 'shorts', [1.15, 0.75, 0.8]),
|
|
149
|
+
part(2, capsule(0.13, 0.26), [0, 0.25, 0.01], 'shirt', [1.25, 1, 0.78], { textured: true }),
|
|
150
|
+
part(2, numberPlane, [0, 0.31, -0.098], 'number', [1, 1, 1], { numbered: true }),
|
|
151
|
+
part(3, new THREE.CylinderGeometry(0.05, 0.055, 0.12, 10), [0, -0.01, 0.01], 'skin'),
|
|
152
|
+
part(3, sphere(0.105), [0, 0.12, 0.015], 'skin', [0.95, 1.08, 1.05]),
|
|
153
|
+
part(3, sphere(0.11), [0, 0.15, -0.005], 'hair', [1, 0.8, 1.05], { hair: ['short', 'long'] }),
|
|
154
|
+
part(3, sphere(0.1), [0, 0.07, -0.05], 'hair', [1.02, 1.4, 0.7], { hair: ['long'] }),
|
|
155
|
+
];
|
|
156
|
+
for (const [shoulder, elbow] of [[4, 5], [6, 7]]) {
|
|
157
|
+
parts.push(part(shoulder, capsule(0.056, 0.2), [0, -0.14, 0], 'shirt'));
|
|
158
|
+
parts.push(part(elbow, capsule(0.042, 0.2), [0, -0.13, 0], 'skin'));
|
|
159
|
+
parts.push(part(elbow, sphere(0.05), [0, -0.3, 0.005], 'skin', [0.8, 1.2, 1]));
|
|
160
|
+
}
|
|
161
|
+
for (const [thigh, knee, ankle] of [[8, 9, 10], [11, 12, 13]]) {
|
|
162
|
+
parts.push(part(thigh, new THREE.CylinderGeometry(0.095, 0.085, 0.2, 12), [0, -0.08, 0], 'shorts'));
|
|
163
|
+
parts.push(part(thigh, capsule(0.072, 0.26), [0, -0.23, 0], 'skin'));
|
|
164
|
+
parts.push(part(knee, capsule(0.058, 0.3), [0, -0.22, 0.02], 'socks'));
|
|
165
|
+
parts.push(part(ankle, new THREE.BoxGeometry(0.095, 0.075, 0.26), [0, -0.04, 0.06], 'boots'));
|
|
166
|
+
}
|
|
167
|
+
return parts;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Shirt numbers 0 to 99 in a 10 by 10 grid, the cell of a number is the number itself. White on
|
|
171
|
+
// transparent, tinted by the material colour, and shared by every figure, so never disposed.
|
|
172
|
+
const ATLAS_SIDE = 10;
|
|
173
|
+
let numberAtlas = null;
|
|
174
|
+
function numberAtlasTexture() {
|
|
175
|
+
if (numberAtlas) {
|
|
176
|
+
return numberAtlas;
|
|
177
|
+
}
|
|
178
|
+
const cell = 64;
|
|
179
|
+
const canvas = document.createElement('canvas');
|
|
180
|
+
canvas.width = cell * ATLAS_SIDE;
|
|
181
|
+
canvas.height = cell * ATLAS_SIDE;
|
|
182
|
+
const ctx = canvas.getContext('2d');
|
|
183
|
+
ctx.fillStyle = '#ffffff';
|
|
184
|
+
ctx.font = `800 ${cell * 0.78}px system-ui, sans-serif`;
|
|
185
|
+
ctx.textAlign = 'center';
|
|
186
|
+
ctx.textBaseline = 'middle';
|
|
187
|
+
for (let n = 0; n < ATLAS_SIDE * ATLAS_SIDE; n++) {
|
|
188
|
+
const i = n % ATLAS_SIDE;
|
|
189
|
+
const j = Math.floor(n / ATLAS_SIDE);
|
|
190
|
+
ctx.fillText(String(n), i * cell + cell / 2, j * cell + cell / 2 + 3, cell * 0.92);
|
|
191
|
+
}
|
|
192
|
+
numberAtlas = new THREE.CanvasTexture(canvas);
|
|
193
|
+
numberAtlas.colorSpace = THREE.SRGBColorSpace;
|
|
194
|
+
return numberAtlas;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Draws the numbers from the atlas, the instance attribute picks the cell. The canvas is flipped
|
|
198
|
+
// on upload, so the first row of cells sits at the top of v.
|
|
199
|
+
function numberMaterial(color) {
|
|
200
|
+
const mat = new THREE.MeshStandardMaterial({
|
|
201
|
+
map: numberAtlasTexture(), color, transparent: true, alphaTest: 0.4, roughness: 0.6,
|
|
202
|
+
polygonOffset: true, polygonOffsetFactor: -2, depthWrite: false,
|
|
203
|
+
});
|
|
204
|
+
mat.onBeforeCompile = (shader) => {
|
|
205
|
+
shader.vertexShader = shader.vertexShader
|
|
206
|
+
.replace('#include <common>', '#include <common>\nattribute float aNumber;')
|
|
207
|
+
.replace('#include <uv_vertex>', `#include <uv_vertex>
|
|
208
|
+
vMapUv = vec2((uv.x + mod(aNumber, ${ATLAS_SIDE}.0)) / ${ATLAS_SIDE}.0,
|
|
209
|
+
(uv.y + (${ATLAS_SIDE - 1}.0 - floor(aNumber / ${ATLAS_SIDE}.0))) / ${ATLAS_SIDE}.0);`);
|
|
210
|
+
};
|
|
211
|
+
mat.customProgramCacheKey = () => 'cuju-number';
|
|
212
|
+
return mat;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// How much of a player is hidden, for a camera inside or just behind its head.
|
|
216
|
+
const HIDE_NONE = 0;
|
|
217
|
+
const HIDE_HEAD = 1;
|
|
218
|
+
const HIDE_ALL = 2;
|
|
219
|
+
const HIDE_MODES = { none: HIDE_NONE, head: HIDE_HEAD, all: HIDE_ALL };
|
|
220
|
+
|
|
221
|
+
// The eyes of GRF's player from its neck node, in the engine frame, forward is minus y.
|
|
222
|
+
const GRF_EYE = new THREE.Vector3(0, -0.1, 0.15);
|
|
223
|
+
const tmpEye = new THREE.Vector3();
|
|
224
|
+
|
|
225
|
+
// The middle of the head of GRF's player from its neck node and its radius, for a player whose
|
|
226
|
+
// head was not measured.
|
|
227
|
+
const GRF_HEAD = { local: new THREE.Vector3(0, -0.02, 0.11), radius: 0.16 };
|
|
228
|
+
// The room left between a head and its glass helmet, in meters.
|
|
229
|
+
const HELMET_MARGIN = 0.04;
|
|
230
|
+
|
|
231
|
+
// The eyes of a player from the neck node of a pose, scaled about its ground point, in three's
|
|
232
|
+
// frame, or another point given in the neck node's frame.
|
|
233
|
+
function neckEye(nodes, base, scale, out, offset = GRF_EYE) {
|
|
234
|
+
const o = base + 3 * 7;
|
|
235
|
+
tmpQuat.set(nodes[o + 3], nodes[o + 4], nodes[o + 5], nodes[o + 6]);
|
|
236
|
+
if (tmpQuat.lengthSq() < 1e-8) {
|
|
237
|
+
tmpQuat.identity();
|
|
238
|
+
}
|
|
239
|
+
tmpEye.copy(offset).applyQuaternion(tmpQuat);
|
|
240
|
+
const gx = nodes[base];
|
|
241
|
+
const gy = nodes[base + 1];
|
|
242
|
+
const x = gx + (nodes[o] + tmpEye.x - gx) * scale;
|
|
243
|
+
const y = gy + (nodes[o + 1] + tmpEye.y - gy) * scale;
|
|
244
|
+
const z = (nodes[o + 2] + tmpEye.z) * scale;
|
|
245
|
+
return out.set(x, z, -y);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// The simple figures of some players, grouped by kit so each group shares one shirt texture.
|
|
249
|
+
class Figures {
|
|
250
|
+
constructor(parent, header, styles, members) {
|
|
251
|
+
this.group = new THREE.Group();
|
|
252
|
+
parent.add(this.group);
|
|
253
|
+
this.parts = figureParts();
|
|
254
|
+
this.kits = [];
|
|
255
|
+
const byKit = new Map();
|
|
256
|
+
for (const k of members) {
|
|
257
|
+
const p = header.players[k];
|
|
258
|
+
const key = `${p.team}:${p.goalkeeper ? 1 : 0}`;
|
|
259
|
+
if (!byKit.has(key)) {
|
|
260
|
+
byKit.set(key, []);
|
|
261
|
+
}
|
|
262
|
+
byKit.get(key).push(k);
|
|
263
|
+
}
|
|
264
|
+
const plain = new THREE.MeshStandardMaterial({ roughness: 0.75, metalness: 0 });
|
|
265
|
+
this.disposables = [plain];
|
|
266
|
+
for (const [key, list] of byKit) {
|
|
267
|
+
const [team, gk] = key.split(':').map(Number);
|
|
268
|
+
const style = styles[team] || styles[0];
|
|
269
|
+
const shirt = new THREE.MeshStandardMaterial({ map: shirtTexture(style, !!gk), roughness: 0.7 });
|
|
270
|
+
const number = numberMaterial(numberColor(style, !!gk));
|
|
271
|
+
this.disposables.push(shirt, shirt.map, number);
|
|
272
|
+
const looks = list.map((k) => playerLook(style, header.players[k], k));
|
|
273
|
+
const meshes = this.parts.map((part) => {
|
|
274
|
+
let geometry = part.geometry;
|
|
275
|
+
let material = plain;
|
|
276
|
+
if (part.textured) {
|
|
277
|
+
material = shirt;
|
|
278
|
+
} else if (part.numbered) {
|
|
279
|
+
material = number;
|
|
280
|
+
geometry = part.geometry.clone();
|
|
281
|
+
const numbers = new Float32Array(list.length);
|
|
282
|
+
looks.forEach((l, i) => {
|
|
283
|
+
numbers[i] = Math.min(99, Math.max(0, Math.round(l.number) || 0));
|
|
284
|
+
});
|
|
285
|
+
geometry.setAttribute('aNumber', new THREE.InstancedBufferAttribute(numbers, 1));
|
|
286
|
+
this.disposables.push(geometry);
|
|
287
|
+
}
|
|
288
|
+
const mesh = new THREE.InstancedMesh(geometry, material, list.length);
|
|
289
|
+
mesh.castShadow = !part.numbered;
|
|
290
|
+
mesh.receiveShadow = true;
|
|
291
|
+
mesh.frustumCulled = false;
|
|
292
|
+
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
|
|
293
|
+
if (!part.textured && !part.numbered) {
|
|
294
|
+
looks.forEach((l, i) => mesh.setColorAt(i, l[part.slot]));
|
|
295
|
+
mesh.instanceColor.needsUpdate = true;
|
|
296
|
+
}
|
|
297
|
+
this.group.add(mesh);
|
|
298
|
+
return mesh;
|
|
299
|
+
});
|
|
300
|
+
this.kits.push({ list, looks, meshes });
|
|
301
|
+
}
|
|
302
|
+
for (const p of this.parts) {
|
|
303
|
+
this.disposables.push(p.geometry);
|
|
304
|
+
}
|
|
305
|
+
this.node = new THREE.Matrix4();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
update(nodes, visible, hidden) {
|
|
309
|
+
for (const kit of this.kits) {
|
|
310
|
+
kit.list.forEach((k, i) => {
|
|
311
|
+
const base = k * PLAYER_FLOATS;
|
|
312
|
+
const look = kit.looks[i];
|
|
313
|
+
const mode = hidden ? hidden[k] : 0;
|
|
314
|
+
for (let j = 0; j < this.parts.length; j++) {
|
|
315
|
+
const part = this.parts[j];
|
|
316
|
+
if (!visible[k] || mode === HIDE_ALL || (mode === HIDE_HEAD && part.node === 3)
|
|
317
|
+
|| (part.hair && !part.hair.includes(look.hairStyle))) {
|
|
318
|
+
kit.meshes[j].setMatrixAt(i, ZERO_SCALE);
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
nodeMatrix(nodes, base + part.node * 7, this.node);
|
|
322
|
+
tmpMat.multiplyMatrices(this.node, part.local);
|
|
323
|
+
kit.meshes[j].setMatrixAt(i, tmpMat);
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
for (const mesh of kit.meshes) {
|
|
327
|
+
mesh.instanceMatrix.needsUpdate = true;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
eye(nodes, k, out) {
|
|
333
|
+
return neckEye(nodes, k * PLAYER_FLOATS, 1, out);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
helmet(nodes, k, out) {
|
|
337
|
+
neckEye(nodes, k * PLAYER_FLOATS, 1, out, GRF_HEAD.local);
|
|
338
|
+
return GRF_HEAD.radius + HELMET_MARGIN;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
headBone() {
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
size() {
|
|
346
|
+
return { radius: 0.32, height: 1.85 };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
dispose() {
|
|
350
|
+
this.group.removeFromParent();
|
|
351
|
+
for (const d of this.disposables) {
|
|
352
|
+
d.dispose();
|
|
353
|
+
}
|
|
354
|
+
for (const kit of this.kits) {
|
|
355
|
+
for (const m of kit.meshes) {
|
|
356
|
+
m.dispose();
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const PATTERN_INDEX = { plain: 0, stripes: 1, hoops: 2, halves: 3, sash: 4, checkers: 5 };
|
|
363
|
+
|
|
364
|
+
// Tints a model material, the whole of it one kit slot, with the team kit. Shirt patterns are
|
|
365
|
+
// drawn from the position in the bind pose, so they need no particular UV layout.
|
|
366
|
+
function applyKit(material, slot, kit, unit) {
|
|
367
|
+
const uniforms = {
|
|
368
|
+
kitPrimary: { value: threeColor(kit.primary) },
|
|
369
|
+
kitSecondary: { value: threeColor(kit.secondary) },
|
|
370
|
+
kitShorts: { value: threeColor(kit.shorts) },
|
|
371
|
+
kitSocks: { value: threeColor(kit.socks) },
|
|
372
|
+
kitPattern: { value: PATTERN_INDEX[kit.pattern] || 0 },
|
|
373
|
+
kitSlot: { value: { shirt: 1, shorts: 2, socks: 3 }[slot] || 0 },
|
|
374
|
+
kitUnit: { value: unit },
|
|
375
|
+
};
|
|
376
|
+
material.onBeforeCompile = (shader) => {
|
|
377
|
+
Object.assign(shader.uniforms, uniforms);
|
|
378
|
+
shader.vertexShader = shader.vertexShader
|
|
379
|
+
.replace('#include <common>', '#include <common>\nvarying vec3 vKitPos;\nuniform float kitUnit;')
|
|
380
|
+
.replace('#include <begin_vertex>', '#include <begin_vertex>\nvKitPos = position * kitUnit;');
|
|
381
|
+
shader.fragmentShader = shader.fragmentShader
|
|
382
|
+
.replace('#include <common>', `#include <common>
|
|
383
|
+
varying vec3 vKitPos;
|
|
384
|
+
uniform vec3 kitPrimary;
|
|
385
|
+
uniform vec3 kitSecondary;
|
|
386
|
+
uniform vec3 kitShorts;
|
|
387
|
+
uniform vec3 kitSocks;
|
|
388
|
+
uniform float kitPattern;
|
|
389
|
+
uniform float kitSlot;
|
|
390
|
+
vec3 kitShirtColor(vec3 p) {
|
|
391
|
+
float m = 0.0;
|
|
392
|
+
if (kitPattern == 1.0) m = step(0.5, fract(p.x * 9.0));
|
|
393
|
+
else if (kitPattern == 2.0) m = step(0.5, fract(p.y * 7.0));
|
|
394
|
+
else if (kitPattern == 3.0) m = step(p.x, 0.0);
|
|
395
|
+
else if (kitPattern == 4.0) m = step(abs(p.x + p.y - 1.3), 0.07);
|
|
396
|
+
else if (kitPattern == 5.0) m = mod(floor(p.x * 9.0) + floor(p.y * 9.0), 2.0);
|
|
397
|
+
return mix(kitPrimary, kitSecondary, m);
|
|
398
|
+
}`)
|
|
399
|
+
.replace('#include <map_fragment>', `#include <map_fragment>
|
|
400
|
+
{
|
|
401
|
+
float lum = clamp(dot(diffuseColor.rgb, vec3(0.299, 0.587, 0.114)) * 1.25, 0.0, 1.0);
|
|
402
|
+
if (kitSlot == 1.0) diffuseColor.rgb = kitShirtColor(vKitPos) * lum;
|
|
403
|
+
else if (kitSlot == 2.0) diffuseColor.rgb = kitShorts * lum;
|
|
404
|
+
else if (kitSlot == 3.0) diffuseColor.rgb = kitSocks * lum;
|
|
405
|
+
}`);
|
|
406
|
+
};
|
|
407
|
+
material.customProgramCacheKey = () => 'cuju-kit-slot';
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function kitSlotOf(name, named, onlyKit) {
|
|
411
|
+
const lower = (name || '').toLowerCase();
|
|
412
|
+
if (named) {
|
|
413
|
+
for (const slot of ['shirt', 'shorts', 'socks', 'skin', 'hair', 'boots']) {
|
|
414
|
+
if ([].concat(named[slot] || []).includes(name)) {
|
|
415
|
+
return slot;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (/shirt|jersey/.test(lower)) return 'shirt';
|
|
420
|
+
if (/short/.test(lower)) return 'shorts';
|
|
421
|
+
if (/sock/.test(lower)) return 'socks';
|
|
422
|
+
if (onlyKit) return null;
|
|
423
|
+
if (/skin/.test(lower)) return 'skin';
|
|
424
|
+
if (/hair/.test(lower)) return 'hair';
|
|
425
|
+
if (/boot|shoe/.test(lower)) return 'boots';
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Common rig names, used to fill a joint mapping. Mixamo first, then a few other conventions.
|
|
430
|
+
const RIG_NAMES = {
|
|
431
|
+
player: ['root', 'Root', 'Armature'],
|
|
432
|
+
body: ['Hips', 'hips', 'pelvis', 'Pelvis'],
|
|
433
|
+
middle: ['Spine2', 'Spine1', 'spine_02', 'Chest', 'chest', 'Spine', 'spine'],
|
|
434
|
+
neck: ['Neck', 'neck', 'neck_01', 'Head', 'head'],
|
|
435
|
+
left_shoulder: ['LeftArm', 'upperarm_l', 'UpperArm.L', 'upper_arm.L', 'LeftUpperArm'],
|
|
436
|
+
left_elbow: ['LeftForeArm', 'lowerarm_l', 'LowerArm.L', 'forearm.L', 'LeftLowerArm'],
|
|
437
|
+
right_shoulder: ['RightArm', 'upperarm_r', 'UpperArm.R', 'upper_arm.R', 'RightUpperArm'],
|
|
438
|
+
right_elbow: ['RightForeArm', 'lowerarm_r', 'LowerArm.R', 'forearm.R', 'RightLowerArm'],
|
|
439
|
+
left_thigh: ['LeftUpLeg', 'thigh_l', 'UpperLeg.L', 'thigh.L', 'LeftUpperLeg'],
|
|
440
|
+
left_knee: ['LeftLeg', 'calf_l', 'LowerLeg.L', 'shin.L', 'LeftLowerLeg'],
|
|
441
|
+
left_ankle: ['LeftFoot', 'foot_l', 'Foot.L', 'foot.L'],
|
|
442
|
+
right_thigh: ['RightUpLeg', 'thigh_r', 'UpperLeg.R', 'thigh.R', 'RightUpperLeg'],
|
|
443
|
+
right_knee: ['RightLeg', 'calf_r', 'LowerLeg.R', 'shin.R', 'RightLowerLeg'],
|
|
444
|
+
right_ankle: ['RightFoot', 'foot_r', 'Foot.R', 'foot.R'],
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
function stripPrefix(name) {
|
|
448
|
+
return name.replace(/^mixamorig\d*[:_]?/i, '').replace(/^.*[:|]/, '');
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Lists the skinned bones of a model, or throws when it has no skin.
|
|
452
|
+
export function skinnedBones(scene) {
|
|
453
|
+
const bones = [];
|
|
454
|
+
const seen = new Set();
|
|
455
|
+
scene.traverse((obj) => {
|
|
456
|
+
if (obj.isSkinnedMesh && obj.skeleton) {
|
|
457
|
+
for (const b of obj.skeleton.bones) {
|
|
458
|
+
if (!seen.has(b)) {
|
|
459
|
+
seen.add(b);
|
|
460
|
+
bones.push(b);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
if (bones.length === 0) {
|
|
466
|
+
throw new Error(t('players.model.noSkin'));
|
|
467
|
+
}
|
|
468
|
+
return bones;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// The skinned bone names as written in the file, GLTFLoader renames nodes for its own use.
|
|
472
|
+
export function fileBoneNames(gltf) {
|
|
473
|
+
const json = gltf.parser && gltf.parser.json;
|
|
474
|
+
return skinnedBones(gltf.scene).map((b) => {
|
|
475
|
+
const assoc = gltf.parser && gltf.parser.associations.get(b);
|
|
476
|
+
const index = assoc ? assoc.nodes : undefined;
|
|
477
|
+
return json && index !== undefined && json.nodes[index] && json.nodes[index].name ? json.nodes[index].name : b.name;
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// Fills a joint mapping by exact names first, then common rig names.
|
|
482
|
+
export function guessJoints(boneNames) {
|
|
483
|
+
const joints = {};
|
|
484
|
+
const exact = new Set(boneNames);
|
|
485
|
+
const stripped = new Map(boneNames.map((n) => [stripPrefix(n).toLowerCase(), n]));
|
|
486
|
+
for (const node of NODE_NAMES) {
|
|
487
|
+
if (exact.has(node)) {
|
|
488
|
+
joints[node] = node;
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
for (const candidate of RIG_NAMES[node]) {
|
|
492
|
+
const hit = stripped.get(candidate.toLowerCase());
|
|
493
|
+
if (hit) {
|
|
494
|
+
joints[node] = hit;
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return joints;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// A skinned model retargeted onto the 14 nodes. A mapped bone's world rotation is the saved
|
|
503
|
+
// rotation times the inverse of GRF's rest rotation times the bone's own bind rotation, so the
|
|
504
|
+
// model's bind pose need not match GRF's. The pelvis follows the saved body position with its
|
|
505
|
+
// height scaled to the model, every other bone keeps its own length from its parent, and
|
|
506
|
+
// unmapped bones keep their bind transform relative to their parent. The rig only reads its
|
|
507
|
+
// scene, which stays with whoever loaded it.
|
|
508
|
+
export class Rig {
|
|
509
|
+
constructor(scene, joints, options = {}) {
|
|
510
|
+
this.scene = scene;
|
|
511
|
+
this.options = options;
|
|
512
|
+
scene.updateMatrixWorld(true);
|
|
513
|
+
const bones = skinnedBones(scene);
|
|
514
|
+
const inverse = new Map();
|
|
515
|
+
scene.traverse((obj) => {
|
|
516
|
+
if (obj.isSkinnedMesh && obj.skeleton) {
|
|
517
|
+
obj.skeleton.bones.forEach((b, i) => inverse.set(b, obj.skeleton.boneInverses[i]));
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
const bind = new Map(bones.map((b) => [b, inverse.has(b) ? inverse.get(b).clone().invert() : b.matrixWorld.clone()]));
|
|
521
|
+
const nodeOf = new Map();
|
|
522
|
+
for (const [node, boneName] of Object.entries(joints || {})) {
|
|
523
|
+
const i = NODE_NAMES.indexOf(node);
|
|
524
|
+
// GLTFLoader drops dots and similar from node names, mappings use the file's names.
|
|
525
|
+
const clean = THREE.PropertyBinding.sanitizeNodeName(String(boneName));
|
|
526
|
+
const bone = bones.find((b) => b.name === boneName) || bones.find((b) => b.name === clean);
|
|
527
|
+
if (i >= 0 && bone) {
|
|
528
|
+
nodeOf.set(bone, i);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
const bodyBone = bones.find((b) => nodeOf.get(b) === 1);
|
|
532
|
+
if (!bodyBone) {
|
|
533
|
+
throw new Error(t('players.model.noBody'));
|
|
534
|
+
}
|
|
535
|
+
// Order bones parents first, and find the skeleton roots.
|
|
536
|
+
const depth = (b) => {
|
|
537
|
+
let d = 0;
|
|
538
|
+
for (let p = b.parent; p; p = p.parent) {
|
|
539
|
+
d++;
|
|
540
|
+
}
|
|
541
|
+
return d;
|
|
542
|
+
};
|
|
543
|
+
const ordered = bones.slice().sort((a, b) => depth(a) - depth(b));
|
|
544
|
+
const index = new Map(ordered.map((b, i) => [b, i]));
|
|
545
|
+
const parentBone = (b) => {
|
|
546
|
+
for (let p = b.parent; p; p = p.parent) {
|
|
547
|
+
if (index.has(p)) {
|
|
548
|
+
return p;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
return null;
|
|
552
|
+
};
|
|
553
|
+
// A missing player joint means the skeleton root plays the player node.
|
|
554
|
+
if (![...nodeOf.values()].includes(0)) {
|
|
555
|
+
const root = ordered[0];
|
|
556
|
+
if (!nodeOf.has(root)) {
|
|
557
|
+
nodeOf.set(root, 0);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
// A model kept in the engine frame, z up, is turned into three's frame first, glTF's
|
|
561
|
+
// usual y up models need no turn.
|
|
562
|
+
const frame = options.engineFrame ? ENGINE_TO_THREE.clone() : new THREE.Matrix4();
|
|
563
|
+
const box = new THREE.Box3().setFromObject(scene).applyMatrix4(frame);
|
|
564
|
+
const bodyBind = new THREE.Vector3().setFromMatrixPosition(bind.get(bodyBone)).applyMatrix4(frame);
|
|
565
|
+
const floor = Number.isFinite(box.min.y) ? box.min.y : 0;
|
|
566
|
+
let pelvis = bodyBind.y - floor;
|
|
567
|
+
// Units, a model whose pelvis stands above ten units is taken to be in centimeters.
|
|
568
|
+
let unit = Number(options.unit) || (pelvis > 10 ? 0.01 : 1);
|
|
569
|
+
// A model much taller than GRF's player is scaled down to MAX_MODEL_HEIGHT, so big
|
|
570
|
+
// characters keep their look without towering over the others.
|
|
571
|
+
const tall = Number.isFinite(box.max.y) ? (box.max.y - box.min.y) * unit : 0;
|
|
572
|
+
this.fit = tall > MAX_MODEL_HEIGHT ? MAX_MODEL_HEIGHT / tall : 1;
|
|
573
|
+
this.standing = tall > 0.3 ? tall * this.fit : GRF_HEIGHT;
|
|
574
|
+
unit *= this.fit;
|
|
575
|
+
pelvis *= unit;
|
|
576
|
+
if (!(pelvis > 0.2)) {
|
|
577
|
+
pelvis = REST_BODY_HEIGHT;
|
|
578
|
+
}
|
|
579
|
+
this.heightRatio = pelvis / REST_BODY_HEIGHT;
|
|
580
|
+
this.unit = unit;
|
|
581
|
+
const S = new THREE.Matrix4().makeScale(unit, unit, unit).premultiply(frame);
|
|
582
|
+
this.bones = ordered.map((b) => {
|
|
583
|
+
const world = S.clone().multiply(bind.get(b));
|
|
584
|
+
const parent = parentBone(b);
|
|
585
|
+
const parentWorld = parent ? S.clone().multiply(bind.get(parent)) : null;
|
|
586
|
+
const local = parentWorld ? parentWorld.clone().invert().multiply(world) : world.clone();
|
|
587
|
+
const p = new THREE.Vector3();
|
|
588
|
+
const q = new THREE.Quaternion();
|
|
589
|
+
const s = new THREE.Vector3();
|
|
590
|
+
world.decompose(p, q, s);
|
|
591
|
+
const node = nodeOf.has(b) ? nodeOf.get(b) : -1;
|
|
592
|
+
return {
|
|
593
|
+
name: b.name,
|
|
594
|
+
node,
|
|
595
|
+
parent: parent ? index.get(parent) : -1,
|
|
596
|
+
local,
|
|
597
|
+
localOffset: new THREE.Vector3().setFromMatrixPosition(local),
|
|
598
|
+
scale: s,
|
|
599
|
+
// The inverse of G times C, applied after the saved rotation.
|
|
600
|
+
correction: node >= 0 ? REST_THREE_QUAT[node].clone().invert().multiply(q) : null,
|
|
601
|
+
position: p,
|
|
602
|
+
rotation: q,
|
|
603
|
+
};
|
|
604
|
+
});
|
|
605
|
+
this.alignLimbs();
|
|
606
|
+
// Animation clips the file carries, which a player can play in place of the nodes.
|
|
607
|
+
this.clips = Array.isArray(options.animations) ? options.animations.filter((c) => c && c.name) : [];
|
|
608
|
+
this.placement = S;
|
|
609
|
+
this.findHead(Number.isFinite(box.max.y) ? box.max.y * unit : NaN);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// The head bone, the bone mapped to the neck or a descendant named head, the bones that a
|
|
613
|
+
// camera inside the head hides, and the eyes in the head bone's own frame.
|
|
614
|
+
findHead(top) {
|
|
615
|
+
const neck = this.bones.findIndex((b) => b.node === 3);
|
|
616
|
+
const below = (j, root) => {
|
|
617
|
+
for (let p = this.bones[j].parent; p >= 0; p = this.bones[p].parent) {
|
|
618
|
+
if (p === root) {
|
|
619
|
+
return true;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
return false;
|
|
623
|
+
};
|
|
624
|
+
let head = neck;
|
|
625
|
+
if (neck >= 0 && !/head/i.test(this.bones[neck].name)) {
|
|
626
|
+
const named = this.bones.findIndex((b, j) => /head/i.test(b.name) && !/end|top|nub/i.test(b.name) && below(j, neck));
|
|
627
|
+
if (named >= 0) {
|
|
628
|
+
head = named;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
this.headIndex = head;
|
|
632
|
+
this.headMask = this.bones.map((b, j) => head >= 0 && (j === head || below(j, head)));
|
|
633
|
+
this.eyeLocal = null;
|
|
634
|
+
if (head >= 0) {
|
|
635
|
+
const hb = this.bones[head];
|
|
636
|
+
const rise = Number.isFinite(top) && top > hb.position.y + 0.05 ? top - hb.position.y : 0.3 * this.heightRatio;
|
|
637
|
+
const eye = new THREE.Vector3(hb.position.x, hb.position.y + 0.45 * rise, hb.position.z + 0.3 * rise);
|
|
638
|
+
this.eyeLocal = eye.applyMatrix4(new THREE.Matrix4().compose(hb.position, hb.rotation, hb.scale).invert());
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// The standing height and the horizontal radius of the model as drawn in GRF's rest pose,
|
|
643
|
+
// from its skinned vertices, measured once.
|
|
644
|
+
size() {
|
|
645
|
+
if (this.measured) {
|
|
646
|
+
return this.measured;
|
|
647
|
+
}
|
|
648
|
+
let radius = 0;
|
|
649
|
+
let top = 0;
|
|
650
|
+
try {
|
|
651
|
+
const inst = this.instantiate();
|
|
652
|
+
this.pose(inst, restPoseNodes(0), 0);
|
|
653
|
+
const v = new THREE.Vector4();
|
|
654
|
+
const w = new THREE.Vector4();
|
|
655
|
+
const m = new THREE.Matrix4();
|
|
656
|
+
inst.root.traverse((obj) => {
|
|
657
|
+
if (!obj.isSkinnedMesh) {
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
const g = obj.geometry;
|
|
661
|
+
const pos = g.attributes.position;
|
|
662
|
+
const si = g.attributes.skinIndex;
|
|
663
|
+
const sw = g.attributes.skinWeight;
|
|
664
|
+
for (let i = 0; i < pos.count; i++) {
|
|
665
|
+
v.set(pos.getX(i), pos.getY(i), pos.getZ(i), 1).applyMatrix4(obj.bindMatrix);
|
|
666
|
+
w.set(0, 0, 0, 0);
|
|
667
|
+
for (let c = 0; c < 4; c++) {
|
|
668
|
+
const weight = sw.getComponent(i, c);
|
|
669
|
+
if (weight) {
|
|
670
|
+
const b = si.getComponent(i, c);
|
|
671
|
+
m.multiplyMatrices(obj.skeleton.bones[b].matrixWorld, obj.skeleton.boneInverses[b]);
|
|
672
|
+
w.addScaledVector(v.clone().applyMatrix4(m), weight);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
radius = Math.max(radius, Math.hypot(w.x, w.z));
|
|
676
|
+
top = Math.max(top, w.y);
|
|
677
|
+
}
|
|
678
|
+
});
|
|
679
|
+
this.release(inst);
|
|
680
|
+
} catch (err) {
|
|
681
|
+
console.warn('model size not measured', err);
|
|
682
|
+
}
|
|
683
|
+
this.measured = {
|
|
684
|
+
radius: radius > 0.05 ? radius : 0.35,
|
|
685
|
+
height: top > 0.3 ? top : this.standing,
|
|
686
|
+
};
|
|
687
|
+
return this.measured;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// The sphere round the head, its middle in the head bone's own frame and its radius in
|
|
691
|
+
// meters, from the skinned vertices that follow the head bone or a bone below it and the
|
|
692
|
+
// rigid meshes hung below them, which takes in the hair and anything worn on the head.
|
|
693
|
+
// Measured once in GRF's rest pose.
|
|
694
|
+
headSphere() {
|
|
695
|
+
if (this.headMeasured !== undefined) {
|
|
696
|
+
return this.headMeasured;
|
|
697
|
+
}
|
|
698
|
+
this.headMeasured = null;
|
|
699
|
+
if (this.headIndex < 0) {
|
|
700
|
+
return null;
|
|
701
|
+
}
|
|
702
|
+
try {
|
|
703
|
+
const inst = this.instantiate();
|
|
704
|
+
this.pose(inst, restPoseNodes(0), 0);
|
|
705
|
+
const onHead = new Set(inst.bones.filter((b, j) => b && this.headMask[j]));
|
|
706
|
+
const points = [];
|
|
707
|
+
const box = new THREE.Box3();
|
|
708
|
+
const m = new THREE.Matrix4();
|
|
709
|
+
inst.root.traverse((obj) => {
|
|
710
|
+
if (obj.isMesh && !obj.isSkinnedMesh) {
|
|
711
|
+
// A rigid mesh worn on the head, such as a hat, hangs below a head bone.
|
|
712
|
+
const rel = new THREE.Matrix4();
|
|
713
|
+
let node = obj;
|
|
714
|
+
while (node && !onHead.has(node)) {
|
|
715
|
+
node.updateMatrix();
|
|
716
|
+
rel.premultiply(node.matrix);
|
|
717
|
+
node = node.parent;
|
|
718
|
+
}
|
|
719
|
+
if (node) {
|
|
720
|
+
m.multiplyMatrices(node.matrixWorld, rel);
|
|
721
|
+
const pos = obj.geometry.attributes.position;
|
|
722
|
+
for (let i = 0; i < pos.count; i++) {
|
|
723
|
+
const p = new THREE.Vector3(pos.getX(i), pos.getY(i), pos.getZ(i)).applyMatrix4(m);
|
|
724
|
+
points.push(p);
|
|
725
|
+
box.expandByPoint(p);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
if (!obj.isSkinnedMesh) {
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
const g = obj.geometry;
|
|
734
|
+
const pos = g.attributes.position;
|
|
735
|
+
const si = g.attributes.skinIndex;
|
|
736
|
+
const sw = g.attributes.skinWeight;
|
|
737
|
+
for (let i = 0; i < pos.count; i++) {
|
|
738
|
+
let best = 0;
|
|
739
|
+
for (let c = 1; c < 4; c++) {
|
|
740
|
+
if (sw.getComponent(i, c) > sw.getComponent(i, best)) {
|
|
741
|
+
best = c;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
const bone = obj.skeleton.bones[si.getComponent(i, best)];
|
|
745
|
+
if (!onHead.has(bone)) {
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
748
|
+
const b = si.getComponent(i, best);
|
|
749
|
+
m.multiplyMatrices(bone.matrixWorld, obj.skeleton.boneInverses[b]);
|
|
750
|
+
const p = new THREE.Vector3(pos.getX(i), pos.getY(i), pos.getZ(i)).applyMatrix4(obj.bindMatrix).applyMatrix4(m);
|
|
751
|
+
points.push(p);
|
|
752
|
+
box.expandByPoint(p);
|
|
753
|
+
}
|
|
754
|
+
});
|
|
755
|
+
if (points.length) {
|
|
756
|
+
const center = box.getCenter(new THREE.Vector3());
|
|
757
|
+
let radius = 0;
|
|
758
|
+
for (const p of points) {
|
|
759
|
+
radius = Math.max(radius, p.distanceTo(center));
|
|
760
|
+
}
|
|
761
|
+
const local = center.clone().applyMatrix4(inst.world[this.headIndex].clone().invert());
|
|
762
|
+
this.headMeasured = { local, radius };
|
|
763
|
+
}
|
|
764
|
+
this.release(inst);
|
|
765
|
+
} catch (err) {
|
|
766
|
+
console.warn('head not measured', err);
|
|
767
|
+
}
|
|
768
|
+
return this.headMeasured;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// The names of the clips as celebrations name them.
|
|
772
|
+
clipNames() {
|
|
773
|
+
return this.clips.map((c) => `clip:${c.name}`);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
clip(name) {
|
|
777
|
+
const bare = String(name || '').replace(/^clip:/, '');
|
|
778
|
+
return this.clips.find((c) => c.name === bare) || null;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// The eyes of a posed instance in three's frame.
|
|
782
|
+
eye(instance, out) {
|
|
783
|
+
if (this.headIndex < 0 || !this.eyeLocal) {
|
|
784
|
+
return null;
|
|
785
|
+
}
|
|
786
|
+
return out.copy(this.eyeLocal).applyMatrix4(instance.world[this.headIndex]);
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
// Limbs of most rigs rest in a T or A pose while GRF's rest pose hangs the arms down and
|
|
790
|
+
// stands the legs straight. Each mapped limb bone gets the turn that points its bind
|
|
791
|
+
// direction, towards its child bone, along GRF's rest direction of the same limb, so the
|
|
792
|
+
// saved rotations act on a model standing as GRF's player does. The trunk, neck and
|
|
793
|
+
// feet keep their bind turn, which keeps the facing.
|
|
794
|
+
alignLimbs() {
|
|
795
|
+
const restPos = REST_ENGINE.map((w) => new THREE.Vector3().setFromMatrixPosition(w).applyMatrix4(ENGINE_TO_THREE));
|
|
796
|
+
const down = new THREE.Vector3(0, -1, 0);
|
|
797
|
+
const childNode = { 4: 5, 5: null, 6: 7, 7: null, 8: 9, 9: 10, 11: 12, 12: 13 };
|
|
798
|
+
const byNode = new Map(this.bones.map((b, i) => [b.node, i]));
|
|
799
|
+
this.bones.forEach((info, i) => {
|
|
800
|
+
if (!(info.node in childNode)) {
|
|
801
|
+
return;
|
|
802
|
+
}
|
|
803
|
+
const c = childNode[info.node];
|
|
804
|
+
const want = c == null ? down.clone() : restPos[c].clone().sub(restPos[info.node]).normalize();
|
|
805
|
+
let child = c != null && byNode.has(c) ? this.bones[byNode.get(c)] : null;
|
|
806
|
+
if (!child) {
|
|
807
|
+
child = this.bones.find((b) => b.parent === i) || null;
|
|
808
|
+
}
|
|
809
|
+
if (!child) {
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
const have = child.position.clone().sub(info.position);
|
|
813
|
+
if (have.lengthSq() < 1e-10) {
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
const turn = new THREE.Quaternion().setFromUnitVectors(have.normalize(), want);
|
|
817
|
+
info.correction = REST_THREE_QUAT[info.node].clone().invert().multiply(turn).multiply(info.rotation);
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
instantiate() {
|
|
822
|
+
const root = SkeletonUtils.clone(this.scene);
|
|
823
|
+
const byName = new Map();
|
|
824
|
+
root.traverse((obj) => {
|
|
825
|
+
if (obj.isBone && !byName.has(obj.name)) {
|
|
826
|
+
byName.set(obj.name, obj);
|
|
827
|
+
}
|
|
828
|
+
if (obj.isMesh) {
|
|
829
|
+
obj.castShadow = true;
|
|
830
|
+
obj.receiveShadow = true;
|
|
831
|
+
obj.frustumCulled = false;
|
|
832
|
+
}
|
|
833
|
+
});
|
|
834
|
+
const bones = this.bones.map((info) => {
|
|
835
|
+
const bone = byName.get(info.name);
|
|
836
|
+
if (bone) {
|
|
837
|
+
bone.matrixAutoUpdate = false;
|
|
838
|
+
bone.matrixWorldAutoUpdate = false;
|
|
839
|
+
}
|
|
840
|
+
return bone || null;
|
|
841
|
+
});
|
|
842
|
+
// Geometries and textures stay shared with the rig's scene, only the materials dress
|
|
843
|
+
// makes and the cloned skeletons belong to the instance.
|
|
844
|
+
return { root, bones, world: this.bones.map(() => new THREE.Matrix4()), materials: [] };
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
release(instance) {
|
|
848
|
+
for (const m of instance.materials) {
|
|
849
|
+
m.dispose();
|
|
850
|
+
}
|
|
851
|
+
disposeSkeletons(instance.root);
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
// Poses an instance from one player's nodes. With hideHead the head bone and the bones below
|
|
855
|
+
// it shrink to a point, which hides the head, its hair and anything worn on it.
|
|
856
|
+
pose(instance, nodes, base, hideHead = false) {
|
|
857
|
+
const world = instance.world;
|
|
858
|
+
for (let j = 0; j < this.bones.length; j++) {
|
|
859
|
+
const info = this.bones[j];
|
|
860
|
+
const out = world[j];
|
|
861
|
+
const parent = info.parent >= 0 ? world[info.parent] : null;
|
|
862
|
+
if (info.node < 0) {
|
|
863
|
+
if (parent) {
|
|
864
|
+
out.multiplyMatrices(parent, info.local);
|
|
865
|
+
} else {
|
|
866
|
+
out.copy(info.local);
|
|
867
|
+
}
|
|
868
|
+
} else {
|
|
869
|
+
const o = base + info.node * 7;
|
|
870
|
+
nodeQuaternion(nodes, o, tmpQuat).multiply(info.correction);
|
|
871
|
+
if (info.node === 1) {
|
|
872
|
+
tmpPos.set(nodes[o], nodes[o + 2] * this.heightRatio, -nodes[o + 1]);
|
|
873
|
+
} else if (info.node === 0 || !parent) {
|
|
874
|
+
tmpPos.set(nodes[o], nodes[o + 2], -nodes[o + 1]);
|
|
875
|
+
} else {
|
|
876
|
+
tmpPos.copy(info.localOffset).applyMatrix4(parent);
|
|
877
|
+
}
|
|
878
|
+
out.compose(tmpPos, tmpQuat, info.scale);
|
|
879
|
+
}
|
|
880
|
+
const bone = instance.bones[j];
|
|
881
|
+
if (bone) {
|
|
882
|
+
if (hideHead && this.headMask[j]) {
|
|
883
|
+
const h = world[this.headIndex].elements;
|
|
884
|
+
bone.matrixWorld.copy(ZERO_SCALE).setPosition(h[12], h[13], h[14]);
|
|
885
|
+
} else {
|
|
886
|
+
bone.matrixWorld.copy(out);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
// Poses an instance from a clip of the model at time t seconds, standing on the ground point
|
|
893
|
+
// and heading of node player of the pose, blended over the pose of the nodes by weight. A
|
|
894
|
+
// weight of one draws the clip alone. Below one, each bone's turn and offset from its parent
|
|
895
|
+
// blend between the two poses, so limbs keep their lengths while the clip fades in or out.
|
|
896
|
+
// The bones stay driven by hand, so a later pose from the nodes alone replaces it whole.
|
|
897
|
+
poseClip(instance, clip, t, weight, nodes, base, hideHead = false) {
|
|
898
|
+
if (!instance.mixer) {
|
|
899
|
+
instance.mixer = new THREE.AnimationMixer(instance.root);
|
|
900
|
+
}
|
|
901
|
+
if (instance.clip !== clip) {
|
|
902
|
+
instance.mixer.stopAllAction();
|
|
903
|
+
instance.action = instance.mixer.clipAction(clip);
|
|
904
|
+
instance.action.play();
|
|
905
|
+
instance.clip = clip;
|
|
906
|
+
}
|
|
907
|
+
const d = clip.duration || 0;
|
|
908
|
+
instance.action.time = d > 0 ? Math.max(0, Math.min(d - 1e-4, t)) : 0;
|
|
909
|
+
instance.mixer.update(0);
|
|
910
|
+
nodeQuaternion(nodes, base, tmpQuat);
|
|
911
|
+
tmpPos.set(0, 0, 1).applyQuaternion(tmpQuat);
|
|
912
|
+
const heading = Math.atan2(tmpPos.x, tmpPos.z);
|
|
913
|
+
const root = instance.root;
|
|
914
|
+
root.matrixAutoUpdate = false;
|
|
915
|
+
// The player node's height lifts the clip with it, onto a podium step or a stage.
|
|
916
|
+
root.matrix.makeRotationY(heading).setPosition(nodes[base], nodes[base + 2] || 0, -nodes[base + 1]).multiply(this.placement);
|
|
917
|
+
for (const b of instance.bones) {
|
|
918
|
+
if (b) {
|
|
919
|
+
b.matrixAutoUpdate = true;
|
|
920
|
+
b.matrixWorldAutoUpdate = true;
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
root.updateMatrixWorld(true);
|
|
924
|
+
for (const b of instance.bones) {
|
|
925
|
+
if (b) {
|
|
926
|
+
b.matrixAutoUpdate = false;
|
|
927
|
+
b.matrixWorldAutoUpdate = false;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
// Skinning reads the bones alone, so the root goes back to where the nodes keep it.
|
|
931
|
+
root.matrix.identity();
|
|
932
|
+
root.matrixAutoUpdate = true;
|
|
933
|
+
const world = instance.world;
|
|
934
|
+
if (weight >= 1) {
|
|
935
|
+
for (let j = 0; j < this.bones.length; j++) {
|
|
936
|
+
const bone = instance.bones[j];
|
|
937
|
+
if (bone) {
|
|
938
|
+
world[j].copy(bone.matrixWorld);
|
|
939
|
+
} else {
|
|
940
|
+
const p = this.bones[j].parent;
|
|
941
|
+
world[j].multiplyMatrices(p >= 0 ? world[p] : root.matrixWorld, this.bones[j].local);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
} else {
|
|
945
|
+
const clipWorld = instance.clipWorld || (instance.clipWorld = this.bones.map(() => new THREE.Matrix4()));
|
|
946
|
+
for (let j = 0; j < this.bones.length; j++) {
|
|
947
|
+
const bone = instance.bones[j];
|
|
948
|
+
const p = this.bones[j].parent;
|
|
949
|
+
if (bone) {
|
|
950
|
+
clipWorld[j].copy(bone.matrixWorld);
|
|
951
|
+
} else {
|
|
952
|
+
clipWorld[j].multiplyMatrices(p >= 0 ? clipWorld[p] : root.matrixWorld, this.bones[j].local);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
this.pose(instance, nodes, base, false);
|
|
956
|
+
const nodeWorld = instance.nodeWorld || (instance.nodeWorld = this.bones.map(() => new THREE.Matrix4()));
|
|
957
|
+
for (let j = 0; j < this.bones.length; j++) {
|
|
958
|
+
nodeWorld[j].copy(world[j]);
|
|
959
|
+
}
|
|
960
|
+
for (let j = 0; j < this.bones.length; j++) {
|
|
961
|
+
const p = this.bones[j].parent;
|
|
962
|
+
if (p >= 0) {
|
|
963
|
+
blendMat.copy(nodeWorld[p]).invert().multiply(nodeWorld[j]);
|
|
964
|
+
blendMat2.copy(clipWorld[p]).invert().multiply(clipWorld[j]);
|
|
965
|
+
} else {
|
|
966
|
+
blendMat.copy(nodeWorld[j]);
|
|
967
|
+
blendMat2.copy(clipWorld[j]);
|
|
968
|
+
}
|
|
969
|
+
blendMat.decompose(blendPa, blendQa, blendSa);
|
|
970
|
+
blendMat2.decompose(blendPb, blendQb, blendSb);
|
|
971
|
+
blendPa.lerp(blendPb, weight);
|
|
972
|
+
blendQa.slerp(blendQb, weight);
|
|
973
|
+
blendSa.lerp(blendSb, weight);
|
|
974
|
+
blendMat.compose(blendPa, blendQa, blendSa);
|
|
975
|
+
if (p >= 0) {
|
|
976
|
+
world[j].multiplyMatrices(world[p], blendMat);
|
|
977
|
+
} else {
|
|
978
|
+
world[j].copy(blendMat);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
for (let j = 0; j < this.bones.length; j++) {
|
|
983
|
+
const bone = instance.bones[j];
|
|
984
|
+
if (!bone) {
|
|
985
|
+
continue;
|
|
986
|
+
}
|
|
987
|
+
if (hideHead && this.headIndex >= 0 && this.headMask[j]) {
|
|
988
|
+
const h = world[this.headIndex].elements;
|
|
989
|
+
bone.matrixWorld.copy(ZERO_SCALE).setPosition(h[12], h[13], h[14]);
|
|
990
|
+
} else {
|
|
991
|
+
bone.matrixWorld.copy(world[j]);
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
// Drops the clip an instance last played, the nodes alone pose it from then on.
|
|
997
|
+
stopClip(instance) {
|
|
998
|
+
if (!instance.clip) {
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
1001
|
+
instance.mixer.stopAllAction();
|
|
1002
|
+
instance.clip = null;
|
|
1003
|
+
instance.action = null;
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
// Recolours an instance's materials with a kit, per the manifest or by material names.
|
|
1007
|
+
dress(instance, style, goalkeeper, look) {
|
|
1008
|
+
const kit = goalkeeper ? { ...style.goalkeeper, pattern: 'plain' } : style.kit;
|
|
1009
|
+
const named = this.options.materials || null;
|
|
1010
|
+
const onlyKit = !!this.options.onlyKit;
|
|
1011
|
+
const copies = new Map();
|
|
1012
|
+
instance.root.traverse((obj) => {
|
|
1013
|
+
if (!obj.isMesh) {
|
|
1014
|
+
return;
|
|
1015
|
+
}
|
|
1016
|
+
const swap = (mat) => {
|
|
1017
|
+
if (!copies.has(mat)) {
|
|
1018
|
+
let copy = mat;
|
|
1019
|
+
const slot = kitSlotOf(mat.name, named, onlyKit);
|
|
1020
|
+
if (slot === 'shirt' || slot === 'shorts' || slot === 'socks') {
|
|
1021
|
+
copy = mat.clone();
|
|
1022
|
+
applyKit(copy, slot, kit, this.unit);
|
|
1023
|
+
} else if (slot && look && look[slot]) {
|
|
1024
|
+
copy = mat.clone();
|
|
1025
|
+
copy.color = look[slot].clone();
|
|
1026
|
+
}
|
|
1027
|
+
if (copy !== mat) {
|
|
1028
|
+
instance.materials.push(copy);
|
|
1029
|
+
}
|
|
1030
|
+
copies.set(mat, copy);
|
|
1031
|
+
}
|
|
1032
|
+
return copies.get(mat);
|
|
1033
|
+
};
|
|
1034
|
+
obj.material = Array.isArray(obj.material) ? obj.material.map(swap) : swap(obj.material);
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// The renderer gives every skeleton it draws a bone texture, which only the skeleton frees.
|
|
1040
|
+
function disposeSkeletons(root) {
|
|
1041
|
+
root.traverse((obj) => {
|
|
1042
|
+
if (obj.isSkinnedMesh && obj.skeleton) {
|
|
1043
|
+
obj.skeleton.dispose();
|
|
1044
|
+
}
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
// Players drawn with rigs, one instance each, each player on the rig of its own model. recolorOf
|
|
1049
|
+
// gives the shared recolor of a team, model and role, the keepers apart from the outfield, or null
|
|
1050
|
+
// to keep the model's colors.
|
|
1051
|
+
class Rigged {
|
|
1052
|
+
constructor(parent, header, styles, members, rigOf, recolorOf) {
|
|
1053
|
+
this.group = new THREE.Group();
|
|
1054
|
+
parent.add(this.group);
|
|
1055
|
+
this.entries = [];
|
|
1056
|
+
this.byK = new Map();
|
|
1057
|
+
// Shirt numbers on the backs of the models, as GRF's player wears its own.
|
|
1058
|
+
this.numbers = new RigNumbers(this.group, numberAtlasTexture(), restPoseNodes(0));
|
|
1059
|
+
try {
|
|
1060
|
+
for (const k of members) {
|
|
1061
|
+
const p = header.players[k];
|
|
1062
|
+
const style = styles[p.team] || styles[0];
|
|
1063
|
+
const { rig, model } = rigOf(k);
|
|
1064
|
+
const instance = rig.instantiate();
|
|
1065
|
+
const entry = { k, rig, model, team: p.team, gk: !!p.goalkeeper, instance, request: null, drawnClip: false };
|
|
1066
|
+
this.entries.push(entry);
|
|
1067
|
+
this.byK.set(k, entry);
|
|
1068
|
+
const recolor = recolorOf(p.team, model, rig, !!p.goalkeeper);
|
|
1069
|
+
if (recolor) {
|
|
1070
|
+
recolor.apply(instance);
|
|
1071
|
+
entry.recolor = recolor;
|
|
1072
|
+
}
|
|
1073
|
+
const look = playerLook(style, p, k);
|
|
1074
|
+
rig.dress(instance, style, p.goalkeeper, look);
|
|
1075
|
+
this.numbers.add(entry, style, model, look.number, numberColor(style, !!p.goalkeeper));
|
|
1076
|
+
this.group.add(instance.root);
|
|
1077
|
+
}
|
|
1078
|
+
} catch (err) {
|
|
1079
|
+
this.dispose();
|
|
1080
|
+
throw err;
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
update(nodes, visible, hidden) {
|
|
1085
|
+
for (const e of this.entries) {
|
|
1086
|
+
const mode = hidden ? hidden[e.k] : 0;
|
|
1087
|
+
const on = !!visible[e.k] && mode !== HIDE_ALL;
|
|
1088
|
+
e.instance.root.visible = on;
|
|
1089
|
+
if (!on) {
|
|
1090
|
+
continue;
|
|
1091
|
+
}
|
|
1092
|
+
const base = e.k * PLAYER_FLOATS;
|
|
1093
|
+
e.hideHead = mode === HIDE_HEAD;
|
|
1094
|
+
// A team presentation package poses its own players, see presentation/index.js.
|
|
1095
|
+
if (this.presentation && this.presentation.pose(e, nodes, base)) {
|
|
1096
|
+
e.drawnClip = true;
|
|
1097
|
+
continue;
|
|
1098
|
+
}
|
|
1099
|
+
const r = e.request;
|
|
1100
|
+
if (r && r.weight > 0) {
|
|
1101
|
+
e.rig.poseClip(e.instance, r.clip, r.t, r.weight, nodes, base, e.hideHead);
|
|
1102
|
+
e.drawnClip = true;
|
|
1103
|
+
} else {
|
|
1104
|
+
e.rig.pose(e.instance, nodes, base, e.hideHead);
|
|
1105
|
+
e.drawnClip = false;
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
this.numbers.update();
|
|
1109
|
+
// A clip lasts one drawing, a player whose clip is not asked for again stands on his
|
|
1110
|
+
// nodes the next time.
|
|
1111
|
+
for (const e of this.entries) {
|
|
1112
|
+
e.request = null;
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
// Recolored materials may be added after the instances were built, by a change of pairs.
|
|
1117
|
+
attach(team, model, gk, recolor) {
|
|
1118
|
+
for (const e of this.entries) {
|
|
1119
|
+
if (e.team === team && e.model === model && e.gk === gk && e.recolor !== recolor) {
|
|
1120
|
+
recolor.apply(e.instance);
|
|
1121
|
+
e.recolor = recolor;
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
// Asks for a clip on player k for the next drawing, at time t and weight, or none.
|
|
1127
|
+
request(k, clip, t = 0, weight = 1) {
|
|
1128
|
+
const e = this.byK.get(k);
|
|
1129
|
+
if (!e) {
|
|
1130
|
+
return false;
|
|
1131
|
+
}
|
|
1132
|
+
e.request = clip && weight > 0 ? { clip, t, weight } : null;
|
|
1133
|
+
return true;
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
// Drops the clip of player k at once, the nodes pose him from the next drawing.
|
|
1137
|
+
stop(k) {
|
|
1138
|
+
const e = this.byK.get(k);
|
|
1139
|
+
if (e) {
|
|
1140
|
+
e.request = null;
|
|
1141
|
+
e.rig.stopClip(e.instance);
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
eye(nodes, k, out) {
|
|
1146
|
+
const e = this.byK.get(k);
|
|
1147
|
+
const head = e.rig.headIndex >= 0 ? e.instance.bones[e.rig.headIndex] : null;
|
|
1148
|
+
if (e.drawnClip && head && e.rig.eyeLocal) {
|
|
1149
|
+
return out.copy(e.rig.eyeLocal).applyMatrix4(head.matrixWorld);
|
|
1150
|
+
}
|
|
1151
|
+
e.rig.pose(e.instance, nodes, k * PLAYER_FLOATS, !!e.hideHead);
|
|
1152
|
+
return e.rig.eye(e.instance, out) || neckEye(nodes, k * PLAYER_FLOATS, e.rig.heightRatio, out);
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
headBone(k) {
|
|
1156
|
+
const e = this.byK.get(k);
|
|
1157
|
+
return e && e.rig.headIndex >= 0 ? e.instance.bones[e.rig.headIndex] : null;
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
// The middle of player k's helmet as drawn, and its radius, 0 for none.
|
|
1161
|
+
helmet(nodes, k, out) {
|
|
1162
|
+
const e = this.byK.get(k);
|
|
1163
|
+
const head = e ? e.rig.headSphere() : null;
|
|
1164
|
+
if (!head) {
|
|
1165
|
+
return 0;
|
|
1166
|
+
}
|
|
1167
|
+
const bone = e.instance.bones[e.rig.headIndex];
|
|
1168
|
+
out.copy(head.local).applyMatrix4(bone ? bone.matrixWorld : e.instance.world[e.rig.headIndex]);
|
|
1169
|
+
return head.radius + HELMET_MARGIN;
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
size(k) {
|
|
1173
|
+
const e = this.byK.get(k);
|
|
1174
|
+
return (this.presentation && this.presentation.size(e)) || e.rig.size();
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
dispose() {
|
|
1178
|
+
this.group.removeFromParent();
|
|
1179
|
+
for (const e of this.entries) {
|
|
1180
|
+
if (e.recolor) {
|
|
1181
|
+
e.recolor.forget(e.instance);
|
|
1182
|
+
}
|
|
1183
|
+
if (this.presentation) {
|
|
1184
|
+
this.presentation.release(e);
|
|
1185
|
+
}
|
|
1186
|
+
if (e.instance.mixer) {
|
|
1187
|
+
e.instance.mixer.stopAllAction();
|
|
1188
|
+
e.instance.mixer.uncacheRoot(e.instance.root);
|
|
1189
|
+
}
|
|
1190
|
+
e.rig.release(e.instance);
|
|
1191
|
+
}
|
|
1192
|
+
this.numbers.dispose();
|
|
1193
|
+
this.entries = [];
|
|
1194
|
+
this.byK.clear();
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
// The radius and height of GRF's player at the reference height, from its bind pose, which is
|
|
1199
|
+
// the straight rest pose, in the engine frame.
|
|
1200
|
+
const grfSizes = new WeakMap();
|
|
1201
|
+
function grfSize(grf) {
|
|
1202
|
+
if (!grfSizes.has(grf)) {
|
|
1203
|
+
let radius = 0;
|
|
1204
|
+
let top = 0;
|
|
1205
|
+
grf.scene.traverse((obj) => {
|
|
1206
|
+
if (obj.isMesh && obj.geometry.attributes.position) {
|
|
1207
|
+
const pos = obj.geometry.attributes.position;
|
|
1208
|
+
for (let i = 0; i < pos.count; i++) {
|
|
1209
|
+
radius = Math.max(radius, Math.hypot(pos.getX(i), pos.getY(i)));
|
|
1210
|
+
top = Math.max(top, pos.getZ(i));
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
});
|
|
1214
|
+
grfSizes.set(grf, { radius: radius || 0.32, height: top || grf.referenceHeight });
|
|
1215
|
+
}
|
|
1216
|
+
return grfSizes.get(grf);
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
// The head of GRF's player at the reference height, from the vertices of its bind pose that
|
|
1220
|
+
// follow the neck bone, its middle in the frame of the neck node of the rest pose.
|
|
1221
|
+
const grfHeads = new WeakMap();
|
|
1222
|
+
function grfHead(grf) {
|
|
1223
|
+
if (!grfHeads.has(grf)) {
|
|
1224
|
+
let head = null;
|
|
1225
|
+
try {
|
|
1226
|
+
const points = [];
|
|
1227
|
+
const box = new THREE.Box3();
|
|
1228
|
+
grf.scene.traverse((obj) => {
|
|
1229
|
+
if (!obj.isSkinnedMesh) {
|
|
1230
|
+
return;
|
|
1231
|
+
}
|
|
1232
|
+
const neck = obj.skeleton.bones.findIndex((b) => b.name === 'neck');
|
|
1233
|
+
const pos = obj.geometry.attributes.position;
|
|
1234
|
+
const si = obj.geometry.attributes.skinIndex;
|
|
1235
|
+
const sw = obj.geometry.attributes.skinWeight;
|
|
1236
|
+
for (let i = 0; neck >= 0 && i < pos.count; i++) {
|
|
1237
|
+
let best = 0;
|
|
1238
|
+
for (let c = 1; c < 4; c++) {
|
|
1239
|
+
if (sw.getComponent(i, c) > sw.getComponent(i, best)) {
|
|
1240
|
+
best = c;
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
if (si.getComponent(i, best) === neck) {
|
|
1244
|
+
const p = new THREE.Vector3(pos.getX(i), pos.getY(i), pos.getZ(i));
|
|
1245
|
+
points.push(p);
|
|
1246
|
+
box.expandByPoint(p);
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
});
|
|
1250
|
+
if (points.length) {
|
|
1251
|
+
const center = box.getCenter(new THREE.Vector3());
|
|
1252
|
+
let radius = 0;
|
|
1253
|
+
for (const p of points) {
|
|
1254
|
+
radius = Math.max(radius, p.distanceTo(center));
|
|
1255
|
+
}
|
|
1256
|
+
const rest = restPoseNodes(0);
|
|
1257
|
+
const o = 3 * 7;
|
|
1258
|
+
const turn = new THREE.Quaternion(rest[o + 3], rest[o + 4], rest[o + 5], rest[o + 6]).invert();
|
|
1259
|
+
const local = center.sub(new THREE.Vector3(rest[o], rest[o + 1], rest[o + 2])).applyQuaternion(turn);
|
|
1260
|
+
head = { local, radius };
|
|
1261
|
+
}
|
|
1262
|
+
} catch (err) {
|
|
1263
|
+
console.warn('GRF head not measured', err);
|
|
1264
|
+
}
|
|
1265
|
+
grfHeads.set(grf, head || GRF_HEAD);
|
|
1266
|
+
}
|
|
1267
|
+
return grfHeads.get(grf);
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
// Hides the head of GRF's player by its skin weights. The head hangs on the neck bone, and every
|
|
1271
|
+
// fragment weighted mostly to it is discarded while hide.value is 1, so no triangle shared with
|
|
1272
|
+
// the collar stretches. The shadow keeps the head.
|
|
1273
|
+
function hideHeadByWeight(material, neck, hide) {
|
|
1274
|
+
material.onBeforeCompile = (shader) => headPatch(shader, neck, hide);
|
|
1275
|
+
material.customProgramCacheKey = () => 'cuju-grf-head';
|
|
1276
|
+
headOf.set(material, neck);
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
// The neck bone of each material whose head hides, so a package's copy of it hides it too.
|
|
1280
|
+
const headOf = new WeakMap();
|
|
1281
|
+
|
|
1282
|
+
// Edits a program of GRF's player so that its head hides while hide.value is 1.
|
|
1283
|
+
function headPatch(shader, neck, hide) {
|
|
1284
|
+
shader.uniforms.cujuHeadBone = { value: neck };
|
|
1285
|
+
shader.uniforms.cujuHideHead = hide;
|
|
1286
|
+
shader.vertexShader = shader.vertexShader
|
|
1287
|
+
.replace('#include <common>', `#include <common>
|
|
1288
|
+
uniform float cujuHeadBone;
|
|
1289
|
+
varying float vCujuHead;`)
|
|
1290
|
+
.replace('#include <skinbase_vertex>', `#include <skinbase_vertex>
|
|
1291
|
+
vCujuHead = dot(skinWeight, vec4(equal(skinIndex, vec4(cujuHeadBone))));`);
|
|
1292
|
+
shader.fragmentShader = shader.fragmentShader
|
|
1293
|
+
.replace('#include <common>', `#include <common>
|
|
1294
|
+
uniform float cujuHideHead;
|
|
1295
|
+
varying float vCujuHead;`)
|
|
1296
|
+
.replace('#include <clipping_planes_fragment>', `#include <clipping_planes_fragment>
|
|
1297
|
+
if (cujuHideHead > 0.5 && vCujuHead > 0.5) discard;`);
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
// GRF's own figure as a package sees a rig, so the sockets, attachments, behaviours, materials,
|
|
1301
|
+
// shaders and lights of a team's presentation package reach GRF's player too. Its bones are
|
|
1302
|
+
// GRF's 14 nodes by their names, and the world matrix of each is the node's transform in three's
|
|
1303
|
+
// frame, scaled by the player's height about his ground point, so a socket's frame at rest has y
|
|
1304
|
+
// up and z toward where the player faces. GRF's figure plays no clip of a package, so the
|
|
1305
|
+
// package's own clips, its team and worker drivers and its layers apply to its models alone.
|
|
1306
|
+
const GRF_BONES = NODE_NAMES.map((name, i) => ({ name, parent: NODE_PARENTS[i], node: i }));
|
|
1307
|
+
const grfWorld = new THREE.Matrix4();
|
|
1308
|
+
const grfNode = new THREE.Matrix4();
|
|
1309
|
+
const grfScale = new THREE.Vector3();
|
|
1310
|
+
|
|
1311
|
+
function grfRig(entry) {
|
|
1312
|
+
return {
|
|
1313
|
+
grf: true,
|
|
1314
|
+
bones: GRF_BONES,
|
|
1315
|
+
headIndex: -1,
|
|
1316
|
+
headMask: [],
|
|
1317
|
+
clip: () => null,
|
|
1318
|
+
// Fills the world matrices of the instance from the nodes, as GrfPlayers draws them.
|
|
1319
|
+
pose(instance, nodes, base) {
|
|
1320
|
+
const gx = nodes[base];
|
|
1321
|
+
const gz = -nodes[base + 1];
|
|
1322
|
+
const s = entry.scale;
|
|
1323
|
+
grfWorld.set(
|
|
1324
|
+
s, 0, 0, gx * (1 - s),
|
|
1325
|
+
0, s, 0, 0,
|
|
1326
|
+
0, 0, s, gz * (1 - s),
|
|
1327
|
+
0, 0, 0, 1,
|
|
1328
|
+
);
|
|
1329
|
+
for (let i = 0; i < NODE_COUNT; i++) {
|
|
1330
|
+
nodeMatrix(nodes, base + i * 7, grfNode);
|
|
1331
|
+
instance.world[i].multiplyMatrices(grfWorld, grfNode);
|
|
1332
|
+
}
|
|
1333
|
+
},
|
|
1334
|
+
// Writes the world matrices into GRF's bones, which keep the engine frame of player.glb.
|
|
1335
|
+
write(instance) {
|
|
1336
|
+
for (let i = 0; i < NODE_COUNT; i++) {
|
|
1337
|
+
const bone = instance.bones[i];
|
|
1338
|
+
if (bone) {
|
|
1339
|
+
bone.matrixWorld.multiplyMatrices(instance.world[i], ENGINE_TO_THREE);
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
},
|
|
1343
|
+
};
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
// GRF's own player, player.glb, kept in the engine frame. Its bind pose has identity rotations
|
|
1347
|
+
// and its inverse binds are pure translations, so a bone's world matrix is the saved node
|
|
1348
|
+
// transform, turned into three's frame, and that reproduces GRF's skinning exactly. GRF scales
|
|
1349
|
+
// the body by height over 1.92 meters about the player's ground point and moves the hair to the
|
|
1350
|
+
// scaled neck without scaling it.
|
|
1351
|
+
class GrfPlayers {
|
|
1352
|
+
constructor(parent, header, styles, members, grf, options = {}) {
|
|
1353
|
+
this.group = new THREE.Group();
|
|
1354
|
+
parent.add(this.group);
|
|
1355
|
+
this.grf = grf;
|
|
1356
|
+
// A referee or another figure brings a kit canvas of its own and no number.
|
|
1357
|
+
this.kitCanvas = options.kitCanvas || null;
|
|
1358
|
+
this.byK = new Map();
|
|
1359
|
+
// Kit textures and cloned materials belong here, the geometries and the textures in grf
|
|
1360
|
+
// are shared by every instance and stay with the assets.
|
|
1361
|
+
this.disposables = [];
|
|
1362
|
+
this.entries = [];
|
|
1363
|
+
try {
|
|
1364
|
+
this.build(header, styles, members);
|
|
1365
|
+
} catch (err) {
|
|
1366
|
+
this.dispose();
|
|
1367
|
+
throw err;
|
|
1368
|
+
}
|
|
1369
|
+
this.node = new THREE.Matrix4();
|
|
1370
|
+
this.scaled = new THREE.Matrix4();
|
|
1371
|
+
this.neck = new THREE.Vector3();
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
build(header, styles, members) {
|
|
1375
|
+
const grf = this.grf;
|
|
1376
|
+
const bases = new Map();
|
|
1377
|
+
const kitBase = (team, gk) => {
|
|
1378
|
+
const key = `${team}:${gk}`;
|
|
1379
|
+
if (!bases.has(key)) {
|
|
1380
|
+
bases.set(key, paintKitBase(grf.mask, styles[team] || styles[0], !!gk));
|
|
1381
|
+
}
|
|
1382
|
+
return bases.get(key);
|
|
1383
|
+
};
|
|
1384
|
+
for (const k of members) {
|
|
1385
|
+
const p = header.players[k];
|
|
1386
|
+
const style = styles[p.team] || styles[0];
|
|
1387
|
+
const look = grfLook(grf, style, p);
|
|
1388
|
+
const root = SkeletonUtils.clone(grf.scene);
|
|
1389
|
+
const entry = {
|
|
1390
|
+
k, root, bones: new Array(NODE_COUNT).fill(null), hair: null, scale: look.height / grf.referenceHeight,
|
|
1391
|
+
hideHead: { value: 0 },
|
|
1392
|
+
};
|
|
1393
|
+
this.entries.push(entry);
|
|
1394
|
+
this.byK.set(k, entry);
|
|
1395
|
+
const bones = entry.bones;
|
|
1396
|
+
const owned = [];
|
|
1397
|
+
const kit = this.kitCanvas
|
|
1398
|
+
? kitTexture(this.kitCanvas, style, false, null)
|
|
1399
|
+
: kitTexture(kitBase(p.team, p.goalkeeper ? 1 : 0), style, !!p.goalkeeper, playerLook(style, p, k).number);
|
|
1400
|
+
this.disposables.push(kit);
|
|
1401
|
+
const copies = new Map();
|
|
1402
|
+
root.traverse((obj) => {
|
|
1403
|
+
const i = NODE_NAMES.indexOf(obj.name);
|
|
1404
|
+
if (obj.isBone && i >= 0) {
|
|
1405
|
+
obj.matrixAutoUpdate = false;
|
|
1406
|
+
obj.matrixWorldAutoUpdate = false;
|
|
1407
|
+
bones[i] = obj;
|
|
1408
|
+
}
|
|
1409
|
+
if (obj.isMesh) {
|
|
1410
|
+
obj.castShadow = true;
|
|
1411
|
+
obj.receiveShadow = true;
|
|
1412
|
+
obj.frustumCulled = false;
|
|
1413
|
+
const neck = obj.isSkinnedMesh ? obj.skeleton.bones.findIndex((b) => b.name === 'neck') : -1;
|
|
1414
|
+
const swap = (mat) => {
|
|
1415
|
+
if (!copies.has(mat)) {
|
|
1416
|
+
const copy = this.dress(mat, look, kit);
|
|
1417
|
+
if (neck >= 0) {
|
|
1418
|
+
hideHeadByWeight(copy, neck, entry.hideHead);
|
|
1419
|
+
}
|
|
1420
|
+
owned.push(copy);
|
|
1421
|
+
copies.set(mat, copy);
|
|
1422
|
+
}
|
|
1423
|
+
return copies.get(mat);
|
|
1424
|
+
};
|
|
1425
|
+
obj.material = Array.isArray(obj.material) ? obj.material.map(swap) : swap(obj.material);
|
|
1426
|
+
}
|
|
1427
|
+
});
|
|
1428
|
+
if (look.hair) {
|
|
1429
|
+
const hair = SkeletonUtils.clone(look.hair);
|
|
1430
|
+
hair.matrixAutoUpdate = false;
|
|
1431
|
+
hair.traverse((obj) => {
|
|
1432
|
+
if (obj.isMesh && !Array.isArray(obj.material)) {
|
|
1433
|
+
obj.castShadow = true;
|
|
1434
|
+
obj.material = obj.material.clone();
|
|
1435
|
+
obj.material.map = look.hairColor;
|
|
1436
|
+
obj.material.color.set(0xffffff);
|
|
1437
|
+
obj.material.needsUpdate = true;
|
|
1438
|
+
this.disposables.push(obj.material);
|
|
1439
|
+
}
|
|
1440
|
+
});
|
|
1441
|
+
entry.hair = hair;
|
|
1442
|
+
this.group.add(hair);
|
|
1443
|
+
}
|
|
1444
|
+
this.group.add(root);
|
|
1445
|
+
// What a team's presentation package reads of the player, as of a rigged model. A
|
|
1446
|
+
// package's copy of a material keeps the hidden head of GRF's player.
|
|
1447
|
+
const instance = {
|
|
1448
|
+
root, bones, materials: owned, world: GRF_BONES.map(() => new THREE.Matrix4()),
|
|
1449
|
+
repatch: (original, copy) => {
|
|
1450
|
+
const neck = headOf.get(original);
|
|
1451
|
+
if (neck === undefined) {
|
|
1452
|
+
return;
|
|
1453
|
+
}
|
|
1454
|
+
const own = copy.onBeforeCompile;
|
|
1455
|
+
const key = copy.customProgramCacheKey;
|
|
1456
|
+
copy.onBeforeCompile = (shader, renderer) => {
|
|
1457
|
+
own.call(copy, shader, renderer);
|
|
1458
|
+
headPatch(shader, neck, entry.hideHead);
|
|
1459
|
+
};
|
|
1460
|
+
copy.customProgramCacheKey = () => `${key.call(copy)}:grf-head`;
|
|
1461
|
+
},
|
|
1462
|
+
};
|
|
1463
|
+
entry.adapter = { k, team: p.team, gk: !!p.goalkeeper, rig: grfRig(entry), instance, hideHead: false, request: null, grf: true };
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
dress(mat, look, kit) {
|
|
1468
|
+
const copy = mat.clone();
|
|
1469
|
+
this.disposables.push(copy);
|
|
1470
|
+
const name = (mat.name || '').toLowerCase();
|
|
1471
|
+
if (name === 'kit') {
|
|
1472
|
+
copy.color.set(0xffffff);
|
|
1473
|
+
copy.map = kit;
|
|
1474
|
+
if (this.grf.kitNormal) {
|
|
1475
|
+
copy.normalMap = this.grf.kitNormal;
|
|
1476
|
+
}
|
|
1477
|
+
copy.roughness = 0.8;
|
|
1478
|
+
copy.metalness = 0;
|
|
1479
|
+
} else if (name === 'skin' && look.skin) {
|
|
1480
|
+
copy.color.set(0xffffff);
|
|
1481
|
+
copy.map = look.skin;
|
|
1482
|
+
copy.roughness = 0.65;
|
|
1483
|
+
} else if (name === 'shoe' && this.grf.shoe) {
|
|
1484
|
+
copy.color.set(0xffffff);
|
|
1485
|
+
copy.map = this.grf.shoe;
|
|
1486
|
+
} else if (name === 'sole' && this.grf.sole) {
|
|
1487
|
+
copy.color.set(0xffffff);
|
|
1488
|
+
copy.map = this.grf.sole;
|
|
1489
|
+
}
|
|
1490
|
+
copy.needsUpdate = true;
|
|
1491
|
+
return copy;
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
update(nodes, visible, hidden) {
|
|
1495
|
+
for (const e of this.entries) {
|
|
1496
|
+
const mode = hidden ? hidden[e.k] : 0;
|
|
1497
|
+
const on = !!visible[e.k] && mode !== HIDE_ALL;
|
|
1498
|
+
e.root.visible = on;
|
|
1499
|
+
e.hideHead.value = mode === HIDE_HEAD ? 1 : 0;
|
|
1500
|
+
if (e.hair) {
|
|
1501
|
+
e.hair.visible = on && mode !== HIDE_HEAD;
|
|
1502
|
+
}
|
|
1503
|
+
if (!on) {
|
|
1504
|
+
continue;
|
|
1505
|
+
}
|
|
1506
|
+
const base = e.k * PLAYER_FLOATS;
|
|
1507
|
+
// A team presentation package poses its players of GRF's figure, see
|
|
1508
|
+
// presentation/index.js, and the hair follows the posed neck.
|
|
1509
|
+
if (this.presentation && e.adapter) {
|
|
1510
|
+
e.adapter.hideHead = mode === HIDE_HEAD;
|
|
1511
|
+
if (this.presentation.pose(e.adapter, nodes, base)) {
|
|
1512
|
+
if (e.hair) {
|
|
1513
|
+
e.adapter.instance.world[3].decompose(tmpPos, tmpQuat, grfScale);
|
|
1514
|
+
e.hair.matrix.compose(tmpPos, tmpQuat, ONE).multiply(ENGINE_TO_THREE);
|
|
1515
|
+
}
|
|
1516
|
+
continue;
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
// Scaling about the ground point g moves g to the origin, scales and moves it back, in
|
|
1520
|
+
// three's frame g has y 0.
|
|
1521
|
+
const gx = nodes[base];
|
|
1522
|
+
const gz = -nodes[base + 1];
|
|
1523
|
+
const s = e.scale;
|
|
1524
|
+
this.scaled.set(
|
|
1525
|
+
s, 0, 0, gx * (1 - s),
|
|
1526
|
+
0, s, 0, 0,
|
|
1527
|
+
0, 0, s, gz * (1 - s),
|
|
1528
|
+
0, 0, 0, 1,
|
|
1529
|
+
);
|
|
1530
|
+
for (let i = 0; i < NODE_COUNT; i++) {
|
|
1531
|
+
const bone = e.bones[i];
|
|
1532
|
+
if (!bone) {
|
|
1533
|
+
continue;
|
|
1534
|
+
}
|
|
1535
|
+
nodeMatrix(nodes, base + i * 7, this.node);
|
|
1536
|
+
bone.matrixWorld.multiplyMatrices(this.scaled, this.node).multiply(ENGINE_TO_THREE);
|
|
1537
|
+
}
|
|
1538
|
+
if (e.hair) {
|
|
1539
|
+
const o = base + 3 * 7;
|
|
1540
|
+
nodeMatrix(nodes, o, this.node);
|
|
1541
|
+
this.neck.set(nodes[o], nodes[o + 2], -nodes[o + 1]).applyMatrix4(this.scaled);
|
|
1542
|
+
this.node.setPosition(this.neck);
|
|
1543
|
+
e.hair.matrix.multiplyMatrices(this.node, ENGINE_TO_THREE);
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
eye(nodes, k, out) {
|
|
1549
|
+
const e = this.byK.get(k);
|
|
1550
|
+
return neckEye(nodes, k * PLAYER_FLOATS, e ? e.scale : 1, out);
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
headBone(k) {
|
|
1554
|
+
const e = this.byK.get(k);
|
|
1555
|
+
return e ? e.bones[3] : null;
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
helmet(nodes, k, out) {
|
|
1559
|
+
const e = this.byK.get(k);
|
|
1560
|
+
const s = e ? e.scale : 1;
|
|
1561
|
+
const head = grfHead(this.grf);
|
|
1562
|
+
neckEye(nodes, k * PLAYER_FLOATS, s, out, head.local);
|
|
1563
|
+
return head.radius * s + HELMET_MARGIN;
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
size(k) {
|
|
1567
|
+
const e = this.byK.get(k);
|
|
1568
|
+
const own = e && e.adapter && this.presentation ? this.presentation.size(e.adapter) : null;
|
|
1569
|
+
if (own) {
|
|
1570
|
+
return own;
|
|
1571
|
+
}
|
|
1572
|
+
const s = e ? e.scale : 1;
|
|
1573
|
+
const base = grfSize(this.grf);
|
|
1574
|
+
return { radius: base.radius * s, height: base.height * s };
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
dispose() {
|
|
1578
|
+
this.group.removeFromParent();
|
|
1579
|
+
for (const d of this.disposables) {
|
|
1580
|
+
d.dispose();
|
|
1581
|
+
}
|
|
1582
|
+
for (const e of this.entries) {
|
|
1583
|
+
if (this.presentation && e.adapter) {
|
|
1584
|
+
this.presentation.release(e.adapter);
|
|
1585
|
+
}
|
|
1586
|
+
disposeSkeletons(e.root);
|
|
1587
|
+
}
|
|
1588
|
+
this.disposables = [];
|
|
1589
|
+
this.entries = [];
|
|
1590
|
+
this.byK.clear();
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
const SKIN_FILES = { light: ['skin_01.png', 'skin_02.png'], medium: ['skin_03.png'], dark: ['skin_04.png'] };
|
|
1595
|
+
const ROSTER_SKIN = { light: 'skin_01.png', tan: 'skin_02.png', medium: 'skin_03.png', dark: 'skin_04.png' };
|
|
1596
|
+
|
|
1597
|
+
// Skin, hair and height of a GRF player, the roster entry when it chooses, else GRF's own look
|
|
1598
|
+
// for the index, the height always GRF's.
|
|
1599
|
+
function grfLook(grf, style, p) {
|
|
1600
|
+
const entry = grf.appearance[p.index % Math.max(1, grf.appearance.length)] || {};
|
|
1601
|
+
const roster = (style.players && style.players[p.index]) || {};
|
|
1602
|
+
const h = (p.index * 7 + p.team * 3) >>> 0;
|
|
1603
|
+
let skin = grf.textures[entry.skin] || grf.textures['skin_01.png'] || null;
|
|
1604
|
+
if (roster.skin && roster.skin !== 'grf') {
|
|
1605
|
+
skin = grf.textures[ROSTER_SKIN[roster.skin]] || skin;
|
|
1606
|
+
} else if (style.skin && style.skin !== 'mixed' && SKIN_FILES[style.skin]) {
|
|
1607
|
+
const files = SKIN_FILES[style.skin];
|
|
1608
|
+
skin = grf.textures[files[h % files.length]] || skin;
|
|
1609
|
+
}
|
|
1610
|
+
let hair = grf.hair[entry.hair] || null;
|
|
1611
|
+
if (roster.hair && roster.hair !== 'grf') {
|
|
1612
|
+
hair = roster.hair === 'bald' ? null : (grf.hair[`hair_${roster.hair}.glb`] || hair);
|
|
1613
|
+
} else if (style.hair === 'bald') {
|
|
1614
|
+
hair = null;
|
|
1615
|
+
} else if (style.hair === 'short' || style.hair === 'long') {
|
|
1616
|
+
const fits = Object.keys(grf.hair).filter((f) => f.includes(style.hair));
|
|
1617
|
+
hair = fits.length ? grf.hair[fits[h % fits.length]] : hair;
|
|
1618
|
+
}
|
|
1619
|
+
let hairColor = grf.textures[entry.hair_color] || grf.textures['hair_black.png'] || null;
|
|
1620
|
+
if (roster.hair_color && roster.hair_color !== 'grf') {
|
|
1621
|
+
hairColor = grf.textures[`hair_${roster.hair_color}.png`] || hairColor;
|
|
1622
|
+
}
|
|
1623
|
+
return { skin, hair, hairColor, height: entry.height_m || grf.referenceHeight };
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
async function loadTexture(loader, url, srgb = true) {
|
|
1627
|
+
const tex = await loader.loadAsync(url);
|
|
1628
|
+
tex.flipY = false;
|
|
1629
|
+
tex.colorSpace = srgb ? THREE.SRGBColorSpace : THREE.NoColorSpace;
|
|
1630
|
+
tex.anisotropy = 4;
|
|
1631
|
+
return tex;
|
|
1632
|
+
}
|
|
1633
|
+
|
|
1634
|
+
async function loadImageData(url) {
|
|
1635
|
+
const res = await fetch(url);
|
|
1636
|
+
if (!res.ok) {
|
|
1637
|
+
throw new Error(`${url} answered ${res.status}`);
|
|
1638
|
+
}
|
|
1639
|
+
const bitmap = await createImageBitmap(await res.blob());
|
|
1640
|
+
const canvas = document.createElement('canvas');
|
|
1641
|
+
canvas.width = bitmap.width;
|
|
1642
|
+
canvas.height = bitmap.height;
|
|
1643
|
+
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
|
1644
|
+
ctx.drawImage(bitmap, 0, 0);
|
|
1645
|
+
bitmap.close();
|
|
1646
|
+
return ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
// Loads the assets the converter wrote, see assets/manifest.json. Any failure leaves the simple
|
|
1650
|
+
// figures and the procedural ball, goals and stands in place. Every file is asked for at once, so
|
|
1651
|
+
// the page waits for the slowest file rather than for each in turn, and onProgress, when given,
|
|
1652
|
+
// hears the count of files done and asked for.
|
|
1653
|
+
export async function loadAssets(base = 'assets/', onProgress = null) {
|
|
1654
|
+
const assets = {
|
|
1655
|
+
manifest: null, grf: null, ball: null, goals: null, crowd: null, adboards: [], pitch: null, error: null,
|
|
1656
|
+
};
|
|
1657
|
+
try {
|
|
1658
|
+
const res = await fetch(`${base}manifest.json`, { cache: 'no-cache' });
|
|
1659
|
+
if (!res.ok) {
|
|
1660
|
+
throw new Error(`manifest.json answered ${res.status}`);
|
|
1661
|
+
}
|
|
1662
|
+
assets.manifest = await res.json();
|
|
1663
|
+
} catch (err) {
|
|
1664
|
+
assets.error = `No assets, using simple figures. ${err.message || err}`;
|
|
1665
|
+
console.info(assets.error);
|
|
1666
|
+
return assets;
|
|
1667
|
+
}
|
|
1668
|
+
const manifest = assets.manifest;
|
|
1669
|
+
const files = manifest.files || [];
|
|
1670
|
+
const byRole = (role) => files.filter((f) => f.role === role);
|
|
1671
|
+
const loader = new GLTFLoader();
|
|
1672
|
+
const textures = new THREE.TextureLoader();
|
|
1673
|
+
let asked = 0;
|
|
1674
|
+
let done = 0;
|
|
1675
|
+
const counted = (promise) => {
|
|
1676
|
+
asked++;
|
|
1677
|
+
return promise.finally(() => {
|
|
1678
|
+
done++;
|
|
1679
|
+
if (onProgress) {
|
|
1680
|
+
onProgress(done, asked);
|
|
1681
|
+
}
|
|
1682
|
+
});
|
|
1683
|
+
};
|
|
1684
|
+
const tryLoad = async (what, fn) => {
|
|
1685
|
+
try {
|
|
1686
|
+
return await counted(fn());
|
|
1687
|
+
} catch (err) {
|
|
1688
|
+
console.warn(`${what} not loaded`, err);
|
|
1689
|
+
return null;
|
|
1690
|
+
}
|
|
1691
|
+
};
|
|
1692
|
+
const want = files.map((f) => f.file).filter((n) => /^(skin_|hair_).*\.png$/.test(n) || n === 'shoe_sole.png');
|
|
1693
|
+
const [ball, goals, crowd, pitch, adboards, grf] = await Promise.all([
|
|
1694
|
+
tryLoad('ball', async () => (await loader.loadAsync(`${base}ball.glb`)).scene),
|
|
1695
|
+
tryLoad('goals', async () => (await loader.loadAsync(`${base}goal.glb`)).scene),
|
|
1696
|
+
tryLoad('crowd', () => textures.loadAsync(`${base}crowd.webp`)),
|
|
1697
|
+
tryLoad('pitch', () => textures.loadAsync(`${base}pitch.webp`)),
|
|
1698
|
+
Promise.all(byRole('adboard').map((f) => tryLoad(f.file, () => textures.loadAsync(base + f.file)))),
|
|
1699
|
+
tryLoad('player', async () => {
|
|
1700
|
+
const hair = {};
|
|
1701
|
+
// Every kit, the goalkeepers' too, is painted from the mask in the team's colours.
|
|
1702
|
+
const [gltf, loaded, , [mask, kitNormal, shoe]] = await Promise.all([
|
|
1703
|
+
counted(loader.loadAsync(`${base}player.glb`)),
|
|
1704
|
+
Promise.all(want.map((n) => counted(loadTexture(textures, base + n)))),
|
|
1705
|
+
Promise.all(byRole('hair').map(async (f) => {
|
|
1706
|
+
const h = await tryLoad(f.file, async () => (await loader.loadAsync(base + f.file)).scene);
|
|
1707
|
+
if (h) {
|
|
1708
|
+
hair[f.file] = h;
|
|
1709
|
+
}
|
|
1710
|
+
})),
|
|
1711
|
+
Promise.all([
|
|
1712
|
+
counted(loadImageData(`${base}kit_mask.png`)),
|
|
1713
|
+
tryLoad('kit normal', () => loadTexture(textures, `${base}kit_normal.webp`, false)),
|
|
1714
|
+
tryLoad('shoe', () => loadTexture(textures, `${base}shoe.webp`)),
|
|
1715
|
+
]),
|
|
1716
|
+
]);
|
|
1717
|
+
const tex = Object.fromEntries(want.map((n, i) => [n, loaded[i]]));
|
|
1718
|
+
return {
|
|
1719
|
+
scene: gltf.scene,
|
|
1720
|
+
mask,
|
|
1721
|
+
kitNormal,
|
|
1722
|
+
shoe,
|
|
1723
|
+
sole: tex['shoe_sole.png'] || null,
|
|
1724
|
+
textures: tex,
|
|
1725
|
+
hair,
|
|
1726
|
+
appearance: (manifest.appearance && manifest.appearance.players) || [],
|
|
1727
|
+
referenceHeight: (manifest.player_scale && manifest.player_scale.reference_height_m) || 1.92,
|
|
1728
|
+
};
|
|
1729
|
+
}),
|
|
1730
|
+
]);
|
|
1731
|
+
assets.ball = ball;
|
|
1732
|
+
assets.goals = goals;
|
|
1733
|
+
assets.crowd = crowd ? crowd.image : null;
|
|
1734
|
+
if (pitch) {
|
|
1735
|
+
pitch.colorSpace = THREE.SRGBColorSpace;
|
|
1736
|
+
pitch.anisotropy = 8;
|
|
1737
|
+
assets.pitch = pitch;
|
|
1738
|
+
}
|
|
1739
|
+
for (const img of adboards) {
|
|
1740
|
+
if (img) {
|
|
1741
|
+
assets.adboards.push(img.image);
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
assets.grf = grf;
|
|
1745
|
+
if (!assets.grf) {
|
|
1746
|
+
assets.error = 'The player model did not load, using simple figures.';
|
|
1747
|
+
}
|
|
1748
|
+
return assets;
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
// A model a team uploaded is someone else's file. Everything it needs must sit inside the glb,
|
|
1752
|
+
// so the loader is only let at data and blob URLs, the ones GLTFLoader makes for embedded
|
|
1753
|
+
// buffers and images, and any other URL turns into an empty data URL that fails to load.
|
|
1754
|
+
function sealedManager() {
|
|
1755
|
+
const manager = new THREE.LoadingManager();
|
|
1756
|
+
manager.setURLModifier((url) => (/^(data|blob):/i.test(url) ? url : 'data:,'));
|
|
1757
|
+
return manager;
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
// Throws a readable error for a glb that names outside files, before the loader sees it.
|
|
1761
|
+
function checkSelfContained(buffer) {
|
|
1762
|
+
const view = new DataView(buffer);
|
|
1763
|
+
if (buffer.byteLength < 20 || view.getUint32(0, true) !== 0x46546c67) {
|
|
1764
|
+
throw new Error(t('players.model.notGlb'));
|
|
1765
|
+
}
|
|
1766
|
+
const length = view.getUint32(12, true);
|
|
1767
|
+
if (view.getUint32(16, true) !== 0x4e4f534a || 20 + length > buffer.byteLength) {
|
|
1768
|
+
throw new Error(t('players.model.damaged'));
|
|
1769
|
+
}
|
|
1770
|
+
const json = JSON.parse(new TextDecoder().decode(new Uint8Array(buffer, 20, length)));
|
|
1771
|
+
const outside = [...(json.buffers || []), ...(json.images || [])]
|
|
1772
|
+
.filter((entry) => typeof entry.uri === 'string' && !/^data:/i.test(entry.uri));
|
|
1773
|
+
if (outside.length) {
|
|
1774
|
+
throw new Error(t('players.model.outside'));
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
// Parses the bytes of a team's own model, the upload of the team editor included.
|
|
1779
|
+
export async function parseModel(buffer) {
|
|
1780
|
+
if (buffer.byteLength > MAX_MODEL_BYTES) {
|
|
1781
|
+
throw new Error(t('players.model.tooLarge', { size: Math.ceil(buffer.byteLength / 1048576), max: MAX_MODEL_BYTES / 1048576 }));
|
|
1782
|
+
}
|
|
1783
|
+
checkSelfContained(buffer);
|
|
1784
|
+
return new GLTFLoader(sealedManager()).parseAsync(buffer, '');
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
async function fetchModel(url) {
|
|
1788
|
+
const res = await fetch(url, { cache: 'no-store' });
|
|
1789
|
+
if (!res.ok) {
|
|
1790
|
+
throw new Error(`model download failed, ${res.status}`);
|
|
1791
|
+
}
|
|
1792
|
+
if (Number(res.headers.get('Content-Length')) > MAX_MODEL_BYTES) {
|
|
1793
|
+
throw new Error(t('players.model.larger', { max: MAX_MODEL_BYTES / 1048576 }));
|
|
1794
|
+
}
|
|
1795
|
+
return res.arrayBuffer();
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
// The registry gives a model's size and joints but nothing that changes with every upload, so the
|
|
1799
|
+
// rig is keyed on a digest of the bytes themselves.
|
|
1800
|
+
async function digest(buffer) {
|
|
1801
|
+
if (globalThis.crypto && crypto.subtle) {
|
|
1802
|
+
const sum = new Uint8Array(await crypto.subtle.digest('SHA-256', buffer));
|
|
1803
|
+
return Array.from(sum, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
1804
|
+
}
|
|
1805
|
+
// Pages served over plain http away from localhost have no crypto.subtle, FNV stands in.
|
|
1806
|
+
const bytes = new Uint8Array(buffer);
|
|
1807
|
+
let h = 0x811c9dc5;
|
|
1808
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
1809
|
+
h = Math.imul(h ^ bytes[i], 16777619);
|
|
1810
|
+
}
|
|
1811
|
+
return `${bytes.length}.${(h >>> 0).toString(16)}`;
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
// The newest rig of each team. A rig replaced by a new upload is not disposed, players built
|
|
1815
|
+
// before it may still draw its scene.
|
|
1816
|
+
const teamRigCache = new Map();
|
|
1817
|
+
|
|
1818
|
+
// Builds a rig for a team's own model from the registry, null when it cannot. A failure is not
|
|
1819
|
+
// kept, so the next rebuild tries again.
|
|
1820
|
+
export async function loadTeamRig(team) {
|
|
1821
|
+
if (!team || !team.model) {
|
|
1822
|
+
return null;
|
|
1823
|
+
}
|
|
1824
|
+
try {
|
|
1825
|
+
const buffer = await fetchModel(`/api/teams/${encodeURIComponent(team.id)}/model`);
|
|
1826
|
+
const key = `${await digest(buffer)}.${JSON.stringify(team.model.joints)}`;
|
|
1827
|
+
let cached = teamRigCache.get(team.id);
|
|
1828
|
+
if (!cached || cached.key !== key) {
|
|
1829
|
+
const entry = { key, rig: null };
|
|
1830
|
+
// A model on KayKit's rig also plays the clips of KayKit's animation packs.
|
|
1831
|
+
entry.rig = parseModel(buffer).then((gltf) => attachKayKitClips(rigFromGltf(gltf, team.model.joints, { onlyKit: true })));
|
|
1832
|
+
entry.rig.catch(() => {
|
|
1833
|
+
if (teamRigCache.get(team.id) === entry) {
|
|
1834
|
+
teamRigCache.delete(team.id);
|
|
1835
|
+
}
|
|
1836
|
+
});
|
|
1837
|
+
teamRigCache.set(team.id, entry);
|
|
1838
|
+
cached = entry;
|
|
1839
|
+
}
|
|
1840
|
+
return await cached.rig;
|
|
1841
|
+
} catch (err) {
|
|
1842
|
+
console.warn('team model not used', team.id, err);
|
|
1843
|
+
return null;
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
|
|
1847
|
+
// A rig from a loaded glTF, with the animation clips the file carries.
|
|
1848
|
+
export function rigFromGltf(gltf, joints, options = {}) {
|
|
1849
|
+
return new Rig(gltf.scene, joints, { ...options, animations: gltf.animations || [] });
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
// The clips of a rig as celebrations name them, clip and the clip's name.
|
|
1853
|
+
export function clipNames(rig) {
|
|
1854
|
+
return rig && rig.clipNames ? rig.clipNames() : [];
|
|
1855
|
+
}
|
|
1856
|
+
|
|
1857
|
+
// The model a roster entry asks for, team follows the team's model.
|
|
1858
|
+
export function playerModel(style, index) {
|
|
1859
|
+
const roster = style && style.players ? style.players[index] : null;
|
|
1860
|
+
if (roster && roster.model && roster.model !== 'team') {
|
|
1861
|
+
return roster.model;
|
|
1862
|
+
}
|
|
1863
|
+
return (style && style.model) || 'grf';
|
|
1864
|
+
}
|
|
1865
|
+
|
|
1866
|
+
// Every model a team's roster uses, the team's own model included, for loading rigs.
|
|
1867
|
+
export function teamModels(style, size = null) {
|
|
1868
|
+
const out = new Set([(style && style.model) || 'grf']);
|
|
1869
|
+
const roster = (style && style.players) || [];
|
|
1870
|
+
roster.slice(0, size == null ? roster.length : size).forEach((p, i) => out.add(playerModel(style, i)));
|
|
1871
|
+
return [...out];
|
|
1872
|
+
}
|
|
1873
|
+
|
|
1874
|
+
// The rigs of each team as a map from model to rig. A team may give one rig, the rig of its own
|
|
1875
|
+
// model, as older callers do, or a Map or plain object of rigs by model.
|
|
1876
|
+
function rigTables(rigs, styles) {
|
|
1877
|
+
return [0, 1].map((team) => {
|
|
1878
|
+
const r = rigs ? rigs[team] : null;
|
|
1879
|
+
const table = new Map();
|
|
1880
|
+
if (r instanceof Rig) {
|
|
1881
|
+
const style = styles[team] || styles[0];
|
|
1882
|
+
table.set(style && style.model && style.model !== 'grf' ? style.model : 'custom', r);
|
|
1883
|
+
} else if (r instanceof Map) {
|
|
1884
|
+
for (const [m, rig] of r) {
|
|
1885
|
+
if (rig instanceof Rig) {
|
|
1886
|
+
table.set(m, rig);
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
} else if (r && typeof r === 'object') {
|
|
1890
|
+
for (const [m, rig] of Object.entries(r)) {
|
|
1891
|
+
if (rig instanceof Rig) {
|
|
1892
|
+
table.set(m, rig);
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
return table;
|
|
1897
|
+
});
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
// Two models look alike on the pitch when their first main colors, as each team shows them, are
|
|
1901
|
+
// closer than ALIKE in CIEDE2000 and their second ones closer than ALIKE_SECOND.
|
|
1902
|
+
const ALIKE = 10;
|
|
1903
|
+
const ALIKE_SECOND = 18;
|
|
1904
|
+
|
|
1905
|
+
function alike(a, b) {
|
|
1906
|
+
if (!a.length || !b.length) {
|
|
1907
|
+
return false;
|
|
1908
|
+
}
|
|
1909
|
+
const d0 = ciede2000(a[0].lab, b[0].lab);
|
|
1910
|
+
const d1 = a[1] && b[1] ? ciede2000(a[1].lab, b[1].lab) : d0;
|
|
1911
|
+
return d0 < ALIKE && d1 < ALIKE_SECOND;
|
|
1912
|
+
}
|
|
1913
|
+
|
|
1914
|
+
// After one team is recolored, the main colors must differ by this much, else the other team
|
|
1915
|
+
// takes its kit's primary as well.
|
|
1916
|
+
const APART = 25;
|
|
1917
|
+
|
|
1918
|
+
function clearlyApart(a, b) {
|
|
1919
|
+
return !a.length || !b.length || ciede2000(a[0].lab, b[0].lab) >= APART;
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
// The pairs of every team and model in use, the team's own, and where two teams would look alike
|
|
1923
|
+
// the main color of the model turned into the kit's primary, the right team's first, then the
|
|
1924
|
+
// left team's when the two are still not clearly apart. The main color is the heaviest that is
|
|
1925
|
+
// not skin.
|
|
1926
|
+
function resolvePairs(styles, used, clash) {
|
|
1927
|
+
const pairs = [0, 1].map((team) => {
|
|
1928
|
+
const style = styles[team] || styles[0];
|
|
1929
|
+
const table = new Map();
|
|
1930
|
+
for (const m of used[team].keys()) {
|
|
1931
|
+
table.set(m, { ...((style.colors && style.colors[m]) || {}) });
|
|
1932
|
+
}
|
|
1933
|
+
return table;
|
|
1934
|
+
});
|
|
1935
|
+
if (!clash) {
|
|
1936
|
+
return pairs;
|
|
1937
|
+
}
|
|
1938
|
+
// The models as drawn, each dressed in its kit with the team's own pairs over it.
|
|
1939
|
+
const shown = (team, m) => shownColors(used[team].get(m), { ...kitPairs(used[team].get(m), (styles[team] || styles[0]).kit), ...pairs[team].get(m) });
|
|
1940
|
+
const turn = (team, m) => {
|
|
1941
|
+
const main = shownColors(used[team].get(m), {})[0];
|
|
1942
|
+
if (main) {
|
|
1943
|
+
pairs[team].get(m)[main.hex] = hex((styles[team] || styles[0]).kit.primary);
|
|
1944
|
+
}
|
|
1945
|
+
};
|
|
1946
|
+
const done = [new Set(), new Set()];
|
|
1947
|
+
for (const m1 of used[1].keys()) {
|
|
1948
|
+
for (const m0 of used[0].keys()) {
|
|
1949
|
+
if (!alike(shown(0, m0), shown(1, m1))) {
|
|
1950
|
+
continue;
|
|
1951
|
+
}
|
|
1952
|
+
if (!done[1].has(m1)) {
|
|
1953
|
+
turn(1, m1);
|
|
1954
|
+
done[1].add(m1);
|
|
1955
|
+
}
|
|
1956
|
+
if (!clearlyApart(shown(0, m0), shown(1, m1)) && !done[0].has(m0)) {
|
|
1957
|
+
turn(0, m0);
|
|
1958
|
+
done[0].add(m0);
|
|
1959
|
+
}
|
|
1960
|
+
}
|
|
1961
|
+
}
|
|
1962
|
+
return pairs;
|
|
1963
|
+
}
|
|
1964
|
+
|
|
1965
|
+
// Clips that settle into a pose, which hold their last frame rather than start over.
|
|
1966
|
+
const ONCE_CLIP = /(_Down|_StandUp|^PickUp$|^Throw$|^Interact$|^Hit_|Death|_Pose$|Spawn|Awaken|Resurrect|^Use_Item$|^Dodge_|^Jump_Start$|^Jump_Land$)/;
|
|
1967
|
+
|
|
1968
|
+
function loopTime(clip, t) {
|
|
1969
|
+
const d = clip.duration || 0;
|
|
1970
|
+
return d > 0 ? t % d : 0;
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
// Glass helmets, a bubble round the head of every player, one instanced sphere drawn after the
|
|
1974
|
+
// players. Only the side facing the camera is drawn, so a camera inside a bubble sees through it,
|
|
1975
|
+
// and the glass never writes depth. It is clear in the middle, with a faint tint, a brighter rim
|
|
1976
|
+
// where it turns away and the highlight of the sun.
|
|
1977
|
+
const HELMET_VERTEX = `
|
|
1978
|
+
varying vec3 vNormal;
|
|
1979
|
+
varying vec3 vView;
|
|
1980
|
+
void main() {
|
|
1981
|
+
mat4 m = modelMatrix * instanceMatrix;
|
|
1982
|
+
vec4 world = m * vec4(position, 1.0);
|
|
1983
|
+
vNormal = normalize(mat3(m) * normal);
|
|
1984
|
+
vView = cameraPosition - world.xyz;
|
|
1985
|
+
gl_Position = projectionMatrix * viewMatrix * world;
|
|
1986
|
+
}
|
|
1987
|
+
`;
|
|
1988
|
+
const HELMET_FRAGMENT = `
|
|
1989
|
+
uniform vec3 uTint;
|
|
1990
|
+
uniform vec3 uSunDir;
|
|
1991
|
+
varying vec3 vNormal;
|
|
1992
|
+
varying vec3 vView;
|
|
1993
|
+
void main() {
|
|
1994
|
+
vec3 n = normalize(vNormal);
|
|
1995
|
+
vec3 v = normalize(vView);
|
|
1996
|
+
float facing = clamp(dot(n, v), 0.0, 1.0);
|
|
1997
|
+
float rim = pow(1.0 - facing, 3.0);
|
|
1998
|
+
float spec = pow(max(dot(n, normalize(uSunDir + v)), 0.0), 120.0);
|
|
1999
|
+
float alpha = clamp(0.04 + 0.5 * rim + 0.9 * spec, 0.0, 0.85);
|
|
2000
|
+
gl_FragColor = vec4(uTint * (0.5 + rim) + vec3(spec), alpha);
|
|
2001
|
+
}
|
|
2002
|
+
`;
|
|
2003
|
+
|
|
2004
|
+
class Helmets {
|
|
2005
|
+
constructor(parent, count) {
|
|
2006
|
+
this.geometry = new THREE.SphereGeometry(1, 24, 16);
|
|
2007
|
+
this.material = new THREE.ShaderMaterial({
|
|
2008
|
+
vertexShader: HELMET_VERTEX,
|
|
2009
|
+
fragmentShader: HELMET_FRAGMENT,
|
|
2010
|
+
uniforms: { uTint: { value: new THREE.Color(0xbfe6ff) }, uSunDir: { value: new THREE.Vector3(0, 1, 0) } },
|
|
2011
|
+
transparent: true,
|
|
2012
|
+
depthWrite: false,
|
|
2013
|
+
});
|
|
2014
|
+
this.mesh = new THREE.InstancedMesh(this.geometry, this.material, Math.max(1, count));
|
|
2015
|
+
this.mesh.frustumCulled = false;
|
|
2016
|
+
this.mesh.renderOrder = 2;
|
|
2017
|
+
this.mesh.visible = false;
|
|
2018
|
+
this.center = new THREE.Vector3();
|
|
2019
|
+
this.scale = new THREE.Vector3();
|
|
2020
|
+
this.matrix = new THREE.Matrix4();
|
|
2021
|
+
this.turn = new THREE.Quaternion();
|
|
2022
|
+
parent.add(this.mesh);
|
|
2023
|
+
}
|
|
2024
|
+
|
|
2025
|
+
// Puts a bubble on every player on the pitch whose head is drawn, from the parts that draw
|
|
2026
|
+
// them.
|
|
2027
|
+
update(nodes, where, visible, hidden, sun) {
|
|
2028
|
+
for (let k = 0; k < where.length; k++) {
|
|
2029
|
+
const part = where[k];
|
|
2030
|
+
let radius = 0;
|
|
2031
|
+
if (part && part.helmet && visible[k] && !hidden[k]) {
|
|
2032
|
+
radius = part.helmet(nodes, k, this.center);
|
|
2033
|
+
}
|
|
2034
|
+
if (radius > 0) {
|
|
2035
|
+
this.matrix.compose(this.center, this.turn, this.scale.setScalar(radius));
|
|
2036
|
+
} else {
|
|
2037
|
+
this.matrix.copy(ZERO_SCALE);
|
|
2038
|
+
}
|
|
2039
|
+
this.mesh.setMatrixAt(k, this.matrix);
|
|
2040
|
+
}
|
|
2041
|
+
this.mesh.instanceMatrix.needsUpdate = true;
|
|
2042
|
+
if (sun) {
|
|
2043
|
+
this.material.uniforms.uSunDir.value.copy(sun);
|
|
2044
|
+
}
|
|
2045
|
+
this.mesh.visible = true;
|
|
2046
|
+
}
|
|
2047
|
+
|
|
2048
|
+
dispose() {
|
|
2049
|
+
this.geometry.dispose();
|
|
2050
|
+
this.material.dispose();
|
|
2051
|
+
this.mesh.removeFromParent();
|
|
2052
|
+
}
|
|
2053
|
+
}
|
|
2054
|
+
|
|
2055
|
+
export class Players {
|
|
2056
|
+
// rigs holds, per team, a Map or object of rigs by model, or the one rig of the team's own
|
|
2057
|
+
// model, assets the GRF model. Each player draws the model its roster entry names, or the
|
|
2058
|
+
// team's, and falls back to GRF's player when that model has no rig. Kits always come from
|
|
2059
|
+
// the team colors, and other models wear them too, each main color of the model taking the
|
|
2060
|
+
// shirt, trim, shorts or socks of the kit or the keeper's kit, under the team's own color
|
|
2061
|
+
// pairs. With options.clash false the models of two teams keep their pairs even when they
|
|
2062
|
+
// look alike. The rigs and the assets belong to the caller and outlive the players.
|
|
2063
|
+
constructor(scene, header, styles, assets, rigs = [null, null], options = {}) {
|
|
2064
|
+
this.header = header;
|
|
2065
|
+
this.styles = styles;
|
|
2066
|
+
this.count = header.players.length;
|
|
2067
|
+
// A team's presentation package, which may pick models and pose its players.
|
|
2068
|
+
this.presentation = options.presentation || null;
|
|
2069
|
+
this.visible = new Uint8Array(this.count).fill(1);
|
|
2070
|
+
this.hidden = new Uint8Array(this.count);
|
|
2071
|
+
this.group = new THREE.Group();
|
|
2072
|
+
scene.add(this.group);
|
|
2073
|
+
const tables = rigTables(rigs, styles);
|
|
2074
|
+
this.choice = header.players.map((p) => {
|
|
2075
|
+
const style = styles[p.team] || styles[0];
|
|
2076
|
+
const wanted = (this.presentation && this.presentation.modelFor(p, style)) || playerModel(style, p.index);
|
|
2077
|
+
const rig = wanted !== 'grf' && tables[p.team] ? tables[p.team].get(wanted) || null : null;
|
|
2078
|
+
return { model: rig ? wanted : 'grf', wanted, rig };
|
|
2079
|
+
});
|
|
2080
|
+
const used = [new Map(), new Map()];
|
|
2081
|
+
header.players.forEach((p, k) => {
|
|
2082
|
+
const c = this.choice[k];
|
|
2083
|
+
if (c.rig && used[p.team]) {
|
|
2084
|
+
used[p.team].set(c.model, c.rig);
|
|
2085
|
+
}
|
|
2086
|
+
});
|
|
2087
|
+
this.used = used;
|
|
2088
|
+
try {
|
|
2089
|
+
this.pairs = resolvePairs(styles, used, options.clash !== false);
|
|
2090
|
+
} catch (err) {
|
|
2091
|
+
console.warn('model colors not compared', err);
|
|
2092
|
+
this.pairs = resolvePairs(styles, used, false);
|
|
2093
|
+
}
|
|
2094
|
+
this.recolors = new Map();
|
|
2095
|
+
const custom = [];
|
|
2096
|
+
const grf = [];
|
|
2097
|
+
const simple = [];
|
|
2098
|
+
header.players.forEach((p, k) => {
|
|
2099
|
+
if (this.choice[k].rig) {
|
|
2100
|
+
custom.push(k);
|
|
2101
|
+
} else if (assets && assets.grf) {
|
|
2102
|
+
grf.push(k);
|
|
2103
|
+
} else {
|
|
2104
|
+
simple.push(k);
|
|
2105
|
+
}
|
|
2106
|
+
});
|
|
2107
|
+
this.parts = [];
|
|
2108
|
+
this.where = new Array(this.count).fill(null);
|
|
2109
|
+
this.rigged = null;
|
|
2110
|
+
if (custom.length) {
|
|
2111
|
+
try {
|
|
2112
|
+
this.rigged = new Rigged(this.group, header, styles, custom,
|
|
2113
|
+
(k) => this.choice[k], (team, model, rig, gk) => this.recolorFor(team, model, rig, gk));
|
|
2114
|
+
this.rigged.presentation = this.presentation;
|
|
2115
|
+
this.parts.push(this.rigged);
|
|
2116
|
+
custom.forEach((k) => {
|
|
2117
|
+
this.where[k] = this.rigged;
|
|
2118
|
+
});
|
|
2119
|
+
} catch (err) {
|
|
2120
|
+
console.warn('custom models failed, using GRF players', err);
|
|
2121
|
+
this.rigged = null;
|
|
2122
|
+
custom.forEach((k) => {
|
|
2123
|
+
this.choice[k] = { ...this.choice[k], model: 'grf', rig: null };
|
|
2124
|
+
});
|
|
2125
|
+
(assets && assets.grf ? grf : simple).push(...custom);
|
|
2126
|
+
}
|
|
2127
|
+
}
|
|
2128
|
+
if (grf.length) {
|
|
2129
|
+
try {
|
|
2130
|
+
const part = new GrfPlayers(this.group, header, styles, grf, assets.grf);
|
|
2131
|
+
part.presentation = this.presentation;
|
|
2132
|
+
this.grfPart = part;
|
|
2133
|
+
this.parts.push(part);
|
|
2134
|
+
grf.forEach((k) => {
|
|
2135
|
+
this.where[k] = part;
|
|
2136
|
+
});
|
|
2137
|
+
} catch (err) {
|
|
2138
|
+
console.warn('GRF players failed, using simple figures', err);
|
|
2139
|
+
simple.push(...grf);
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
if (simple.length) {
|
|
2143
|
+
const part = new Figures(this.group, header, styles, simple);
|
|
2144
|
+
this.parts.push(part);
|
|
2145
|
+
simple.forEach((k) => {
|
|
2146
|
+
this.where[k] = part;
|
|
2147
|
+
});
|
|
2148
|
+
}
|
|
2149
|
+
this.markers = new Markers(this.group, header, styles);
|
|
2150
|
+
this.helmets = null;
|
|
2151
|
+
this.arenaGroup = null;
|
|
2152
|
+
}
|
|
2153
|
+
|
|
2154
|
+
// The pairs a model is drawn with, its colors dressed in the kit of the role, the keeper's or
|
|
2155
|
+
// the outfield's, as the match shows it, the away kit included, and the team's own pairs over
|
|
2156
|
+
// them. A pair of a color to itself keeps that color.
|
|
2157
|
+
drawnPairs(team, model, rig, gk) {
|
|
2158
|
+
const style = this.styles[team] || this.styles[0];
|
|
2159
|
+
const kit = gk ? { ...style.goalkeeper, pattern: 'plain' } : style.kit;
|
|
2160
|
+
const own = this.pairs[team] ? this.pairs[team].get(model) : null;
|
|
2161
|
+
return { ...kitPairs(rig, kit), ...(own || {}) };
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
// The shared recolor of a team, model and role, made when its pairs change anything.
|
|
2165
|
+
recolorFor(team, model, rig, gk = false) {
|
|
2166
|
+
const key = `${team}:${model}:${gk ? 'gk' : 'field'}`;
|
|
2167
|
+
if (this.recolors.has(key)) {
|
|
2168
|
+
return this.recolors.get(key);
|
|
2169
|
+
}
|
|
2170
|
+
const pairs = this.drawnPairs(team, model, rig, gk);
|
|
2171
|
+
if (!Object.keys(pairs).length) {
|
|
2172
|
+
return null;
|
|
2173
|
+
}
|
|
2174
|
+
try {
|
|
2175
|
+
const r = new Recolor(rig, pairs);
|
|
2176
|
+
this.recolors.set(key, r);
|
|
2177
|
+
return r;
|
|
2178
|
+
} catch (err) {
|
|
2179
|
+
console.warn('model not recolored', model, err);
|
|
2180
|
+
this.recolors.set(key, null);
|
|
2181
|
+
return null;
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
|
|
2185
|
+
// Redraws the colors of a team on one model in place, for a live preview, after its pairs or
|
|
2186
|
+
// its kit changed. Pairs given replace the team's own. Returns false when no player of that
|
|
2187
|
+
// team draws that model.
|
|
2188
|
+
setColors(team, model, pairs) {
|
|
2189
|
+
const rig = this.used[team] ? this.used[team].get(model) : null;
|
|
2190
|
+
if (!rig) {
|
|
2191
|
+
return false;
|
|
2192
|
+
}
|
|
2193
|
+
if (pairs !== undefined) {
|
|
2194
|
+
this.pairs[team].set(model, { ...(pairs || {}) });
|
|
2195
|
+
}
|
|
2196
|
+
for (const gk of [false, true]) {
|
|
2197
|
+
const key = `${team}:${model}:${gk ? 'gk' : 'field'}`;
|
|
2198
|
+
const r = this.recolors.get(key);
|
|
2199
|
+
if (r) {
|
|
2200
|
+
r.set(this.drawnPairs(team, model, rig, gk));
|
|
2201
|
+
continue;
|
|
2202
|
+
}
|
|
2203
|
+
this.recolors.delete(key);
|
|
2204
|
+
const made = this.recolorFor(team, model, rig, gk);
|
|
2205
|
+
if (made && this.rigged) {
|
|
2206
|
+
this.rigged.attach(team, model, gk, made);
|
|
2207
|
+
}
|
|
2208
|
+
}
|
|
2209
|
+
return true;
|
|
2210
|
+
}
|
|
2211
|
+
|
|
2212
|
+
// Redraws every model of a team after its kit changed, its own pairs kept.
|
|
2213
|
+
setKit(team) {
|
|
2214
|
+
for (const model of this.used[team] ? this.used[team].keys() : []) {
|
|
2215
|
+
this.setColors(team, model);
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
// The pairs a team and model are drawn with, the clash recolor included.
|
|
2220
|
+
colorsOf(team, model) {
|
|
2221
|
+
return { ...((this.pairs[team] && this.pairs[team].get(model)) || {}) };
|
|
2222
|
+
}
|
|
2223
|
+
|
|
2224
|
+
// What a team's presentation package reads of player k, the entry of his rigged model or of
|
|
2225
|
+
// GRF's figure, or null for a simple figure.
|
|
2226
|
+
packageEntry(k) {
|
|
2227
|
+
const rigged = this.rigged ? this.rigged.byK.get(k) : null;
|
|
2228
|
+
if (rigged) {
|
|
2229
|
+
return rigged;
|
|
2230
|
+
}
|
|
2231
|
+
const grf = this.grfPart ? this.grfPart.byK.get(k) : null;
|
|
2232
|
+
return grf ? grf.adapter || null : null;
|
|
2233
|
+
}
|
|
2234
|
+
|
|
2235
|
+
// Every entry a package may present, with the group its attachments join.
|
|
2236
|
+
packageEntries() {
|
|
2237
|
+
const out = (this.rigged ? this.rigged.entries : []).map((e) => ({ entry: e, group: this.rigged.group }));
|
|
2238
|
+
for (const e of this.grfPart ? this.grfPart.entries : []) {
|
|
2239
|
+
if (e.adapter) {
|
|
2240
|
+
out.push({ entry: e.adapter, group: this.grfPart.group });
|
|
2241
|
+
}
|
|
2242
|
+
}
|
|
2243
|
+
return out;
|
|
2244
|
+
}
|
|
2245
|
+
|
|
2246
|
+
// The model player k draws, grf when its own had no rig.
|
|
2247
|
+
modelOf(k) {
|
|
2248
|
+
return this.choice[k] ? this.choice[k].model : 'grf';
|
|
2249
|
+
}
|
|
2250
|
+
|
|
2251
|
+
rigOf(k) {
|
|
2252
|
+
return this.choice[k] ? this.choice[k].rig : null;
|
|
2253
|
+
}
|
|
2254
|
+
|
|
2255
|
+
update(pose, frame) {
|
|
2256
|
+
for (let k = 0; k < this.count; k++) {
|
|
2257
|
+
// Bit 0 is on the pitch, a player sent off disappears.
|
|
2258
|
+
this.visible[k] = frame.playerFlags[k] & 1;
|
|
2259
|
+
}
|
|
2260
|
+
for (const part of this.parts) {
|
|
2261
|
+
part.update(pose.nodes, this.visible, this.hidden);
|
|
2262
|
+
}
|
|
2263
|
+
this.markers.update(pose, frame, this.visible, this.hidden);
|
|
2264
|
+
this.updateHelmets(pose);
|
|
2265
|
+
}
|
|
2266
|
+
|
|
2267
|
+
// Glass helmets while the arena of the scene asks for them, as the space station does. A
|
|
2268
|
+
// player whose head a camera hides wears none.
|
|
2269
|
+
updateHelmets(pose) {
|
|
2270
|
+
const scene = this.group.parent;
|
|
2271
|
+
if (!this.arenaGroup || this.arenaGroup.parent !== scene) {
|
|
2272
|
+
this.arenaGroup = scene ? scene.children.find((o) => o.name === 'arena') || null : null;
|
|
2273
|
+
}
|
|
2274
|
+
const arena = this.arenaGroup ? this.arenaGroup.userData : null;
|
|
2275
|
+
if (!arena || !arena.helmets) {
|
|
2276
|
+
if (this.helmets) {
|
|
2277
|
+
this.helmets.mesh.visible = false;
|
|
2278
|
+
}
|
|
2279
|
+
return;
|
|
2280
|
+
}
|
|
2281
|
+
if (!this.helmets) {
|
|
2282
|
+
this.helmets = new Helmets(this.group, this.count);
|
|
2283
|
+
}
|
|
2284
|
+
this.helmets.update(pose.nodes, this.where, this.visible, this.hidden, arena.sun);
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
// Hides player k for a camera at its eyes, none, head for the head and hair, or all.
|
|
2288
|
+
setHidden(k, mode = 'none') {
|
|
2289
|
+
if (k >= 0 && k < this.count) {
|
|
2290
|
+
this.hidden[k] = HIDE_MODES[mode] || 0;
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
|
|
2294
|
+
// The eyes of player k in three's frame, from the model's own head bone, or from GRF's neck.
|
|
2295
|
+
eye(pose, k, out = new THREE.Vector3()) {
|
|
2296
|
+
const part = this.where[k];
|
|
2297
|
+
if (!part) {
|
|
2298
|
+
return this.position(pose, k, out).setY(1.7);
|
|
2299
|
+
}
|
|
2300
|
+
return part.eye(pose.nodes, k, out);
|
|
2301
|
+
}
|
|
2302
|
+
|
|
2303
|
+
// The bone that carries player k's head, whose matrixWorld follows the pose, or null.
|
|
2304
|
+
headBone(k) {
|
|
2305
|
+
const part = this.where[k];
|
|
2306
|
+
return part ? part.headBone(k) : null;
|
|
2307
|
+
}
|
|
2308
|
+
|
|
2309
|
+
// The horizontal radius in meters of player k's model as drawn, from its ground point to the
|
|
2310
|
+
// farthest point of the body standing at rest, arms down.
|
|
2311
|
+
radius(k) {
|
|
2312
|
+
const part = this.where[k];
|
|
2313
|
+
return part ? part.size(k).radius : 0.32;
|
|
2314
|
+
}
|
|
2315
|
+
|
|
2316
|
+
// The standing height in meters of player k's model as drawn.
|
|
2317
|
+
height(k) {
|
|
2318
|
+
const part = this.where[k];
|
|
2319
|
+
return part ? part.size(k).height : 1.85;
|
|
2320
|
+
}
|
|
2321
|
+
|
|
2322
|
+
// The animation clips of player k's model, named as celebrations name them.
|
|
2323
|
+
clipNames(k) {
|
|
2324
|
+
return clipNames(this.rigOf(k));
|
|
2325
|
+
}
|
|
2326
|
+
|
|
2327
|
+
// The clip that name plays at time t seconds on player k's model, with its own time, or
|
|
2328
|
+
// null when the model lacks it. A name of two clips joined by a greater than sign plays the
|
|
2329
|
+
// first once and then loops the second. A clip that settles into a pose plays once and holds
|
|
2330
|
+
// its last frame, the others loop.
|
|
2331
|
+
resolveClip(k, name, t) {
|
|
2332
|
+
const rig = this.rigOf(k);
|
|
2333
|
+
if (!rig || typeof rig.clip !== 'function' || !name) {
|
|
2334
|
+
return null;
|
|
2335
|
+
}
|
|
2336
|
+
// A semantic role plays the clip a team's presentation package gives it.
|
|
2337
|
+
if (String(name).startsWith('role:')) {
|
|
2338
|
+
name = this.presentation ? this.presentation.roleClip(k, String(name).slice(5)) : null;
|
|
2339
|
+
if (!name) {
|
|
2340
|
+
return null;
|
|
2341
|
+
}
|
|
2342
|
+
}
|
|
2343
|
+
const parts = String(name).replace(/^clip:/, '').split('>');
|
|
2344
|
+
const first = rig.clip(parts[0]);
|
|
2345
|
+
if (!first) {
|
|
2346
|
+
return null;
|
|
2347
|
+
}
|
|
2348
|
+
const time = Math.max(0, Number(t) || 0);
|
|
2349
|
+
if (parts.length > 1) {
|
|
2350
|
+
const then = rig.clip(parts[1]);
|
|
2351
|
+
if (then && time >= first.duration) {
|
|
2352
|
+
return { clip: then, t: loopTime(then, time - first.duration) };
|
|
2353
|
+
}
|
|
2354
|
+
return { clip: first, t: Math.min(time, first.duration) };
|
|
2355
|
+
}
|
|
2356
|
+
return { clip: first, t: ONCE_CLIP.test(first.name) ? Math.min(time, first.duration) : loopTime(first, time) };
|
|
2357
|
+
}
|
|
2358
|
+
|
|
2359
|
+
// Whether player k's model has the clip that name plays.
|
|
2360
|
+
hasClip(k, name) {
|
|
2361
|
+
return !!this.resolveClip(k, name, 0);
|
|
2362
|
+
}
|
|
2363
|
+
|
|
2364
|
+
// The length in seconds of one play of a clip on player k's model, both parts of a joined
|
|
2365
|
+
// name, or 0 when the model lacks it.
|
|
2366
|
+
clipDuration(k, name) {
|
|
2367
|
+
const rig = this.rigOf(k);
|
|
2368
|
+
if (!rig || typeof rig.clip !== 'function' || !name) {
|
|
2369
|
+
return 0;
|
|
2370
|
+
}
|
|
2371
|
+
return String(name).replace(/^clip:/, '').split('>').reduce((sum, part) => {
|
|
2372
|
+
const c = rig.clip(part);
|
|
2373
|
+
return sum + (c ? c.duration : 0);
|
|
2374
|
+
}, 0);
|
|
2375
|
+
}
|
|
2376
|
+
|
|
2377
|
+
// Plays a clip of player k's model at time t seconds for the next drawing, standing where
|
|
2378
|
+
// node player of the pose stands, blended over the pose of the nodes by weight from 0 to 1,
|
|
2379
|
+
// which defaults to 1, the clip alone. A clip lasts one drawing, so a caller asks again every
|
|
2380
|
+
// frame, and a player not asked for, or asked at weight 0, is drawn from his nodes exactly.
|
|
2381
|
+
// False when the model has no such clip, then the caller poses a procedural animation.
|
|
2382
|
+
playClip(k, name, t, weight) {
|
|
2383
|
+
const found = this.rigged ? this.resolveClip(k, name, t) : null;
|
|
2384
|
+
if (!found) {
|
|
2385
|
+
return false;
|
|
2386
|
+
}
|
|
2387
|
+
let w = weight == null ? 1 : Number(weight);
|
|
2388
|
+
w = w > 0 ? Math.min(1, w) : 0;
|
|
2389
|
+
return this.rigged.request(k, found.clip, found.t, w);
|
|
2390
|
+
}
|
|
2391
|
+
|
|
2392
|
+
// Draws player k from his nodes from the next drawing on.
|
|
2393
|
+
stopClip(k) {
|
|
2394
|
+
if (this.rigged && this.rigOf(k)) {
|
|
2395
|
+
this.rigged.stop(k);
|
|
2396
|
+
}
|
|
2397
|
+
}
|
|
2398
|
+
|
|
2399
|
+
// The celebration player k performs after a goal, a clip of its model where it names one the
|
|
2400
|
+
// model has, else a name of the animation library.
|
|
2401
|
+
celebration(k, seed = 0) {
|
|
2402
|
+
const p = this.header.players[k];
|
|
2403
|
+
const style = this.styles[p.team] || this.styles[0];
|
|
2404
|
+
const name = pickCelebration(style, p.index, seed);
|
|
2405
|
+
if (name.startsWith('clip:')) {
|
|
2406
|
+
return this.hasClip(k, name) ? name : fallbackCelebration(null, seed + k);
|
|
2407
|
+
}
|
|
2408
|
+
return hasAnimation(name) ? name : fallbackCelebration(name, seed + k);
|
|
2409
|
+
}
|
|
2410
|
+
|
|
2411
|
+
// Ground position of a player in three's frame.
|
|
2412
|
+
position(pose, k, out) {
|
|
2413
|
+
const o = k * PLAYER_FLOATS;
|
|
2414
|
+
return out.set(pose.nodes[o], 0, -pose.nodes[o + 1]);
|
|
2415
|
+
}
|
|
2416
|
+
|
|
2417
|
+
dispose() {
|
|
2418
|
+
for (const part of this.parts) {
|
|
2419
|
+
part.dispose();
|
|
2420
|
+
}
|
|
2421
|
+
this.parts = [];
|
|
2422
|
+
for (const r of this.recolors.values()) {
|
|
2423
|
+
if (r) {
|
|
2424
|
+
r.dispose();
|
|
2425
|
+
}
|
|
2426
|
+
}
|
|
2427
|
+
this.recolors.clear();
|
|
2428
|
+
this.markers.dispose();
|
|
2429
|
+
if (this.helmets) {
|
|
2430
|
+
this.helmets.dispose();
|
|
2431
|
+
this.helmets = null;
|
|
2432
|
+
}
|
|
2433
|
+
this.group.removeFromParent();
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
|
|
2437
|
+
// Rings on the ground. Every player stands on a thin ring of its team's color, faint enough to
|
|
2438
|
+
// sit quietly at broadcast distance, the controlled players on a wider ring and the owner of the
|
|
2439
|
+
// ball on a pulsing one.
|
|
2440
|
+
class Markers {
|
|
2441
|
+
constructor(parent, header, styles) {
|
|
2442
|
+
this.group = new THREE.Group();
|
|
2443
|
+
parent.add(this.group);
|
|
2444
|
+
const ring = new THREE.RingGeometry(0.55, 0.72, 40);
|
|
2445
|
+
ring.rotateX(-Math.PI / 2);
|
|
2446
|
+
// The engine hands a side's control to the designated player as play moves, and a frame
|
|
2447
|
+
// marks the player controlled on its step, so every player of a side with an agent gets
|
|
2448
|
+
// a ring that shows while the player is controlled.
|
|
2449
|
+
this.header = header;
|
|
2450
|
+
const agentTeams = new Set(header.players.filter((p) => p.controlled).map((p) => p.team));
|
|
2451
|
+
this.controlled = header.players.map((p) => {
|
|
2452
|
+
if (!agentTeams.has(p.team)) {
|
|
2453
|
+
return null;
|
|
2454
|
+
}
|
|
2455
|
+
const style = styles[p.team] || styles[0];
|
|
2456
|
+
const mat = new THREE.MeshBasicMaterial({
|
|
2457
|
+
color: threeColor(style.kit.primary), transparent: true, opacity: 0.85, depthWrite: false,
|
|
2458
|
+
});
|
|
2459
|
+
mat.color.lerp(new THREE.Color(0xffffff), 0.35);
|
|
2460
|
+
const mesh = new THREE.Mesh(ring, mat);
|
|
2461
|
+
mesh.renderOrder = 2;
|
|
2462
|
+
this.group.add(mesh);
|
|
2463
|
+
return mesh;
|
|
2464
|
+
});
|
|
2465
|
+
const teamGeo = new THREE.RingGeometry(0.34, 0.42, 36);
|
|
2466
|
+
teamGeo.rotateX(-Math.PI / 2);
|
|
2467
|
+
this.teamRing = new THREE.InstancedMesh(teamGeo, new THREE.MeshBasicMaterial({
|
|
2468
|
+
transparent: true, opacity: 0.42, depthWrite: false,
|
|
2469
|
+
}), header.players.length);
|
|
2470
|
+
this.teamRing.frustumCulled = false;
|
|
2471
|
+
this.teamRing.renderOrder = 1;
|
|
2472
|
+
this.teamRing.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
|
|
2473
|
+
const white = new THREE.Color(0xffffff);
|
|
2474
|
+
header.players.forEach((p, k) => {
|
|
2475
|
+
const style = styles[p.team] || styles[0];
|
|
2476
|
+
this.teamRing.setColorAt(k, threeColor(style.kit.primary).lerp(white, 0.2));
|
|
2477
|
+
});
|
|
2478
|
+
this.teamRing.instanceColor.needsUpdate = true;
|
|
2479
|
+
this.group.add(this.teamRing);
|
|
2480
|
+
this.ringMatrix = new THREE.Matrix4();
|
|
2481
|
+
const ownerGeo = new THREE.RingGeometry(0.8, 0.95, 48);
|
|
2482
|
+
ownerGeo.rotateX(-Math.PI / 2);
|
|
2483
|
+
this.owner = new THREE.Mesh(ownerGeo, new THREE.MeshBasicMaterial({
|
|
2484
|
+
color: 0xfff6c0, transparent: true, opacity: 0.9, depthWrite: false,
|
|
2485
|
+
}));
|
|
2486
|
+
this.owner.renderOrder = 2;
|
|
2487
|
+
this.group.add(this.owner);
|
|
2488
|
+
this.start = performance.now();
|
|
2489
|
+
this.ring = ring;
|
|
2490
|
+
}
|
|
2491
|
+
|
|
2492
|
+
// Shows or hides the team rings, and sets how strongly they show, 0.42 by default.
|
|
2493
|
+
setTeamRings(visible, opacity = null) {
|
|
2494
|
+
this.teamRing.visible = !!visible;
|
|
2495
|
+
if (opacity != null) {
|
|
2496
|
+
this.teamRing.material.opacity = opacity;
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
|
|
2500
|
+
dispose() {
|
|
2501
|
+
this.group.removeFromParent();
|
|
2502
|
+
this.ring.dispose();
|
|
2503
|
+
for (const m of this.controlled) {
|
|
2504
|
+
if (m) {
|
|
2505
|
+
m.material.dispose();
|
|
2506
|
+
}
|
|
2507
|
+
}
|
|
2508
|
+
this.teamRing.geometry.dispose();
|
|
2509
|
+
this.teamRing.material.dispose();
|
|
2510
|
+
this.teamRing.dispose();
|
|
2511
|
+
this.owner.geometry.dispose();
|
|
2512
|
+
this.owner.material.dispose();
|
|
2513
|
+
}
|
|
2514
|
+
|
|
2515
|
+
update(pose, frame, visible, hidden = null) {
|
|
2516
|
+
// Frames of an older cuju carry no mark of control, the header's agents keep their rings.
|
|
2517
|
+
const flags = frame && frame.playerFlags;
|
|
2518
|
+
let marked = false;
|
|
2519
|
+
if (flags) {
|
|
2520
|
+
for (let k = 0; k < flags.length && !marked; k++) {
|
|
2521
|
+
marked = !!(flags[k] & 8);
|
|
2522
|
+
}
|
|
2523
|
+
}
|
|
2524
|
+
for (let k = 0; k < this.controlled.length; k++) {
|
|
2525
|
+
const o = k * PLAYER_FLOATS;
|
|
2526
|
+
const on = !!visible[k] && !(hidden && hidden[k] === HIDE_ALL);
|
|
2527
|
+
if (on) {
|
|
2528
|
+
this.ringMatrix.makeTranslation(pose.nodes[o], 0.022, -pose.nodes[o + 1]);
|
|
2529
|
+
this.teamRing.setMatrixAt(k, this.ringMatrix);
|
|
2530
|
+
} else {
|
|
2531
|
+
this.teamRing.setMatrixAt(k, ZERO_SCALE);
|
|
2532
|
+
}
|
|
2533
|
+
const m = this.controlled[k];
|
|
2534
|
+
if (!m) {
|
|
2535
|
+
continue;
|
|
2536
|
+
}
|
|
2537
|
+
m.visible = !!visible[k] && (marked ? !!(flags[k] & 8) : !!this.header.players[k].controlled);
|
|
2538
|
+
m.position.set(pose.nodes[o], 0.03, -pose.nodes[o + 1]);
|
|
2539
|
+
}
|
|
2540
|
+
this.teamRing.instanceMatrix.needsUpdate = true;
|
|
2541
|
+
const owner = frame.ownedPlayer;
|
|
2542
|
+
if (owner >= 0 && owner < this.controlled.length && visible[owner]) {
|
|
2543
|
+
const o = owner * PLAYER_FLOATS;
|
|
2544
|
+
this.owner.visible = true;
|
|
2545
|
+
this.owner.position.set(pose.nodes[o], 0.035, -pose.nodes[o + 1]);
|
|
2546
|
+
const s = 1 + Math.sin((performance.now() - this.start) / 200) * 0.06;
|
|
2547
|
+
this.owner.scale.set(s, 1, s);
|
|
2548
|
+
} else {
|
|
2549
|
+
this.owner.visible = false;
|
|
2550
|
+
}
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2553
|
+
|
|
2554
|
+
// The referee's kit, painted from GRF's kit template, black with a white collar, white cuffs and
|
|
2555
|
+
// white tops to the socks.
|
|
2556
|
+
const REFEREE_BLACK = [22, 22, 24];
|
|
2557
|
+
const REFEREE_WHITE = [240, 240, 240];
|
|
2558
|
+
|
|
2559
|
+
function paintRefereeKit(mask) {
|
|
2560
|
+
const { width: w, height: h, data: m } = mask;
|
|
2561
|
+
const canvas = document.createElement('canvas');
|
|
2562
|
+
canvas.width = w;
|
|
2563
|
+
canvas.height = h;
|
|
2564
|
+
const ctx = canvas.getContext('2d');
|
|
2565
|
+
const out = ctx.createImageData(w, h);
|
|
2566
|
+
const d = out.data;
|
|
2567
|
+
for (let y = 0; y < h; y++) {
|
|
2568
|
+
const v = (y + 0.5) / h;
|
|
2569
|
+
for (let x = 0; x < w; x++) {
|
|
2570
|
+
const i = (y * w + x) * 4;
|
|
2571
|
+
const u = (x + 0.5) / w;
|
|
2572
|
+
const cu = u >= 0.5 ? (u - 0.5) * 2 : u * 2;
|
|
2573
|
+
const r = m[i] / 255;
|
|
2574
|
+
const b = m[i + 2] / 255;
|
|
2575
|
+
let c = REFEREE_BLACK;
|
|
2576
|
+
if (r > 0.5 && v < 0.02 && Math.abs(cu - 0.5) < 0.16) {
|
|
2577
|
+
c = REFEREE_WHITE;
|
|
2578
|
+
} else if (r > 0.5 && v < 0.16 && (cu < 0.035 || cu > 0.965)) {
|
|
2579
|
+
c = REFEREE_WHITE;
|
|
2580
|
+
} else if (b > 0.5 && v < 0.6) {
|
|
2581
|
+
c = REFEREE_WHITE;
|
|
2582
|
+
}
|
|
2583
|
+
d[i] = c[0];
|
|
2584
|
+
d[i + 1] = c[1];
|
|
2585
|
+
d[i + 2] = c[2];
|
|
2586
|
+
d[i + 3] = 255;
|
|
2587
|
+
}
|
|
2588
|
+
}
|
|
2589
|
+
ctx.putImageData(out, 0, 0);
|
|
2590
|
+
return canvas;
|
|
2591
|
+
}
|
|
2592
|
+
|
|
2593
|
+
const CARD_COLORS = { yellow: 0xf4d20b, red: 0xd71920 };
|
|
2594
|
+
|
|
2595
|
+
// The referee, GRF's player in black, with a card in the right hand on request. update takes one
|
|
2596
|
+
// player's nodes in the engine frame, as a Float32Array of PLAYER_FLOATS, and whether it shows.
|
|
2597
|
+
export function createReferee(scene, assets) {
|
|
2598
|
+
const group = new THREE.Group();
|
|
2599
|
+
group.name = 'referee';
|
|
2600
|
+
scene.add(group);
|
|
2601
|
+
const header = { players: [{ team: 0, index: 6, goalkeeper: false, controlled: false }], teams: [{}, {}] };
|
|
2602
|
+
const kit = { pattern: 'plain', primary: REFEREE_BLACK, secondary: REFEREE_WHITE, shorts: REFEREE_BLACK, socks: REFEREE_BLACK };
|
|
2603
|
+
const style = {
|
|
2604
|
+
kit, goalkeeper: kit, skin: 'mixed', hair: 'short', badge: '', number: 'white', players: [], art: null,
|
|
2605
|
+
};
|
|
2606
|
+
let body = null;
|
|
2607
|
+
if (assets && assets.grf) {
|
|
2608
|
+
try {
|
|
2609
|
+
body = new GrfPlayers(group, header, [style, style], [0], assets.grf, {
|
|
2610
|
+
kitCanvas: paintRefereeKit(assets.grf.mask),
|
|
2611
|
+
});
|
|
2612
|
+
} catch (err) {
|
|
2613
|
+
console.warn('referee model failed, using a simple figure', err);
|
|
2614
|
+
body = null;
|
|
2615
|
+
}
|
|
2616
|
+
}
|
|
2617
|
+
if (!body) {
|
|
2618
|
+
body = new Figures(group, header, [style, style], [0]);
|
|
2619
|
+
}
|
|
2620
|
+
const scale = body.entries && body.entries[0] ? body.entries[0].scale : 1;
|
|
2621
|
+
const cardGeo = new THREE.BoxGeometry(0.075, 0.105, 0.005);
|
|
2622
|
+
const cardMats = Object.fromEntries(Object.entries(CARD_COLORS).map(([k, c]) => [k, new THREE.MeshStandardMaterial({
|
|
2623
|
+
color: c, emissive: c, emissiveIntensity: 0.25, roughness: 0.5,
|
|
2624
|
+
})]));
|
|
2625
|
+
const card = new THREE.Mesh(cardGeo, cardMats.yellow);
|
|
2626
|
+
card.castShadow = true;
|
|
2627
|
+
card.visible = false;
|
|
2628
|
+
card.matrixAutoUpdate = false;
|
|
2629
|
+
group.add(card);
|
|
2630
|
+
const visible = new Uint8Array(1);
|
|
2631
|
+
const q = new THREE.Quaternion();
|
|
2632
|
+
const p = new THREE.Vector3();
|
|
2633
|
+
const along = new THREE.Vector3();
|
|
2634
|
+
const face = new THREE.Vector3();
|
|
2635
|
+
const side = new THREE.Vector3();
|
|
2636
|
+
const basis = new THREE.Matrix4();
|
|
2637
|
+
let kind = null;
|
|
2638
|
+
return {
|
|
2639
|
+
group,
|
|
2640
|
+
update(nodes, show = true) {
|
|
2641
|
+
visible[0] = show ? 1 : 0;
|
|
2642
|
+
body.update(nodes, visible, null);
|
|
2643
|
+
card.visible = !!kind && !!show;
|
|
2644
|
+
if (!card.visible) {
|
|
2645
|
+
return;
|
|
2646
|
+
}
|
|
2647
|
+
// At the fingertips of the right hand, the long side along the forearm and the face
|
|
2648
|
+
// turned to where the referee faces, whatever the twist of the arm.
|
|
2649
|
+
const o = 7 * 7;
|
|
2650
|
+
q.set(nodes[o + 3], nodes[o + 4], nodes[o + 5], nodes[o + 6]);
|
|
2651
|
+
if (q.lengthSq() < 1e-8) {
|
|
2652
|
+
q.identity();
|
|
2653
|
+
}
|
|
2654
|
+
along.set(0, 0, -1).applyQuaternion(q);
|
|
2655
|
+
p.copy(along).multiplyScalar(0.36);
|
|
2656
|
+
const gx = nodes[0];
|
|
2657
|
+
const gy = nodes[1];
|
|
2658
|
+
const x = gx + (nodes[o] + p.x - gx) * scale;
|
|
2659
|
+
const y = gy + (nodes[o + 1] + p.y - gy) * scale;
|
|
2660
|
+
const z = (nodes[o + 2] + p.z) * scale;
|
|
2661
|
+
q.set(nodes[3], nodes[4], nodes[5], nodes[6]);
|
|
2662
|
+
if (q.lengthSq() < 1e-8) {
|
|
2663
|
+
q.identity();
|
|
2664
|
+
}
|
|
2665
|
+
face.set(0, -1, 0).applyQuaternion(q).addScaledVector(along, -along.dot(face));
|
|
2666
|
+
if (face.lengthSq() < 1e-6) {
|
|
2667
|
+
face.set(0, 0, 1);
|
|
2668
|
+
}
|
|
2669
|
+
face.normalize();
|
|
2670
|
+
side.crossVectors(along, face);
|
|
2671
|
+
basis.makeBasis(side, along, face).premultiply(ENGINE_TO_THREE);
|
|
2672
|
+
card.matrix.copy(basis).setPosition(x, z, -y);
|
|
2673
|
+
},
|
|
2674
|
+
// Shows a yellow or a red card in the right hand, null puts it away.
|
|
2675
|
+
setCard(next) {
|
|
2676
|
+
kind = next === 'yellow' || next === 'red' ? next : null;
|
|
2677
|
+
if (kind) {
|
|
2678
|
+
card.material = cardMats[kind];
|
|
2679
|
+
}
|
|
2680
|
+
card.visible = !!kind;
|
|
2681
|
+
},
|
|
2682
|
+
eye(nodes, out = new THREE.Vector3()) {
|
|
2683
|
+
return neckEye(nodes, 0, scale, out);
|
|
2684
|
+
},
|
|
2685
|
+
dispose() {
|
|
2686
|
+
body.dispose();
|
|
2687
|
+
cardGeo.dispose();
|
|
2688
|
+
for (const m of Object.values(cardMats)) {
|
|
2689
|
+
m.dispose();
|
|
2690
|
+
}
|
|
2691
|
+
group.removeFromParent();
|
|
2692
|
+
},
|
|
2693
|
+
};
|
|
2694
|
+
}
|