Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: rendering #1346

Merged
merged 16 commits into from
May 23, 2024
55 changes: 44 additions & 11 deletions src/foundation/components/MeshRenderer/MeshRendererComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,63 @@ export class MeshRendererComponent extends Component {
const primitiveUids = primitives.map((primitive) => primitive.primitiveUid);
primitiveUids.push(-1);

renderPass._lastOpaqueIndex = primitives.length - 1;
renderPass._lastTransparentIndex = -1;
renderPass._firstTransparentSortKey = -1;
renderPass._lastTransparentSortKey = -1;
let _lastOpaqueIndex = primitives.length - 1;
let _lastTransparentIndex = -1;
let _firstTransparentSortKey = -1;
let _lastTransparentSortKey = -1;

for (let i = 0; i < primitives.length; i++) {
const primitive = primitives[i];
const bitOffset = PrimitiveSortKey_BitOffset_TranslucencyType + 1;
const isTranslucency = (primitive._sortkey >> bitOffset) & 1;
if (isTranslucency) {
renderPass._lastOpaqueIndex = i - 1;
renderPass._firstTransparentSortKey = primitive._sortkey;
_lastOpaqueIndex = i - 1;
_firstTransparentSortKey = primitive._sortkey;
break;
}
}

if (primitives.length > 0) {
renderPass._lastTransparentIndex = primitives.length - 1;
renderPass._lastTransparentSortKey = primitives[primitives.length - 1]._sortkey;
_lastTransparentIndex = primitives.length - 1;
_lastTransparentSortKey = primitives[primitives.length - 1]._sortkey;
}

let resultChanged = false;
if (_lastOpaqueIndex != renderPass._lastOpaqueIndex) {
renderPass._lastOpaqueIndex = _lastOpaqueIndex;
resultChanged ||= true;
}
if (_lastTransparentIndex != renderPass._lastTransparentIndex) {
renderPass._lastTransparentIndex = _lastTransparentIndex;
resultChanged ||= true;
}
if (_firstTransparentSortKey != renderPass._firstTransparentSortKey) {
renderPass._firstTransparentSortKey = _firstTransparentSortKey;
resultChanged ||= true;
}
if (_lastTransparentSortKey != renderPass._lastTransparentSortKey) {
renderPass._lastTransparentSortKey = _lastTransparentSortKey;
resultChanged ||= true;
}

if (primitiveUids.length !== renderPass._lastPrimitiveUids.length) {
resultChanged ||= true;
} else {
for (let i = _lastOpaqueIndex + 1; i < primitiveUids.length; i++) {
if (primitiveUids[i] !== renderPass._lastPrimitiveUids[i]) {
resultChanged ||= true;
break;
}
}
}
renderPass._isChangedSortRenderResult = resultChanged;

renderPass._lastPrimitiveUids = primitiveUids;

renderPass._lastTransformComponentsUpdateCount = TransformComponent.updateCount;
renderPass._lastCameraControllerComponentsUpdateCount = CameraControllerComponent.updateCount;
renderPass._lastPrimitiveUids = primitiveUids;
renderPass._lastSceneGraphComponentsUpdateCount = SceneGraphComponent.updateCount;
renderPass._renderedSomethingBefore = true;

return primitiveUids;
}
Expand Down Expand Up @@ -307,9 +340,9 @@ export class MeshRendererComponent extends Component {
processStage: ProcessStageEnum;
renderPassTickCount: Count;
primitiveUids: PrimitiveUID[];
}) {
}): boolean {
// Call common_$render of WebGLRenderingStrategy
MeshRendererComponent.__cgApiRenderingStrategy!.common_$render(
return MeshRendererComponent.__cgApiRenderingStrategy!.common_$render(
primitiveUids,
renderPass,
renderPassTickCount
Expand Down
14 changes: 13 additions & 1 deletion src/foundation/components/Transform/TransformComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class TransformComponent extends Component {
MutableQuaternion.fromCopyQuaternion(transform.rotationInner),
transform.matrixInner
);
TransformComponent.__updateCount++;
}

get localTransformRest() {
Expand All @@ -106,10 +107,12 @@ export class TransformComponent extends Component {
MutableQuaternion.fromCopyQuaternion(transform.rotationInner),
transform.matrixInner
);
TransformComponent.__updateCount++;
}

set localPosition(vec: IVector3) {
this.__pose.position = vec;
TransformComponent.__updateCount++;
}

/**
Expand All @@ -134,6 +137,7 @@ export class TransformComponent extends Component {
this.__rest = this.__pose.clone();
}
this.__rest.position = vec;
TransformComponent.__updateCount++;
}

/**
Expand All @@ -152,6 +156,7 @@ export class TransformComponent extends Component {

set localEulerAngles(vec: IVector3) {
this.__pose.eulerAngles = vec;
TransformComponent.__updateCount++;
}

/**
Expand All @@ -176,6 +181,7 @@ export class TransformComponent extends Component {
this.__rest = this.__pose.clone();
}
this.__rest.eulerAngles = vec;
TransformComponent.__updateCount++;
}

/**
Expand All @@ -194,6 +200,7 @@ export class TransformComponent extends Component {

set localScale(vec: IVector3) {
this.__pose.scale = vec;
TransformComponent.__updateCount++;
}

/**
Expand All @@ -218,6 +225,7 @@ export class TransformComponent extends Component {
this.__rest = this.__pose.clone();
}
this.__rest.scale = vec;
TransformComponent.__updateCount++;
}

/**
Expand All @@ -236,6 +244,7 @@ export class TransformComponent extends Component {

set localRotation(quat: IQuaternion) {
this.__pose.rotation = quat;
TransformComponent.__updateCount++;
}

/**
Expand All @@ -260,6 +269,7 @@ export class TransformComponent extends Component {
this.__rest = this.__pose.clone();
}
this.__rest.rotation = quat;
TransformComponent.__updateCount++;
}

/**
Expand All @@ -278,6 +288,7 @@ export class TransformComponent extends Component {

set localMatrix(mat: IMatrix44) {
this.__pose.matrix = mat;
TransformComponent.__updateCount++;
}

/**
Expand All @@ -302,6 +313,7 @@ export class TransformComponent extends Component {
this.__rest = this.__pose.clone();
}
this.__rest.matrix = mat;
TransformComponent.__updateCount++;
}

/**
Expand All @@ -326,7 +338,6 @@ export class TransformComponent extends Component {
if (this.__updateCountAtLastLogic !== this.__pose.updateCount) {
this.entity.tryToGetSceneGraph()!.setWorldMatrixDirty();
this.__updateCountAtLastLogic = this.__pose.updateCount;
TransformComponent.__updateCount++;
}
}

Expand All @@ -337,6 +348,7 @@ export class TransformComponent extends Component {
this.__rest = component.__rest.clone();
}
this.__updateCountAtLastLogic = component.__updateCountAtLastLogic;
TransformComponent.__updateCount++;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/foundation/geometry/Primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ export class Primitive extends RnObject {
return this.__primitives[primitiveUid];
}

static getPrimitiveCount() {
return this.__primitiveCount;
}

onAccessorUpdated(accessorVersion: number) {
this.__positionAccessorVersion = accessorVersion;
if (this.__mesh != null) {
Expand Down
33 changes: 31 additions & 2 deletions src/foundation/helpers/MaterialHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function createPbrUberMaterial({
(useNormalTexture ? '' : '-normalTexture');

let additionalShaderSemanticInfo: ShaderSemanticsInfo[] = [];
if (isMorphing) {
if (true) {
additionalShaderSemanticInfo = [
{
semantic: ShaderSemantics.DataTextureMorphOffsetPosition,
Expand Down Expand Up @@ -423,6 +423,35 @@ function createClassicUberMaterial({
(isSkinning ? '+skinning' : '') +
(isLighting ? '' : '-lighting');

const additionalShaderSemanticInfo = [
{
semantic: ShaderSemantics.DataTextureMorphOffsetPosition,
componentType: ComponentType.Int,
compositionType: CompositionType.ScalarArray,
arrayLength: Config.maxVertexMorphNumberInShader,
stage: ShaderType.VertexShader,
isCustomSetting: true,
soloDatum: true,
initialValue: new VectorN(new Int32Array(Config.maxVertexMorphNumberInShader)),
min: -Number.MAX_VALUE,
max: Number.MAX_VALUE,
needUniformInDataTextureMode: true,
},
{
semantic: ShaderSemantics.MorphWeights,
componentType: ComponentType.Float,
compositionType: CompositionType.ScalarArray,
arrayLength: Config.maxVertexMorphNumberInShader,
stage: ShaderType.VertexShader,
isCustomSetting: true,
soloDatum: true,
initialValue: new VectorN(new Float32Array(Config.maxVertexMorphNumberInShader)),
min: -Number.MAX_VALUE,
max: Number.MAX_VALUE,
needUniformInDataTextureMode: true,
},
];

const materialNode = new CustomMaterialContent({
name: 'ClassicUber',
isSkinning,
Expand All @@ -436,7 +465,7 @@ function createClassicUberMaterial({
vertexShaderWebGpu: ClassicSingleShaderVertexWebGpu,
pixelShaderWebGpu: ClassicSingleShaderFragmentWebgpu,
noUseCameraTransform: false,
additionalShaderSemanticInfo: [],
additionalShaderSemanticInfo,
});
materialNode.isSingleOperation = true;
const material = createMaterial(materialName, materialNode, maxInstancesNumber);
Expand Down
1 change: 1 addition & 0 deletions src/foundation/importer/Gltf2Importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ export class Gltf2Importer {

const materialVariant = {
materialObject: gltfJson.materials[mapping.material],
material: mapping.material,
variants: variants,
};

Expand Down
Loading
Loading