Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics

Best way to get an objects direction into a material snippet?

A topic by ArtUnitFive created Sep 30, 2019 Views: 110 Replies: 1
Viewing posts 1 to 2
(2 edits)

Hello, first off thanks for the toolkit it's great.

Before I found the toolkit I was working on my own raymarcher. I learned a ton from it and got some pretty good results, but the toolkit does (mostly) everything I want to do and is far more performative and easy to use.

That said I had a few effects that I built in my own raymarcher that aren't present in the toolkit which I now want to port over.

Fortunately the workflow you've provided with snippets allows me to do this, but when it comes to unity programming (especially shaders) I am very much at the level of a monkey banging their head against the keyboard, so I'm struggling with a few bits.

In my raymarcher I had a simple specular highlight function that would add gloss based on the position of the scene's light (I could only support one light for my raymarched scene.) The code looks like this:

float3 applySpecular(float3 col, float3 ray, float shadow) {
    float3 reflected = - 2.0 * dot(, n) * n;
    float specular = max(dot(reflected, _lightDir), 0.0);
    specular = pow(specular, _specularHighlight);
    col += specular * _lightCol * shadow * _specularIntensity;
    return col;

_lightDir is a


of a light object which is passed to my raymarching shader as a float3

I experimented with the transform snippet input type but wasn't able to get a useful result.

So all of this is a long winded way of asking. Do you have any suggestions for how I might get that


Into my material snippet or use the resulting float4x4 from the transform input to extract the vector I need?


Found an answer, for anyone else looking:

float3 lightDir = normalize(;

The final code looks like this (Like I say I'm really new to shader programming so this may not be the 'right' way to do things but it does work)

In CustomIncludes.cginc

float4 applySpecular(in float3 n, in float4 col, float3 lightDir, float4 lightCol, float3 ray, float specularHighlight, float specularIntensity) {
  float3 reflected = - 2.0 * dot(, n) * n;   float specular = max(dot(reflected, lightDir), 0.0);
  specular = pow(specular, specularHighlight);
  col += specular * lightCol * specularIntensity;
  return col;

in Assets/snippets/materials/specular.asset

float3 lightDir = normalize(;
$color = applySpecular(normal, $color, lightDir, $lightCol, rayDir, $specularHighlight, $specularIntensity);
return $color;