Skip to content

Commit

Permalink
Fix Background Texture Error (#446)
Browse files Browse the repository at this point in the history
* refactor: fix background texture bug
  • Loading branch information
gz65555 authored Aug 20, 2021
1 parent 33415c6 commit ab0fde6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
24 changes: 12 additions & 12 deletions packages/core/src/Background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,24 @@ export class Background {

switch (this._textureFillMode) {
case BackgroundTextureFillMode.Fill:
positions[0].setValue(-1, -1, -1);
positions[1].setValue(1, -1, -1);
positions[2].setValue(-1, 1, -1);
positions[3].setValue(1, 1, -1);
positions[0].setValue(-1, -1, 1);
positions[1].setValue(1, -1, 1);
positions[2].setValue(-1, 1, 1);
positions[3].setValue(1, 1, 1);
break;
case BackgroundTextureFillMode.AspectFitWidth:
const fitWidthScale = 1 / ((this._texture.width * (height / width)) / height);
positions[0].setValue(-1, -fitWidthScale, -1);
positions[1].setValue(1, -fitWidthScale, -1);
positions[2].setValue(-1, fitWidthScale, -1);
positions[3].setValue(1, fitWidthScale, -1);
positions[0].setValue(-1, -fitWidthScale, 1);
positions[1].setValue(1, -fitWidthScale, 1);
positions[2].setValue(-1, fitWidthScale, 1);
positions[3].setValue(1, fitWidthScale, 1);
break;
case BackgroundTextureFillMode.AspectFitHeight:
const fitHeightScale = (this._texture.width * (height / width)) / height;
positions[0].setValue(-fitHeightScale, -1, -1);
positions[1].setValue(fitHeightScale, -1, -1);
positions[2].setValue(-fitHeightScale, 1, -1);
positions[3].setValue(fitHeightScale, 1, -1);
positions[0].setValue(-fitHeightScale, -1, 1);
positions[1].setValue(fitHeightScale, -1, 1);
positions[2].setValue(-fitHeightScale, 1, 1);
positions[3].setValue(fitHeightScale, 1, 1);
break;
}
_backgroundTextureMesh.setPositions(positions);
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { RenderState } from "./shader/state/RenderState";
import { Texture2D, TextureCubeFace, TextureCubeMap, TextureFormat } from "./texture";
import { PhysicsManager } from "./PhysicsManager";
import { ModelMesh, PrimitiveMesh } from "./mesh";
import { CompareFunction } from "./shader";

/** TODO: delete */
const engineFeatureManager = new FeatureManager<EngineFeature>();
Expand Down Expand Up @@ -190,6 +191,7 @@ export class Engine extends EventDispatcher {

this._backgroundTextureMaterial = new Material(this, Shader.find("background-texture"));
this._backgroundTextureMaterial.isGCIgnored = true;
this._backgroundTextureMaterial.renderState.depthState.compareFunction = CompareFunction.LessEqual;

this._backgroundTextureMesh = PrimitiveMesh.createPlane(this, 2, 2, 1, 1, false);
this._backgroundTextureMesh.isGCIgnored = true;
Expand Down

0 comments on commit ab0fde6

Please sign in to comment.