Skip to content

Commit

Permalink
Fix NME preview (#14774)
Browse files Browse the repository at this point in the history
  • Loading branch information
Popov72 committed Feb 8, 2024
1 parent c398f5c commit 58b5801
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ export class HeightToNormalBlock extends NodeMaterialBlock {

const heightToNormal = `
vec4 heightToNormal(in float height, in vec3 position, in vec3 tangent, in vec3 normal) {
${startCode}
${this.automaticNormalizationTangent ? "tangent = normalize(tangent);" : ""}
${this.automaticNormalizationNormal ? "normal = normalize(normal);" : ""}
${startCode}
vec3 worlddX = dFdx(position);
vec3 worlddY = dFdy(position);
vec3 crossX = cross(normal, worlddX);
Expand Down
11 changes: 11 additions & 0 deletions packages/dev/core/src/Meshes/mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2786,6 +2786,17 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
this.setVerticesData(VertexBuffer.NormalKind, data, (<VertexBuffer>this.getVertexBuffer(VertexBuffer.NormalKind)).isUpdatable());
}

// Tangents
if (this.isVerticesDataPresent(VertexBuffer.TangentKind)) {
data = <FloatArray>this.getVerticesData(VertexBuffer.TangentKind);
for (index = 0; index < data.length; index += 4) {
Vector3.TransformNormalFromFloatsToRef(data[index], data[index + 1], data[index + 2], transform, temp)
.normalize()
.toArray(data, index);
}
this.setVerticesData(VertexBuffer.TangentKind, data, (<VertexBuffer>this.getVertexBuffer(VertexBuffer.TangentKind)).isUpdatable());
}

// flip faces?
if (transform.determinant() < 0) {
this.flipFaces();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,20 +420,21 @@ export class PreviewManager {
const bakeTransformation = (mesh: Mesh) => {
mesh.bakeCurrentTransformIntoVertices();
mesh.refreshBoundingInfo();
mesh.parent = null;
};

if (this._globalState.mode === NodeMaterialModes.Material) {
switch (this._globalState.previewType) {
case PreviewType.Box:
SceneLoader.AppendAsync("https://assets.babylonjs.com/meshes/", "roundedCube.glb", this._scene).then(() => {
bakeTransformation(this._scene.meshes[1] as Mesh);
bakeTransformation(this._scene.getMeshByName("__root__")!.getChildMeshes(true)[0] as Mesh);
this._meshes.push(...this._scene.meshes);
this._prepareScene();
});
return;
case PreviewType.Sphere:
SceneLoader.AppendAsync("https://assets.babylonjs.com/meshes/", "previewSphere.glb", this._scene).then(() => {
bakeTransformation(this._scene.meshes[1] as Mesh);
bakeTransformation(this._scene.getMeshByName("__root__")!.getChildMeshes(true)[0] as Mesh);
this._meshes.push(...this._scene.meshes);
this._prepareScene();
});
Expand All @@ -459,7 +460,7 @@ export class PreviewManager {
return;
case PreviewType.Plane: {
SceneLoader.AppendAsync("https://assets.babylonjs.com/meshes/", "highPolyPlane.glb", this._scene).then(() => {
bakeTransformation(this._scene.meshes[1] as Mesh);
bakeTransformation(this._scene.getMeshByName("__root__")!.getChildMeshes(true)[0] as Mesh);
this._meshes.push(...this._scene.meshes);
this._prepareScene();
});
Expand Down

0 comments on commit 58b5801

Please sign in to comment.