Карты окружения с проекцией перспективы

From Valve Developer Community
< Ru
Jump to navigation Jump to search
English (en)Français (fr)Русский (ru)Translate (Translate)
Info content.png
This page needs to be translated.
This page either contains information that is only partially or incorrectly translated, or there isn't a translation yet.
If this page cannot be translated for some reason, or is left untranslated for an extended period of time after this notice is posted, the page should be requested to be deleted.
Also, please make sure the article complies with the alternate languages guide.(en)
Warning.pngВозможна путаница:Source For the third-party Source brush entity responsible for setting cubemap boundaries, use parallax_obb(en)
Source 2 For the Source 2 point entity responsible for both creating both the cubemap and parallax bounding box, use env_cubemap_box(en)
Отличие карты окружения с проекцией перспективы

Обзор

Исходная реализация карт окружения(en) в Source(en) не позволяет отслеживать перспективу взгляда из точки обзора игрока. Однако в большинстве случаев это возможно, и расчёт отражений из точки обзора игрока повысит реалистичность, особенно при использовании отражений большой размерности. Метод, позволяющий добиться этого, называется проекция перспективы.

Карты окружения с проекцией перспективы используют объём ограничивающего короба(en) для расчёта отражения на основе прилегающих поверхностей вокруг него и специальное оттенение(en) для его отображения.

Это руководство основано на материалах Брайана Чарльза. Демонстрационное видео доступно по ссылке: Кубические карты с проекцией перспективы в Source.

Игры, средства разработки и ветви Source, поддерживающие проекцию перспективы

Инструменты

Код

Сначала добавьте файл

в директорию <src code directory>/src/utils/vbsp/.

Next, open the project solution (everything.sln) and navigate to /utils/vbsp/cubemap.cpp.

cubemap.cpp

Добавьте следующий код после функции SideHasCubemapAndWasntManuallyReferenced( int iSide ):

char *g_pParallaxObbStrs[MAX_MAP_CUBEMAPSAMPLES];

Затем измените объявление функции CubeMap_InsertSample( ... ) следующим образом:

void Cubemap_InsertSample( const Vector &origin, int size, char *pParallaxObbStr = "" )

В начале кода функции, добавьте:

g_pParallaxObbStrs[g_nCubemapSamples] = pParallaxObbStr;

Обновите объявление функции PatchEnvmapForMaterialAndDependents( ... ) следующим образом:

static bool PatchEnvmapForMaterialAndDependents( const char *pMaterialName, const PatchInfo_t &info, const char *pCubemapTexture, const char *pParallaxObbMatrix = "" )

В условии if ( pDependentMaterial ) замените код на:

bDependentMaterialPatched = PatchEnvmapForMaterialAndDependents( pDependentMaterial, info, pCubemapTexture, pParallaxObbMatrix );

Замените

MaterialPatchInfo_t pPatchInfo[2];

на:

MaterialPatchInfo_t pPatchInfo[6];

Перед строкой

char pDependentPatchedMaterialName[1024];

добавьте:

// Parallax cubemap matrix
CUtlVector<char *> matRowList;
if ( pParallaxObbMatrix[0] != '\0' )
{
		V_SplitString( pParallaxObbMatrix, ";", matRowList );
	
	pPatchInfo[nPatchCount].m_pKey = "$envMapParallaxOBB1";
	pPatchInfo[nPatchCount].m_pValue = matRowList[0];
	++nPatchCount;
	pPatchInfo[nPatchCount].m_pKey = "$envMapParallaxOBB2";
	pPatchInfo[nPatchCount].m_pValue = matRowList[1];
	++nPatchCount;
	pPatchInfo[nPatchCount].m_pKey = "$envMapParallaxOBB3";
	pPatchInfo[nPatchCount].m_pValue = matRowList[2];
	++nPatchCount;
	pPatchInfo[nPatchCount].m_pKey = "$envMapOrigin";
	pPatchInfo[nPatchCount].m_pValue = matRowList[3];
	++nPatchCount;
}

В конце функции, замените:

CreateMaterialPatch( pMaterialName, pPatchedMaterialName, nPatchCount, pPatchInfo, PATCH_REPLACE );

на:

CreateMaterialPatch( pMaterialName, pPatchedMaterialName, nPatchCount, pPatchInfo, PATCH_INSERT );

