static public int GetPixels(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); if (argc == 2) { UnityEngine.Cubemap self = (UnityEngine.Cubemap)checkSelf(l); UnityEngine.CubemapFace a1; checkEnum(l, 2, out a1); var ret = self.GetPixels(a1); pushValue(l, true); pushValue(l, ret); return(2); } else if (argc == 3) { UnityEngine.Cubemap self = (UnityEngine.Cubemap)checkSelf(l); UnityEngine.CubemapFace a1; checkEnum(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); var ret = self.GetPixels(a1, a2); pushValue(l, true); pushValue(l, ret); return(2); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function to call"); return(2); } catch (Exception e) { return(error(l, e)); } }
private void CopyTextureCube(string texturePath, Cubemap cubemap, BabylonTexture babylonTexture) { if (!babylonScene.AddTextureCube(texturePath)) { return; } try { foreach (CubemapFace face in Enum.GetValues(typeof(CubemapFace))) { var faceTexturePath = Path.Combine(babylonScene.OutputPath, Path.GetFileNameWithoutExtension(texturePath)); switch (face) { case CubemapFace.PositiveX: faceTexturePath += "_px.jpg"; break; case CubemapFace.NegativeX: faceTexturePath += "_nx.jpg"; break; case CubemapFace.PositiveY: faceTexturePath += "_py.jpg"; break; case CubemapFace.NegativeY: faceTexturePath += "_ny.jpg"; break; case CubemapFace.PositiveZ: faceTexturePath += "_pz.jpg"; break; case CubemapFace.NegativeZ: faceTexturePath += "_nz.jpg"; break; default: continue; } var tempTexture = new Texture2D(cubemap.width, cubemap.height, TextureFormat.RGB24, false); tempTexture.SetPixels(cubemap.GetPixels(face)); tempTexture.Apply(); // Flip faces in cube texture. tempTexture = FlipTexture(tempTexture); File.WriteAllBytes(faceTexturePath, tempTexture.EncodeToJPG()); } } catch (Exception ex) { Debug.LogException(ex); } var textureName = Path.GetFileNameWithoutExtension(texturePath); babylonTexture.name = textureName; babylonTexture.isCube = true; babylonTexture.level = exportationOptions.ReflectionDefaultLevel; babylonTexture.coordinatesMode = 3; }
static public int GetPixels(IntPtr l) { try{ if (matchType(l, 2, typeof(UnityEngine.CubemapFace), typeof(int))) { UnityEngine.Cubemap self = (UnityEngine.Cubemap)checkSelf(l); UnityEngine.CubemapFace a1; checkEnum(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); UnityEngine.Color[] ret = self.GetPixels(a1, a2); pushValue(l, ret); return(1); } else if (matchType(l, 2, typeof(UnityEngine.CubemapFace))) { UnityEngine.Cubemap self = (UnityEngine.Cubemap)checkSelf(l); UnityEngine.CubemapFace a1; checkEnum(l, 2, out a1); UnityEngine.Color[] ret = self.GetPixels(a1); pushValue(l, ret); return(1); } LuaDLL.luaL_error(l, "No matched override function to call"); return(0); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
public static void CreateSubMips(Cubemap cube) { AssetDatabase.StartAssetEditing(); int faceSize = cube.width; string cubePath = AssetDatabase.GetAssetPath(cube); //load all sub-assets UnityEngine.Object[] mips = AssetDatabase.LoadAllAssetRepresentationsAtPath(cubePath); if(mips != null) { for(int i=0; i<mips.Length; ++i) { if( mips[i] != null && AssetDatabase.IsSubAsset(mips[i]) ) UnityEngine.Object.DestroyImmediate(mips[i], true); } } AssetDatabase.Refresh(); // skip mip level 0, its in the cubemap itself faceSize = faceSize >> 1; for( int mip = 1; faceSize > 0; ++mip ) { // extract mipmap faces from a cubemap and add them as textures in the sub image Texture2D tex = new Texture2D(faceSize, faceSize*6,TextureFormat.ARGB32,false); tex.name = "mip"+mip; for( int face = 0; face<6; ++face ) { tex.SetPixels(0, face*faceSize, faceSize, faceSize, cube.GetPixels((CubemapFace)face,mip)); } tex.Apply(); AssetDatabase.AddObjectToAsset(tex, cubePath); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(tex)); faceSize = faceSize >> 1; } AssetDatabase.StopAssetEditing(); AssetDatabase.Refresh(); }
static public int GetPixels(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif int argc = LuaDLL.lua_gettop(l); if (argc == 2) { UnityEngine.Cubemap self = (UnityEngine.Cubemap)checkSelf(l); UnityEngine.CubemapFace a1; a1 = (UnityEngine.CubemapFace)LuaDLL.luaL_checkinteger(l, 2); var ret = self.GetPixels(a1); pushValue(l, true); pushValue(l, ret); return(2); } else if (argc == 3) { UnityEngine.Cubemap self = (UnityEngine.Cubemap)checkSelf(l); UnityEngine.CubemapFace a1; a1 = (UnityEngine.CubemapFace)LuaDLL.luaL_checkinteger(l, 2); System.Int32 a2; checkType(l, 3, out a2); var ret = self.GetPixels(a1, a2); pushValue(l, true); pushValue(l, ret); return(2); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function GetPixels to call"); return(2); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
private static Color[] GetPixels(Cubemap c, CubemapFace f) { var pixels = c.GetPixels(f); var halfH = c.height / 2; for (var y = 0; y < halfH; y++) { var o = c.width * y; var n = c.width * (c.height - y - 1); for (var x = 0; x < c.width; x++) { var a = pixels[o + x]; var b = pixels[n + x]; pixels[o + x] = b; pixels[n + x] = a; } } return pixels; }
//-------------------------------------------------------------------------------------- // Irridiance Convolution based on SH void ConvolveIrradianceEnvironmentMap(Cubemap irrCubeMap) { int a_Size = irrCubeMap.width; Vector4[] m_NormCubeMapArray = new Vector4[a_Size*a_Size*6]; BuildNormalizerSolidAngleArray(a_Size, ref m_NormCubeMapArray); //This is a custom implementation of D3DXSHProjectCubeMap to avoid to deal with LPDIRECT3DSURFACE9 pointer //Use Sh order 2 for a total of 9 coefficient as describe in http://www.cs.berkeley.edu/~ravir/papers/envmap/ //accumulators are 64-bit floats in order to have the precision needed //over a summation of a large number of pixels double[] SHr = new double[25]; // NUM_SH_COEFFICIENT double[] SHg = new double[25]; double[] SHb = new double[25]; double[] SHdir = new double[25]; double weightAccum = 0.0; double weight = 0.0; int startFacePtr = 0; for (int iFaceIdx = 0; iFaceIdx < 6; iFaceIdx++) { // read pixels of m_NormCubeMap //var m_NormCubeMap_pixels = new Color[m_NormCubeMap.width*m_NormCubeMap.height]; //m_NormCubeMap_pixels = m_NormCubeMap.GetPixels((CubemapFace)iFaceIdx); // Pointer to the start of the given face in m_NormCubeMapArray startFacePtr = a_Size*a_Size*iFaceIdx; // read all pixels of irrCubeMap var cubeMap_pixels = new Color[irrCubeMap.width * irrCubeMap.height]; cubeMap_pixels = irrCubeMap.GetPixels((CubemapFace)iFaceIdx); for (int y = 0; y < a_Size; y++) { for (int x = 0; x < a_Size; x++) { // read normalCube single pixel Vector4 m_NormCubeMap_pixel = m_NormCubeMapArray[startFacePtr + y*a_Size + x]; // read originalCube single pixel Color cubeMap_pixel = cubeMap_pixels[y*a_Size + x]; // solid angle stored in 4th channel of normalizer/solid angle cube map weight = m_NormCubeMap_pixel[3]; //weight = TexelCoordSolidAngle(iFaceIdx, (float)x, (float)y, a_Size); // pointer to direction and solid angle in cube map associated with texel Vector3 texelVect; texelVect.x = m_NormCubeMap_pixel[0]; texelVect.y = m_NormCubeMap_pixel[1]; texelVect.z = m_NormCubeMap_pixel[2]; //texelVect = TexelToVect(iFaceIdx, (float)x, (float)y, a_Size); EvalSHBasis(texelVect, ref SHdir); // read original colors and convert to float64 double R = cubeMap_pixel[0]; double G = cubeMap_pixel[1]; double B = cubeMap_pixel[2]; for (int i = 0; i < 25; i++) { SHr[i] += R * SHdir[i] * weight; SHg[i] += G * SHdir[i] * weight; SHb[i] += B * SHdir[i] * weight; } weightAccum += weight; } } } // Normalization - The sum of solid angle should be equal to the solid angle of the sphere (4 PI), so // Normalize in order our weightAccum exactly match 4 PI. for (int i = 0; i < 25; ++i) { SHr[i] *= 4.0 * CP_PI / weightAccum; SHg[i] *= 4.0 * CP_PI / weightAccum; SHb[i] *= 4.0 * CP_PI / weightAccum; } // Second step - Generate cubemap from SH coefficient // Normalized vectors per cubeface and per-texel solid angle // Why do we do it a 2nd time???? BuildNormalizerSolidAngleArray(a_Size, ref m_NormCubeMapArray); for (int iFaceIdx = 0; iFaceIdx < 6; iFaceIdx++) { // Pointer to the start of the given face in m_NormCubeMapArray startFacePtr = a_Size*a_Size*iFaceIdx; for (int y = 0; y < a_Size; y++) { for (int x = 0; x < a_Size; x++) { // read normalCube pixel Vector4 m_NormCubeMap_pixel = m_NormCubeMapArray[startFacePtr + y*a_Size + x]; // read normalvector and pass it to EvalSHBasis to get SHdir Vector3 texelVect; texelVect.x = m_NormCubeMap_pixel[0]; texelVect.y = m_NormCubeMap_pixel[1]; texelVect.z = m_NormCubeMap_pixel[2]; //texelVect = TexelToVect(iFaceIdx, (float)x, (float)y, a_Size); EvalSHBasis( texelVect, ref SHdir); // set color values double R = 0.0; double G = 0.0; double B = 0.0; for (int i = 0; i < 25; ++i) { R += (SHr[i] * SHdir[i] * SHBandFactor[i]); G += (SHg[i] * SHdir[i] * SHBandFactor[i]); B += (SHb[i] * SHdir[i] * SHBandFactor[i]); } // Lux needs alpha! irrCubeMap.SetPixel((CubemapFace)iFaceIdx, x, y, new Color((float)R,(float)G,(float)B, 1.0f )); } } } irrCubeMap.Apply(); }
//-------------------------------------------------------------------------------------- // Fixup cube edges void FixupCubeEdges (Cubemap CubeMap) { int maxMipLevels = (int)(Mathf.Log((float)CubeMap.width, 2.0f)) + 1; int base_Size = CubeMap.width; // Do not perform any edge fixed for mip level 0 for (int mipLevel = 1; mipLevel < maxMipLevels; mipLevel++) { // declare jagged array for all faces and init its child arrays Color[][] PixelsOfAllFaces = new Color[6][]; PixelsOfAllFaces[0] = CubeMap.GetPixels(CubemapFace.PositiveX, mipLevel); PixelsOfAllFaces[1] = CubeMap.GetPixels(CubemapFace.NegativeX, mipLevel); PixelsOfAllFaces[2] = CubeMap.GetPixels(CubemapFace.PositiveY, mipLevel); PixelsOfAllFaces[3] = CubeMap.GetPixels(CubemapFace.NegativeY, mipLevel); PixelsOfAllFaces[4] = CubeMap.GetPixels(CubemapFace.PositiveZ, mipLevel); PixelsOfAllFaces[5] = CubeMap.GetPixels(CubemapFace.NegativeZ, mipLevel); int a_Size = base_Size >> mipLevel; // As we use a_Size as pointer in our arrays we have to lower it by 1 a_Size -= 1; int a_FixupWidth = 3; int fixupDist = (int)Mathf.Min( a_FixupWidth, a_Size / 2); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[0,0]], PixelsOfAllFaces[sg_CubeCornerList[0,1]], PixelsOfAllFaces[sg_CubeCornerList[0,2]], 0, 0, a_Size, a_Size, a_Size, 0); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[1,0]], PixelsOfAllFaces[sg_CubeCornerList[1,1]], PixelsOfAllFaces[sg_CubeCornerList[1,2]], a_Size, 0, a_Size, 0, 0, 0); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[2,0]], PixelsOfAllFaces[sg_CubeCornerList[2,1]], PixelsOfAllFaces[sg_CubeCornerList[2,2]], 0, a_Size, a_Size, 0, a_Size, a_Size); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[3,0]], PixelsOfAllFaces[sg_CubeCornerList[3,1]], PixelsOfAllFaces[sg_CubeCornerList[3,2]], a_Size, a_Size, a_Size, a_Size, 0, a_Size); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[4,0]], PixelsOfAllFaces[sg_CubeCornerList[4,1]], PixelsOfAllFaces[sg_CubeCornerList[4,2]], a_Size, 0, 0, a_Size, 0, 0); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[5,0]], PixelsOfAllFaces[sg_CubeCornerList[5,1]], PixelsOfAllFaces[sg_CubeCornerList[5,2]], 0, 0, 0, 0, a_Size, 0); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[6,0]], PixelsOfAllFaces[sg_CubeCornerList[6,1]], PixelsOfAllFaces[sg_CubeCornerList[6,2]], a_Size, a_Size, 0, 0, 0, a_Size); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[7,0]], PixelsOfAllFaces[sg_CubeCornerList[7,1]], PixelsOfAllFaces[sg_CubeCornerList[7,2]], 0, a_Size, 0, a_Size, a_Size, a_Size); // Perform 2nd iteration in reverse order AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[7,0]], PixelsOfAllFaces[sg_CubeCornerList[7,1]], PixelsOfAllFaces[sg_CubeCornerList[7,2]], 0, a_Size, 0, a_Size, a_Size, a_Size); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[6,0]], PixelsOfAllFaces[sg_CubeCornerList[6,1]], PixelsOfAllFaces[sg_CubeCornerList[6,2]], a_Size, a_Size, 0, 0, 0, a_Size); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[5,0]], PixelsOfAllFaces[sg_CubeCornerList[5,1]], PixelsOfAllFaces[sg_CubeCornerList[5,2]], 0, 0, 0, 0, a_Size, 0); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[4,0]], PixelsOfAllFaces[sg_CubeCornerList[4,1]], PixelsOfAllFaces[sg_CubeCornerList[4,2]], a_Size, 0, 0, a_Size, 0, 0); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[3,0]], PixelsOfAllFaces[sg_CubeCornerList[3,1]], PixelsOfAllFaces[sg_CubeCornerList[3,2]], a_Size, a_Size, a_Size, a_Size, 0, a_Size); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[2,0]], PixelsOfAllFaces[sg_CubeCornerList[2,1]], PixelsOfAllFaces[sg_CubeCornerList[2,2]], 0, a_Size, a_Size, 0, a_Size, a_Size); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[1,0]], PixelsOfAllFaces[sg_CubeCornerList[1,1]], PixelsOfAllFaces[sg_CubeCornerList[1,2]], a_Size, 0, a_Size, 0, 0, 0); AverageCorner(a_Size, PixelsOfAllFaces[sg_CubeCornerList[0,0]], PixelsOfAllFaces[sg_CubeCornerList[0,1]], PixelsOfAllFaces[sg_CubeCornerList[0,2]], 0, 0, a_Size, a_Size, a_Size, 0); // Average Edges // Note that this loop does not process the corner texels, since they have already been averaged for (int i = 1; i < (a_Size - 1); i++) { AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[0,0]], PixelsOfAllFaces[sg_CubeEdgeList[0,1]], i, 0, a_Size, a_Size - i, fixupDist, new Vector4(0, 1, -1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[1,0]], PixelsOfAllFaces[sg_CubeEdgeList[1,1]], i, a_Size, a_Size, i, fixupDist, new Vector4(0, -1, -1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[2,0]], PixelsOfAllFaces[sg_CubeEdgeList[2,1]], 0, i, a_Size, i, fixupDist, new Vector4(1, 0, -1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[3,0]], PixelsOfAllFaces[sg_CubeEdgeList[3,1]], a_Size, i, 0, i, fixupDist, new Vector4(-1, 0, 1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[4,0]], PixelsOfAllFaces[sg_CubeEdgeList[4,1]], i, 0, 0, i, fixupDist, new Vector4(0, 1, 1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[5,0]], PixelsOfAllFaces[sg_CubeEdgeList[5,1]], i, a_Size, 0, a_Size - i, fixupDist, new Vector4(0, -1, 1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[6,0]], PixelsOfAllFaces[sg_CubeEdgeList[6,1]], a_Size, i, 0, i, fixupDist, new Vector4(-1, 0, 1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[7,0]], PixelsOfAllFaces[sg_CubeEdgeList[7,1]], 0, i, a_Size, i, fixupDist, new Vector4(1, 0, -1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[8,0]], PixelsOfAllFaces[sg_CubeEdgeList[8,1]], i, a_Size, i, 0, fixupDist, new Vector4(0, -1, 0, 1) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[9,0]], PixelsOfAllFaces[sg_CubeEdgeList[9,1]], i, 0, a_Size - i, 0, fixupDist, new Vector4(0, 1, 0, 1) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[10,0]], PixelsOfAllFaces[sg_CubeEdgeList[10,1]], i, 0, i, a_Size, fixupDist, new Vector4(0, 1, 0, -1) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[11,0]], PixelsOfAllFaces[sg_CubeEdgeList[11,1]], i, a_Size, a_Size - i, a_Size, fixupDist, new Vector4(0, -1, 0, -1) ); } // Perform 2nd iteration in reverse order /* for (int i = 0; i < (a_Size); i++) { AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[11,0]], PixelsOfAllFaces[sg_CubeEdgeList[11,1]], i, a_Size, a_Size - i, a_Size, fixupDist, new Vector4(0, -1, 0, -1) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[10,0]], PixelsOfAllFaces[sg_CubeEdgeList[10,1]], i, 0, i, a_Size, fixupDist, new Vector4(0, 1, 0, -1) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[9,0]], PixelsOfAllFaces[sg_CubeEdgeList[9,1]], i, 0, a_Size - i, 0, fixupDist, new Vector4(0, 1, 0, 1) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[8,0]], PixelsOfAllFaces[sg_CubeEdgeList[8,1]], i, a_Size, i, 0, fixupDist, new Vector4(0, -1, 0, 1) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[7,0]], PixelsOfAllFaces[sg_CubeEdgeList[7,1]], 0, i, a_Size, i, fixupDist, new Vector4(1, 0, -1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[6,0]], PixelsOfAllFaces[sg_CubeEdgeList[6,1]], a_Size, i, 0, i, fixupDist, new Vector4(-1, 0, 1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[5,0]], PixelsOfAllFaces[sg_CubeEdgeList[5,1]], i, a_Size, 0, a_Size - i, fixupDist, new Vector4(0, -1, 1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[4,0]], PixelsOfAllFaces[sg_CubeEdgeList[4,1]], i, 0, 0, i, fixupDist, new Vector4(0, 1, 1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[3,0]], PixelsOfAllFaces[sg_CubeEdgeList[3,1]], a_Size, i, 0, i, fixupDist, new Vector4(-1, 0, 1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[2,0]], PixelsOfAllFaces[sg_CubeEdgeList[2,1]], 0, i, a_Size, i, fixupDist, new Vector4(1, 0, -1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[1,0]], PixelsOfAllFaces[sg_CubeEdgeList[1,1]], i, a_Size, a_Size, i, fixupDist, new Vector4(0, -1, -1, 0) ); AverageEdge(a_Size, PixelsOfAllFaces[sg_CubeEdgeList[0,0]], PixelsOfAllFaces[sg_CubeEdgeList[0,1]], i, 0, a_Size, a_Size - i, fixupDist, new Vector4(0, 1, -1, 0) ); } */ // Write Pixel from the jagged array back to the cubemap faces for (int writeFace = 0; writeFace < 6; writeFace++ ) { Color[] tempColors = PixelsOfAllFaces[writeFace]; CubeMap.SetPixels(tempColors, (CubemapFace)writeFace, mipLevel); } } CubeMap.Apply(false); }
///<summary> /// Convert an environment map into an array of SH coefficients. ///</summary> static float[] Convert(Cubemap cubemap) { // Initialize arrays. double[] SHr = new double[NUM_SH_COEFFICIENT]; double[] SHg = new double[NUM_SH_COEFFICIENT]; double[] SHb = new double[NUM_SH_COEFFICIENT]; double[] SHdir = new double[NUM_SH_COEFFICIENT]; Vector4[, ,] CubeInfo = new Vector4[6, cubemap.width, cubemap.height]; // Loop all texels of a cubemap to calculate every texel's direction and area. // Loop 6 faces. for (int CubeFace = 0; CubeFace < 6; CubeFace++) { // Loop every texel of a face. for (int v = 0; v < cubemap.height; v++) for (int u = 0; u < cubemap.width; u++) { // Compute direction. Vector3 vect = TexelCoordToVect(CubeFace, u, v, cubemap.width); // Compute area. float area = TexelCoordSolidAngle(CubeFace, u, v, cubemap.width); CubeInfo[CubeFace, u, v] = new Vector4(vect.x, vect.y, vect.z, area); } } // Loop all texels of a cubemap to compute SH coefficients. double weightAccum = 0.0; double weight = 0.0; Texture2D temp = new Texture2D(cubemap.width, cubemap.height, cubemap.format, false); for (int CubeFace = 0; CubeFace < 6; CubeFace++) { // Get color data from a cubemap. switch (CubeFace) { case 0: temp.SetPixels(cubemap.GetPixels(CubemapFace.PositiveX)); break; case 1: temp.SetPixels(cubemap.GetPixels(CubemapFace.NegativeX)); break; case 2: temp.SetPixels(cubemap.GetPixels(CubemapFace.PositiveY)); break; case 3: temp.SetPixels(cubemap.GetPixels(CubemapFace.NegativeY)); break; case 4: temp.SetPixels(cubemap.GetPixels(CubemapFace.PositiveZ)); break; case 5: temp.SetPixels(cubemap.GetPixels(CubemapFace.NegativeZ)); break; } // Loop every texel of a face. for (int u = 0; u < cubemap.width; u++) for (int v = 0; v < cubemap.height; v++) { // Evaluate the SH basis functions. weight = (double)(CubeInfo[CubeFace, u, v].w); Vector4 texelVect = CubeInfo[CubeFace, u, v]; EvalSHBasis(texelVect, SHdir); Color rgb = temp.GetPixel(u, v); // Accumulate SH coefficients. for (int i = 0; i < NUM_SH_COEFFICIENT; i++) { SHr[i] += rgb.r * SHdir[i] * weight; SHg[i] += rgb.g * SHdir[i] * weight; SHb[i] += rgb.b * SHdir[i] * weight; } weightAccum += weight; } } // Normalization - The sum of solid angle should be equal to the solid angle of the sphere (4 PI). for (int i = 0; i < NUM_SH_COEFFICIENT; ++i) { SHr[i] *= 4.0 / (weightAccum); SHg[i] *= 4.0 / (weightAccum); SHb[i] *= 4.0 / (weightAccum); } for (int i = 0; i < NUM_SH_COEFFICIENT; ++i) { SHr[i] = (SHr[i] * SHBandFactor[i]); SHg[i] = (SHg[i] * SHBandFactor[i]); SHb[i] = (SHb[i] * SHBandFactor[i]); } float[] output = new float[NUM_SH_COEFFICIENT * 3]; for (int i = 0; i < NUM_SH_COEFFICIENT; i++) { output[i * 3] = (float)SHr[i] * Mathf.PI; output[i * 3 + 1] = (float)SHg[i] * Mathf.PI; output[i * 3 + 2] = (float)SHb[i] * Mathf.PI; } return output; }
// brute force: no look up table used void FilterCubefacesBF (Cubemap RadianceCubeMap, int mipLevel, float a_FilterConeAngle, float a_SpecularPower) { // Read the first CubeFace Color[] InputCubeFacePixels = RadianceCubeMap.GetPixels(CubemapFace.PositiveX, mipLevel); // Get its dimensions int faceLength = InputCubeFacePixels.Length; int a_Size = (int)Mathf.Sqrt(faceLength); // Create new array for all Faces Color[] PixelsOfAllFaces = new Color[faceLength * 6]; // Copy first face InputCubeFacePixels.CopyTo(PixelsOfAllFaces, 0); // Copy all other Faces for (int readFace = 1; readFace < 6; readFace++ ) { InputCubeFacePixels = RadianceCubeMap.GetPixels((CubemapFace)readFace, mipLevel); InputCubeFacePixels.CopyTo(PixelsOfAllFaces, faceLength * readFace); } InputCubeFacePixels = null; // declare jagged output array and init its child arrays Color[][] OutputCubeFacePixels = new Color[6][]; OutputCubeFacePixels[0] = new Color[faceLength]; OutputCubeFacePixels[1] = new Color[faceLength]; OutputCubeFacePixels[2] = new Color[faceLength]; OutputCubeFacePixels[3] = new Color[faceLength]; OutputCubeFacePixels[4] = new Color[faceLength]; OutputCubeFacePixels[5] = new Color[faceLength]; // FilterCubeSurfaces float srcTexelAngle; float dotProdThresh; int filterSize; //angle about center tap to define filter cone float filterAngle; //min angle a src texel can cover (in degrees) srcTexelAngle = (180.0f / CP_PI) * Mathf.Atan2(1.0f, (float)a_Size); //filter angle is 1/2 the cone angle filterAngle = a_FilterConeAngle / 2.0f; //ensure filter angle is larger than a texel if(filterAngle < srcTexelAngle) { filterAngle = srcTexelAngle; } //ensure filter cone is always smaller than the hemisphere if(filterAngle > 90.0f) { filterAngle = 90.0f; } // The maximum number of texels in 1D the filter cone angle will cover // Used to determine bounding box size for filter extents filterSize = (int)Mathf.Ceil(filterAngle / srcTexelAngle); // Ensure conservative region always covers at least one texel if(filterSize < 1) { filterSize = 1; } // dotProdThresh threshold based on cone angle to determine whether or not taps // Reside within the cone angle dotProdThresh = Mathf.Cos( (CP_PI / 180.0f) * filterAngle ); // Process required faces for (int a_FaceIdx = 0; a_FaceIdx < 6; a_FaceIdx++) { // Iterate over dst cube map face texel for (int a_V = 0; a_V < a_Size; a_V++) { for (int a_U = 0; a_U < a_Size; a_U++) { Vector4 Sample = new Vector4(); Color tempCol = new Color(); // get center tap direction Vector3 centerTapDir = TexelToVect(a_FaceIdx, (float)a_U, (float)a_V, a_Size); //-------------------------------------------------------------------------------------- // ProcessFilterExtents float weightAccum = 0.0f; // Iterate over cubefaces for(int iFaceIdx = 0; iFaceIdx < 6; iFaceIdx++ ) { for(int v = 0; v < a_Size; v++) { for(int u = 0; u < a_Size; u++) { // CP_FILTER_TYPE_COSINE_POWER // Pointer to direction in cube map associated with texel Vector3 texelVect = TexelToVect(iFaceIdx, (float)u, (float)v, a_Size); // Check dot product to see if texel is within cone float tapDotProd = Vector3.Dot(texelVect, centerTapDir); float weight = 0.0f; if( tapDotProd >= dotProdThresh && tapDotProd > 0.0f ) { // Weight should be proportional to the solid angle of the tap weight = TexelCoordSolidAngle(iFaceIdx, (float)u, (float)v, a_Size); // Here we decide if we use a Phong/Blinn or a Phong/Blinn BRDF. // Phong/Blinn BRDF is just the Phong/Blinn model multiply by the cosine of the lambert law // so just adding one to specularpower do the trick. // weight *= pow(tapDotProd, (a_SpecularPower + (float32)IsPhongBRDF)); // CP_FILTER_TYPE_COSINE_POWER weight *= Mathf.Pow(tapDotProd, a_SpecularPower); // CP_FILTER_TYPE_COSINE //weight *= tapDotProd; } // Accumulate weight weightAccum += weight; // Get pixel from the input cubeMap array tempCol = PixelsOfAllFaces[a_Size*a_Size*iFaceIdx + a_Size*v + u]; Sample += new Vector4(tempCol.r * weight, tempCol.g * weight, tempCol.b * weight, 1.0f); } } } // one pixel processed // Lux needs alpha! OutputCubeFacePixels[a_FaceIdx][a_V*a_Size + a_U] = new Color(Sample.x/ weightAccum, Sample.y/ weightAccum, Sample.z/ weightAccum, 1.0f); // end inner loops } } } // Write Pixel from the jagged array back to the cubemap faces for (int writeFace = 0; writeFace < 6; writeFace++ ) { Color[] tempColors = OutputCubeFacePixels[writeFace]; RadianceCubeMap.SetPixels(tempColors, (CubemapFace)writeFace, mipLevel); } }
void UseColor() { var othersCube = new Cubemap (LightShapeProbe.LightShapeManager.CubeMapSize, TextureFormat.RGB24, false) as Cubemap; CubeMapColorsR = othersCube.GetPixels(CubemapFace.PositiveX); for(var rXp = 0; rXp < CubeMapColorsR.Length; rXp++) { CubeMapColorsR[rXp] = LightShapeProbe.reflectedColor; } othersCube.SetPixels(CubeMapColorsR, CubemapFace.PositiveX); othersCube.Apply(); CubeMapColorsR = othersCube.GetPixels(CubemapFace.NegativeX); for(var rXn = 0; rXn < CubeMapColorsR.Length; rXn++) { CubeMapColorsR[rXn] = LightShapeProbe.reflectedColor; } othersCube.SetPixels(CubeMapColorsR, CubemapFace.NegativeX); othersCube.Apply(); CubeMapColorsR = othersCube.GetPixels(CubemapFace.PositiveY); for(var rYp = 0; rYp < CubeMapColorsR.Length; rYp++) { CubeMapColorsR[rYp] = LightShapeProbe.reflectedColor; } othersCube.SetPixels(CubeMapColorsR, CubemapFace.PositiveY); othersCube.Apply(); CubeMapColorsR = othersCube.GetPixels(CubemapFace.NegativeY); for(var rYn = 0; rYn < CubeMapColorsR.Length; rYn++) { CubeMapColorsR[rYn] = LightShapeProbe.reflectedColor; } othersCube.SetPixels(CubeMapColorsR, CubemapFace.NegativeY); othersCube.Apply(); CubeMapColorsR = othersCube.GetPixels(CubemapFace.PositiveZ); for(var rZp = 0; rZp < CubeMapColorsR.Length; rZp++) { CubeMapColorsR[rZp] = LightShapeProbe.reflectedColor; } othersCube.SetPixels(CubeMapColorsR, CubemapFace.PositiveZ); othersCube.Apply(); CubeMapColorsR = othersCube.GetPixels(CubemapFace.NegativeZ); for(var rZn = 0; rZn < CubeMapColorsR.Length; rZn++) { CubeMapColorsR[rZn] = LightShapeProbe.reflectedColor; } othersCube.SetPixels(CubeMapColorsR, CubemapFace.NegativeZ); othersCube.Apply(); GameObject[] wObjects = LightShapeProbe.LightShapeManager.cubeProbes; foreach(GameObject o in wObjects) { var oo = o.GetComponent("LightShapeProbe") as LightShapeProbe; foreach(GameObject ooo in oo.myObjects) { var tempMat = ooo.GetComponent<MeshRenderer>(); tempMat.sharedMaterial.SetTexture("_Cube", othersCube); } } }
public IEnumerator RenderCube(GameObject lightShapeObjTemp, GameObject lightShapeObjTempAlt, GameObject groundShapeObjTemp, Material gMat, Material lsMat, Material alsMat) { yield return new WaitForEndOfFrame(); var cubeCamera = new GameObject( "CubemapCamera", typeof(Camera) ) as GameObject; cubeCamera.hideFlags = HideFlags.HideInHierarchy; var cubeCam = cubeCamera.GetComponent("Camera") as Camera; var tempFull = new Cubemap (LightShapeProbe.LightShapeManager.CubeMapSize, TextureFormat.RGB24, LightShapeProbe.LightShapeManager.hasMips) as Cubemap; yield return new WaitForEndOfFrame(); cubeCam.nearClipPlane = 0.001f; cubeCam.aspect = 1.0f; if(LightShapeProbe.LightShapeManager.worldCamera) { if(!RenderSettings.skybox) { if(!LightShapeProbe.overrideCameraColor) { cubeCam.backgroundColor = LightShapeProbe.LightShapeManager.worldCamera.backgroundColor; } if(LightShapeProbe.overrideCameraColor) { cubeCam.backgroundColor = LightShapeProbe.BGColor; } } if(RenderSettings.skybox) { if(!LightShapeProbe.overrideCameraColor) { cubeCam.clearFlags = CameraClearFlags.Skybox; } if(LightShapeProbe.overrideCameraColor) { cubeCam.clearFlags = CameraClearFlags.SolidColor; cubeCam.backgroundColor = LightShapeProbe.BGColor; } } } cubeCam.fov = 90; cubeCam.cullingMask = 1 << 0; cubeCamera.transform.position = transform.position; cubeCamera.transform.eulerAngles = new Vector3(0, 90, 0); Texture2D tex = new Texture2D (LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize, TextureFormat.RGB24, false); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors = tex.GetPixels(); Color [,] bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors[i]; } Color [,] flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveX); tempFull.Apply(); cubeCamera.transform.eulerAngles = new Vector3(0, 270, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeX); tempFull.Apply(); cubeCamera.transform.eulerAngles = new Vector3(0, 0, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveZ); tempFull.Apply(); cubeCamera.transform.eulerAngles = new Vector3(0, 180, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeZ); tempFull.Apply(); cubeCamera.transform.eulerAngles = new Vector3(270, 0, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveY); tempFull.Apply(); cubeCamera.transform.eulerAngles = new Vector3(90, 0, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeY); tempFull.Apply(); yield return new WaitForEndOfFrame(); var cubeCamera2 = new GameObject( "CubemapCamera", typeof(Camera) ) as GameObject; cubeCamera2.hideFlags = HideFlags.HideInHierarchy; var cubeCam2 = cubeCamera2.GetComponent("Camera") as Camera; var tempFull2 = new Cubemap (LightShapeProbe.LightShapeManager.CubeMapSize, TextureFormat.RGB24, LightShapeProbe.LightShapeManager.hasMips) as Cubemap; yield return new WaitForEndOfFrame(); cubeCam2.clearFlags = CameraClearFlags.SolidColor; cubeCam2.nearClipPlane = 0.001f; cubeCam2.aspect = 1.0f; cubeCam2.cullingMask = 2 << 0; cubeCam2.fov = 90; cubeCam2.backgroundColor = Color.black; yield return new WaitForEndOfFrame(); cubeCamera2.transform.position = transform.position; cubeCamera2.transform.eulerAngles = new Vector3(0, 90, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors2 = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors2[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors2[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull2.SetPixels(CubeMapColors2, CubemapFace.PositiveX); tempFull2.Apply(); cubeCamera2.transform.eulerAngles = new Vector3(0, 270, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors2 = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors2[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors2[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull2.SetPixels(CubeMapColors2, CubemapFace.NegativeX); tempFull2.Apply(); cubeCamera2.transform.eulerAngles = new Vector3(0, 0, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors2 = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors2[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors2[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull2.SetPixels(CubeMapColors2, CubemapFace.PositiveZ); tempFull2.Apply(); cubeCamera2.transform.eulerAngles = new Vector3(0, 180, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors2 = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors2[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors2[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull2.SetPixels(CubeMapColors2, CubemapFace.NegativeZ); tempFull2.Apply(); cubeCamera2.transform.eulerAngles = new Vector3(270, 0, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors2 = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors2[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors2[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull2.SetPixels(CubeMapColors2, CubemapFace.PositiveY); tempFull2.Apply(); cubeCamera2.transform.eulerAngles = new Vector3(90, 0, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors2 = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors2[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors2[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull2.SetPixels(CubeMapColors2, CubemapFace.NegativeY); tempFull2.Apply(); yield return new WaitForEndOfFrame(); CubeMapColors = tempFull.GetPixels(CubemapFace.PositiveX); CubeMapColors2 = tempFull2.GetPixels(CubemapFace.PositiveX); SetPixels(CubeMapColors, CubeMapColors2); tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveX); tempFull.Apply(); CubeMapColors = tempFull.GetPixels(CubemapFace.NegativeX); CubeMapColors2 = tempFull2.GetPixels(CubemapFace.NegativeX); SetPixels(CubeMapColors, CubeMapColors2); tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeX); tempFull.Apply(); CubeMapColors = tempFull.GetPixels(CubemapFace.PositiveY); CubeMapColors2 = tempFull2.GetPixels(CubemapFace.PositiveY); SetPixels(CubeMapColors, CubeMapColors2); tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveY); tempFull.Apply(); CubeMapColors = tempFull.GetPixels(CubemapFace.NegativeY); CubeMapColors2 = tempFull2.GetPixels(CubemapFace.NegativeY); SetPixels(CubeMapColors, CubeMapColors2); tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeY); tempFull.Apply(); CubeMapColors = tempFull.GetPixels(CubemapFace.PositiveZ); CubeMapColors2 = tempFull2.GetPixels(CubemapFace.PositiveZ); SetPixels(CubeMapColors, CubeMapColors2); tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveZ); tempFull.Apply(); CubeMapColors = tempFull.GetPixels(CubemapFace.NegativeZ); CubeMapColors2 = tempFull2.GetPixels(CubemapFace.NegativeZ); SetPixels(CubeMapColors, CubeMapColors2); tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeZ); tempFull.Apply(); if(lightShapeObjTemp) { Destroy(lightShapeObjTemp.gameObject); Destroy(lsMat); } if(lightShapeObjTempAlt) { Destroy(lightShapeObjTempAlt.gameObject); Destroy(alsMat); } if(LightShapeProbe.hasGround) { var cubeCamera3 = new GameObject( "CubemapCamera", typeof(Camera) ) as GameObject; var cubeCam3 = cubeCamera3.GetComponent("Camera") as Camera; cubeCam3.clearFlags = CameraClearFlags.SolidColor; cubeCam3.nearClipPlane = 0.001f; cubeCam3.farClipPlane = 5f; cubeCam3.aspect = 1.0f; cubeCam3.cullingMask = 2 << 0; cubeCam3.fov = 90; cubeCam3.backgroundColor = Color.white; yield return new WaitForEndOfFrame(); cubeCamera3.transform.position = transform.position; cubeCamera3.transform.rotation = transform.rotation; var tempFull3 = new Cubemap (LightShapeProbe.LightShapeManager.CubeMapSize, TextureFormat.RGB24, false) as Cubemap; cubeCamera3.transform.eulerAngles = new Vector3(0, 90, 0); //1 yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors3 = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors3[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors3[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull3.SetPixels(CubeMapColors3, CubemapFace.PositiveX); tempFull3.Apply(); //2 cubeCamera3.transform.eulerAngles = new Vector3(0, 270, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors3 = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors3[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors3[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull3.SetPixels(CubeMapColors3, CubemapFace.NegativeX); tempFull3.Apply(); //3 cubeCamera3.transform.eulerAngles = new Vector3(0, 0, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors3 = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors3[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors3[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull3.SetPixels(CubeMapColors3, CubemapFace.PositiveZ); tempFull3.Apply(); //4 cubeCamera3.transform.eulerAngles = new Vector3(0, 180, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors3 = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors3[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors3[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull3.SetPixels(CubeMapColors3, CubemapFace.NegativeZ); tempFull3.Apply(); //5 cubeCamera3.transform.eulerAngles = new Vector3(270, 0, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors3 = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors3[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors3[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull3.SetPixels(CubeMapColors3, CubemapFace.PositiveY); tempFull3.Apply(); //6 cubeCamera3.transform.eulerAngles = new Vector3(90, 0, 0); yield return new WaitForEndOfFrame(); tex.ReadPixels (new Rect(1, 1, LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize), 0, 0); yield return new WaitForEndOfFrame(); tex.Apply (); CubeMapColors3 = tex.GetPixels(); bigPicture = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; bigPicture[row, i % LightShapeProbe.LightShapeManager.CubeMapSize] = CubeMapColors3[i]; } flippedX = new Color[LightShapeProbe.LightShapeManager.CubeMapSize, LightShapeProbe.LightShapeManager.CubeMapSize]; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { for( int j = 0; j < LightShapeProbe.LightShapeManager.CubeMapSize; ++j) { flippedX[LightShapeProbe.LightShapeManager.CubeMapSize - 1 - i, j] = bigPicture[i, j]; } } row = -1; for( int i = 0; i < LightShapeProbe.LightShapeManager.CubeMapSize * LightShapeProbe.LightShapeManager.CubeMapSize; ++i ) { if ( i % LightShapeProbe.LightShapeManager.CubeMapSize == 0 ) ++row; CubeMapColors3[i] = flippedX[row, i % LightShapeProbe.LightShapeManager.CubeMapSize]; } tempFull3.SetPixels(CubeMapColors3, CubemapFace.NegativeY); tempFull3.Apply(); yield return new WaitForEndOfFrame(); //1 CubeMapColors = tempFull.GetPixels(CubemapFace.PositiveY); CubeMapColors3 = tempFull3.GetPixels(CubemapFace.PositiveY); SetGround(CubeMapColors, CubeMapColors3); tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveY); tempFull.Apply(); //2 CubeMapColors = tempFull.GetPixels(CubemapFace.NegativeY); CubeMapColors3 = tempFull3.GetPixels(CubemapFace.NegativeY); SetGround(CubeMapColors, CubeMapColors3); tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeY); tempFull.Apply(); //3 CubeMapColors = tempFull.GetPixels(CubemapFace.PositiveX); CubeMapColors3 = tempFull3.GetPixels(CubemapFace.PositiveX); SetGround(CubeMapColors, CubeMapColors3); tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveX); tempFull.Apply(); //4 CubeMapColors = tempFull.GetPixels(CubemapFace.NegativeX); CubeMapColors3 = tempFull3.GetPixels(CubemapFace.NegativeX); SetGround(CubeMapColors, CubeMapColors3); tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeX); tempFull.Apply(); //5 CubeMapColors = tempFull.GetPixels(CubemapFace.PositiveZ); CubeMapColors3 = tempFull3.GetPixels(CubemapFace.PositiveZ); SetGround(CubeMapColors, CubeMapColors3); tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveZ); tempFull.Apply(); //6 CubeMapColors = tempFull.GetPixels(CubemapFace.NegativeZ); CubeMapColors3 = tempFull3.GetPixels(CubemapFace.NegativeZ); SetGround(CubeMapColors, CubeMapColors3); tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeZ); tempFull.Apply(); DestroyImmediate( cubeCamera3 ); } if(groundShapeObjTemp) { Destroy(groundShapeObjTemp.gameObject); Destroy(gMat); } AssetDatabase.CreateAsset(tempFull, LightShapeProbe.LightShapeManager.savePath + LightShapeProbe.LightShapeManager.folderName + "/Cube_" + LightShapeProbe.LightShapeManager.CubeMapSize + "_" + (LightShapeProbe.myIndex + 1) + ".cubemap"); yield return new WaitForEndOfFrame(); if(LightShapeProbe.LightShapeManager.AltMethodManger.curProbeInt == LightShapeProbe.LightShapeManager.cubeProbes.Length) { if(!LightShapeProbe.LightShapeManager.AltMethodManger.secondPass) { LightShapeProbe.LightShapeManager.AltMethodManger.StopBatch(); } } if(LightShapeProbe.LightShapeManager.AltMethodManger.secondPass) { if(LightShapeProbe.LightShapeManager.AltMethodManger.curProbeInt == LightShapeProbe.LightShapeManager.AltMethodManger.reflectObjs.Length) { LightShapeProbe.LightShapeManager.AltMethodManger.StopBatch(); } if(LightShapeProbe.LightShapeManager.AltMethodManger.curProbeInt < LightShapeProbe.LightShapeManager.AltMethodManger.reflectObjs.Length) { StartCoroutine(LightShapeProbe.LightShapeManager.AltMethodManger.RenderCubeMaps()); } } if(!LightShapeProbe.LightShapeManager.AltMethodManger.secondPass) { if(LightShapeProbe.LightShapeManager.AltMethodManger.curProbeInt < LightShapeProbe.LightShapeManager.cubeProbes.Length) { StartCoroutine(LightShapeProbe.LightShapeManager.AltMethodManger.RenderCubeMaps()); } } UpdateCubeMaps(); rewardInt = Random.Range(0, LightShapeProbe.LightShapeManager.rewardStrings.Length); if(!LightShapeProbe.LightShapeManager.renderingCubeMaps) { Debug.Log(LightShapeProbe.LightShapeManager.rewardStrings[rewardInt] + " CubeMap Rendered!"); EditorApplication.isPlaying = false; } }
private void convolve_internal(Texture dstTex, Texture srcCube, bool dstRGBM, bool srcRGBM, bool linear, Camera cam, Material skyMat, Matrix4x4 matrix) { bool prevHDR = cam.hdr; CameraClearFlags prevFlags = cam.clearFlags; int prevMask = cam.cullingMask; cam.clearFlags = CameraClearFlags.Skybox; cam.cullingMask = 0; cam.hdr = !dstRGBM; // might need HDR camera buffer when we're not rendering to RGBM encoded pixels skyMat.name = "Internal Convolve Skybox"; skyMat.shader = Shader.Find("Hidden/Marmoset/RGBM Convolve"); //toggleKeywordPair("MARMO_LINEAR", "MARMO_GAMMA", linear); toggleKeywordPair("MARMO_RGBM_INPUT_ON", "MARMO_RGBM_INPUT_OFF", srcRGBM); toggleKeywordPair("MARMO_RGBM_OUTPUT_ON", "MARMO_RGBM_OUTPUT_OFF", dstRGBM); skyMat.SetMatrix("_SkyMatrix", matrix); skyMat.SetTexture("_CubeHDR", srcCube); bindRandomValueTable(skyMat,"_PhongRands", srcCube.width); Material prevSkyMat = UnityEngine.RenderSettings.skybox; UnityEngine.RenderSettings.skybox = skyMat; Cubemap dstCube = dstTex as Cubemap; RenderTexture dstRT = dstTex as RenderTexture; if( dstCube ) { if( generateMipChain ) { int mipCount = mset.QPow.Log2i(dstCube.width) - 1; int mip = highestMipIsMirror ? 1 : 0; for( ; mip<mipCount; ++mip ) { int mipSize = 1 << (mipCount-mip); float mipExp = mset.QPow.clampedDownShift(this.maxExponent, highestMipIsMirror ? (mip-1) : mip, 1); skyMat.SetFloat("_SpecularExp", mipExp); skyMat.SetFloat ("_SpecularScale", this.convolutionScale); Cubemap mipCube = new Cubemap(mipSize, dstCube.format, false); cam.RenderToCubemap(mipCube); for(int f=0; f<6; ++f) { CubemapFace face = (CubemapFace)f; dstCube.SetPixels(mipCube.GetPixels(face), face, mip); } Cubemap.DestroyImmediate(mipCube); } dstCube.Apply(false); } else { skyMat.SetFloat("_SpecularExp", this.maxExponent); skyMat.SetFloat ("_SpecularScale", this.convolutionScale); cam.RenderToCubemap(dstCube); } } else if(dstRT) { skyMat.SetFloat("_SpecularExp", this.maxExponent); skyMat.SetFloat ("_SpecularScale", this.convolutionScale); cam.RenderToCubemap(dstRT); } cam.clearFlags = prevFlags; cam.cullingMask = prevMask; cam.hdr = prevHDR; UnityEngine.RenderSettings.skybox = prevSkyMat; }
public SphericalHarmonicsL2 ProjectCubeIntoSH3Func(Cubemap src, int miplevel) { Vector4[] array = new Vector4[] { new Vector4(0f, 0f, -1f, 0f), new Vector4(0f, -1f, 0f, 0f), new Vector4(-1f, 0f, 0f, 0f), new Vector4(0f, 0f, 1f, 0f), new Vector4(0f, -1f, 0f, 0f), new Vector4(1f, 0f, 0f, 0f), new Vector4(1f, 0f, 0f, 0f), new Vector4(0f, 0f, 1f, 0f), new Vector4(0f, -1f, 0f, 0f), new Vector4(1f, 0f, 0f, 0f), new Vector4(0f, 0f, -1f, 0f), new Vector4(0f, 1f, 0f, 0f), new Vector4(1f, 0f, 0f, 0f), new Vector4(0f, -1f, 0f, 0f), new Vector4(0f, 0f, -1f, 0f), new Vector4(-1f, 0f, 0f, 0f), new Vector4(0f, -1f, 0f, 0f), new Vector4(0f, 0f, 1f, 0f) }; Quaternion rotation = base.gameObject.transform.rotation; Matrix4x4 matrix4x = this.QuaternionToMatrix(rotation); for (int i = 0; i < 6; i++) { array[i] = matrix4x * array[i]; } Shader.SetGlobalMatrix("_SkyRotation", matrix4x); float num = 0f; SphericalHarmonicsL2 sphericalHarmonicsL = default(SphericalHarmonicsL2); sphericalHarmonicsL.Clear(); for (int j = 0; j < 6; j++) { Vector3 a = array[j * 3]; Vector3 a2 = -array[j * 3 + 1]; Vector3 a3 = -array[j * 3 + 2]; Color[] pixels = src.GetPixels((CubemapFace)j, miplevel); int num2 = src.width >> miplevel; if (num2 < 1) { num2 = 1; } float num3 = -1f + 1f / (float)num2; float num4 = 2f * (1f - 1f / (float)num2) / ((float)num2 - 1f); for (int k = 0; k < num2; k++) { float num5 = (float)k * num4 + num3; for (int l = 0; l < num2; l++) { Color a4 = pixels[l + k * num2]; float num6 = (float)l * num4 + num3; float num7 = 1f + num6 * num6 + num5 * num5; float num8 = 4f / (Mathf.Sqrt(num7) * num7); Vector3 a5 = a3 + a * num6 + a2 * num5; a5.Normalize(); Color color = a4 * a4.a * 8f; sphericalHarmonicsL.AddDirectionalLight(-a5, (QualitySettings.activeColorSpace != ColorSpace.Linear) ? color : color.linear, num8 * 0.5f); num += num8; } } } float rhs = 4f / num; sphericalHarmonicsL *= rhs; return sphericalHarmonicsL; }
public static void clearCheckerCube( ref Cubemap cube ) { Color gray0 = new Color(0.25f,0.25f,0.25f,0.25f); Color gray1 = new Color(0.50f,0.50f,0.50f,0.25f); Color[] c = cube.GetPixels(CubemapFace.NegativeX); int w = cube.width; int sqw = Mathf.Max(1,w/4); //width of square for( int face=0; face<6; ++face ) { for(int x=0; x<w; ++x) for(int y=0; y<w; ++y) { if(((x/sqw)%2) == ((y/sqw)%2)) c[y*w + x] = gray0; else c[y*w + x] = gray1; } cube.SetPixels(c, (CubemapFace)face); } cube.Apply(true); }
void Render(GameObject lightShapeObjTemp, GameObject lightShapeObjTempAlt, GameObject groundShapeObjTemp, Material gMat, Material lsMat, Material alsMat ) { if(!LightShapeManager.useAltMethod) { TurnOffCubes(); var cubeCamera = new GameObject( "CubemapCamera", typeof(Camera) ) as GameObject; cubeCamera.tag = "LightShapeObject"; var cubeCam = cubeCamera.GetComponent("Camera") as Camera; cubeCam.nearClipPlane = 0.001f; cubeCam.farClipPlane = 1000f; cubeCam.cullingMask = 1 << 0; if(!LightShapeManager.worldCamera) { cubeCam.backgroundColor = BGColor; } if(LightShapeManager.worldCamera) { if(!RenderSettings.skybox) { if(!overrideCameraColor) { cubeCam.backgroundColor = LightShapeManager.worldCamera.backgroundColor; } if(overrideCameraColor) { cubeCam.backgroundColor = BGColor; } } if(RenderSettings.skybox) { if(!overrideCameraColor) { cubeCam.clearFlags = CameraClearFlags.Skybox; } if(overrideCameraColor) { cubeCam.clearFlags = CameraClearFlags.SolidColor; cubeCam.backgroundColor = BGColor; } } } cubeCamera.transform.position = transform.position; cubeCamera.transform.rotation = Quaternion.identity; var cubeCamera2 = new GameObject( "CubemapCamera", typeof(Camera) ) as GameObject; cubeCamera2.tag = "LightShapeObject"; var cubeCam2 = cubeCamera2.GetComponent("Camera") as Camera; cubeCam2.clearFlags = CameraClearFlags.SolidColor; cubeCam2.cullingMask = 2 << 0; cubeCam2.backgroundColor = Color.black; cubeCamera2.transform.position = cubeCamera.transform.position; cubeCamera2.transform.rotation = cubeCamera.transform.rotation; var tempFull = new Cubemap (LightShapeManager.CubeMapSize, TextureFormat.RGB24, LightShapeManager.hasMips) as Cubemap; cubeCamera.GetComponent<Camera>().RenderToCubemap( tempFull ); cubeCam2.nearClipPlane = 0.001f; var tempFull2 = new Cubemap (LightShapeManager.CubeMapSize, TextureFormat.RGB24, LightShapeManager.hasMips) as Cubemap; cubeCamera2.GetComponent<Camera>().RenderToCubemap( tempFull2 ); CubeMapColors = tempFull.GetPixels(CubemapFace.PositiveY); CubeMapColors2 = tempFull2.GetPixels(CubemapFace.PositiveY); SetPixels(CubeMapColors, CubeMapColors2); tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveY); tempFull.Apply(); CubeMapColors = tempFull.GetPixels(CubemapFace.NegativeY); CubeMapColors2 = tempFull2.GetPixels(CubemapFace.NegativeY); SetPixels(CubeMapColors, CubeMapColors2); tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeY); tempFull.Apply(); CubeMapColors = tempFull.GetPixels(CubemapFace.PositiveX); CubeMapColors2 = tempFull2.GetPixels(CubemapFace.PositiveX); SetPixels(CubeMapColors, CubeMapColors2); tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveX); tempFull.Apply(); CubeMapColors = tempFull.GetPixels(CubemapFace.NegativeX); CubeMapColors2 = tempFull2.GetPixels(CubemapFace.NegativeX); SetPixels(CubeMapColors, CubeMapColors2); tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeX); tempFull.Apply(); CubeMapColors = tempFull.GetPixels(CubemapFace.PositiveZ); CubeMapColors2 = tempFull2.GetPixels(CubemapFace.PositiveZ); SetPixels(CubeMapColors, CubeMapColors2); tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveZ); tempFull.Apply(); CubeMapColors = tempFull.GetPixels(CubemapFace.NegativeZ); CubeMapColors2 = tempFull2.GetPixels(CubemapFace.NegativeZ); SetPixels(CubeMapColors, CubeMapColors2); tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeZ); tempFull.Apply(); if(lightShapeObjTemp) { DestroyImmediate(lightShapeObjTemp.gameObject); DestroyImmediate(lsMat); } if(lightShapeObjTempAlt) { DestroyImmediate(lightShapeObjTempAlt.gameObject); DestroyImmediate(alsMat); } DestroyImmediate( cubeCamera ); DestroyImmediate( tempFull2 ); DestroyImmediate( cubeCamera2 ); if(hasGround) { var cubeCamera3 = new GameObject( "CubemapCamera", typeof(Camera) ) as GameObject; cubeCamera3.tag = "LightShapeObject"; var cubeCam3 = cubeCamera3.GetComponent("Camera") as Camera; cubeCam3.clearFlags = CameraClearFlags.SolidColor; cubeCam3.aspect = 1.0f; cubeCam3.cullingMask = 2 << 0; cubeCam3.nearClipPlane = 0.001f; cubeCam3.farClipPlane = 5f; cubeCam3.backgroundColor = Color.white; cubeCamera3.transform.position = transform.position; cubeCamera3.transform.rotation = transform.rotation; var tempFull3 = new Cubemap (LightShapeManager.CubeMapSize, TextureFormat.RGB24, false) as Cubemap; cubeCamera3.GetComponent<Camera>().RenderToCubemap( tempFull3 ); //1 CubeMapColors = tempFull.GetPixels(CubemapFace.PositiveY); CubeMapColors3 = tempFull3.GetPixels(CubemapFace.PositiveY); SetGround(CubeMapColors, CubeMapColors3); tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveY); tempFull.Apply(); //2 CubeMapColors = tempFull.GetPixels(CubemapFace.NegativeY); CubeMapColors3 = tempFull3.GetPixels(CubemapFace.NegativeY); SetGround(CubeMapColors, CubeMapColors3); tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeY); tempFull.Apply(); //3 CubeMapColors = tempFull.GetPixels(CubemapFace.PositiveX); CubeMapColors3 = tempFull3.GetPixels(CubemapFace.PositiveX); SetGround(CubeMapColors, CubeMapColors3); tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveX); tempFull.Apply(); //4 CubeMapColors = tempFull.GetPixels(CubemapFace.NegativeX); CubeMapColors3 = tempFull3.GetPixels(CubemapFace.NegativeX); SetGround(CubeMapColors, CubeMapColors3); tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeX); tempFull.Apply(); //5 CubeMapColors = tempFull.GetPixels(CubemapFace.PositiveZ); CubeMapColors3 = tempFull3.GetPixels(CubemapFace.PositiveZ); SetGround(CubeMapColors, CubeMapColors3); tempFull.SetPixels(CubeMapColors, CubemapFace.PositiveZ); tempFull.Apply(); //6 CubeMapColors = tempFull.GetPixels(CubemapFace.NegativeZ); CubeMapColors3 = tempFull3.GetPixels(CubemapFace.NegativeZ); SetGround(CubeMapColors, CubeMapColors3); tempFull.SetPixels(CubeMapColors, CubemapFace.NegativeZ); tempFull.Apply(); // DestroyImmediate( tempFull3 ); DestroyImmediate( cubeCamera3 ); } if(groundShapeObjTemp) { DestroyImmediate(groundShapeObjTemp.gameObject); DestroyImmediate(gMat); } AssetDatabase.CreateAsset(tempFull, LightShapeManager.savePath + LightShapeManager.folderName + "/Cube_" + LightShapeManager.CubeMapSize + "_" + (myIndex + 1) + ".cubemap"); myCubeMap = AssetDatabase.LoadAssetAtPath(LightShapeManager.savePath + LightShapeManager.folderName + "/Cube_" + LightShapeManager.CubeMapSize + "_" + (myIndex + 1) + ".cubemap", typeof(Cubemap)) as Cubemap; List<Color> oldColors = new List<Color>(CubeMapColors); oldColors.Clear(); CubeMapColors = oldColors.ToArray(); CubeMapColors2 = oldColors.ToArray(); CubeMapColors3 = oldColors.ToArray(); CubeMapColorsR = oldColors.ToArray(); UpdateCubeMaps(); } if(LightShapeManager.useAltMethod) { TurnOffCubes(); if(!LightShapeManager.renderingCubeMaps) { LightShapeManager.curProbeInt = myIndex; } AltMethod.StartRender(); } EditorUtility.UnloadUnusedAssets(); if(!LightShapeManager.renderingCubeMaps) { Debug.Log(LightShapeManager.rewardStrings[rewardInt] + " CubeMap Rendered!"); } }
IEnumerator MakeSnapshot(Cubemap c, CubemapFace face, CubemapNode node) { // We should only read the screen buffer after rendering is complete yield return new WaitForEndOfFrame(); int width = Screen.width; int height = Screen.height; //Create the blank texture container Texture2D snapshot = new Texture2D(width, height, textureFormat, useMipMaps); snapshot.wrapMode = TextureWrapMode.Clamp; // Rectangle Area from the Camera Rect copyRect = new Rect((camera.pixelWidth * 0.5f) - (snapshot.width * 0.5f), (camera.pixelHeight * 0.5f) - (snapshot.height * 0.5f), snapshot.width, snapshot.height); //Read the current render into the texture container, snapshot snapshot.ReadPixels(copyRect, 0, 0, false); yield return null; snapshot.Apply(); // Resize our Texture snapshot = Scale(snapshot, nodeResolution, nodeResolution); // Write mirrored pixels from our Texture to Cubemap Color cubemapColor; for (int y = 0; y<nodeResolution; y++) { for (int x = 0; x<nodeResolution; x++) { cubemapColor = snapshot.GetPixel(nodeResolution + x, (nodeResolution-1) - y); c.SetPixel(face, x, y, cubemapColor); } } c.Apply(); // Optional PNG generation. Double-check it with overriding Node setting if(makePNG && node.allowGeneratePNG) { // Mirror the snapshot image for our PNG in order to be identical with the cubemap faces snapshot.SetPixels32( MirrorColor32( c.GetPixels(face) ) ); snapshot.Apply(); // Convert to PNG file byte[] bytes = snapshot.EncodeToPNG(); // Save the file string path = Application.dataPath + "/" + pathCubemapPNG + "/" + sceneName + "_" + node.name + "_" + face.ToString() + ".png"; //System.IO.File.WriteAllBytes(path, bytes); // deprecated because not available on Webplayer System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs); bw.Write(bytes); bw.Close(); fs.Close(); // Fix compression state string finalImagePath = "Assets/" + pathCubemapPNG + "/" + sceneName + "_" + node.name + "_" + face.ToString() + ".png"; if (finalImagePath.Contains("//")) finalImagePath = finalImagePath.Replace("//", "/"); AssetDatabase.Refresh(); // refresh necessary before we can use the textureimporter TextureImporter textureImporter = AssetImporter.GetAtPath(finalImagePath) as TextureImporter; if (textureImporter != null) { textureImporter.textureFormat = TextureImporterFormat.RGB24; AssetDatabase.ImportAsset(finalImagePath); } } // Delete our screenshot texture as clean up DestroyImmediate(snapshot); yield return null; }
private void CopyTextureCube(string texturePath, Cubemap cubemap, BabylonTexture babylonTexture, bool hdr = false) { if (!babylonScene.AddTextureCube(texturePath)) { return; } ExporterWindow.ReportProgress(1, "Parsing texture cube: " + texturePath); var srcTexturePath = AssetDatabase.GetAssetPath(cubemap); bool textureNotExistsMode = (SceneController != null && SceneController.skyboxOptions.textureFile == BabylonTextureExport.IfNotExists); bool png = (exportationOptions.DefaultImageFormat == (int)BabylonImageFormat.PNG); string fext = (png == true) ? ".png" : ".jpg"; try { if (hdr) { var hdrTexturePath = Path.Combine(babylonScene.OutputPath, Path.GetFileName(texturePath)); bool writeHdrTexture = true; string textureHdrName = Path.GetFileName(hdrTexturePath); if (textureNotExistsMode && File.Exists(hdrTexturePath)) { writeHdrTexture = false; ExporterWindow.ReportProgress(1, "Texture hdr cube item exists: " + textureHdrName); } if (writeHdrTexture) { ExporterWindow.ReportProgress(1, "Copying hdr texture cube item: " + textureHdrName); File.Copy(srcTexturePath, hdrTexturePath, true); } } else { var importTool = new BabylonTextureImporter(srcTexturePath); bool isReadable = importTool.IsReadable(); if (!isReadable) importTool.SetReadable(); try { foreach (CubemapFace face in Enum.GetValues(typeof(CubemapFace))) { var faceTexturePath = Path.Combine(babylonScene.OutputPath, Path.GetFileNameWithoutExtension(texturePath)); switch (face) { case CubemapFace.PositiveX: faceTexturePath += ("_px" + fext); break; case CubemapFace.NegativeX: faceTexturePath += ("_nx" + fext); break; case CubemapFace.PositiveY: faceTexturePath += ("_py" + fext); break; case CubemapFace.NegativeY: faceTexturePath += ("_ny" + fext); break; case CubemapFace.PositiveZ: faceTexturePath += ("_pz" + fext); break; case CubemapFace.NegativeZ: faceTexturePath += ("_nz" + fext); break; default: continue; } bool writeFaceTexture = true; string textureFaceName = Path.GetFileName(faceTexturePath); if (textureNotExistsMode && File.Exists(faceTexturePath)) { writeFaceTexture = false; ExporterWindow.ReportProgress(1, "Texture cube item exists: " + textureFaceName); } if (writeFaceTexture) { ExporterWindow.ReportProgress(1, "Exporting texture cube item: " + textureFaceName); var tempTexture = new Texture2D(cubemap.width, cubemap.height, TextureFormat.ARGB32, false); Color[] pixels = cubemap.GetPixels(face); tempTexture.SetPixels(pixels); tempTexture.Apply(); // Flip faces in cube texture. tempTexture = FlipTexture(tempTexture); // Encode cube face texture if (png) { File.WriteAllBytes(faceTexturePath, tempTexture.EncodeToPNG()); } else { File.WriteAllBytes(faceTexturePath, tempTexture.EncodeToJPG(exportationOptions.DefaultQualityLevel)); } } } } finally { if (!isReadable) importTool.ForceUpdate(); } } } catch (Exception ex) { Debug.LogException(ex); } if (babylonTexture != null) { var textureName = Path.GetFileNameWithoutExtension(texturePath); if (hdr) textureName += ".hdr"; babylonTexture.name = textureName; babylonTexture.isCube = true; babylonTexture.level = exportationOptions.ReflectionDefaultLevel; babylonTexture.coordinatesMode = 3; } }
private void CopyCubemapFace(CubemapFace face, Cubemap source, ref Cubemap target) { //Create the blank texture container Texture2D snapshot = new Texture2D(source.width, source.height, source.format, m_useMipMaps, false); snapshot.wrapMode = TextureWrapMode.Clamp; // Read Face Pixels into the Texture snapshot.SetPixels(source.GetPixels(face), 0); // Resize to new size snapshot = Scale(snapshot, m_resolution, m_resolution); // Finally write the contents to the new Cubemap target.SetPixels(snapshot.GetPixels(), face, 0); target.Apply(); }
void FakeHDR (Cubemap CubeMap, bool hasmipLevels) { int maxMipLevels = 1; if (hasmipLevels) { maxMipLevels = (int)(Mathf.Log((float)CubeMap.width, 2.0f)) + 1; } int base_Size = CubeMap.width; Vector4 rgbmColor = new Vector4(); for (int mipLevel = 0; mipLevel < maxMipLevels; mipLevel++) { int a_Size = base_Size >> mipLevel; // As we use a_Size as pointer in our arrays we have to lower it by 1 //a_Size -= 1; for (int a_FaceIdx = 0; a_FaceIdx < 6; a_FaceIdx++) { // Get all pixels of the given face Color[] FaceColors = CubeMap.GetPixels((CubemapFace)a_FaceIdx, mipLevel); // Iterate over dst cube map face texel for (int a_V = 0; a_V < a_Size; a_V++) { for (int a_U = 0; a_U < a_Size; a_U++) { rgbmColor.x = FaceColors[a_V * a_Size + a_U].r; rgbmColor.y = FaceColors[a_V * a_Size + a_U].g; rgbmColor.z = FaceColors[a_V * a_Size + a_U].b; rgbmColor *= 1.0f/6.0f; rgbmColor.w = Mathf.Clamp01(Mathf.Max(Mathf.Max(rgbmColor.x, rgbmColor.y), rgbmColor.z)); rgbmColor.w = Mathf.Ceil(rgbmColor.w*255.0f) / 255.0f; rgbmColor.x = rgbmColor.x / rgbmColor.w; rgbmColor.y = rgbmColor.y / rgbmColor.w; rgbmColor.z = rgbmColor.z / rgbmColor.w; FaceColors[a_V * a_Size + a_U] = rgbmColor; } } CubeMap.SetPixels(FaceColors,(CubemapFace)a_FaceIdx, mipLevel); } } CubeMap.Apply(false); }
public static void projectCube(ref mset.SHEncoding sh, Cubemap cube, int mip, bool hdr) { sh.clearToBlack(); float totalarea = 0f; ulong faceSize = (ulong)cube.width; mip = Mathf.Min(mset.QPow.Log2i(faceSize)+1, mip); faceSize = (ulong)(faceSize >> mip); float[] dc = new float[9]; Vector3 u = Vector3.zero; for(ulong face=0; face<6; ++face) { Color rgba = Color.black; Color[] pixels = cube.GetPixels((CubemapFace)face, mip); for(ulong y=0; y<faceSize; ++y) for(ulong x=0; x<faceSize; ++x) { //compute cube direction float areaweight = 1f; mset.Util.invCubeLookup(ref u, ref areaweight, face, x, y, faceSize); float shscale = 4f / 3f; ulong index = y*faceSize + x; if(hdr) mset.RGB.fromRGBM(ref rgba, pixels[index], true); else rgba = pixels[index]; //project on basis functions, and accumulate dc[0] = project_l0_m0(u); dc[1] = project_l1_mneg1(u); dc[2] = project_l1_m0(u); dc[3] = project_l1_m1(u); dc[4] = project_l2_mneg2(u); dc[5] = project_l2_mneg1(u); dc[6] = project_l2_m0(u); dc[7] = project_l2_m1(u); dc[8] = project_l2_m2(u); for(int i=0; i<9; ++i ) { sh.c[3*i + 0] += shscale * areaweight * rgba[0] * dc[i]; sh.c[3*i + 1] += shscale * areaweight * rgba[1] * dc[i]; sh.c[3*i + 2] += shscale * areaweight * rgba[2] * dc[i]; } totalarea += areaweight; } } //complete the integration by dividing by total area scale( ref sh, 16f / totalarea ); }
IEnumerator ConvolveSpecularCubeMap() { int size = 0; int samples = 0; size = CubeSizeSetup(false); samples = qualitySetup(false); if(radianceModel == radianceEnum.BlinnPhong) { convolveSpecularSkybox = new Material(Shader.Find("Hidden/Antonov Suit/Radiance/Blinn")); } if(radianceModel == radianceEnum.GGX) { convolveSpecularSkybox = new Material(Shader.Find("Hidden/Antonov Suit/Radiance/GGX")); } convolveSpecularSkybox.SetInt("_specSamples",samples); convolveSpecularSkybox.SetInt("_specularSize", size); convolveSpecularSkybox.SetTexture("_SpecCubeIBL", specularCube); UnityEngine.RenderSettings.skybox = convolveSpecularSkybox; Cubemap tempCube = new Cubemap(size, TextureFormat.ARGB32, true); for(int mip = 0; (size >> mip) > 0; mip++) { // v0.035 better way to get exponent with different cubemap size float minExponent = 0.005f; float exponent = Mathf.Max( (float)specularExponent / (float)size * (float)mip, minExponent ); /* float[] expVal = new float [] { 0.01f,0.1f,0.2f,0.3f,0.4f,0.5f,0.6f,0.7f,0.8f,0.9f,1.0f }; float exponent = expVal[mip]; convolveSpecularSkybox.SetFloat("_Shininess", exponent ); */ if( mip == 0 ) { convolveSpecularSkybox.SetFloat("_Shininess", minExponent); } if( mip != 0 && radianceModel == radianceEnum.GGX) { convolveSpecularSkybox.SetFloat("_Shininess", exponent + 0.05f); } if( mip != 0 && radianceModel == radianceEnum.BlinnPhong) { convolveSpecularSkybox.SetFloat("_Shininess", exponent); } int cubeSize = Mathf.Max(1, tempCube.width >> mip ); Cubemap mipCube = new Cubemap(cubeSize, TextureFormat.ARGB32, false); if( hasPro == true ) { cubeCamera.RenderToCubemap(mipCube); for(int f=0; f<6; ++f) { CubemapFace face = (CubemapFace)f; tempCube.SetPixels(mipCube.GetPixels(face), face, mip); } } else { yield return StartCoroutine(CaptureImportanceSample(tempCube, CubemapFace.PositiveZ, cubeCamera,mip)); yield return StartCoroutine(CaptureImportanceSample(tempCube, CubemapFace.PositiveX, cubeCamera,mip)); yield return StartCoroutine(CaptureImportanceSample(tempCube, CubemapFace.NegativeX, cubeCamera,mip)); yield return StartCoroutine(CaptureImportanceSample(tempCube, CubemapFace.NegativeZ, cubeCamera,mip)); yield return StartCoroutine(CaptureImportanceSample(tempCube, CubemapFace.PositiveY, cubeCamera,mip)); yield return StartCoroutine(CaptureImportanceSample(tempCube, CubemapFace.NegativeY, cubeCamera,mip)); } } // v0.035 this fix the ugly mipmap transition tempCube.filterMode = FilterMode.Trilinear; tempCube.wrapMode = TextureWrapMode.Clamp; if (SystemInfo.graphicsShaderLevel != 50) { tempCube.SmoothEdges(smoothEdge); } tempCube.Apply(false); specularCube = tempCube; string convolvedSpecularPath = GetOutPutPath(specularCube,false); AssetDatabase.CreateAsset(specularCube, convolvedSpecularPath); SerializedObject serializedCubemap = new SerializedObject(specularCube); SetLinearSpace(ref serializedCubemap, false); yield return StartCoroutine(CaptureFinished()); }
public static Texture2D CreateSphericalMapFromCubeMap(Cubemap cubemap) { int sphericalMapSizeX = 5000;// cubemap.width * 2; int sphericalMapSizeY = 2500;// cubemap.height * 1; Debug.Log("CubeMap transfer to SphericalMap start"); Color[][] facePixels = new Color[6][]; for (int a = 0; a < 6; a++) { facePixels[a] = cubemap.GetPixels((CubemapFace)a); //facePixels[a] = new Color[cubemap.width * cubemap.height]; //pixels.CopyTo(facePixels[a], 0); } Color[] sphereColors = null; Color32[] sphereColors32 = null; if (ThreadProcessData.useColor32) sphereColors32 = new Color32[sphericalMapSizeX * sphericalMapSizeY]; else sphereColors = new Color[sphericalMapSizeX * sphericalMapSizeY]; //Texture2D sphereTex = new Texture2D(sphericalMapSizeX, sphericalMapSizeY, TextureFormat.RGB24, false); //for (int a = 0; a < sphereColors.Length; a++) //{ // sphereColors[a] = Color.red; //} //I prefer use 1 thread to process at least 1 line on spherical map. Debug.Assert(sphericalMapSizeY >= ThreadProcessData.maxThreadAmount);//I don't want to handle sphericalMapSizeY/maxThreadAmount=0 int eachThreadProcessLine = Mathf.FloorToInt((float)sphericalMapSizeY / (float)ThreadProcessData.maxThreadAmount + 0.5f); ThreadProcessData[] threadProcessData = new ThreadProcessData[ThreadProcessData.maxThreadAmount]; int processLineStart = 1;//start from 1 (not 0) int processLineEnd = processLineStart + eachThreadProcessLine - 1; for (int a = 0; a < ThreadProcessData.maxThreadAmount; a++) { threadProcessData[a] = new ThreadProcessData(); threadProcessData[a].processLineStart = processLineStart; threadProcessData[a].processLineEnd = processLineEnd; threadProcessData[a].sphericalMapSizeX = sphericalMapSizeX; threadProcessData[a].sphericalMapSizeY = sphericalMapSizeY; threadProcessData[a].facePixels = facePixels; threadProcessData[a].cubemapW = cubemap.width; threadProcessData[a].cubemapH = cubemap.height; threadProcessData[a].sphereColor = sphereColors; threadProcessData[a].sphereColor32 = sphereColors32; processLineStart = processLineEnd + 1; processLineEnd = processLineStart + eachThreadProcessLine - 1; if (processLineEnd > sphericalMapSizeY) processLineEnd = sphericalMapSizeY; } System.Threading.ManualResetEvent[] doneEvents = new System.Threading.ManualResetEvent[threadProcessData.Length]; int count = 0; foreach (ThreadProcessData data in threadProcessData) { doneEvents[count] = new System.Threading.ManualResetEvent(false); data.doneEvent = doneEvents[count]; System.Threading.WaitCallback waitCallback = new System.Threading.WaitCallback(WorkA); System.Threading.ThreadPool.QueueUserWorkItem(waitCallback, data); count++; } // Wait for all threads in pool to calculate. System.Threading.WaitHandle.WaitAll(doneEvents); Debug.Log("All calculations are complete."); //sphereColor Texture2D sphereTex = null; if (ThreadProcessData.useColor32) { sphereTex = new Texture2D(sphericalMapSizeX, sphericalMapSizeY, TextureFormat.ARGB32, false); sphereTex.SetPixels32(sphereColors32); } else { sphereTex = new Texture2D(sphericalMapSizeX, sphericalMapSizeY, TextureFormat.RGB24, false); sphereTex.SetPixels(sphereColors); } //else //{ // //for (int x = 0; x < sphereTex.width; x++) // // for (int y = 0; y < sphereTex.height; y++) // // sphereTex.SetPixel(x, y, Color.black); // for (int x = 0; x < sphericalMapSizeX; x++) // { // for (int y = 0; y < sphericalMapSizeY; y++) // { // Color color; // _processSpherePixel(x, y, sphericalMapSizeX, sphericalMapSizeY, facePixels, cubemap.width, cubemap.height, out color); // sphereTex.SetPixel(x, y, color); // } // } //} //var bytes = sphereTex.EncodeToJPG(); //File.WriteAllBytes(Application.dataPath + "/cube2sphere.jpg", bytes); //DestroyImmediate(sphereTex); Debug.Log("CubeMap transfer to SphericalMap done!!"); return sphereTex; }
/// <summary> /// Save unity cubemap into NPOT 6x1 cubemap/texture atlas in the following format PX NX PY NY PZ NZ /// </summary> /// <description> /// Supported format: PNG/JPG /// Default file name: using current time OVR_hh_mm_ss.png /// </description> public static bool SaveCubemapCapture(Cubemap cubemap, string pathName = null) { string fileName; string dirName; int width = cubemap.width; int height = cubemap.height; int x = 0; int y = 0; bool saveToPNG = true; if (string.IsNullOrEmpty(pathName)) { dirName = Application.persistentDataPath + "/OVR_ScreenShot360/"; fileName = null; } else { dirName = Path.GetDirectoryName(pathName); fileName = Path.GetFileName(pathName); if (dirName[dirName.Length - 1] != '/' || dirName[dirName.Length - 1] != '\\') dirName += "/"; } if (string.IsNullOrEmpty(fileName)) fileName = "OVR_" + System.DateTime.Now.ToString("hh_mm_ss") + ".png"; string extName = Path.GetExtension(fileName); if (extName == ".png") { saveToPNG = true; } else if (extName == ".jpg") { saveToPNG = false; } else { Debug.LogError("Unsupported file format" + extName); return false; } // Validate path DirectoryInfo dirInfo; try { dirInfo = System.IO.Directory.CreateDirectory(dirName); } catch (System.Exception e) { Debug.LogError("Failed to create path " + dirName + " since " + e.ToString()); return false; } // Create the new texture Texture2D tex = new Texture2D(width * 6, height, TextureFormat.RGB24, false); if (tex == null) { Debug.LogError("[OVRScreenshotWizard] Failed creating the texture!"); return false; } // Merge all the cubemap faces into the texture // Reference cubemap format: http://docs.unity3d.com/Manual/class-Cubemap.html CubemapFace[] faces = new CubemapFace[] { CubemapFace.PositiveX, CubemapFace.NegativeX, CubemapFace.PositiveY, CubemapFace.NegativeY, CubemapFace.PositiveZ, CubemapFace.NegativeZ }; for (int i = 0; i < faces.Length; i++) { // get the pixels from the cubemap Color[] srcPixels = null; Color[] pixels = cubemap.GetPixels(faces[i]); // if desired, flip them as they are ordered left to right, bottom to top srcPixels = new Color[pixels.Length]; for (int y1 = 0; y1 < height; y1++) { for (int x1 = 0; x1 < width; x1++) { srcPixels[y1 * width + x1] = pixels[((height - 1 - y1) * width) + x1]; } } // Copy them to the dest texture tex.SetPixels(x, y, width, height, srcPixels); x += width; } try { // Encode the texture and save it to disk byte[] bytes = saveToPNG ? tex.EncodeToPNG() : tex.EncodeToJPG(); System.IO.File.WriteAllBytes(dirName + fileName, bytes); Debug.Log("Cubemap file created " + dirName + fileName); } catch (System.Exception e) { Debug.LogError("Failed to save cubemap file since " + e.ToString()); return false; } DestroyImmediate(tex); return true; }
static public void CubemapToPNG(Cubemap sourceCubemap, string outputFolderPath, bool includePositiveX, bool includeNegativeX, bool includePositiveY, bool includeNegativeY, bool includePositiveZ, bool includeNegativeZ) { // Set up our local variables for later use int sizeX = sourceCubemap.width; int sizeY = sourceCubemap.height; CubemapFace face = CubemapFace.PositiveX; // Loop through all Directions for(int b=0; b<6; b++) { switch(b) { case 0: if (includePositiveX) face = CubemapFace.PositiveX; break; case 1: if(includeNegativeX) face = CubemapFace.NegativeX; break; case 2: if(includePositiveY) face = CubemapFace.PositiveY; break; case 3: if(includeNegativeY) face = CubemapFace.NegativeY; break; case 4: if(includePositiveZ) face = CubemapFace.PositiveZ; break; case 5: if(includeNegativeZ) face = CubemapFace.NegativeZ; break; } // If Face is +X but +X is not allowed then we just skip it // We have to do this because our initial Face variable is set to PositiveX, // which without this condition would cause PositiveX to always be created if (face == CubemapFace.PositiveX && !includePositiveX) { // Do Nothing } else { //Create the blank texture container Texture2D snapshot = new Texture2D(sizeX, sizeY, TextureFormat.RGB24, true); snapshot.wrapMode = TextureWrapMode.Clamp; Color[] cubemapColors = sourceCubemap.GetPixels(face); snapshot.SetPixels32(CubemapHelpers.MirrorColor32(cubemapColors)); // Mirror the snapshot image for our PNG in order to be identical with the cubemap faces snapshot.Apply(); // Convert to PNG file byte[] bytes = snapshot.EncodeToPNG(); // Save the file string path = Application.dataPath + "/" + outputFolderPath + "/" + sourceCubemap.name + "_" + face.ToString() + ".png"; System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs); bw.Write(bytes); bw.Close(); fs.Close(); // Fix compression state string finalImagePath = CubemapHelpers.MakeUnityPath(Application.dataPath + "/" + outputFolderPath + "/" + sourceCubemap.name + "_" + face.ToString() + ".png"); if(finalImagePath.Contains("//")) finalImagePath = finalImagePath.Replace("//", "/"); AssetDatabase.Refresh(); // refresh necessary before we can use the textureimporter TextureImporter textureImporter = AssetImporter.GetAtPath(finalImagePath) as TextureImporter; if (textureImporter != null) { textureImporter.textureFormat = TextureImporterFormat.RGB24; AssetDatabase.ImportAsset(finalImagePath); } DestroyImmediate(snapshot); } } }