|
|
|
|
|
|
float ADD_IDX(SampleHeightmap)(LayerTexCoord layerTexCoord) |
|
|
|
{ |
|
|
|
#ifdef _HEIGHTMAP |
|
|
|
return (SAMPLE_TEXTURE2D(ADD_IDX(_HeightMap), ADD_IDX(sampler_HeightMap), ADD_IDX(layerTexCoord.base).uv).r - ADD_IDX(_HeightCenter)) * ADD_IDX(_HeightAmplitude); |
|
|
|
return (SAMPLE_TEXTURE2D(ADD_IDX(_HeightMap), ADD_ZERO_IDX(sampler_HeightMap), ADD_IDX(layerTexCoord.base).uv).r - ADD_IDX(_HeightCenter)) * ADD_IDX(_HeightAmplitude); |
|
|
|
#else |
|
|
|
return 0.0; |
|
|
|
#endif |
|
|
|
|
|
|
float2 uv = ADD_IDX(layerTexCoord.base).uv; |
|
|
|
|
|
|
|
// Compute lod as we will sample inside a lop (so can't use regular sampling) |
|
|
|
float lod = CALCULATE_TEXTURE2D_LOD(ADD_IDX(_HeightMap), ADD_IDX(sampler_HeightMap), uv); |
|
|
|
float lod = CALCULATE_TEXTURE2D_LOD(ADD_IDX(_HeightMap), ADD_ZERO_IDX(sampler_HeightMap), uv); |
|
|
|
float prevHeight = SAMPLE_TEXTURE2D_LOD(ADD_IDX(_HeightMap), ADD_IDX(sampler_HeightMap), ADD_IDX(layerTexCoord.base).uv + texOffsetCurrent, lod).r * ADD_IDX(_HeightAmplitude); |
|
|
|
float prevHeight = SAMPLE_TEXTURE2D_LOD(ADD_IDX(_HeightMap), ADD_ZERO_IDX(sampler_HeightMap), uv + texOffsetCurrent, lod).r; |
|
|
|
float currHeight = SAMPLE_TEXTURE2D_LOD(ADD_IDX(_HeightMap), ADD_IDX(sampler_HeightMap), ADD_IDX(layerTexCoord.base).uv + texOffsetCurrent, lod).r * ADD_IDX(_HeightAmplitude); |
|
|
|
float currHeight = SAMPLE_TEXTURE2D_LOD(ADD_IDX(_HeightMap), ADD_ZERO_IDX(sampler_HeightMap), uv + texOffsetCurrent, lod).r; |
|
|
|
float rayHeight = 1.0 - stepSize; // Start at top less one sample |
|
|
|
|
|
|
|
// Linear search |
|
|
|
|
|
|
texOffsetCurrent += texOffsetPerStep; |
|
|
|
|
|
|
|
// Sample height map which in this case is stored in the alpha channel of the normal map: |
|
|
|
currHeight = SAMPLE_TEXTURE2D_LOD(ADD_IDX(_HeightMap), ADD_IDX(sampler_HeightMap), ADD_IDX(layerTexCoord.base).uv + texOffsetCurrent, lod).r * ADD_IDX(_HeightAmplitude); |
|
|
|
currHeight = SAMPLE_TEXTURE2D_LOD(ADD_IDX(_HeightMap), ADD_ZERO_IDX(sampler_HeightMap), uv + texOffsetCurrent, lod).r; |
|
|
|
float t0 = rayHeight + stepSize; |
|
|
|
float t1 = rayHeight; |
|
|
|
float delta0 = t0 - prevHeight; |
|
|
|
float delta1 = t1 - currHeight; |
|
|
|
|
|
|
|
/* |
|
|
|
float t0 = rayHeight + stepSize; |
|
|
|
float t1 = rayHeight; |
|
|
|
float delta0 = t0 - prevHeight; |
|
|
|
float delta1 = t1 - currHeight; |
|
|
|
*/ |
|
|
|
|
|
|
|
float delta0 = currHeight - rayHeight; |
|
|
|
float delta1 = (rayHeight + stepSize) - prevHeight; |
|
|
|
float ratio = delta0 / (delta0 + delta1); |
|
|
|
float2 offset = (ratio) * (texOffsetCurrent - texOffsetPerStep) + (1.0 - ratio) * texOffsetCurrent; |
|
|
|
|
|
|
|
// Apply offset |
|
|
|
// Apply offset only on base. Details could use another mapping and will not be consistant... |
|
|
|
// Don't know if this will still ok. |
|
|
|
// TODO: check with artists |
|
|
|
ADD_IDX(layerTexCoord.details).uv += offset; |
|
|
|
} |
|
|
|
|
|
|
|
// Return opacity |
|
|
|