Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sprite scaling causes incorrect positioning and rotation starting from r63 #8134

Closed
Marco-Sulla opened this issue Feb 13, 2016 · 16 comments
Closed

Comments

@Marco-Sulla
Copy link

This is a minimal testcase to show the problem:

r62:
https://jsfiddle.net/qqefadu8/4/

r63:
https://jsfiddle.net/qqefadu8/5/

As you can see, sprites in r62 are correctly positioned, and their rotation is correct. In r63, their position is weird, and they seems to rotate respect to a center that is far from the expected one.

It seems the problem is caused by sprite scaling. Indeed using sprite.scale.set(2,1,1.0); instead of sprite.scale.set(100,50,1.0); and a larger font size seems to relax the problem.

In tests I use an older version of TrackballControls, but the issue remains also using the newer one or OrbitControls. Furthermore the problem happens also using r74, of course.

browser: Firefox 41.0.2
OS: Linux (Lubuntu 14.04)
Graphic card: Intel HD Graphics 4400 (integrated in cpu Intel i5-4210U)

@WestLangley
Copy link
Collaborator

https://github.com/mrdoob/three.js/releases/tag/r63

If you need help, please use stackoverflow. But post your question using the current release of three.js. The r.62/63 versions are more than 2 years old.

@Marco-Sulla
Copy link
Author

Not a request of help. Code seems to be correct to me. Maybe I'm wrong, but IMHO it's a bug.

@Marco-Sulla
Copy link
Author

Furthermore I'm using r63 to show the point of the behaviour change, but this happens also with the latest release, of course.

@Marco-Sulla
Copy link
Author

Excuse me for multiple posts, I forgot to mention that the problem is probably caused by sprite scaling. I updated the first post.

@Marco-Sulla Marco-Sulla changed the title Sprite rotation and position is incorrect starting from r63 Sprite scaling causes incorrect positioning and rotation starting from r63 Feb 13, 2016
@mrdoob
Copy link
Owner

mrdoob commented Feb 14, 2016

I don't think the provided jsfiddles are clear enough. What's the issue exactly?

@Marco-Sulla
Copy link
Author

The sprite with the text "1" should be near the vertex, as in the r62 fiddle. But in r63 fiddle the sprite is quite distant.

Furthermore, if you rotate the camera, you see the "1" rotating around the cube vertex, while in r62 it rotates around its own upper left corner.

It seems the width of the sprite is considered much larger than its real content, once scaled. If you don't scale it, the behavior is correct.

@Marco-Sulla
Copy link
Author

Se this fiddle for example: https://jsfiddle.net/m08abjcL/
Here I'm using the latest three.js revision, but without scaling. I've simply resized the cube and used a larger font. Without scaling, sprite positioning is correct.

@WestLangley
Copy link
Collaborator

You are not setting the canvas size. It defaults to 300 x 150. It should be square to match the aspect ratio of the sprite.
https://jsfiddle.net/m08abjcL/1/
If you need further help, please use stackoverflow.

@Marco-Sulla
Copy link
Author

Nope, it does not fix the problem completely. I'll hope this will convince you this is a bug :)

See this fiddle:
https://jsfiddle.net/nwLLq8q1/

if you rotate the camera, the numbers seems to go inside and outside the grid. For example, if you rotate the camera so grid is in its normal position (blue grid behind, red one below and green on the left), numbers are on the left of the green grid. If you turn the camera 180°, you'll see the numbers on the left of the green grid again. But they should be on the right!

In r62 this problem does not happen:
https://jsfiddle.net/cjfwg2c4/

Note that I removed scaling and applied the resize of the canvas, as you suggested.

@WestLangley
Copy link
Collaborator

I have, again, modified your code to show you what is happening.

62: https://jsfiddle.net/cjfwg2c4/1/ - sprite upper left located at position
74: https://jsfiddle.net/nwLLq8q1/1/ - sprite center located at position

As stated in https://github.com/mrdoob/three.js/releases/tag/r63, sprite alignment was removed, and the sprite is now centered.

@MasterJames
Copy link
Contributor

BTW They are both rendered black on Mobile Android.

@Marco-Sulla
Copy link
Author

Uops, so I was wrong, this is not a bug after all. Excuse me.

@Ryan-Haines
Copy link

Ryan-Haines commented Jul 20, 2017

I don't understand how this is not a bug. I cannot wrap my head around why having a sprite change position when it is scaled is not a bug. Scaling a sprite should not alter it's position.

I've reached a real frustration point over this. I want to draw a line between two points, put a sprite at one of the points, and scale up the sprite, but I simply cannot achieve this because the sprite moves once scaling is applied!

I am setting the size of the canvas. I've read through this thread, and pretty much every stackoverflow post I could find, without finding any solution. Can anyone shed some light on the pattern to scale a sprite in place? This should be simple but is proving to be extremely difficult, and that this worked correctly in r62 as demonstrated by MarcoSulla is even more baffling.

@mrdoob
Copy link
Owner

mrdoob commented Jul 21, 2017

@Ryan-Haines Didn't @MarcoSulla said is not a bug on the post before yours?

@yiakwy
Copy link

yiakwy commented Feb 9, 2018

@mrdoob @WestLangley I thinks this should be a bug.

When you use

context.fillRect( 0, 0, canvas.width, canvas.height );

The canvas width is much lager than what people actually need. However you can change canvas.width to adjust the size. There is no solution to perfectly place the spritey without making twists on original correct position data (using shift)

@mrdoob
Copy link
Owner

mrdoob commented Feb 9, 2018

#12931 Added a center property to Sprite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants