Skip to content

Commit

Permalink
Merge branch 'main' into grid
Browse files Browse the repository at this point in the history
  • Loading branch information
kpal81xd authored Dec 17, 2024
2 parents 5f608db + c58fe2f commit c2d9e35
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 12 deletions.
53 changes: 53 additions & 0 deletions src/extras/exporters/gltf-exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class GltfExporter extends CoreExporter {
cameras: [],
entities: [],
materials: [],
skins: [],
textures: [],

// entry: { node, meshInstances}
Expand Down Expand Up @@ -200,6 +201,11 @@ class GltfExporter extends CoreExporter {
if (buffers.indexOf(indexBuffer) < 0) {
buffers.push(indexBuffer);
}

// Collect skin
if (mesh.skin && resources.skins.indexOf(mesh.skin) < 0) {
resources.skins.push(mesh.skin);
}
});
};

Expand Down Expand Up @@ -472,6 +478,12 @@ class GltfExporter extends CoreExporter {
const entityMeshInstance = resources.entityMeshInstances.find(e => e.node === entity);
if (entityMeshInstance) {
node.mesh = resources.entityMeshInstances.indexOf(entityMeshInstance);

// Add skin reference if this node has a skinned mesh
const meshInstance = entityMeshInstance.meshInstances[0];
if (meshInstance && meshInstance.mesh.skin) {
node.skin = resources.skins.indexOf(meshInstance.mesh.skin);
}
}

if (entity.children.length > 0) {
Expand Down Expand Up @@ -622,6 +634,46 @@ class GltfExporter extends CoreExporter {
return primitive;
}

writeSkins(resources, json) {
if (resources.skins.length > 0) {
json.skins = resources.skins.map((skin) => {
// Create float32 array for inverse bind matrices
const matrices = new Float32Array(skin.inverseBindPose.length * 16);
for (let i = 0; i < skin.inverseBindPose.length; i++) {
const ibm = skin.inverseBindPose[i];
matrices.set(ibm.data, i * 16);
}

// Create buffer view for matrices
const matrixBuffer = matrices.buffer;
GltfExporter.writeBufferView(resources, json, matrixBuffer);
resources.buffers.push(matrixBuffer);
const bufferView = resources.bufferViewMap.get(matrixBuffer);

// Create accessor for inverse bind matrices
const accessor = {
bufferView: bufferView[0],
componentType: getComponentType(TYPE_FLOAT32),
count: skin.inverseBindPose.length,
type: 'MAT4'
};
const accessorIndex = json.accessors.push(accessor) - 1;

// Find joint nodes by bone names
const joints = skin.boneNames.map((boneName) => {
const node = resources.entities.find(entity => entity.name === boneName);
return resources.entities.indexOf(node);
});

// Create skin
return {
inverseBindMatrices: accessorIndex,
joints: joints
};
});
}
}

convertTextures(srcTextures, options) {

const textureOptions = {
Expand Down Expand Up @@ -761,6 +813,7 @@ class GltfExporter extends CoreExporter {
this.writeMeshes(resources, json, options);
this.writeMaterials(resources, json);
this.writeNodes(resources, json, options);
this.writeSkins(resources, json);
await this.writeTextures(resources, textureCanvases, json, options);

// delete unused properties
Expand Down
4 changes: 3 additions & 1 deletion src/framework/components/anim/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class AnimComponentSystem extends ComponentSystem {

cloneComponent(entity, clone) {
let masks;
// If the component animaites from the components entity, any layer mask hierarchy should be updated from the old entity to the cloned entity.
// If the component animates from the components entity, any layer mask hierarchy should be
// updated from the old entity to the cloned entity.
if (!entity.anim.rootBone || entity.anim.rootBone === entity) {
masks = {};
entity.anim.layers.forEach((layer, i) => {
Expand All @@ -122,6 +123,7 @@ class AnimComponentSystem extends ComponentSystem {
});
}
const data = {
enabled: entity.anim.enabled,
stateGraphAsset: entity.anim.stateGraphAsset,
animationAssets: entity.anim.animationAssets,
speed: entity.anim.speed,
Expand Down
2 changes: 2 additions & 0 deletions src/framework/components/joint/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class JointComponentSystem extends ComponentSystem {

initializeComponentData(component, data, properties) {
component.initFromData(data);

super.initializeComponentData(component, data, _schema);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/framework/components/screen/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ScreenComponentSystem extends ComponentSystem {

// queue up a draw order sync
component.syncDrawOrder();
super.initializeComponentData(component, data, properties);
super.initializeComponentData(component, data, _schema);
}

destroy() {
Expand Down
1 change: 1 addition & 0 deletions src/framework/components/zone/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ZoneComponentSystem extends ComponentSystem {

cloneComponent(entity, clone) {
const data = {
enabled: entity.zone.enabled,
size: entity.zone.size
};

Expand Down
14 changes: 10 additions & 4 deletions src/platform/graphics/texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
isIntegerPixelFormat, FILTER_NEAREST, TEXTURELOCK_NONE, TEXTURELOCK_READ,
TEXPROPERTY_MIN_FILTER, TEXPROPERTY_MAG_FILTER, TEXPROPERTY_ADDRESS_U, TEXPROPERTY_ADDRESS_V,
TEXPROPERTY_ADDRESS_W, TEXPROPERTY_COMPARE_ON_READ, TEXPROPERTY_COMPARE_FUNC, TEXPROPERTY_ANISOTROPY,
TEXPROPERTY_ALL, requiresManualGamma
TEXPROPERTY_ALL, requiresManualGamma,
pixelFormatInfo

} from './constants.js';
import { TextureUtils } from './texture-utils.js';
Expand Down Expand Up @@ -295,7 +296,7 @@ class Texture {
// track the texture
graphicsDevice.textures.push(this);

Debug.trace(TRACEID_TEXTURE_ALLOC, `Alloc: Id ${this.id} ${this.name}: ${this.width}x${this.height} ` +
Debug.trace(TRACEID_TEXTURE_ALLOC, `Alloc: Id ${this.id} ${this.name}: ${this.width}x${this.height} [${pixelFormatInfo.get(this.format)?.name}]` +
`${this.cubemap ? '[Cubemap]' : ''}` +
`${this.volume ? '[Volume]' : ''}` +
`${this.array ? '[Array]' : ''}` +
Expand Down Expand Up @@ -954,8 +955,13 @@ class Texture {
this._levelsUpdated[mipLevel] = true;
}

width = source.width;
height = source.height;
if (source instanceof HTMLVideoElement) {
width = source.videoWidth;
height = source.videoHeight;
} else {
width = source.width;
height = source.height;
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/scene/gsplat/gsplat.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ class GSplat {

// normalize
for (let j = 0; j < 15; ++j) {
c[j * 3 + 0] = Math.floor((c[j * 3 + 0] / max * 0.5 + 0.5) * t11 + 0.5);
c[j * 3 + 1] = Math.floor((c[j * 3 + 1] / max * 0.5 + 0.5) * t10 + 0.5);
c[j * 3 + 2] = Math.floor((c[j * 3 + 2] / max * 0.5 + 0.5) * t11 + 0.5);
c[j * 3 + 0] = Math.max(0, Math.min(t11, Math.floor((c[j * 3 + 0] / max * 0.5 + 0.5) * t11 + 0.5)));
c[j * 3 + 1] = Math.max(0, Math.min(t10, Math.floor((c[j * 3 + 1] / max * 0.5 + 0.5) * t10 + 0.5)));
c[j * 3 + 2] = Math.max(0, Math.min(t11, Math.floor((c[j * 3 + 2] / max * 0.5 + 0.5) * t11 + 0.5)));
}

// pack
Expand Down
2 changes: 1 addition & 1 deletion src/scene/materials/standard-material-options-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class StandardMaterialOptionsBuilder {
options.litOptions.ambientEncoding = null;
} else {
const envAtlas = stdMat.envAtlas || (stdMat.useSkybox && scene.envAtlas ? scene.envAtlas : null);
if (envAtlas) {
if (envAtlas && !stdMat.sphereMap) {
options.litOptions.ambientSource = 'envAtlas';
options.litOptions.ambientEncoding = envAtlas.encoding;
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/scene/shader-lib/chunks/gsplat/vert/gsplat.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ void main(void) {
vec3 modelCenter = readCenter(source);
SplatCenter center;
initCenter(source, modelCenter, center);
if (!initCenter(source, modelCenter, center)) {
gl_Position = discardVec;
return;
}
// project center to screen space
SplatCorner corner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mat3 quatToMat3(vec4 R) {
// read center
vec3 readCenter(SplatSource source) {
uint w = uint(textureSize(chunkTexture, 0).x) / 5;
uint w = uint(textureSize(chunkTexture, 0).x) / 5u;
uint chunkId = source.id / 256u;
ivec2 chunkUV = ivec2((chunkId % w) * 5u, chunkId / w);
Expand Down
9 changes: 9 additions & 0 deletions test/framework/entity.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ describe('Entity', function () {
}
});

for (const name in components) {
it(`clones the enabled state of ${name} components correctly`, function () {
const entity = new Entity('Test');
entity.addComponent(name, { enabled: false });
const clone = entity.clone();
expect(clone[name].enabled).to.equal(false);
});
}

it('clones an entity hierarchy', function () {
const root = new Entity('Test');
const child = new Entity('Child');
Expand Down

0 comments on commit c2d9e35

Please sign in to comment.