Home Unreal Engine

UDK Spherical World Shader - Question on Implementation

polycounter lvl 9
Offline / Send Message
Yog-Shoggoth polycounter lvl 9
Hi Folks,

Got a shader creation question for the UDK shader gurus on here. I'm busy teaching myself UDK and as an experiment I've been trying to implement a shader in the UDK material editor that would give the illusion of the game world being curved.

I found an example of a cylindrical world shader for Unity and tweaked that to create the effect I was wanting in Unity, see the web player here for an example of this in action https://dl.dropbox.com/u/108988/Test/Test.html (use W and S to scroll forwards/backwards).

However trying to translate this across to UDK is where I come slightly unstuck :(.

The actual shader code from Unity is this:
      CGPROGRAM
      #pragma vertex vert
      #pragma fragment frag
      #include "UnityCG.cginc"

      sampler2D _MainTex;
      float _Offset;

      struct Input {
      float4 vertex : POSITION;
      float2 texCoord: TEXCOORD;
      };

      struct Output
      {
      float4 vertex : SV_POSITION;
      float2 texCoord: TEXCOORD;
      };

      Output vert(Input v)
      {
      Output result;
      result.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
      result.vertex.y -= result.vertex.z * result.vertex.z *_Offset;
      result.vertex.y -= result.vertex.x * result.vertex.x *_Offset;
      result.texCoord = v.texCoord;
      return result;
      }

      fixed4 frag(Output o) : COLOR
      {
      return tex2D(_MainTex, o.texCoord);
      }

      ENDCG

With the help of online guides to CG etc. I've been able to decipher and understand what the various steps of the Unity shader are doing, at least at a basic/high-level.

I realise that to implement parts of the shader such as
result.vertex.z * result.vertex.z * _Offset
I'd be looking to use ComponentMask nodes to filter out the appropriate vertex/axis and then a Multiply node to perform the multiplication and the _Offset would be fed from a single value Constant node.

I think my real issue, at least at the moment, is determining what the equivalent of the UNITY_MATRIX_MVP would be in the UDK Material editor. I know that this is Unity's matrix for model*view*projection, but I'm uncertain of what combination of nodes in UDK would enable me to recreate that.

Hopefully once I have that it's just a case of deriving the various vertices and applying the appropriate transforms etc. Of course this is probably a gross oversimplification and it'll be a lot harder than that :\.

Obviously I'm not asking anyone to write the shader for me, just help nudge a UDK shader noob in the right direction etc.

Apologies for length, typos, stupidity etc.

Replies

  • ambershee
    Options
    Offline / Send Message
    ambershee polycounter lvl 17
    You don't have access to the view projection matrix in UDK, but you do have access to things like the camera vector. I would make some adjustments and work from that.
  • Yog-Shoggoth
    Options
    Offline / Send Message
    Yog-Shoggoth polycounter lvl 9
    ambershee wrote: »
    You don't have access to the view projection matrix in UDK, but you do have access to things like the camera vector. I would make some adjustments and work from that.

    Thanks for the info on that.

    I went away over the weekend and continued tinkering with the UDK material editor. I came up with the node setup shown in the attachment below which seems to roughly emulate the code
    result.vertex.y -= result.vertex.z * result.vertex.z *_Offset;
    
    from the Unity shader. Inasmuch as it moves/curves the object down on the Y-axis the further it is from the camera :).

    Now I just need to get the X-axis 'curvature' working as I'd like. My first attempt at just replicating the Y-axis setup with an X-axis filter and combining it with the Y-axis setup didn't quite work as expected. The object moved down/away in both axes but didn't have the slightly spherical distortion/warping you'd expect if it was moving on a curved surface.

    Guess some more tinkering is in order :\.
  • Yog-Shoggoth
    Options
    Offline / Send Message
    Yog-Shoggoth polycounter lvl 9

    Now I just need to get the X-axis 'curvature' working as I'd like. My first attempt at just replicating the Y-axis setup with an X-axis filter and combining it with the Y-axis setup didn't quite work as expected. The object moved down/away in both axes but didn't have the slightly spherical distortion/warping you'd expect if it was moving on a curved surface.

    Guess some more tinkering is in order :\.

    Scratch that, having played with the material in the actual engine on some basic BSP meshes it does give the effect I was looking for :).

    Don't think I'll break out the champagne just yet though, I'm sure there's still going to be some sort of issue with it :\.

    Now on to the other technical issues and combining this element of the material with everything else :).
  • tuxmask75
    Options
    Offline / Send Message
    tuxmask75 polycounter lvl 10
    This is sort of what I've been toying around with for the past few years for my game,

    another issue you will bump into:
    Lighting does not seem to work correctly with the shader. all lighting you see is baked into the texture than animated threw a flipbook.

    The only way I can think of having the lighting to work correctly would be to move the light source in 3D space to follow the vertex shader.
    [ame="http://www.youtube.com/watch?v=ORJn-0FoNu4"]Project AXSX: New years progress report! - YouTube[/ame]
  • ambershee
    Options
    Offline / Send Message
    ambershee polycounter lvl 17
    I would have thought dynamic lighting would still work fine?
  • tuxmask75
    Options
    Offline / Send Message
    tuxmask75 polycounter lvl 10
    Think about it like this, there is a light in the environment, than that object moves away from that light.
    In this case the vertex are moving to or away from a light source.
    This also screws the shadows as well.

    Unless there is a way to have the light effect some other aspect of the model and not the visual vertex that are in motion.
    or maybe render the light on one object and project it to the vertex visual mesh somehow.
    This problem has had me stumped for sometime.
  • davidonabus
    Options
    Offline / Send Message
    This looks promising, Tux!
  • mAlkAv!An
    Options
    Offline / Send Message
    mAlkAv!An polycounter lvl 5
    I know this might be long time obsolete, just want to point out that you can indeed access the view projection matrix in UDK materials.
    Just use a custom hlsl node with something like this: return mul(x, ViewProjectionMatrix);
Sign In or Register to comment.