Затем добавьте:

// Clean up parallax stuff
matRowList.PurgeAndDeleteElements();

Обновите объявление функции Cubemap_CreateTexInfo( ... ) следующим образом:

static int Cubemap_CreateTexInfo( int originalTexInfo, int origin[3], int cubemapIndex )

После

GeneratePatchedName( "c", info, false, pTextureName, 1024 );

добавьте:

// Append origin info if this cubemap has a parallax OBB
char originAppendedString[1024] = "";
	if ( g_pParallaxObbStrs[cubemapIndex][0] != '\0' )
{
		Q_snprintf( originAppendedString, 1024, "%s;[%d %d %d]", g_pParallaxObbStrs[cubemapIndex], origin[0], origin[1], origin[2] );
}

Замените

if ( !PatchEnvmapForMaterialAndDependents( pMaterialName, info, pTextureName ) )

на:

if ( !PatchEnvmapForMaterialAndDependents( pMaterialName, info, pTextureName, originAppendedString ) )

В функции Cubemap_FixupBrushSidesMaterials( void ), замените:

pSide->texinfo = Cubemap_CreateTexInfo( pSide->texinfo, g_CubemapSamples[cubemapID].origin );

на:

pSide->texinfo = Cubemap_CreateTexInfo( pSide->texinfo, g_CubemapSamples[cubemapID].origin, cubemapID );

В Cubemap_AttachDefaultCubemapToSpecularSides( void ), замените:

pSide->texinfo = Cubemap_CreateTexInfo( pSide->texinfo, g_CubemapSamples[iCubemap].origin );

на:

pSide->texinfo = Cubemap_CreateTexInfo( pSide->texinfo, g_CubemapSamples[iCubemap].origin, iCubemap );

This completes the changes for cubemap.cpp.

vbsp.h

Замените объявление:

void Cubemap_InsertSample( const Vector& origin, int size );

на:

void Cubemap_InsertSample( const Vector &origin, int size, char *pParallaxObbStr );

Above that, add:

extern char *g_pParallaxObbStrs[MAX_MAP_CUBEMAPSAMPLES];

This completes the changes for vbsp.h.

map.cpp

В самом начале, добавьте:

#include "matrixinvert.h"

В функции LoadEntityCallback( ... ), после:

const char *pSideListStr = ValueForKey( mapent, "sides" );

добавьте:

char *pParallaxObbStr = ValueForKey( mapent, "parallaxobb" );

Замените:

Cubemap_InsertSample( mapent->origin, size );

на:

Cubemap_InsertSample( mapent->origin, size, pParallaxObbStr );

После закрывающейся скобки условия if, добавьте:

//
// parallax_obb brushes are removed after the transformation matrix is found and saved into 
// the entity's data (ent will be removed after data transferred to patched materials)
//
	if ( !strcmp( "parallax_obb", pClassName ) )
{
	matrix3x4_t obbMatrix, invObbMatrix;
	SetIdentityMatrix( obbMatrix );
	SetIdentityMatrix( invObbMatrix );

		// Get corner and its 3 edges (scaled, local x, y, and z axes)
	mapbrush_t *brush = &mapbrushes[mapent->firstbrush];
	Vector corner, x, y, z;

		// Find first valid winding (with these whiles, if not enough valid windings then identity matrix is passed through to VMTs)
	int i = 0;
	while ( i < brush->numsides )
	{
			winding_t *wind = brush->original_sides[i].winding;
		if ( !wind )
		{
			i++;
			continue;
		}

		corner = wind->p[0];
		y = wind->p[1] - corner;
		z = wind->p[3] - corner;
			x = CrossProduct( y, z ).Normalized();

		i++;
		break;
	}

	// Skip second valid winding (opposite face from first, unusable for finding Z's length)
	while ( i < brush->numsides )
	{
		winding_t *wind = brush->original_sides[i].winding;
		if ( !wind )
		{
			i++;
			continue;
		}
		i++;
		break;
	}

	// Find third valid winding
	while ( i < brush->numsides )
	{
		winding_t *wind = brush->original_sides[i].winding;
		if ( !wind )
		{
			i++;
			continue;
		}

			// Find length of X
			// Start with diagonal, then scale X by the projection of diag onto X
		Vector diag = wind->p[0] - wind->p[2];
		x *= abs( DotProduct( diag, x ) );

		// Build transformation matrix (what is needed to turn a [0,0,0] - [1,1,1] cube into this brush)
		MatrixSetColumn( x, 0, obbMatrix );
		MatrixSetColumn( y, 1, obbMatrix );
		MatrixSetColumn( z, 2, obbMatrix );
		MatrixSetColumn( corner, 3, obbMatrix );

			// Find inverse (we need the world to local matrix, "transformationmatrix" is kind of a misnomer)
		MatrixInversion( obbMatrix, invObbMatrix );
		break;
	}
			
	char szMatrix[1024];
	Q_snprintf( szMatrix, 1024, "[%f %f %f %f];[%f %f %f %f];[%f %f %f %f]", invObbMatrix[0][0], invObbMatrix[0][1], invObbMatrix[0][2], invObbMatrix[0][3], invObbMatrix[1][0], invObbMatrix[1][1], invObbMatrix[1][2], invObbMatrix[1][3], invObbMatrix[2][0], invObbMatrix[2][1], invObbMatrix[2][2], invObbMatrix[2][3] );
	SetKeyValue( mapent, "transformationmatrix", szMatrix );

	return ( ChunkFile_Ok );
}

В функции LoadMapFile( ... ), после:

	g_LoadingMap->CheckForInstances( pszFileName );

добавьте:

	if ( g_MainMap == g_LoadingMap )
	{
// Fill out parallax obb matrix array
		for ( int i = 0; i < g_nCubemapSamples; i++ )
{
			if ( g_pParallaxObbStrs[i][0] != '\0' )
	{
				entity_t *obbEnt = EntityByName( g_pParallaxObbStrs[i] );
				g_pParallaxObbStrs[i] = ValueForKey( obbEnt, "transformationmatrix" );
	}
}

// Remove parallax_obb entities (in a nice slow linear search)
for ( int i = 0; i < g_MainMap->num_entities; i++ )
{
	entity_t *mapent = &g_MainMap->entities[i];
	const char *pClassName = ValueForKey( mapent, "classname" );
	if ( !strcmp( "parallax_obb", pClassName ) )
		{
			mapent->numbrushes = 0;
			mapent->epairs = NULL;
			}
		}
	}

This completes the changes for map.cpp.

The VBSP VBSP project should now compile without errors. If errors occur, review the changes for accuracy.

Once vbsp.exe is compiled, move it to the game's bin/ folder (not the mod's bin/; the game's - typically the same one where Hammer(en) is located).

Warning.pngПредупреждение: Steam(en) often replaces custom vbsp.exe with its original version whenever it verifies the file integrity of the game.
PlacementTip.pngОбходной приём: To prevent the loss of the custom vbsp.exe, rename it, then point to it in Hammer(en)'s game configuration.

Оттенения

The Parallax Corrected Cubemaps that are now patched into materials require a custom LightmappedGeneric(en) shader for proper display.

To edit shaders, open the everything.sln file.

Warning.pngПредупреждение: This section is the lengthiest and most tedious, due to the need to compile a custom LightmappedGeneric shader, which can take upwards of 30 minutes per compile, even on high-end computers.
Note.pngПримечание: It is recommended to follow the Shader Authoring(en) tutorial to become familiar with shader compilation. This tutorial assumes a basic understanding of that process.

C++ файлы оттенений

First, update the LightmappedGeneric shader to recognize the Parallax Corrected bounding boxes stored in the material.

lightmappedgeneric_dx9_helper.h

В конце структуры LightmappedGeneric_DX9_Vars_t (после переменной int m_nOutlineEnd1;), добавьте следующие строки:

    // Parallax cubemaps
    int m_nEnvmapParallaxObb1;
    int m_nEnvmapParallaxObb2;
    int m_nEnvmapParallaxObb3;
    int m_nEnvmapOrigin;

lightmappedgeneric_dx9.cpp

Добавьте следующие строки внтури блока:

BEGIN_SHADER_PARAMS

, после

SHADER_PARAM( OUTLINEEND1, SHADER_PARAM_TYPE_FLOAT, "0.0", "outer end value for outline")

