Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by Rook3D · Nov 20, 2011 at 07:54 AM · shadergameobjectgraphics

SHADER: Get *object* position or distinct value per *object*

Is it possible to get the current Object's 'base' world position in a vertex function?

I was thinking something like this:

float3 baseWorldPos = mul ( _Object2World, float4(0,0,0,0) ).xyz;

...but on testing I'm fairly certain it's returning a zero vector ( because its a multiplication? ).

Otherwise is there another way to get a distinct value per object, so that a shader's effects can be made unique for each individual object?

Thanks.

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by mig · Mar 26, 2012 at 02:57 PM

To get the position, you need to transform float4(0,0,0,1).

Comment
Add comment · Show 2 · 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 Pawl · Jun 21, 2014 at 08:04 PM 0
Share

I just came here to say this works great, thanks!

avatar image Brenden-Frank · Oct 26, 2015 at 07:28 PM 0
Share

Am I missing something here? None of this appears to work (Unity 5.2 Deferred Rendering) float timeOffset = length(mul(_Object2World, float4(0,0,0,1)));

avatar image
0

Answer by tonyoakden2 · Oct 23, 2017 at 04:21 AM

I think the position is in the last row of the matrix and you can use the HLSL swizzle command to get it directly thus:

float3 baseWorldPos = unity_ObjectToWorld._m30_m31_m32;

Having said that it's quite possible the shader compiler optimizes the whatever the code anyway.

also it might be ObjectToWorld._m03_m13_m23; :)

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 defrummel · Aug 27, 2020 at 04:03 PM 0
Share

Its ObjectToWorld._m03_m13_m23

avatar image
0

Answer by defrummel · Aug 27, 2020 at 04:04 PM

float3 baseWorldPos = unity_ObjectToWorld._m03_m13_m23;

Comment
Add comment · 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
0

Answer by JLJac · Oct 19, 2020 at 08:08 AM

Hi, I'm trying to do this with deferred rendering and have a surprising issue - at certain camera angles the world pos (unity_ObjectToWorld._m03_m13_m_23) jumps to (0,0,0).

This confuses me, because to my understanding ObjectToWorld should be independent of the camera? I don't get why the transformation between object and world space would be affected by the projection matrix at all, to my understanding they should be completely separate.

The issue is not there in forward rendering mode.

If anyone with more insight could help explain that would be much appreciated!

Comment
Add comment · Show 2 · 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 sarahnorthway · Jul 19, 2021 at 05:05 AM 2
Share

Batching! As soon as 2+ objects are batched, unity_ObjectToWorld._m03_m13_m_23 becomes 0,0,0 (rather than say a point between the batched objects). Moving the camera can change the batches.

avatar image JLJac sarahnorthway · Jul 19, 2021 at 07:33 AM 0
Share

Yeaah that makes a lot of sense! Thank you!

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

12 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

Related Questions

Creating a Material from shader string 2 Answers

Always included shaders (Graphics settings) 1 Answer

Unity 5 Custom Deferred shader 0 Answers

Raymarching Bug - Rendering 3D texture in real-time 0 Answers

dynamic hole in layer / texture / camera 0 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