Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by stulleman · Oct 09, 2014 at 02:22 PM · vector3floatefficiencycast

IntVector2 Struct useful?

Hey guys,

I'm developing a game and I'm trying to optimize it.
Because I have a terrain (minecraft like), I don't need Float Vectors to store the positions.
What I've done now is something like this

 public struct IntVector2 {
         public int x, y;
 
         public IntVector2(int x, int y) {
             this.x = x;
             this.y = y;
         }
 
         public static IntVector2 operator +(IntVector2 a, IntVector2 b) {
             return new IntVector2(a.x + b.x, a.y + b.y);
         }
 
         public static IntVector2 operator +(IntVector2 a, Vector2 b) {
             return new IntVector2(a.x + (int)b.x, a.y + (int)b.y);
         }
 
         public static IntVector2 operator -(IntVector2 a, IntVector2 b) {
             return new IntVector2(a.x - b.x, a.y - b.y);
         }
 
         public static IntVector2 operator -(IntVector2 a, Vector2 b) {
             return new IntVector2(a.x - (int)b.x, a.y - (int)b.y);
         }
 
         public static IntVector2 operator *(IntVector2 a, int b) {
             return new IntVector2(a.x * b, a.y * b);
         }
 }

The Problem is that meshes need Vector3's as vertices. This means I have to cast everything back to Vector3's. And that seems very ineffective. Now I wanted to ask how you would solve something like this? Any suggestions?

Thanks!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Venceremos · Oct 09, 2014 at 02:29 PM 1
Share

Since gpu uses 32 bits floating points as the vertex positions, you don't need to implement integer vector, it won't make performance advantages. I think.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Owen-Reynolds · Oct 09, 2014 at 04:52 PM

This is an "it depends" no-correct-answer time vs. space optimization problem. Yes, sometimes using a lot less space, but being a little slower to use it (converting to float) will be a big gain. Merely using less space will speed up a program (but do phones also have L1 caches?) Yes, that is the sort of data structure built for real programs. But it depends.

How long are floats and ints on the target platform? Can you use short int instead? short unsigned int? Just how large is the terrain and how much are you saving (if it shrinks from 100K to 50K, who cares?) Does your game run too slowly now?

Can you make an even more efficient data structure (similar to a heightmap)? What operations will you be performing most (so you can organize to make them run faster)?

Then, you could probably make a quick test in an hour ("play with" each position over and over, using both methods, and time it.)

I think the most common solution is to go with the simplest method (Vector3s,) but do the usual bit of putting a wrapper around it. In other words, everyone calls Vector3 getHeight(x,y) instead of reading straight from H[x,y]. Standard encapsulation. Then, if you really need to redo how terrain words, it won't be too bad.

But I use Vector3's even when I know y is always 0, just because it's easier and runs fast enough.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Oct 09, 2014 at 04:58 PM 0
Share

Floats are always 4 bytes and ints are always 4 bytes, on all platforms.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot cast from source type to destination type. Can't figure it out!! 1 Answer

get Pitch and Roll values from object 3 Answers

Why camera.velocity is not consistant? 1 Answer

How to make Vector3s communicate with Floats? 2 Answers

How to cast Vector3 on a plane to get Vector2? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges