@combos-fun/plugin-renderer-3d 0.1.12 → 0.1.14-beta.0
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.
- package/dist/plugin-renderer-3d.cjs.js +239 -12
- package/dist/plugin-renderer-3d.cjs.js.map +1 -1
- package/dist/plugin-renderer-3d.cjs.prod.js +1 -1
- package/dist/plugin-renderer-3d.d.ts +30 -7
- package/dist/plugin-renderer-3d.esm.js +238 -15
- package/dist/plugin-renderer-3d.esm.js.map +1 -1
- package/package.json +3 -3
|
@@ -4,6 +4,7 @@ var tslib = require('tslib');
|
|
|
4
4
|
var engine = require('@combos-fun/engine');
|
|
5
5
|
var three = require('three');
|
|
6
6
|
var inspectorDecorator = require('@combos-fun/inspector-decorator');
|
|
7
|
+
var KTX2Loader_js = require('three/addons/loaders/KTX2Loader.js');
|
|
7
8
|
|
|
8
9
|
const COMBOS_GAME_OBJECT_ID = 'combosGameObjectId';
|
|
9
10
|
const COMBOS_TRANSFORM3D = 'combosTransform3D';
|
|
@@ -139,7 +140,14 @@ class ThreeContext {
|
|
|
139
140
|
this.camera = new three.PerspectiveCamera(75, width / height, 0.1, 1000);
|
|
140
141
|
this.camera.position.z = 5;
|
|
141
142
|
this.fallbackCamera = this.camera;
|
|
142
|
-
const
|
|
143
|
+
const pixelRatio = params.pixelRatio ?? 1;
|
|
144
|
+
this.width = width;
|
|
145
|
+
this.height = height;
|
|
146
|
+
this.pixelRatio = pixelRatio;
|
|
147
|
+
const rendererOpts = {
|
|
148
|
+
antialias,
|
|
149
|
+
powerPreference: params.powerPreference ?? 'high-performance',
|
|
150
|
+
};
|
|
143
151
|
if (canvas) {
|
|
144
152
|
rendererOpts.canvas = canvas;
|
|
145
153
|
}
|
|
@@ -147,14 +155,17 @@ class ThreeContext {
|
|
|
147
155
|
rendererOpts.alpha = true;
|
|
148
156
|
}
|
|
149
157
|
this.renderer = new three.WebGLRenderer(rendererOpts);
|
|
158
|
+
this.renderer.setPixelRatio(pixelRatio);
|
|
150
159
|
this.renderer.setSize(width, height);
|
|
151
|
-
this.renderer.setPixelRatio(window.devicePixelRatio);
|
|
152
160
|
this.renderer.setClearColor(backgroundColor, backgroundAlpha);
|
|
153
161
|
this.renderer.toneMapping = TONE_MAPPING[params.toneMapping ?? 'aces'] ?? three.ACESFilmicToneMapping;
|
|
154
162
|
this.renderer.toneMappingExposure = params.toneMappingExposure ?? 1;
|
|
163
|
+
this.anisotropy = readAnisotropy(this.renderer);
|
|
164
|
+
const softShadows = params.softShadows ?? pixelRatio < 2;
|
|
165
|
+
const shadowMapSize = params.shadowMapSize ?? (softShadows ? 1024 : 512);
|
|
155
166
|
if (this.shadows) {
|
|
156
167
|
this.renderer.shadowMap.enabled = true;
|
|
157
|
-
this.renderer.shadowMap.type = three.PCFSoftShadowMap;
|
|
168
|
+
this.renderer.shadowMap.type = softShadows ? three.PCFSoftShadowMap : three.PCFShadowMap;
|
|
158
169
|
}
|
|
159
170
|
if (!canvas && container) {
|
|
160
171
|
container.appendChild(this.renderer.domElement);
|
|
@@ -166,7 +177,7 @@ class ThreeContext {
|
|
|
166
177
|
directionalLight.position.set(5, 10, 7.5);
|
|
167
178
|
if (this.shadows) {
|
|
168
179
|
directionalLight.castShadow = true;
|
|
169
|
-
directionalLight.shadow.mapSize.set(
|
|
180
|
+
directionalLight.shadow.mapSize.set(shadowMapSize, shadowMapSize);
|
|
170
181
|
}
|
|
171
182
|
this.scene.add(directionalLight);
|
|
172
183
|
this.defaultLights.push(directionalLight);
|
|
@@ -182,13 +193,43 @@ class ThreeContext {
|
|
|
182
193
|
}
|
|
183
194
|
this.clock = new three.Clock();
|
|
184
195
|
}
|
|
185
|
-
update() {
|
|
196
|
+
update(options) {
|
|
186
197
|
const delta = this.clock.getDelta();
|
|
198
|
+
if (options?.skipDraw)
|
|
199
|
+
return;
|
|
187
200
|
for (const mixer of this.mixers.values()) {
|
|
188
201
|
mixer.update(delta);
|
|
189
202
|
}
|
|
190
203
|
this.renderer.render(this.scene, this.camera);
|
|
191
204
|
}
|
|
205
|
+
setPixelRatio(pixelRatio) {
|
|
206
|
+
if (!(pixelRatio > 0) || pixelRatio === this.pixelRatio)
|
|
207
|
+
return;
|
|
208
|
+
this.pixelRatio = pixelRatio;
|
|
209
|
+
this.renderer.setPixelRatio(pixelRatio);
|
|
210
|
+
this.renderer.setSize(this.width, this.height);
|
|
211
|
+
}
|
|
212
|
+
configureTexture(texture) {
|
|
213
|
+
if (!texture?.isTexture || this.anisotropy <= 1)
|
|
214
|
+
return;
|
|
215
|
+
if (texture.anisotropy === this.anisotropy)
|
|
216
|
+
return;
|
|
217
|
+
texture.anisotropy = this.anisotropy;
|
|
218
|
+
texture.needsUpdate = true;
|
|
219
|
+
}
|
|
220
|
+
configureObject(object) {
|
|
221
|
+
if (!object || this.anisotropy <= 1)
|
|
222
|
+
return;
|
|
223
|
+
object.traverse((child) => {
|
|
224
|
+
const mesh = child;
|
|
225
|
+
if (!mesh.isMesh || !mesh.material)
|
|
226
|
+
return;
|
|
227
|
+
const materials = Array.isArray(mesh.material) ? mesh.material : [mesh.material];
|
|
228
|
+
for (const material of materials) {
|
|
229
|
+
configureMaterialTextures(material, (texture) => this.configureTexture(texture));
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
}
|
|
192
233
|
hasTransform3DRoot(id) {
|
|
193
234
|
return isTransform3DRoot(this.nodes.get(id));
|
|
194
235
|
}
|
|
@@ -240,6 +281,7 @@ class ThreeContext {
|
|
|
240
281
|
*/
|
|
241
282
|
attachVisual(id, object, gameObject) {
|
|
242
283
|
tagObject3D(object, id);
|
|
284
|
+
this.configureObject(object);
|
|
243
285
|
const root = this.nodes.get(id);
|
|
244
286
|
if (isTransform3DRoot(root) && root !== object) {
|
|
245
287
|
if (object.parent !== root) {
|
|
@@ -436,6 +478,8 @@ class ThreeContext {
|
|
|
436
478
|
}
|
|
437
479
|
}
|
|
438
480
|
resize(width, height) {
|
|
481
|
+
this.width = width;
|
|
482
|
+
this.height = height;
|
|
439
483
|
this.camera.aspect = width / height;
|
|
440
484
|
this.camera.updateProjectionMatrix();
|
|
441
485
|
if (this.fallbackCamera !== this.camera) {
|
|
@@ -459,6 +503,33 @@ class ThreeContext {
|
|
|
459
503
|
this.clock = null;
|
|
460
504
|
}
|
|
461
505
|
}
|
|
506
|
+
const MATERIAL_MAPS = [
|
|
507
|
+
'map',
|
|
508
|
+
'alphaMap',
|
|
509
|
+
'aoMap',
|
|
510
|
+
'bumpMap',
|
|
511
|
+
'displacementMap',
|
|
512
|
+
'emissiveMap',
|
|
513
|
+
'lightMap',
|
|
514
|
+
'metalnessMap',
|
|
515
|
+
'normalMap',
|
|
516
|
+
'roughnessMap',
|
|
517
|
+
];
|
|
518
|
+
function configureMaterialTextures(material, apply) {
|
|
519
|
+
const record = material;
|
|
520
|
+
for (const key of MATERIAL_MAPS) {
|
|
521
|
+
const texture = record[key];
|
|
522
|
+
if (texture?.isTexture)
|
|
523
|
+
apply(texture);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
function readAnisotropy(renderer) {
|
|
527
|
+
const capabilities = renderer.capabilities;
|
|
528
|
+
const max = capabilities?.getMaxAnisotropy?.() ?? 0;
|
|
529
|
+
if (!(max > 1))
|
|
530
|
+
return 1;
|
|
531
|
+
return Math.min(8, max);
|
|
532
|
+
}
|
|
462
533
|
function isIdentityPoseObject(transform) {
|
|
463
534
|
const pose = readPose(transform);
|
|
464
535
|
return pose.positionX === 0 && pose.positionY === 0 && pose.positionZ === 0
|
|
@@ -651,18 +722,31 @@ let Renderer3DSystem$1 = class Renderer3DSystem extends engine.System {
|
|
|
651
722
|
constructor() {
|
|
652
723
|
super(...arguments);
|
|
653
724
|
this.name = 'Renderer3DSystem';
|
|
725
|
+
this.adaptive = null;
|
|
726
|
+
this.pauseWhenHidden = true;
|
|
654
727
|
}
|
|
655
728
|
static { this.systemName = 'Renderer3DSystem'; }
|
|
656
729
|
init(params) {
|
|
657
730
|
const opts = params || {};
|
|
658
731
|
const width = opts.width || 750;
|
|
659
732
|
const height = opts.height || 1334;
|
|
733
|
+
const quality = engine.resolveRenderQuality(opts, { adaptivePixelRatio: true });
|
|
734
|
+
this.pauseWhenHidden = quality.pauseWhenHidden;
|
|
735
|
+
this.adaptive = new engine.AdaptivePixelRatio({
|
|
736
|
+
pixelRatio: quality.pixelRatio,
|
|
737
|
+
ceiling: quality.pixelRatioCeiling,
|
|
738
|
+
enabled: quality.adaptivePixelRatio,
|
|
739
|
+
});
|
|
660
740
|
this.threeContext = new ThreeContext({
|
|
661
741
|
canvas: opts.canvas,
|
|
662
742
|
container: opts.container,
|
|
663
743
|
width,
|
|
664
744
|
height,
|
|
665
|
-
antialias:
|
|
745
|
+
antialias: quality.antialias,
|
|
746
|
+
pixelRatio: quality.pixelRatio,
|
|
747
|
+
powerPreference: quality.powerPreference,
|
|
748
|
+
softShadows: opts.softShadows,
|
|
749
|
+
shadowMapSize: opts.shadowMapSize,
|
|
666
750
|
backgroundColor: opts.backgroundColor,
|
|
667
751
|
backgroundAlpha: opts.backgroundAlpha,
|
|
668
752
|
fog: opts.fog,
|
|
@@ -713,8 +797,17 @@ let Renderer3DSystem$1 = class Renderer3DSystem extends engine.System {
|
|
|
713
797
|
}
|
|
714
798
|
this.threeContext?.ensureTransformRoot(gameObject, transform);
|
|
715
799
|
}
|
|
716
|
-
lateUpdate() {
|
|
717
|
-
this.threeContext
|
|
800
|
+
lateUpdate(e) {
|
|
801
|
+
if (!this.threeContext)
|
|
802
|
+
return;
|
|
803
|
+
if (engine.shouldPauseRendering(this.pauseWhenHidden)) {
|
|
804
|
+
this.threeContext.update({ skipDraw: true });
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
const next = this.adaptive?.sample(e?.deltaTime ?? 0);
|
|
808
|
+
if (next != null)
|
|
809
|
+
this.threeContext.setPixelRatio(next);
|
|
810
|
+
this.threeContext.update();
|
|
718
811
|
}
|
|
719
812
|
resize(width, height) {
|
|
720
813
|
this.threeContext?.resize(width, height);
|
|
@@ -734,7 +827,7 @@ var Renderer3DSystem = Renderer3DSystem$1;
|
|
|
734
827
|
/** Auto-generated by scripts/build-package.mjs — do not edit. */
|
|
735
828
|
Object.assign(Renderer3DSystem, {
|
|
736
829
|
packageName: "@combos-fun/plugin-renderer-3d",
|
|
737
|
-
packageVersion: "0.1.
|
|
830
|
+
packageVersion: "0.1.14-beta.0",
|
|
738
831
|
});
|
|
739
832
|
|
|
740
833
|
class Renderer3D extends engine.System {
|
|
@@ -1395,6 +1488,47 @@ Light3DSystem = tslib.__decorate([
|
|
|
1395
1488
|
], Light3DSystem);
|
|
1396
1489
|
var Light3DSystem_default = Light3DSystem;
|
|
1397
1490
|
|
|
1491
|
+
const BASIS_TRANSCODER_PATH = 'https://unpkg.com/three@0.172.0/examples/jsm/libs/basis/';
|
|
1492
|
+
const COMPRESSED_EXTENSIONS = [
|
|
1493
|
+
'WEBGL_compressed_texture_astc',
|
|
1494
|
+
'EXT_texture_compression_bptc',
|
|
1495
|
+
'WEBGL_compressed_texture_s3tc',
|
|
1496
|
+
'WEBGL_compressed_texture_etc',
|
|
1497
|
+
];
|
|
1498
|
+
let sharedKtx2 = null;
|
|
1499
|
+
let detectedFor = null;
|
|
1500
|
+
function rendererSupportsCompressedTexture(renderer) {
|
|
1501
|
+
return COMPRESSED_EXTENSIONS.some((name) => renderer.extensions.has(name));
|
|
1502
|
+
}
|
|
1503
|
+
/** One Basis transcoder shared by glTF models and standalone KTX2 images. */
|
|
1504
|
+
function ensureKtx2Loader(renderer) {
|
|
1505
|
+
if (!sharedKtx2) {
|
|
1506
|
+
sharedKtx2 = new KTX2Loader_js.KTX2Loader();
|
|
1507
|
+
sharedKtx2.setTranscoderPath(BASIS_TRANSCODER_PATH);
|
|
1508
|
+
}
|
|
1509
|
+
if (renderer && detectedFor !== renderer) {
|
|
1510
|
+
sharedKtx2.detectSupport(renderer);
|
|
1511
|
+
detectedFor = renderer;
|
|
1512
|
+
}
|
|
1513
|
+
return sharedKtx2;
|
|
1514
|
+
}
|
|
1515
|
+
async function loadThreeKtx2(url, renderer) {
|
|
1516
|
+
if (!rendererSupportsCompressedTexture(renderer))
|
|
1517
|
+
return null;
|
|
1518
|
+
try {
|
|
1519
|
+
const texture = await ensureKtx2Loader(renderer).loadAsync(url);
|
|
1520
|
+
const compressed = texture;
|
|
1521
|
+
if (compressed?.isCompressedTexture && compressed.format !== three.RGBAFormat) {
|
|
1522
|
+
return texture;
|
|
1523
|
+
}
|
|
1524
|
+
texture?.dispose();
|
|
1525
|
+
return null;
|
|
1526
|
+
}
|
|
1527
|
+
catch {
|
|
1528
|
+
return null;
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1398
1532
|
async function requireNamedResource(name) {
|
|
1399
1533
|
const trimmed = String(name ?? '').trim();
|
|
1400
1534
|
if (!trimmed) {
|
|
@@ -1406,8 +1540,79 @@ async function requireNamedResource(name) {
|
|
|
1406
1540
|
}
|
|
1407
1541
|
return res;
|
|
1408
1542
|
}
|
|
1543
|
+
function htmlImage(data) {
|
|
1544
|
+
if (typeof HTMLImageElement !== 'undefined' && data instanceof HTMLImageElement)
|
|
1545
|
+
return data;
|
|
1546
|
+
return null;
|
|
1547
|
+
}
|
|
1548
|
+
function asArrayBuffer(data) {
|
|
1549
|
+
if (data instanceof ArrayBuffer)
|
|
1550
|
+
return data;
|
|
1551
|
+
if (ArrayBuffer.isView(data)) {
|
|
1552
|
+
const view = data;
|
|
1553
|
+
return view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
|
|
1554
|
+
}
|
|
1555
|
+
return null;
|
|
1556
|
+
}
|
|
1557
|
+
function loadHtmlImage(url) {
|
|
1558
|
+
return new Promise((resolve, reject) => {
|
|
1559
|
+
const image = new Image();
|
|
1560
|
+
image.crossOrigin = 'anonymous';
|
|
1561
|
+
image.onload = () => resolve(image);
|
|
1562
|
+
image.onerror = () => reject(new Error(`Failed to load image ${url}`));
|
|
1563
|
+
image.src = url;
|
|
1564
|
+
});
|
|
1565
|
+
}
|
|
1566
|
+
async function textureFromImageElement(image) {
|
|
1567
|
+
let source = image;
|
|
1568
|
+
if (typeof createImageBitmap === 'function') {
|
|
1569
|
+
try {
|
|
1570
|
+
source = await createImageBitmap(image);
|
|
1571
|
+
}
|
|
1572
|
+
catch {
|
|
1573
|
+
source = image;
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
const texture = new three.Texture(source);
|
|
1577
|
+
texture.colorSpace = three.SRGBColorSpace;
|
|
1578
|
+
texture.needsUpdate = true;
|
|
1579
|
+
if (typeof ImageBitmap !== 'undefined' && source instanceof ImageBitmap) {
|
|
1580
|
+
const bitmap = source;
|
|
1581
|
+
const dispose = texture.dispose.bind(texture);
|
|
1582
|
+
texture.dispose = () => {
|
|
1583
|
+
dispose();
|
|
1584
|
+
try {
|
|
1585
|
+
bitmap.close();
|
|
1586
|
+
}
|
|
1587
|
+
catch {
|
|
1588
|
+
// Already closed.
|
|
1589
|
+
}
|
|
1590
|
+
};
|
|
1591
|
+
}
|
|
1592
|
+
return texture;
|
|
1593
|
+
}
|
|
1594
|
+
async function loadCompressed(res, renderer) {
|
|
1595
|
+
if (!renderer)
|
|
1596
|
+
return null;
|
|
1597
|
+
const url = engine.compressedTextureUrl(res.src?.image);
|
|
1598
|
+
if (!url)
|
|
1599
|
+
return null;
|
|
1600
|
+
const buffer = asArrayBuffer(res.data?.image);
|
|
1601
|
+
if (!buffer)
|
|
1602
|
+
return loadThreeKtx2(url, renderer);
|
|
1603
|
+
const blobUrl = URL.createObjectURL(new Blob([buffer]));
|
|
1604
|
+
try {
|
|
1605
|
+
const texture = await loadThreeKtx2(blobUrl, renderer);
|
|
1606
|
+
if (texture)
|
|
1607
|
+
return texture;
|
|
1608
|
+
}
|
|
1609
|
+
finally {
|
|
1610
|
+
URL.revokeObjectURL(blobUrl);
|
|
1611
|
+
}
|
|
1612
|
+
return loadThreeKtx2(url, renderer);
|
|
1613
|
+
}
|
|
1409
1614
|
function textureFromResourceImage(res) {
|
|
1410
|
-
const image = res.data?.image;
|
|
1615
|
+
const image = htmlImage(res.data?.image);
|
|
1411
1616
|
if (!image) {
|
|
1412
1617
|
throw new Error(`Resource "${res.name}" has no image data; use RESOURCE_TYPE.IMAGE with src.image.url`);
|
|
1413
1618
|
}
|
|
@@ -1416,10 +1621,28 @@ function textureFromResourceImage(res) {
|
|
|
1416
1621
|
texture.needsUpdate = true;
|
|
1417
1622
|
return texture;
|
|
1418
1623
|
}
|
|
1419
|
-
async function
|
|
1624
|
+
async function textureFromResourceImageAsync(res, renderer) {
|
|
1625
|
+
const compressed = await loadCompressed(res, renderer);
|
|
1626
|
+
if (compressed)
|
|
1627
|
+
return compressed;
|
|
1628
|
+
const image = htmlImage(res.data?.image);
|
|
1629
|
+
if (image)
|
|
1630
|
+
return textureFromImageElement(image);
|
|
1631
|
+
const fallback = engine.imageFallbackUrl(res.src?.image);
|
|
1632
|
+
if (fallback) {
|
|
1633
|
+
try {
|
|
1634
|
+
return textureFromImageElement(await loadHtmlImage(fallback));
|
|
1635
|
+
}
|
|
1636
|
+
catch {
|
|
1637
|
+
// The message below reports the missing image.
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
throw new Error(`Resource "${res.name}" has no image data; use RESOURCE_TYPE.IMAGE with src.image.url`);
|
|
1641
|
+
}
|
|
1642
|
+
async function textureFromNamedImage(name, renderer) {
|
|
1420
1643
|
// Always build a Three.Texture from data.image so 2D Pixi
|
|
1421
1644
|
// registerInstance(IMAGE) is never overwritten or reused.
|
|
1422
|
-
return
|
|
1645
|
+
return textureFromResourceImageAsync(await requireNamedResource(name), renderer);
|
|
1423
1646
|
}
|
|
1424
1647
|
|
|
1425
1648
|
exports.COMBOS_GAME_OBJECT_ID = COMBOS_GAME_OBJECT_ID;
|
|
@@ -1438,17 +1661,21 @@ exports.Transform3D = Transform3D;
|
|
|
1438
1661
|
exports.Transform3DSystem = Transform3DSystem_default;
|
|
1439
1662
|
exports.VisualPoseBridge = VisualPoseBridge;
|
|
1440
1663
|
exports.copyPose = copyPose;
|
|
1664
|
+
exports.ensureKtx2Loader = ensureKtx2Loader;
|
|
1441
1665
|
exports.gameObjectIdFromIntersection = gameObjectIdFromIntersection;
|
|
1442
1666
|
exports.gameObjectIdFromObject3D = gameObjectIdFromObject3D;
|
|
1443
1667
|
exports.has3DChildGameObjects = has3DChildGameObjects;
|
|
1444
1668
|
exports.is3DSceneParent = is3DSceneParent;
|
|
1445
1669
|
exports.isIdentityPose = isIdentityPose;
|
|
1446
1670
|
exports.isTransform3DRoot = isTransform3DRoot;
|
|
1671
|
+
exports.loadThreeKtx2 = loadThreeKtx2;
|
|
1447
1672
|
exports.poseKeyOf = poseKeyOf;
|
|
1448
1673
|
exports.readPose = readPose;
|
|
1674
|
+
exports.rendererSupportsCompressedTexture = rendererSupportsCompressedTexture;
|
|
1449
1675
|
exports.requireNamedResource = requireNamedResource;
|
|
1450
1676
|
exports.seedTransform3D = seedTransform3D;
|
|
1451
1677
|
exports.tagObject3D = tagObject3D;
|
|
1452
1678
|
exports.textureFromNamedImage = textureFromNamedImage;
|
|
1453
1679
|
exports.textureFromResourceImage = textureFromResourceImage;
|
|
1680
|
+
exports.textureFromResourceImageAsync = textureFromResourceImageAsync;
|
|
1454
1681
|
//# sourceMappingURL=plugin-renderer-3d.cjs.js.map
|