следующие строки:

        // Parallax cubemaps
        SHADER_PARAM( ENVMAPPARALLAXOBB1, SHADER_PARAM_TYPE_VEC4, "[1 0 0 0]", "The first line of the parallax correction OBB matrix" )
        SHADER_PARAM( ENVMAPPARALLAXOBB2, SHADER_PARAM_TYPE_VEC4, "[0 1 0 0]", "The second line of the parallax correction OBB matrix" )
        SHADER_PARAM( ENVMAPPARALLAXOBB3, SHADER_PARAM_TYPE_VEC4, "[0 0 1 0]", "The third line of the parallax correction OBB matrix" )
        SHADER_PARAM( ENVMAPORIGIN, SHADER_PARAM_TYPE_VEC3, "[0 0 0]", "The world space position of the env_cubemap being corrected" )

В том же файле, внутри функции

void SetupVars( LightmappedGeneric_DX9_Vars_t& info )

в конце (после info.m_nOutlineEnd1 = OUTLINEEND1;), добавьте следуюшие строки:

        // Parallax cubemaps
        info.m_nEnvmapParallaxObb1 = ENVMAPPARALLAXOBB1;
        info.m_nEnvmapParallaxObb2 = ENVMAPPARALLAXOBB2;
        info.m_nEnvmapParallaxObb3 = ENVMAPPARALLAXOBB3;
        info.m_nEnvmapOrigin = ENVMAPORIGIN;

lightmappedgeneric_dx9_helper.cpp

Внутри

void InitParamsLightmappedGeneric_DX9( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, LightmappedGeneric_DX9_Vars_t &info )

добавьте после InitFloatParam( info.m_nOutlineAlpha, params, 1.0 ); следующие строки:

    // Parallax cubemaps
	// Cubemap parallax correction requires all 4 lines (if the 2nd, 3rd, or 4th are undef, undef the first one (checking done on first var)
	if ( !( params[info.m_nEnvmapParallaxObb2]->IsDefined() && params[info.m_nEnvmapParallaxObb3]->IsDefined() && params[info.m_nEnvmapOrigin]->IsDefined() ) )
    {
		params[info.m_nEnvmapParallaxObb1]->SetIntValue( 0 );
    }

В функции

void DrawLightmappedGeneric_DX9_Internal( ... )

, после

bool hasNormalMapAlphaEnvmapMask = IS_FLAG_SET( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK );

добавьте:

        // Parallax cubemaps
	bool hasParallaxCorrection = params[info.m_nEnvmapParallaxObb1]->GetType() == MATERIAL_VAR_TYPE_VECTOR;

В условии if ( g_pHardwareConfig->SupportsPixelShaders_2_b() ), добавьте перед

	SET_STATIC_PIXEL_SHADER( lightmappedgeneric_ps20b );

следующие строки:

// Parallax cubemaps enabled for 2_0b and onwards
SET_STATIC_PIXEL_SHADER_COMBO( PARALLAXCORRECT, hasParallaxCorrection );

In the else statement for the above, before SET_STATIC_PIXEL_SHADER( lightmappedgeneric_ps20 );, add:

// Parallax cubemaps
	SET_STATIC_PIXEL_SHADER_COMBO( PARALLAXCORRECT, 0 ); // No parallax cubemaps with ps_2_0 :(

Перед pContextData->m_SemiStaticCmdsOut.End();, добавьте:

            // Parallax cubemaps
            if ( hasParallaxCorrection )
            {
                pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 21, params[info.m_nEnvmapOrigin]->GetVecValue() );

                float *vecs[3];
                vecs[0] = const_cast<float *>( params[info.m_nEnvmapParallaxObb1]->GetVecValue() );
                vecs[1] = const_cast<float *>( params[info.m_nEnvmapParallaxObb2]->GetVecValue() );
                vecs[2] = const_cast<float *>( params[info.m_nEnvmapParallaxObb3]->GetVecValue() );
                float matrix[4][4];
                for ( int i = 0; i < 3; i++ )
                {
                    for ( int j = 0; j < 4; j++ )
                    {
                        matrix[i][j] = vecs[i][j];
                    }
                }
                matrix[3][0] = matrix[3][1] = matrix[3][2] = 0;
                matrix[3][3] = 1;
                pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 22, &matrix[0][0], 4 );
             }

Файлы оттенений

lightmappedgeneric_ps2_3_x.h

После строки

