JSFL:检测元素何时被翻转 [英] JSFL: Detecting when an Element has been flipped

查看:222
本文介绍了JSFL:检测元素何时被翻转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在JSFL中编写导出程序,将Flash动画导出为可以在自定义播放器中重播的格式。导出器基本上遍历时间轴和每个关键帧的所有元素,并写出元素的名称,位置,旋转,缩放和本地偏移量。这些被读入自定义播放器,它将数据馈送给一个精灵引擎来重新创建每一帧动画。



我想要做的是检测一个元素已被翻转(即在Flash中选择元素(符号),然后修改 - >变换 - >翻转水平),以便导出器也可以包含该信息,从而允许玩家的精灵引擎翻转纹理来复制Flash中发生的事情。这对于(比方说)使用一个符号来表示一个角色的右手是有用的,只是将它翻转为左手,而不是创建一个全新的符号。

<不幸的是,我看不到任何找到这些信息的方法。我所获得的关于元素的信息都没有显示出任何形式的翻转。我如何检测翻转?如果算法不能完成的话,我会为动画师们决定手动指出一个符号已经被翻转了(通过创建一些插件来给它们一个用setPersistentData() ),例如),但我不知道如何制作这种插件。帮助!

解决方案

scaleX不起作用,它似乎总是给一个正面价值。最后,我不得不对矩阵做出这个答案。这是基于这样一个想法,即我们知道矩阵总是包含一个2D旋转,并且我们知道角度,所以我们可以摆脱矩阵元素的旋转,只留下规模。这太可怕了,但它似乎工作。

此外,旋转有时出现为NaN,特别是当元素已翻转。使用skewX的价值似乎工作的东西,你知道没有倾斜,但我希望我的出口商能够处理倾斜的元素,所以我认为这可能是在这里的另一个问题的基础。

  var rotationRadians; 
if(isNaN(someElement.rotation)){
rotationRadians = someElement.skewX * Math.PI / 180;
}
else {
rotationRadians = someElement.rotation * Math.PI / 180;
}

var sinRot = Math.sin(rotationRadians);
var cosRot = Math.cos(rotationRadians);
var SOME_EPSILON = 0.01;
var flipScaleX,flipScaleY;

if(Math.abs(cosRot)< SOME_EPSILON){
//避免被零除。我们可以使用正弦和矩阵的其他两个元素来代替。
flipScaleX =(someElement.matrix.b / sinRot);
flipScaleY =(someElement.matrix.c / -sinRot);
}
else {
flipScaleX = someElement.matrix.a / cosRot;
flipScaleY = someElement.matrix.d / cosRot;



$ b $ p $如果水平翻转,flipScaleX出现在〜-1,如果不是-1 。如果flipScaleY垂直翻转,则为-1;如果不是,则为-1。

I'm writing an exporter in JSFL, to export Flash animations into a format that can be replayed in a custom player. The exporter basically iterates through the timeline and through all the elements at each keyframe, and writes out the element's name, position, rotation, scale and a local offset. These are read into the custom player which feeds the data to a sprite engine to recreate each frame of the animation.

What I want to be able to do is detect whether a given Element has been flipped (i.e. in Flash you Select the element (a symbol), then Modify->Transform->Flip Horizontal) so that the exporter can include that information too, allowing the sprite engine in the player to flip the UVs of the texture to replicate what's happening in Flash. This would be useful for (say) using one symbol for a character's right hand, and just flipping it to be their left hand, rather than having to create a whole new symbol.

Unfortunately I can't see any way of finding this information out. None of the information I have available for the Elements seems to imply that any kind of flipping has occurred. How can I detect flipping? If it can't be done algorithmically, I'd settle for the animator having to manually indicate that a symbol had been flipped (by creating some kind of plugin that gives them a tick-box which writes a value into the Element with setPersistentData(), for example), but I don't know how to make that sort of plugin either. Help!

解决方案

scaleX doesn't work, it always seems to give a positive value. In the end I had to do this to the matrix to get the answer out. This is based on the idea that we know the matrix always contains a 2D rotation, and we know the angle, so we can get rid of the rotation from the matrix elements to just leave us with the scale. It's horrible but it seems to work.

Also, rotation sometimes comes out as NaN, particularly when the element has been flipped. using the value of skewX seems to work for things which you know aren't skewed, but I want my exporter to be able to handle skewed elements, so I think this might be the basis for another question here.

var rotationRadians;
if(isNaN(someElement.rotation)) {
    rotationRadians = someElement.skewX * Math.PI / 180;
}
else {
    rotationRadians = someElement.rotation * Math.PI / 180;
}   

var sinRot = Math.sin(rotationRadians);
var cosRot = Math.cos(rotationRadians);
var SOME_EPSILON = 0.01;
var flipScaleX, flipScaleY;

if(Math.abs(cosRot) <  SOME_EPSILON) {
    // Avoid divide by zero. We can use sine and the other two elements of the matrix instead.
    flipScaleX = (someElement.matrix.b / sinRot);
    flipScaleY = (someElement.matrix.c / -sinRot);
}
else {
    flipScaleX = someElement.matrix.a / cosRot;
    flipScaleY = someElement.matrix.d / cosRot;
}

flipScaleX comes out at ~-1 if it's flipped horizontally, ~1 if not. flipScaleY is ~-1 if it's flipped vertically, ~1 if not.

这篇关于JSFL:检测元素何时被翻转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