Texture proiettiva e illuminazione differita


10

Nella mia domanda precedente , ho chiesto se è possibile realizzare trame proiettive con illuminazione differita. Ora (più di sei mesi dopo) ho un problema con la mia implementazione della stessa cosa. Sto cercando di applicare questa tecnica nel passaggio della luce. (il mio proiettore non influenza l'albedo). Ho questo proiettore Visualizza una matrice di proiezione:

Matrix projection = Matrix.CreateOrthographicOffCenter(-halfWidth * Scale, halfWidth * Scale, -halfHeight * Scale, halfHeight * Scale, 1, 100000);
Matrix view       = Matrix.CreateLookAt(Position, Target, Vector3.Up);

Dove halfWidthed halfHeightè metà della larghezza e dell'altezza della trama, Positionè la posizione targetdel proiettore ed è l'obiettivo del proiettore. Questo sembra essere ok. Sto disegnando quad a schermo intero con questo shader:

float4x4 InvViewProjection;

texture2D DepthTexture;
texture2D NormalTexture;
texture2D ProjectorTexture;

float4x4 ProjectorViewProjection;

sampler2D depthSampler = sampler_state {
    texture = <DepthTexture>;
    minfilter = point;
    magfilter = point;
    mipfilter = point;
};

sampler2D normalSampler = sampler_state {
    texture = <NormalTexture>;
    minfilter = point;
    magfilter = point;
    mipfilter = point;
};

sampler2D projectorSampler = sampler_state {
    texture = <ProjectorTexture>;
    AddressU  = Clamp;
    AddressV  = Clamp;
};

float viewportWidth;
float viewportHeight;

// Calculate the 2D screen position of a 3D position
float2 postProjToScreen(float4 position) {
    float2 screenPos = position.xy / position.w;
    return 0.5f * (float2(screenPos.x, -screenPos.y) + 1);
}

// Calculate the size of one half of a pixel, to convert
// between texels and pixels
float2 halfPixel() {
    return 0.5f / float2(viewportWidth, viewportHeight);
}

struct VertexShaderInput {
    float4 Position : POSITION0;
};

struct VertexShaderOutput {
    float4 Position :POSITION0;
    float4 PositionCopy : TEXCOORD1;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input) {
    VertexShaderOutput output;
    output.Position = input.Position;
    output.PositionCopy=output.Position;
    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 {
    float2 texCoord =postProjToScreen(input.PositionCopy) + halfPixel();

    // Extract the depth for this pixel from the depth map
    float4 depth = tex2D(depthSampler, texCoord);
    //return float4(depth.r,0,0,1);
    // Recreate the position with the UV coordinates and depth value
    float4 position;
    position.x = texCoord.x * 2 - 1;
    position.y = (1 - texCoord.y) * 2 - 1;
    position.z = depth.r;
    position.w = 1.0f;

    // Transform position from screen space to world space
    position = mul(position, InvViewProjection);
    position.xyz /= position.w;
    //compute projection
    float3 projection=tex2D(projectorSampler,postProjToScreen(mul(position,ProjectorViewProjection)) + halfPixel());
    return float4(projection,1);
}

Nella prima parte del pixel shader viene recuperata la posizione dal buffer G (questo codice che sto usando in altri shader senza alcun problema) e quindi viene trasformato nello spazio di proiezione del proiettore. Il problema è che la proiezione non appare. Ecco un'immagine della mia situazione:

immagine del problema

Le linee verdi sono il frustum del proiettore renderizzato. Dove è nascosto il mio errore? Sto usando XNA 4. Grazie per il consiglio e scusate il mio inglese.

MODIFICARE:

Lo shader sopra funziona ma la proiezione era troppo piccola. Quando ho modificato la proprietà Scala su un valore elevato (ad es. 100), viene visualizzata la proiezione. Ma quando la telecamera si sposta verso la proiezione, la proiezione si espande, come si vede in questo video di YouTube .

Risposte:


5

Nel tuo shader, dovrai rimuovere position.xyz /= w;poiché questo è ciò che sta causando il tuo problema di ridimensionamento:

// Transform position from screen space to world space 
position = mul(position, InvViewProjection); 
position.xyz /= position.w;  <<-- Comment this out

Questo dovrebbe fare il trucco.


0

Se si ottengono risultati troppo piccoli e si arriva a quella dimensione, potrebbe essere perché il frustum è ancora nello spazio da 1 a -1 o da 0 a 1 o simili? È il tuo origo su cui si basa il frustum?

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.