const float4 g_ShadowTweaks : register( c19 );

добавьте:

// Parallax cubemaps
#if ( PARALLAXCORRECT )
const float3 g_CubemapPos : register( c21 );
const float4x4 g_ObbMatrix : register( c22 ); // Through c25
#endif

Within the cubemap processing block, after fresnel = fresnel * g_OneMinusFresnelReflection + g_FresnelReflection;, add:

	// Parallax correction (2_0b and beyond)
	// Adapted from http://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
#if !( defined( SHADER_MODEL_PS_1_1 ) || defined( SHADER_MODEL_PS_1_4 ) || defined( SHADER_MODEL_PS_2_0 ) )
#if ( PARALLAXCORRECT )
        float3 worldPos = i.worldPos_projPosZ.xyz;
        float3 positionLS = mul( float4( worldPos, 1 ), g_ObbMatrix );
	float3 rayLS = mul( reflectVect, (float3x3)g_ObbMatrix );

        float3 firstPlaneIntersect = ( float3( 1.0f, 1.0f, 1.0f ) - positionLS ) / rayLS;
        float3 secondPlaneIntersect = ( -positionLS ) / rayLS;
        float3 furthestPlane = max( firstPlaneIntersect, secondPlaneIntersect );
        float distance = min( furthestPlane.x, min( furthestPlane.y, furthestPlane.z ) );

        // Use distance in WS directly to recover intersection
        float3 intersectPositionWS = worldPos + reflectVect * distance;
        reflectVect = intersectPositionWS - g_CubemapPos;
#endif
#endif

lightmappedgeneric_ps2x.fxc

После // STATIC: "FLASHLIGHT" "0..1" [ps20b] [XBOX], добавьте:

// STATIC: "PARALLAXCORRECT" "0..1"

После // SKIP ($DETAIL_BLEND_MODE == 11 ) && ($BUMPMAP != 0 ), добавьте:

// SKIP: $PARALLAXCORRECT && !$CUBEMAP
// SKIP: $PARALLAXCORRECT [ps20]

Компиляция оттенений

Ensure the following shaders are listed in stdshader_dx9_20b.txt (or the custom game file):

lightmappedgeneric_ps2x.fxc
lightmappedgeneric_ps11.fxc
lightmappedgeneric_vs20.fxc

Compile the shaders. This process may take a significant amount of time. Upon completion, the appropriate lightmappedgeneric_*.inc and lightmappedgeneric_*.vcs files will be generated. Copy these to the game's shaders/fxc/ folder.

With the new .inc(en) files, compile the game_shader_dx9.sln project. The resulting game_shader_dx9.dll should be copied to the game's bin/ folder, alongside the server and client DLLs.

Что дальше?

FGD-файл

parallaxcubes.fgd
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
@include "YOUR FGD HERE. THIS ONE WILL OVERRIDE THE REGULAR ENV_CUBEMAP ENTITY AND ALSO ADD THE PARALLAX_OBB ENTITY. ONLY INCLUDE THIS ONE IN HAMMER." @PointClass color(0 0 255) sidelist(sides) iconsprite("editor/env_cubemap.vmt") = env_cubemap : "An entity that creates a sample point for the Cubic Environment Map." [ cubemapsize(choices) : "Cubemap Size" : 0 = [ 0 : "Default" 1 : "1x1" 2 : "2x2" 3 : "4x4" 4 : "8x8" 5 : "16x16" 6 : "32x32" 7 : "64x64" 8 : "128x128" 9 : "256x256" ] sides(sidelist) : "Brush faces": : "(Optional) Brushes faces to directly attach to the env_cubemap. Press Pick then click on faces in the 3D View to select them. Use CTRL while clicking to add or remove from the selection." parallaxobb(target_destination) : "Cubemap Bounds" : : "(Optional) assigns this cubemap a bounding box for parallax correction (brush entity tied to parallax_obb)." ] @SolidClass = parallax_obb [ targetname(target_source) : "Name" : : "The name that other entities refer to this entity by." ]

(Необязательно) Проверка карты

Инструкцию о создании карт окружения с проекцией перспективы можно найти в статье Parallax Corrected Cubemaps/Creation(en).

Нужно сделать: Test Map coming soon

Заключение

At this point, the mod should be ready to launch with parallax-corrected cubemaps enabled.