From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Graphics/CubemapProcessor.cpp | 515 ++++++++++++++++++++++++++++++++++ 1 file changed, 515 insertions(+) create mode 100644 Runtime/Graphics/CubemapProcessor.cpp (limited to 'Runtime/Graphics/CubemapProcessor.cpp') diff --git a/Runtime/Graphics/CubemapProcessor.cpp b/Runtime/Graphics/CubemapProcessor.cpp new file mode 100644 index 0000000..2cf535e --- /dev/null +++ b/Runtime/Graphics/CubemapProcessor.cpp @@ -0,0 +1,515 @@ +#include "UnityPrefix.h" +#include "CubemapProcessor.h" + +//-------------------------------------------------------------------------------------- +//Based on CCubeMapProcessor from CubeMapGen v1.4 +// Class for filtering and processing cubemaps +// +// +//-------------------------------------------------------------------------------------- +// (C) 2005 ATI Research, Inc., All rights reserved. +//-------------------------------------------------------------------------------------- + +//used to index cube faces +#define CP_FACE_X_POS 0 +#define CP_FACE_X_NEG 1 +#define CP_FACE_Y_POS 2 +#define CP_FACE_Y_NEG 3 +#define CP_FACE_Z_POS 4 +#define CP_FACE_Z_NEG 5 + +//used to index image edges +// NOTE.. the actual number corresponding to the edge is important +// do not change these, or the code will break +// +// CP_EDGE_LEFT is u = 0 +// CP_EDGE_RIGHT is u = width-1 +// CP_EDGE_TOP is v = 0 +// CP_EDGE_BOTTOM is v = height-1 +#define CP_EDGE_LEFT 0 +#define CP_EDGE_RIGHT 1 +#define CP_EDGE_TOP 2 +#define CP_EDGE_BOTTOM 3 + +//corners of CUBE map (P or N specifys if it corresponds to the +// positive or negative direction each of X, Y, and Z +#define CP_CORNER_NNN 0 +#define CP_CORNER_NNP 1 +#define CP_CORNER_NPN 2 +#define CP_CORNER_NPP 3 +#define CP_CORNER_PNN 4 +#define CP_CORNER_PNP 5 +#define CP_CORNER_PPN 6 +#define CP_CORNER_PPP 7 + + +//information about cube maps neighboring face after traversing +// across an edge +struct CPCubeMapNeighbor +{ + UInt8 m_Face; //index of neighboring face + UInt8 m_Edge; //edge in neighboring face that abuts this face +}; + +//------------------------------------------------------------------------------ +// D3D cube map face specification +// mapping from 3D x,y,z cube map lookup coordinates +// to 2D within face u,v coordinates +// +// --------------------> U direction +// | (within-face texture space) +// | _____ +// | | | +// | | +Y | +// | _____|_____|_____ _____ +// | | | | | | +// | | -X | +Z | +X | -Z | +// | |_____|_____|_____|_____| +// | | | +// | | -Y | +// | |_____| +// | +// v V direction +// (within-face texture space) +//------------------------------------------------------------------------------ + +//Information about neighbors and how texture coorrdinates change across faces +// in ORDER of left, right, top, bottom (e.g. edges corresponding to u=0, +// u=1, v=0, v=1 in the 2D coordinate system of the particular face. +//Note this currently assumes the D3D cube face ordering and orientation +CPCubeMapNeighbor sg_CubeNgh[6][4] = +{ + //XPOS face + {{CP_FACE_Z_POS, CP_EDGE_RIGHT }, + {CP_FACE_Z_NEG, CP_EDGE_LEFT }, + {CP_FACE_Y_POS, CP_EDGE_RIGHT }, + {CP_FACE_Y_NEG, CP_EDGE_RIGHT }}, + //XNEG face + {{CP_FACE_Z_NEG, CP_EDGE_RIGHT }, + {CP_FACE_Z_POS, CP_EDGE_LEFT }, + {CP_FACE_Y_POS, CP_EDGE_LEFT }, + {CP_FACE_Y_NEG, CP_EDGE_LEFT }}, + //YPOS face + {{CP_FACE_X_NEG, CP_EDGE_TOP }, + {CP_FACE_X_POS, CP_EDGE_TOP }, + {CP_FACE_Z_NEG, CP_EDGE_TOP }, + {CP_FACE_Z_POS, CP_EDGE_TOP }}, + //YNEG face + {{CP_FACE_X_NEG, CP_EDGE_BOTTOM}, + {CP_FACE_X_POS, CP_EDGE_BOTTOM}, + {CP_FACE_Z_POS, CP_EDGE_BOTTOM}, + {CP_FACE_Z_NEG, CP_EDGE_BOTTOM}}, + //ZPOS face + {{CP_FACE_X_NEG, CP_EDGE_RIGHT }, + {CP_FACE_X_POS, CP_EDGE_LEFT }, + {CP_FACE_Y_POS, CP_EDGE_BOTTOM }, + {CP_FACE_Y_NEG, CP_EDGE_TOP }}, + //ZNEG face + {{CP_FACE_X_POS, CP_EDGE_RIGHT }, + {CP_FACE_X_NEG, CP_EDGE_LEFT }, + {CP_FACE_Y_POS, CP_EDGE_TOP }, + {CP_FACE_Y_NEG, CP_EDGE_BOTTOM }} +}; + + +//The 12 edges of the cubemap, (entries are used to index into the neighbor table) +// this table is used to average over the edges. +int sg_CubeEdgeList[12][2] = { + {CP_FACE_X_POS, CP_EDGE_LEFT}, + {CP_FACE_X_POS, CP_EDGE_RIGHT}, + {CP_FACE_X_POS, CP_EDGE_TOP}, + {CP_FACE_X_POS, CP_EDGE_BOTTOM}, + + {CP_FACE_X_NEG, CP_EDGE_LEFT}, + {CP_FACE_X_NEG, CP_EDGE_RIGHT}, + {CP_FACE_X_NEG, CP_EDGE_TOP}, + {CP_FACE_X_NEG, CP_EDGE_BOTTOM}, + + {CP_FACE_Z_POS, CP_EDGE_TOP}, + {CP_FACE_Z_POS, CP_EDGE_BOTTOM}, + {CP_FACE_Z_NEG, CP_EDGE_TOP}, + {CP_FACE_Z_NEG, CP_EDGE_BOTTOM} +}; + + +//Information about which of the 8 cube corners are correspond to the +// the 4 corners in each cube face +// the order is upper left, upper right, lower left, lower right +int sg_CubeCornerList[6][4] = { + { CP_CORNER_PPP, CP_CORNER_PPN, CP_CORNER_PNP, CP_CORNER_PNN }, // XPOS face + { CP_CORNER_NPN, CP_CORNER_NPP, CP_CORNER_NNN, CP_CORNER_NNP }, // XNEG face + { CP_CORNER_NPN, CP_CORNER_PPN, CP_CORNER_NPP, CP_CORNER_PPP }, // YPOS face + { CP_CORNER_NNP, CP_CORNER_PNP, CP_CORNER_NNN, CP_CORNER_PNN }, // YNEG face + { CP_CORNER_NPP, CP_CORNER_PPP, CP_CORNER_NNP, CP_CORNER_PNP }, // ZPOS face + { CP_CORNER_PPN, CP_CORNER_NPN, CP_CORNER_PNN, CP_CORNER_NNN } // ZNEG face +}; + + + +//========================================================================================================== +//void FixupCubeEdges(CImageSurface *a_CubeMap, int a_FixupType, int a_FixupWidth); +// +//Apply edge fixup to a cubemap mip level. +// +//a_CubeMap [in/out] Array of 6 images comprising cubemap miplevel to apply edge fixup to. +//a_FixupType [in] Specifies the technique used for edge fixup. Choose one of the following, +// CP_FIXUP_NONE, CP_FIXUP_PULL_LINEAR, CP_FIXUP_PULL_HERMITE, CP_FIXUP_AVERAGE_LINEAR, +// CP_FIXUP_AVERAGE_HERMITE +//a_FixupWidth [in] Fixup width in texels +// +//========================================================================================================== + + +//-------------------------------------------------------------------------------------- +// Fixup cube edges +// +// average texels on cube map faces across the edges +//-------------------------------------------------------------------------------------- + +void FixupCubeEdges(CImageSurface *a_CubeMap, int a_FixupType, int a_FixupWidth) +{ + int i, j, k; + int face; + int edge; + int neighborFace; + int neighborEdge; + + int nChannels = a_CubeMap[0].m_NumChannels; + int size = a_CubeMap[0].m_Width; + + CPCubeMapNeighbor neighborInfo; + + CP_ITYPE* edgeStartPtr; + CP_ITYPE* neighborEdgeStartPtr; + + int edgeWalk; + int neighborEdgeWalk; + + //pointer walk to walk one texel away from edge in perpendicular direction + int edgePerpWalk; + int neighborEdgePerpWalk; + + //number of texels inward towards cubeface center to apply fixup to + int fixupDist; + int iFixup; + + // note that if functionality to filter across the three texels for each corner, then + CP_ITYPE *cornerPtr[8][3]; //indexed by corner and face idx + CP_ITYPE *faceCornerPtrs[4]; //corner pointers for face + int cornerNumPtrs[8]; //indexed by corner and face idx + int iCorner; //corner iterator + int iFace; //iterator for faces + int corner; + + //if there is no fixup, or fixup width = 0, do nothing + if((a_FixupType == CP_FIXUP_NONE) || + (a_FixupWidth == 0) ) + { + return; + } + + //special case 1x1 cubemap, average face colors + if( a_CubeMap[0].m_Width == 1 ) + { + //iterate over channels + for(k=0; k