Passing a float3 from script?

Node-based Shader Editor

Passing a float3 from script?

Postby UndertoneFX_dj » Fri Mar 01, 2019 12:32 am

he leading questions is... can I and if so how do I pass a float3 from c# to a shader? I think this will solve my problem.

Here's the whole story:

I have a character made up of several interchangable models. Each of the models has a pivot that don't necessarily align. I want to design a shader that has a treatment toward the bottom of the model set.

Here's a picture of a test case:

uv projection.jpg
example screenshot
uv projection.jpg (174.9 KiB) Viewed 1697 times
T

I got it working on a pair of test shapes (on the left), a sphere and a cylinder. I just pass vertex position in to the UV's, and they have an aligned projected texture through the whole set, sweet!

But... on the right... is the scenario I'm working with. It's multiple models with variant offsets. And the character can move around the level and rotate, etc... But because the models have different positions, compared to the parent "Empty", the uv's are not coherent, so what's at the bottom of one of the meshes doesn't line up with what's at the bottom of the next piece.

So... to solve this, I'm imagining I can use a script to just pass the position of the parent "Empty" node, and use that as an offset (added to world position node). But I don't know if/how a shader can take in external data like that from script. Is that possible?
UndertoneFX_dj
 
Posts: 1
Joined: Fri Mar 01, 2019 12:21 am

Re: Passing a float3 from script?

Postby Amplify_Borba » Fri Mar 01, 2019 4:59 pm

Hello, thank you for getting in touch and for your support!

It's fairly common for object position data to be passed into a shader property through script. Our Smear sample provides a great example on how you can use a script in this manner and also manipulate the mesh through the Local Vertex port for creating the smear effect as it moves from one point to another. You can also refer to the ForceShield sample.

If you experiment using a Vertex Position node, which contains data for the vertices position in object space, and Add a Float3 value to it, you can see its vertices being properly offset from its origin to the given coordinates, as a simple example.

Do note that you need to set your node Type to Property, Instanced Property or Global in order to pass data to it via script:

Property - the value becomes available in the properties of the material that uses the shader and can be changed in the material inspector or by script.

Instanced Property - the value can only be set by script and this defines the shader as an instanced shader.

Global - the value can only be set by script and this defines a static variable that is shared between all shaders that use it. It's useful to change a value globally.

Image

Please let me know if this helps, thanks!
Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Amplify_Borba
 
Posts: 1239
Joined: Mon Jul 24, 2017 9:50 am


Return to Amplify Shader Editor

Who is online

Users browsing this forum: No registered users and 8 guests