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

Data Buffer Memory allocation And KHR_materials_emissive_strength cause I pushed it all... #11345

Merged
merged 7 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions dist/preview release/glTF2Interface/babylon.glTF2Interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,16 @@ declare module BABYLON.GLTF2 {
ior: number;
}

/**
* Interfaces from the KHR_materials_emissive_strength extension
* !!! Experimental Extension Subject to Changes !!!
*/

/** @hidden */
interface IKHRMaterialsEmissiveStrength extends IMaterialExtension {
emissiveStrength: number;
}

/**
* Interfaces from the KHR_materials_pbrSpecularGlossiness extension
*/
Expand Down
1 change: 1 addition & 0 deletions dist/preview release/what's new.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
- Added support for pure geometry files to OBJ loader ([Deltakosh](https://github.com/deltakosh))
- Added an observable for when loader state changed. ([bghgary](https://github.com/bghgary))
- Fixed an issue where errors for loading certain assets (e.g. <20-byte GLBs) are not catchable. ([bghgary](https://github.com/bghgary))
- Added support for `KHR_materials_emissive_strength` for glTF loader. ([sebavan](https://github.com/sebavan))

### Navigation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export class GLTFComponent extends React.Component<IGLTFComponentProps> {
<CheckBoxLineComponent label="KHR_draco_mesh_compression" isSelected={() => extensionStates["KHR_draco_mesh_compression"].enabled} onSelect={(value) => (extensionStates["KHR_draco_mesh_compression"].enabled = value)} />
<CheckBoxLineComponent label="KHR_materials_pbrSpecularGloss..." isSelected={() => extensionStates["KHR_materials_pbrSpecularGlossiness"].enabled} onSelect={(value) => (extensionStates["KHR_materials_pbrSpecularGlossiness"].enabled = value)} />
<CheckBoxLineComponent label="KHR_materials_clearcoat" isSelected={() => extensionStates["KHR_materials_clearcoat"].enabled} onSelect={(value) => (extensionStates["KHR_materials_clearcoat"].enabled = value)} />
<CheckBoxLineComponent label="KHR_materials_emissive_strength" isSelected={() => extensionStates["KHR_materials_emissive_strength"].enabled} onSelect={(value) => (extensionStates["KHR_materials_emissive_strength"].enabled = value)} />
<CheckBoxLineComponent label="KHR_materials_ior" isSelected={() => extensionStates["KHR_materials_ior"].enabled} onSelect={(value) => (extensionStates["KHR_materials_ior"].enabled = value)} />
<CheckBoxLineComponent label="KHR_materials_sheen" isSelected={() => extensionStates["KHR_materials_sheen"].enabled} onSelect={(value) => (extensionStates["KHR_materials_sheen"].enabled = value)} />
<CheckBoxLineComponent label="KHR_materials_specular" isSelected={() => extensionStates["KHR_materials_specular"].enabled} onSelect={(value) => (extensionStates["KHR_materials_specular"].enabled = value)} />
Expand Down
1 change: 1 addition & 0 deletions inspector/src/components/globalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class GlobalState {
KHR_mesh_quantization: { enabled: true },
KHR_materials_pbrSpecularGlossiness: { enabled: true },
KHR_materials_clearcoat: { enabled: true },
KHR_materials_emissive_strength: { enabled: true },
KHR_materials_ior: { enabled: true },
KHR_materials_sheen: { enabled: true },
KHR_materials_specular: { enabled: true },
Expand Down
67 changes: 67 additions & 0 deletions loaders/src/glTF/2.0/Extensions/KHR_materials_emissive_strength.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Nullable } from "babylonjs/types";
import { PBRMaterial } from "babylonjs/Materials/PBR/pbrMaterial";
import { Material } from "babylonjs/Materials/material";

import { IMaterial } from "../glTFLoaderInterfaces";
import { IGLTFLoaderExtension } from "../glTFLoaderExtension";
import { GLTFLoader } from "../glTFLoader";
import { IKHRMaterialsEmissiveStrength } from 'babylonjs-gltf2interface';

const NAME = "KHR_materials_emissive_strength";

/**
* [Experimental Spec](https://github.com/KhronosGroup/glTF/pull/1994)
*/
export class KHR_materials_emissive_strength implements IGLTFLoaderExtension {
/**
* The name of this extension.
*/
public readonly name = NAME;

/**
* Defines whether this extension is enabled.
*/
public enabled: boolean;

/**
* Defines a number that determines the order the extensions are applied.
*/
public order = 170;

private _loader: GLTFLoader;

/** @hidden */
constructor(loader: GLTFLoader) {
this._loader = loader;
this.enabled = this._loader.isExtensionUsed(NAME);
}

/** @hidden */
public dispose() {
(this._loader as any) = null;
}

/** @hidden */
public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material): Nullable<Promise<void>> {
return GLTFLoader.LoadExtensionAsync<IKHRMaterialsEmissiveStrength>(context, material, this.name, (extensionContext, extension) => {
const promises = new Array<Promise<any>>();
promises.push(this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial));
promises.push(this._loadEmissivePropertiesAsync(extensionContext, extension, babylonMaterial));
return Promise.all(promises).then(() => { });
sebavan marked this conversation as resolved.
Show resolved Hide resolved
});
}

private _loadEmissivePropertiesAsync(context: string, properties: IKHRMaterialsEmissiveStrength, babylonMaterial: Material): Promise<void> {
if (!(babylonMaterial instanceof PBRMaterial)) {
throw new Error(`${context}: Material type not supported`);
}

if (properties.emissiveStrength !== undefined) {
babylonMaterial.emissiveColor.scaleToRef(properties.emissiveStrength, babylonMaterial.emissiveColor);
}

return Promise.resolve();
}
}
sebavan marked this conversation as resolved.
Show resolved Hide resolved

GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_materials_emissive_strength(loader));
1 change: 1 addition & 0 deletions loaders/src/glTF/2.0/Extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from "./KHR_lights_punctual";
export * from "./KHR_materials_pbrSpecularGlossiness";
export * from "./KHR_materials_unlit";
export * from "./KHR_materials_clearcoat";
export * from "./KHR_materials_emissive_strength";
export * from "./KHR_materials_sheen";
export * from "./KHR_materials_specular";
export * from "./KHR_materials_ior";
Expand Down
5 changes: 3 additions & 2 deletions src/Buffers/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export class Buffer {
}
if (this._engine._releaseBuffer(this._buffer)) {
this._buffer = null;
this._data = null;
}
}
}
Expand Down Expand Up @@ -432,8 +433,8 @@ export class VertexBuffer {
const count = totalVertices * this.getSize();

if (this.type !== VertexBuffer.FLOAT || this.byteStride !== tightlyPackedByteStride) {
const copy: number[] = [];
this.forEach(count, (value) => copy.push(value));
const copy = new Float32Array(count);
this.forEach(count, (value, index) => copy[index] = value);
sebavan marked this conversation as resolved.
Show resolved Hide resolved
return copy;
}

Expand Down