Skip to content

Commit

Permalink
Twist logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rexrainbow committed Aug 16, 2023
1 parent 1bf17ce commit c47e431
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
4 changes: 4 additions & 0 deletions examples/plane/project-matrix.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set main=./examples/plane/project-matrix.js
cd ..
cd ..
npm run watch
47 changes: 47 additions & 0 deletions examples/plane/project-matrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import phaser from 'phaser/src/phaser.js';
import PerspectiveImagePlugin from '../../plugins/perspectiveimage-plugin.js';

class Demo extends Phaser.Scene {
constructor() {
super({
key: 'examples'
})
}

preload() {
this.load.image('card', 'assets/images/card2.png');
}

create() {
var card = this.add.plane(400, 300, 'card').setScale(0.7)
// var card = this.add.rexPerspectiveImage(400, 300, 'card', null, { hideCCW: false }).setScale(0.7);
card.projectionMatrix
.translateXYZ(0, 2, 0)
.rotateX(Phaser.Math.DegToRad(-45));
}

update() {
}
}

var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
},
scene: Demo,
backgroundColor: 0x33333,
plugins: {
global: [{
key: 'rexPerspectiveImage',
plugin: PerspectiveImagePlugin,
start: true
}]
}
};

var game = new Phaser.Game(config);
11 changes: 6 additions & 5 deletions plugins/gameobjects/mesh/perspective/image/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ const GenerateGridVerts = Phaser.Geom.Mesh.GenerateGridVerts;
const RadToDeg = Phaser.Math.RadToDeg;
const DegToRad = Phaser.Math.DegToRad;

const FOV = 45;
const PanZ = 1 + (1 / Math.sin(DegToRad(FOV)));

class Image extends MeshBase {
constructor(scene, x, y, key, frame, config) {
if (IsPlainObject(x)) {
Expand All @@ -25,7 +22,6 @@ class Image extends MeshBase {
this.setSizeToFrame();

this.resetPerspective();
this.panZ(PanZ);
this.hideCCW = GetValue(config, 'hideCCW', true);

var gridWidth = GetValue(config, 'gridWidth', 0);
Expand Down Expand Up @@ -54,7 +50,12 @@ class Image extends MeshBase {
}

resetPerspective() {
this.setPerspective(this.width, this.height, FOV);
this.setPerspective(this.width, this.height);

var vFOV = this.fov * (Math.PI / 180);
this.viewPosition.z = (this.height / this.frame.height) / (Math.tan(vFOV / 2));
this.dirtyCache[10] = 1;

return this;
}

Expand Down

0 comments on commit c47e431

Please sign in to comment.