Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How can I get the camera vector in worldspace in a material?

I wanted to create a material that would give me the angle in radians in X and Y between the camera and the object the material is applied to. (I wanted this as a basis for a effect that would change depending on whether I looked to the left, right, top, or bottom of the object).

I'm not a programmer so I wrote down the calculations I thought I'd need in order to get this to work. Basically I figured I would need the WSposition of the object and the WSposition of the camera in order to get the WSvector from the camera to the object. And I would need the WSvector of the camera. Then I could just just normalize those vectors and use a asin to get radians for both of them in WS, and subtract one from the other in order to get the angle in rad between the cameraforward and the direction of the object.

Problem is, apparently for some reason Camera vector gives your the vector of the camera compared to the normal of each pixel? And I have no idea what "cameradirectionvector" does but not what I thought it would.

Is there a way to get the rotation of the camera in a material blueprint or is there something I'm missing? I'm guessing I might be able to do it by making it a dynamic material and driving a few parameters through blueprints but since you directly have the "cameravector" node in the material blueprint editor I figured it should logically be possible there as well, or is there a reason that's not possible?

Any help would be greatly appreciated

Product Version: UE 4.19
more ▼

asked Oct 30 '18 at 01:11 PM in Blueprint Scripting

avatar image

6 2 2

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

There are multiple nodes in material editor dealing with camera position and "Camera vector" is not what you need, you should use "Camera Position" which gives you the camera position in World Space and also for getting your object position you should use "Actor Position" node instead of "Absolute World Position". Hope it helps

more ▼

answered Oct 30 '18 at 02:23 PM

avatar image

42 1 3 9

avatar image S.g.m.Klessens Oct 30 '18 at 02:28 PM

Thanks for the reply, I did use the actor position for the object itself, and that works just fine, and camera position is great for getting the position of the camera. But I also need to know the direction the camera is looking at and the position doesn't tell me that. I figured camera vector would, but from what I can tell that gives me the camera vector in local pixel space.

(I know I don't know the official terms, trying to improvise).

avatar image mosssi Oct 30 '18 at 02:57 PM

Could you describe what you want to achieve with an image?

avatar image S.g.m.Klessens Oct 30 '18 at 03:18 PM

Sure, I wanted to learn how to work with custom nodes for shaders in unreal. I figured out how to do some basic glsl shader programming with shadertoy last week and I figured it would be a good exercise to try and get that to work using custom blueprints.

The shader was a simple emoticon that would look left/right/up/down depending on the position of your mouse. I couldn't really do that in standard unreal (with the fps character) since your mouse is basically locked at the center of your screen, but since it was just a test that didn't really matter, I just needed two values between -1 and 1 that would drive that particular variable.

I figured that I would drive the variable by where you were aiming at (where the camera is pointing towards). So if the material was on a plane, and you looked to the right of the plane, the emoticon would look in the proper direction.

I thought the easiest way to do this would be to take the vector from the camera position to the object position, and the forward vector of the camera. If I had those two, it would be easy to figure out the relative rotation of the camera vector in radians using "asin". Which I could in turn use to drive the emoticon.

alt text

smiley.png (754.4 kB)
avatar image mosssi Oct 30 '18 at 03:43 PM

Actually I'm not much expert at shaders but I can suggest two other methods that you may find them useful. There is a "Transform Vector" node that can convert different spaces to each other. Also you can drive any material parameter that you want from blueprints. So you can calculate your data and feed it in the material as a simple "Scalar Parameter" (Set Scalar Parameter)

avatar image S.g.m.Klessens Oct 30 '18 at 04:00 PM

Yeah I think driving them through regular blueprints with a dynamic material should work, just wondering how I could get it to work in just the material.

I'll look into Transform Vector, thanks a lot for the help.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question