Search Unity

Lighting Issues When Using Custom Shader With ECS

Discussion in 'Entity Component System' started by tomweiland, Oct 30, 2019.

  1. tomweiland

    tomweiland

    Joined:
    Dec 15, 2017
    Posts:
    19
    I'm having a strange issue with one of my shaders. When I attach it to a regular GameObject, it works fine and all the lighting is correct, however when I assign the material to an entity's RenderMesh component, it displays fine, except for the lighting.

    The lighting seems to be calculated as though the light was a child of the entity and rotated with it. When the entity's rotation changes, the lit part of the object rotates with it, which should obviously not be the case with a non-rotating directional light. The only difference between the standard shader (which works fine with GameObjects and entities) and my shader (which works with GameObjects but not with entities) is this line in the fragment shader:
    input.normalWS = normalize(cross(ddy(input.cameraRelativeWorldPos.xyz), ddx(input.cameraRelativeWorldPos.xyz)));


    If I remove this line the lighting works fine, however I need this normal recalculation in order to achieve the desired effect.
    input.cameraRelativeWorldPos
    is calculated like this in the vertex shader:
    cameraRelativeWorldPos = (mul(unity_ObjectToWorld, float4(input.positionOS.xyz, 1.0)) - float4(_WorldSpaceCameraPos, 0.0)).xyz;


    Any help would be greatly appreciated, as the amount of information out there about this stuff is quite sparse!
     
    Beacom likes this.