44 * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License }
55 */
66
7- var SetTransform = require ( '../../../../src/renderer/canvas/utils/SetTransform' ) ;
7+ var CounterClockwise = require ( '../../../../src/math/angle/CounterClockwise' ) ;
8+ var RadToDeg = require ( '../../../../src/math/RadToDeg' ) ;
9+ var Wrap = require ( '../../../../src/math/Wrap' ) ;
810
911/**
1012 * Renders this Game Object with the Canvas Renderer to the given Camera.
@@ -29,27 +31,93 @@ var SpineGameObjectCanvasRenderer = function (renderer, src, interpolationPercen
2931 var skeleton = src . skeleton ;
3032 var skeletonRenderer = plugin . skeletonRenderer ;
3133
32- if ( ! skeleton || ! SetTransform ( renderer , context , src , camera , parentMatrix ) )
34+ var GameObjectRenderMask = 15 ;
35+
36+ var willRender = ! ( GameObjectRenderMask !== src . renderFlags || ( src . cameraFilter !== 0 && ( src . cameraFilter & camera . id ) ) ) ;
37+
38+ if ( ! skeleton || ! willRender )
3339 {
3440 return ;
3541 }
3642
37- skeletonRenderer . ctx = context ;
43+ var camMatrix = renderer . _tempMatrix1 ;
44+ var spriteMatrix = renderer . _tempMatrix2 ;
45+ var calcMatrix = renderer . _tempMatrix3 ;
3846
39- context . save ( ) ;
47+ spriteMatrix . applyITRS ( src . x , src . y , src . rotation , Math . abs ( src . scaleX ) , Math . abs ( src . scaleY ) ) ;
4048
41- skeletonRenderer . draw ( skeleton ) ;
49+ camMatrix . copyFrom ( camera . matrix ) ;
4250
43- if ( plugin . drawDebug || src . drawDebug )
51+ if ( parentMatrix )
52+ {
53+ // Multiply the camera by the parent matrix
54+ camMatrix . multiplyWithOffset ( parentMatrix , - camera . scrollX * src . scrollFactorX , - camera . scrollY * src . scrollFactorY ) ;
55+
56+ // Undo the camera scroll
57+ spriteMatrix . e = src . x ;
58+ spriteMatrix . f = src . y ;
59+
60+ // Multiply by the Sprite matrix, store result in calcMatrix
61+ camMatrix . multiply ( spriteMatrix , calcMatrix ) ;
62+ }
63+ else
4464 {
45- context . strokeStyle = '#00ff00' ;
46- context . beginPath ( ) ;
47- context . moveTo ( - 1000 , 0 ) ;
48- context . lineTo ( 1000 , 0 ) ;
49- context . moveTo ( 0 , - 1000 ) ;
50- context . lineTo ( 0 , 1000 ) ;
51- context . stroke ( ) ;
65+ spriteMatrix . e -= camera . scrollX * src . scrollFactorX ;
66+ spriteMatrix . f -= camera . scrollY * src . scrollFactorY ;
67+
68+ // Multiply by the Sprite matrix, store result in calcMatrix
69+ camMatrix . multiply ( spriteMatrix , calcMatrix ) ;
70+ }
71+
72+ skeleton . x = calcMatrix . tx ;
73+ skeleton . y = calcMatrix . ty ;
74+
75+ skeleton . scaleX = calcMatrix . scaleX ;
76+
77+ // Inverse or we get upside-down skeletons
78+ skeleton . scaleY = calcMatrix . scaleY * - 1 ;
79+
80+ if ( src . scaleX < 0 )
81+ {
82+ skeleton . scaleX *= - 1 ;
83+
84+ src . root . rotation = RadToDeg ( calcMatrix . rotationNormalized ) ;
5285 }
86+ else
87+ {
88+ // +90 degrees to account for the difference in Spine vs. Phaser rotation
89+ src . root . rotation = Wrap ( RadToDeg ( CounterClockwise ( calcMatrix . rotationNormalized ) ) + 90 , 0 , 360 ) ;
90+ }
91+
92+ if ( src . scaleY < 0 )
93+ {
94+ skeleton . scaleY *= - 1 ;
95+
96+ if ( src . scaleX < 0 )
97+ {
98+ src . root . rotation -= ( RadToDeg ( calcMatrix . rotationNormalized ) * 2 ) ;
99+ }
100+ else
101+ {
102+ src . root . rotation += ( RadToDeg ( calcMatrix . rotationNormalized ) * 2 ) ;
103+ }
104+ }
105+
106+ if ( camera . renderToTexture )
107+ {
108+ skeleton . y = calcMatrix . ty ;
109+ skeleton . scaleY *= - 1 ;
110+ }
111+
112+ // Add autoUpdate option
113+ skeleton . updateWorldTransform ( ) ;
114+
115+ skeletonRenderer . ctx = context ;
116+ skeletonRenderer . debugRendering = ( plugin . drawDebug || src . drawDebug ) ;
117+
118+ context . save ( ) ;
119+
120+ skeletonRenderer . draw ( skeleton ) ;
53121
54122 context . restore ( ) ;
55123} ;
0 commit comments