|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2020 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Clamp = require('../../../../src/math/Clamp'); |
| 8 | +var CounterClockwise = require('../../../../src/math/angle/CounterClockwise'); |
| 9 | +var GetCalcMatrix = require('../../../../src/gameobjects/GetCalcMatrix'); |
| 10 | +var RadToDeg = require('../../../../src/math/RadToDeg'); |
| 11 | +var Wrap = require('../../../../src/math/Wrap'); |
| 12 | + |
| 13 | +/** |
| 14 | + * Directly renders this Game Object with the WebGL Renderer to the given Camera. |
| 15 | + * The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera. |
| 16 | + * This method should not be called directly. It is a utility function of the Render module. |
| 17 | + * |
| 18 | + * @method SpineGameObject#renderDirect |
| 19 | + * @since 3.50.0 |
| 20 | + * @private |
| 21 | + * |
| 22 | + * @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer. |
| 23 | + * @param {SpineGameObject} src - The Game Object being rendered in this call. |
| 24 | + * @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object. |
| 25 | + * @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested |
| 26 | + * @param {SpineContainer} [container] - If this Spine object is in a Spine Container, this is a reference to it. |
| 27 | + */ |
| 28 | +var SpineGameObjectWebGLDirect = function (renderer, src, camera, parentMatrix, container) |
| 29 | +{ |
| 30 | + var plugin = src.plugin; |
| 31 | + var skeleton = src.skeleton; |
| 32 | + var sceneRenderer = plugin.sceneRenderer; |
| 33 | + |
| 34 | + // flush + clear previous pipeline if this is a new type |
| 35 | + renderer.pipelines.clear(); |
| 36 | + |
| 37 | + sceneRenderer.begin(); |
| 38 | + |
| 39 | + var scrollFactorX = src.scrollFactorX; |
| 40 | + var scrollFactorY = src.scrollFactorY; |
| 41 | + var alpha = skeleton.color.a; |
| 42 | + |
| 43 | + if (container) |
| 44 | + { |
| 45 | + src.scrollFactorX = container.scrollFactorX; |
| 46 | + src.scrollFactorY = container.scrollFactorY; |
| 47 | + |
| 48 | + skeleton.color.a = Clamp(alpha * container.alpha, 0, 1); |
| 49 | + } |
| 50 | + |
| 51 | + var calcMatrix = GetCalcMatrix(src, camera, parentMatrix).calc; |
| 52 | + |
| 53 | + var viewportHeight = renderer.height; |
| 54 | + |
| 55 | + skeleton.x = calcMatrix.tx; |
| 56 | + skeleton.y = viewportHeight - calcMatrix.ty; |
| 57 | + |
| 58 | + skeleton.scaleX = calcMatrix.scaleX; |
| 59 | + skeleton.scaleY = calcMatrix.scaleY; |
| 60 | + |
| 61 | + if (src.scaleX < 0) |
| 62 | + { |
| 63 | + skeleton.scaleX *= -1; |
| 64 | + |
| 65 | + // -180 degrees to account for the difference in Spine vs. Phaser rotation when inversely scaled |
| 66 | + src.root.rotation = Wrap(RadToDeg(calcMatrix.rotationNormalized) - 180, 0, 360); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + // +90 degrees to account for the difference in Spine vs. Phaser rotation |
| 71 | + src.root.rotation = Wrap(RadToDeg(CounterClockwise(calcMatrix.rotationNormalized)) + 90, 0, 360); |
| 72 | + } |
| 73 | + |
| 74 | + if (src.scaleY < 0) |
| 75 | + { |
| 76 | + skeleton.scaleY *= -1; |
| 77 | + |
| 78 | + if (src.scaleX < 0) |
| 79 | + { |
| 80 | + src.root.rotation -= (RadToDeg(calcMatrix.rotationNormalized) * 2); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + src.root.rotation += (RadToDeg(calcMatrix.rotationNormalized) * 2); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /* |
| 89 | + if (renderer.currentFramebuffer !== null) |
| 90 | + { |
| 91 | + skeleton.y = calcMatrix.ty; |
| 92 | + skeleton.scaleY *= -1; |
| 93 | + } |
| 94 | + */ |
| 95 | + |
| 96 | + skeleton.updateWorldTransform(); |
| 97 | + |
| 98 | + // Draw the current skeleton |
| 99 | + |
| 100 | + sceneRenderer.drawSkeleton(skeleton, src.preMultipliedAlpha); |
| 101 | + |
| 102 | + if (container) |
| 103 | + { |
| 104 | + src.scrollFactorX = scrollFactorX; |
| 105 | + src.scrollFactorY = scrollFactorY; |
| 106 | + skeleton.color.a = alpha; |
| 107 | + } |
| 108 | + |
| 109 | + if (plugin.drawDebug || src.drawDebug) |
| 110 | + { |
| 111 | + // Because if we don't, the bones render positions are completely wrong (*sigh*) |
| 112 | + var oldX = skeleton.x; |
| 113 | + var oldY = skeleton.y; |
| 114 | + |
| 115 | + skeleton.x = 0; |
| 116 | + skeleton.y = 0; |
| 117 | + |
| 118 | + sceneRenderer.drawSkeletonDebug(skeleton, src.preMultipliedAlpha); |
| 119 | + |
| 120 | + skeleton.x = oldX; |
| 121 | + skeleton.y = oldY; |
| 122 | + } |
| 123 | + |
| 124 | + // The next object in the display list is not a Spine Game Object or Spine Container, so we end the batch |
| 125 | + sceneRenderer.end(); |
| 126 | + |
| 127 | + // And rebind the previous pipeline |
| 128 | + renderer.pipelines.rebind(); |
| 129 | +}; |
| 130 | + |
| 131 | +module.exports = SpineGameObjectWebGLDirect; |
0 commit comments