public bool AllocTextureArray(int numCubeMaps, int width, TextureFormat format, bool isMipMapped) { var res = AllocTextureArray(numCubeMaps); m_NumMipLevels = GetNumMips(width, width); // will calculate same way whether we have cube array or not if (!TextureCache.supportsCubemapArrayTextures) { if (!m_CubeBlitMaterial) { m_CubeBlitMaterial = new Material(Shader.Find("Hidden/CubeToPano")) { hideFlags = HideFlags.HideAndDontSave } } ; int panoWidthTop = 4 * width; int panoHeightTop = 2 * width; // create panorama 2D array. Hardcoding the render target for now. No convenient way atm to // map from TextureFormat to RenderTextureFormat and don't want to deal with sRGB issues for now. m_CacheNoCubeArray = new Texture2DArray(panoWidthTop, panoHeightTop, numCubeMaps, TextureFormat.RGBAHalf, isMipMapped) { hideFlags = HideFlags.HideAndDontSave, wrapMode = TextureWrapMode.Repeat, wrapModeV = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, anisoLevel = 0 }; m_NumPanoMipLevels = isMipMapped ? GetNumMips(panoWidthTop, panoHeightTop) : 1; m_StagingRTs = new RenderTexture[m_NumPanoMipLevels]; for (int m = 0; m < m_NumPanoMipLevels; m++) { m_StagingRTs[m] = new RenderTexture(Mathf.Max(1, panoWidthTop >> m), Mathf.Max(1, panoHeightTop >> m), 0, RenderTextureFormat.ARGBHalf) { hideFlags = HideFlags.HideAndDontSave }; } if (m_CubeBlitMaterial) { m_CubeMipLevelPropName = Shader.PropertyToID("_cubeMipLvl"); m_cubeSrcTexPropName = Shader.PropertyToID("_srcCubeTexture"); } } else { m_Cache = new CubemapArray(width, numCubeMaps, format, isMipMapped) { hideFlags = HideFlags.HideAndDontSave, wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, anisoLevel = 0 // It is important to set 0 here, else unity force anisotropy filtering }; } return(res); }
void CreateCubeArrayAsset() { //Texture2DArray array = new Texture2DArray(textures[0].width, textures[0].height, textures.Length, GraphicsFormatUtility.GetGraphicsFormat(TextureFormat.ARGB32, false), TextureCreationFlags.None); TextureFormat tf = cubeMaps[0].format; int mipLevel = cubeMaps[0].mipmapCount; CubemapArray array = new CubemapArray(cubeMaps[0].width, cubeMaps.Length, texFormat, mipmaps); for (int i = 0; i < 6; i++) //iterate for each cube face { for (int j = 0; j < cubeMaps.Length; j++) { for (int m = 0; m < mipLevel; m++) { CubemapFace face = (CubemapFace)i; array.SetPixels(cubeMaps[j].GetPixels(face), face, j, m); } } } for (int j = 0; j < 6; j++) { //CubemapFace face = (CubemapFace)i; // array.SetPixels(textures[j].GetPixels(), face, j); for (int i = 0; i < mipLevel; i++) { //for() // Graphics.CopyTexture(textures[j], 0, i, array, j, i); } } array.Apply(); AssetDatabase.CreateAsset(array, "Assets/CubemapArray.asset"); }
public SkyRenderingContext(int resolution, int bsdfCount, bool supportsConvolution, SphericalHarmonicsL2 ambientProbe, string name) { m_AmbientProbe = ambientProbe; this.supportsConvolution = supportsConvolution; // Compute buffer storing the resulting SH from diffuse convolution. L2 SH => 9 float per component. ambientProbeResult = new ComputeBuffer(27, 4); // Buffer is stored packed to be used directly by shader code (27 coeffs in 7 float4) // Compute buffer storing the pre-convolved resulting SH For volumetric lighting. L2 SH => 9 float per component. volumetricAmbientProbeBuffer = new ComputeBuffer(7, 16); // Compute buffer storing the diffuse convolution SH For diffuse ambient lighting. L2 SH => 9 float per component. diffuseAmbientProbeBuffer = new ComputeBuffer(7, 16); skyboxCubemapRT = RTHandles.Alloc(resolution, resolution, colorFormat: GraphicsFormat.R16G16B16A16_SFloat, dimension: TextureDimension.Cube, useMipMap: true, autoGenerateMips: false, filterMode: FilterMode.Trilinear, name: name); if (supportsConvolution) { skyboxBSDFCubemapArray = new CubemapArray(resolution, bsdfCount, GraphicsFormat.R16G16B16A16_SFloat, TextureCreationFlags.MipChain) { hideFlags = HideFlags.HideAndDontSave, wrapMode = TextureWrapMode.Repeat, wrapModeV = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, anisoLevel = 0, name = "SkyboxCubemapConvolution" }; } }
void InitializeBlackCubemapArray() { if (m_BlackCubemapArray == null) { m_BlackCubemapArray = new CubemapArray(1, m_IBLFilterArray.Length, TextureFormat.RGBA32, false) { hideFlags = HideFlags.HideAndDontSave, wrapMode = TextureWrapMode.Repeat, wrapModeV = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, anisoLevel = 0, name = "BlackCubemapArray" }; Color32[] black = { new Color32(0, 0, 0, 0) }; for (int element = 0; element < m_IBLFilterArray.Length; ++element) { for (int i = 0; i < 6; i++) { m_BlackCubemapArray.SetPixels32(black, (CubemapFace)i, element); } } m_BlackCubemapArray.Apply(); } }
public void Init() { System.IO.Directory.CreateDirectory("Assets/Temp"); m_cubeEmpty = GameObject.CreatePrimitive(PrimitiveType.Cube); m_sphereEmpty = GameObject.CreatePrimitive(PrimitiveType.Sphere); m_pathTexture2D_A = "Assets/texture2D_A.asset"; m_pathTexture2D_B = "Assets/texture2D_B.asset"; m_texture2D_A = new Texture2D(16, 16); m_texture2D_B = new Texture2D(32, 32); AssetDatabase.CreateAsset(m_texture2D_A, m_pathTexture2D_A); AssetDatabase.CreateAsset(m_texture2D_B, m_pathTexture2D_B); m_texture2D_A = AssetDatabase.LoadAssetAtPath <Texture2D>(m_pathTexture2D_A); m_texture2D_B = AssetDatabase.LoadAssetAtPath <Texture2D>(m_pathTexture2D_B); m_pathTexture2DArray_A = "Assets/texture2DArray_A.asset"; m_pathTexture2DArray_B = "Assets/texture2DArray_B.asset"; m_texture2DArray_A = new Texture2DArray(16, 16, 4, TextureFormat.ARGB32, false); m_texture2DArray_B = new Texture2DArray(32, 32, 4, TextureFormat.ARGB32, false); AssetDatabase.CreateAsset(m_texture2DArray_A, m_pathTexture2DArray_A); AssetDatabase.CreateAsset(m_texture2DArray_B, m_pathTexture2DArray_B); m_texture2DArray_A = AssetDatabase.LoadAssetAtPath <Texture2DArray>(m_pathTexture2DArray_A); m_texture2DArray_B = AssetDatabase.LoadAssetAtPath <Texture2DArray>(m_pathTexture2DArray_B); m_pathTexture3D_A = "Assets/texture3D_A.asset"; m_pathTexture3D_B = "Assets/texture3D_B.asset"; m_texture3D_A = new Texture3D(16, 16, 16, TextureFormat.ARGB32, false); m_texture3D_B = new Texture3D(8, 8, 8, TextureFormat.ARGB32, false); AssetDatabase.CreateAsset(m_texture3D_A, m_pathTexture3D_A); AssetDatabase.CreateAsset(m_texture3D_B, m_pathTexture3D_B); m_texture3D_A = AssetDatabase.LoadAssetAtPath <Texture3D>(m_pathTexture3D_A); m_texture3D_B = AssetDatabase.LoadAssetAtPath <Texture3D>(m_pathTexture3D_B); m_pathTextureCube_A = "Assets/textureCube_A.asset"; m_pathTextureCube_B = "Assets/textureCube_B.asset"; m_textureCube_A = new Cubemap(16, TextureFormat.ARGB32, false); m_textureCube_B = new Cubemap(32, TextureFormat.ARGB32, false); AssetDatabase.CreateAsset(m_textureCube_A, m_pathTextureCube_A); AssetDatabase.CreateAsset(m_textureCube_B, m_pathTextureCube_B); m_textureCube_A = AssetDatabase.LoadAssetAtPath <Cubemap>(m_pathTextureCube_A); m_textureCube_B = AssetDatabase.LoadAssetAtPath <Cubemap>(m_pathTextureCube_B); m_pathTextureCubeArray_A = "Assets/textureCubeArray_A.asset"; m_pathTextureCubeArray_B = "Assets/textureCubeArray_B.asset"; m_textureCubeArray_A = new CubemapArray(16, 4, TextureFormat.ARGB32, false); m_textureCubeArray_B = new CubemapArray(32, 4, TextureFormat.ARGB32, false); AssetDatabase.CreateAsset(m_textureCubeArray_A, m_pathTextureCubeArray_A); AssetDatabase.CreateAsset(m_textureCubeArray_B, m_pathTextureCubeArray_B); m_textureCubeArray_A = AssetDatabase.LoadAssetAtPath <CubemapArray>(m_pathTextureCubeArray_A); m_textureCubeArray_B = AssetDatabase.LoadAssetAtPath <CubemapArray>(m_pathTextureCubeArray_B); m_mainObject = new GameObject("TestObject"); m_mainCamera = new GameObject(); var camera = m_mainCamera.AddComponent <Camera>(); camera.transform.localPosition = Vector3.one; camera.transform.LookAt(m_mainCamera.transform); }
public static int constructor(IntPtr l) { int result; try { int num = LuaDLL.lua_gettop(l); if (num == 5) { int faceSize; LuaObject.checkType(l, 2, out faceSize); int cubemapCount; LuaObject.checkType(l, 3, out cubemapCount); TextureFormat format; LuaObject.checkEnum <TextureFormat>(l, 4, out format); bool mipmap; LuaObject.checkType(l, 5, out mipmap); CubemapArray o = new CubemapArray(faceSize, cubemapCount, format, mipmap); LuaObject.pushValue(l, true); LuaObject.pushValue(l, o); result = 2; } else if (num == 6) { int faceSize2; LuaObject.checkType(l, 2, out faceSize2); int cubemapCount2; LuaObject.checkType(l, 3, out cubemapCount2); TextureFormat format2; LuaObject.checkEnum <TextureFormat>(l, 4, out format2); bool mipmap2; LuaObject.checkType(l, 5, out mipmap2); bool linear; LuaObject.checkType(l, 6, out linear); CubemapArray o = new CubemapArray(faceSize2, cubemapCount2, format2, mipmap2, linear); LuaObject.pushValue(l, true); LuaObject.pushValue(l, o); result = 2; } else { result = LuaObject.error(l, "New object failed."); } } catch (Exception e) { result = LuaObject.error(l, e); } return(result); }
void Create() { #if UNITY_EDITOR CubemapArray textureArray = new CubemapArray(6, textureList.Length, TextureFormat.RGB24, false); for (int i = 0; i < textureList.Length; i++) { Texture2D tex = (Texture2D)textureList[i]; textureArray.SetPixels(tex.GetPixels(0), CubemapFace.PositiveX, 0); } textureArray.Apply(); AssetDatabase.CreateAsset(textureArray, savePath); Debug.Log("Saved asset to " + savePath); #endif }
public static int SetPixels32(IntPtr l) { int result; try { int num = LuaDLL.lua_gettop(l); if (num == 4) { CubemapArray cubemapArray = (CubemapArray)LuaObject.checkSelf(l); Color32[] colors; LuaObject.checkArray <Color32>(l, 2, out colors); CubemapFace face; LuaObject.checkEnum <CubemapFace>(l, 3, out face); int arrayElement; LuaObject.checkType(l, 4, out arrayElement); cubemapArray.SetPixels32(colors, face, arrayElement); LuaObject.pushValue(l, true); result = 1; } else if (num == 5) { CubemapArray cubemapArray2 = (CubemapArray)LuaObject.checkSelf(l); Color32[] colors2; LuaObject.checkArray <Color32>(l, 2, out colors2); CubemapFace face2; LuaObject.checkEnum <CubemapFace>(l, 3, out face2); int arrayElement2; LuaObject.checkType(l, 4, out arrayElement2); int miplevel; LuaObject.checkType(l, 5, out miplevel); cubemapArray2.SetPixels32(colors2, face2, arrayElement2, miplevel); LuaObject.pushValue(l, true); result = 1; } else { LuaObject.pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function SetPixels32 to call"); result = 2; } } catch (Exception e) { result = LuaObject.error(l, e); } return(result); }
public bool AllocTextureArray(int numCubeMaps, int width, TextureFormat format, bool isMipMapped) { var res = AllocTextureArray(6 * numCubeMaps); m_NumMipLevels = GetNumMips(width, width); m_Cache = new CubemapArray(width, numCubeMaps, format, isMipMapped) { hideFlags = HideFlags.HideAndDontSave, wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, anisoLevel = 0 // It is important to set 0 here, else unity force anisotropy filtering }; return(res); }
public static int Apply(IntPtr l) { int result; try { int num = LuaDLL.lua_gettop(l); if (num == 1) { CubemapArray cubemapArray = (CubemapArray)LuaObject.checkSelf(l); cubemapArray.Apply(); LuaObject.pushValue(l, true); result = 1; } else if (num == 2) { CubemapArray cubemapArray2 = (CubemapArray)LuaObject.checkSelf(l); bool updateMipmaps; LuaObject.checkType(l, 2, out updateMipmaps); cubemapArray2.Apply(updateMipmaps); LuaObject.pushValue(l, true); result = 1; } else if (num == 3) { CubemapArray cubemapArray3 = (CubemapArray)LuaObject.checkSelf(l); bool updateMipmaps2; LuaObject.checkType(l, 2, out updateMipmaps2); bool makeNoLongerReadable; LuaObject.checkType(l, 3, out makeNoLongerReadable); cubemapArray3.Apply(updateMipmaps2, makeNoLongerReadable); LuaObject.pushValue(l, true); result = 1; } else { LuaObject.pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function Apply to call"); result = 2; } } catch (Exception e) { result = LuaObject.error(l, e); } return(result); }
public static int get_format(IntPtr l) { int result; try { CubemapArray cubemapArray = (CubemapArray)LuaObject.checkSelf(l); LuaObject.pushValue(l, true); LuaObject.pushEnum(l, (int)cubemapArray.format); result = 2; } catch (Exception e) { result = LuaObject.error(l, e); } return(result); }
public static int GetPixels(IntPtr l) { int result; try { int num = LuaDLL.lua_gettop(l); if (num == 3) { CubemapArray cubemapArray = (CubemapArray)LuaObject.checkSelf(l); CubemapFace face; LuaObject.checkEnum <CubemapFace>(l, 2, out face); int arrayElement; LuaObject.checkType(l, 3, out arrayElement); Color[] pixels = cubemapArray.GetPixels(face, arrayElement); LuaObject.pushValue(l, true); LuaObject.pushValue(l, pixels); result = 2; } else if (num == 4) { CubemapArray cubemapArray2 = (CubemapArray)LuaObject.checkSelf(l); CubemapFace face2; LuaObject.checkEnum <CubemapFace>(l, 2, out face2); int arrayElement2; LuaObject.checkType(l, 3, out arrayElement2); int miplevel; LuaObject.checkType(l, 4, out miplevel); Color[] pixels2 = cubemapArray2.GetPixels(face2, arrayElement2, miplevel); LuaObject.pushValue(l, true); LuaObject.pushValue(l, pixels2); result = 2; } else { LuaObject.pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function GetPixels to call"); result = 2; } } catch (Exception e) { result = LuaObject.error(l, e); } return(result); }
public void RebuildTextures(int resolution) { bool updateNeeded = m_SkyboxCubemapRT == null || (m_SkyboxCubemapRT.rt.width != resolution); // Cleanup first if needed if (updateNeeded) { RTHandles.Release(m_SkyboxCubemapRT); m_SkyboxCubemapRT = null; RTHandles.Release(m_SkyboxBSDFCubemapIntermediate); m_SkyboxBSDFCubemapIntermediate = null; CoreUtils.Destroy(m_SkyboxBSDFCubemapArray); m_SkyboxBSDFCubemapArray = null; } // Reallocate everything if (m_SkyboxCubemapRT == null) { m_SkyboxCubemapRT = RTHandles.Alloc(resolution, resolution, colorFormat: HDRenderPipeline.OverrideRTGraphicsFormat(GraphicsFormat.R16G16B16A16_SFloat), dimension: TextureDimension.Cube, useMipMap: true, autoGenerateMips: false, filterMode: FilterMode.Trilinear, name: "SkyboxCubemap"); if (m_SupportsConvolution) { m_SkyboxBSDFCubemapIntermediate = RTHandles.Alloc(resolution, resolution, colorFormat: UnityEngine.Experimental.Rendering.HDPipeline.HDRenderPipeline.OverrideRTGraphicsFormat(GraphicsFormat.R16G16B16A16_SFloat), dimension: TextureDimension.Cube, useMipMap: true, autoGenerateMips: false, filterMode: FilterMode.Trilinear, name: "SkyboxBSDFIntermediate"); m_SkyboxBSDFCubemapArray = new CubemapArray(resolution, m_IBLFilterArray.Length, TextureFormat.RGBAHalf, true) { hideFlags = HideFlags.HideAndDontSave, wrapMode = TextureWrapMode.Repeat, wrapModeV = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, anisoLevel = 0, name = "SkyboxCubemapConvolution" }; } } m_CubemapScreenSize = new Vector4((float)resolution, (float)resolution, 1.0f / (float)resolution, 1.0f / (float)resolution); if (updateNeeded) { m_NeedUpdate = true; // Special case. Even if update mode is set to OnDemand, we need to regenerate the environment after destroying the texture. RebuildSkyMatrices(resolution); } }
public SkyRenderingContext(int resolution, int bsdfCount, bool supportsConvolution, SphericalHarmonicsL2 ambientProbe) { m_AmbientProbe = ambientProbe; this.supportsConvolution = supportsConvolution; // Compute buffer storing the resulting SH from diffuse convolution. L2 SH => 9 float per component. ambientProbeResult = new ComputeBuffer(27, 4); skyboxCubemapRT = RTHandles.Alloc(resolution, resolution, colorFormat: GraphicsFormat.R16G16B16A16_SFloat, dimension: TextureDimension.Cube, useMipMap: true, autoGenerateMips: false, filterMode: FilterMode.Trilinear, name: "SkyboxCubemap"); if (supportsConvolution) { skyboxBSDFCubemapArray = new CubemapArray(resolution, bsdfCount, TextureFormat.RGBAHalf, true) { hideFlags = HideFlags.HideAndDontSave, wrapMode = TextureWrapMode.Repeat, wrapModeV = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, anisoLevel = 0, name = "SkyboxCubemapConvolution" }; } }
static public VFXValue <int> Constant(CubemapArray value) { return(new VFXTextureCubeArrayValue(value.GetInstanceID(), Mode.Constant)); }
static public VFXValue <Texture> Constant(CubemapArray value) { return(new VFXTextureCubeArrayValue(value, Mode.Constant)); }
public void RebuildTextures(int resolution) { bool updateNeeded = m_SkyboxCubemapRT == null || (m_SkyboxCubemapRT.rt.width != resolution); // Cleanup first if needed if (updateNeeded) { RTHandles.Release(m_SkyboxCubemapRT); m_SkyboxCubemapRT = null; RTHandles.Release(m_SkyboxBSDFCubemapIntermediate); m_SkyboxBSDFCubemapIntermediate = null; CoreUtils.Destroy(m_SkyboxBSDFCubemapArray); m_SkyboxBSDFCubemapArray = null; RTHandles.Release(m_SkyboxConditionalCdfRT); m_SkyboxConditionalCdfRT = null; RTHandles.Release(m_SkyboxMarginalRowCdfRT); m_SkyboxMarginalRowCdfRT = null; } // Reallocate everything if (m_SkyboxCubemapRT == null) { m_SkyboxCubemapRT = RTHandles.Alloc(resolution, resolution, colorFormat: GraphicsFormat.R16G16B16A16_SFloat, dimension: TextureDimension.Cube, useMipMap: true, autoGenerateMips: false, filterMode: FilterMode.Trilinear, name: "SkyboxCubemap"); if (m_SupportsConvolution) { m_SkyboxBSDFCubemapIntermediate = RTHandles.Alloc(resolution, resolution, colorFormat: GraphicsFormat.R16G16B16A16_SFloat, dimension: TextureDimension.Cube, useMipMap: true, autoGenerateMips: false, filterMode: FilterMode.Trilinear, name: "SkyboxBSDFIntermediate"); m_SkyboxBSDFCubemapArray = new CubemapArray(resolution, m_IBLFilterArray.Length, TextureFormat.RGBAHalf, true) { hideFlags = HideFlags.HideAndDontSave, wrapMode = TextureWrapMode.Repeat, wrapModeV = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, anisoLevel = 0, name = "SkyboxCubemapConvolution" }; } if (m_SupportsMIS) { // Temporary, it should be dependent on the sky resolution int width = (int)LightSamplingParameters.TextureWidth; int height = (int)LightSamplingParameters.TextureHeight; // + 1 because we store the value of the integral of the cubemap at the end of the texture. m_SkyboxMarginalRowCdfRT = RTHandles.Alloc(height + 1, 1, colorFormat: GraphicsFormat.R32_SFloat, useMipMap: false, enableRandomWrite: true, filterMode: FilterMode.Point, name: "SkyboxMarginalRowCdf"); // TODO: switch the format to R16 (once it's available) to save some bandwidth. m_SkyboxConditionalCdfRT = RTHandles.Alloc(width, height, colorFormat: GraphicsFormat.R32_SFloat, useMipMap: false, enableRandomWrite: true, filterMode: FilterMode.Point, name: "SkyboxConditionalRowCdf"); } } m_CubemapScreenSize = new Vector4((float)resolution, (float)resolution, 1.0f / (float)resolution, 1.0f / (float)resolution); if (updateNeeded) { m_NeedUpdate = true; // Special case. Even if update mode is set to OnDemand, we need to regenerate the environment after destroying the texture. RebuildSkyMatrices(resolution); } }
// Use this for initialization public void Awake() { OnDestroy(); if (UseCustomResolution) { _targetResolution = new Resolution { width = CustomResolution.x, height = CustomResolution.y } } ; else { _targetResolution = Screen.currentResolution; } AmbientOcclusionDrt.Init("AmbientOcclusion", _targetResolution, RenderTextureFormat.ARGBFloat, TextureDimension.Tex2DArray, 2); // Create Render Texture _deferredOutput = new RenderTexture(_targetResolution.width, _targetResolution.height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear) { enableRandomWrite = true }; _deferredOutput.Create(); _sphereTracingData = new RenderTexture(_targetResolution.width, _targetResolution.height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear) { enableRandomWrite = true, useMipMap = false, autoGenerateMips = false, dimension = TextureDimension.Tex2DArray, volumeDepth = 6 }; _sphereTracingData.Create(); _sphereTracingDataLow = new RenderTexture(AmbientOcclusionDrt.Resolution.width, AmbientOcclusionDrt.Resolution.height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear) { enableRandomWrite = true, useMipMap = false, autoGenerateMips = false, dimension = TextureDimension.Tex2DArray, volumeDepth = 6 }; _sphereTracingDataLow.Create(); _fakeCubemapRenderTexture = new RenderTexture(CubemapResolution, CubemapResolution, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear) { enableRandomWrite = true, useMipMap = false, autoGenerateMips = false, anisoLevel = 0, dimension = TextureDimension.Tex2DArray, volumeDepth = 6 }; _fakeCubemapRenderTexture.Create(); _fakeCubemapArrayRenderTexture = new RenderTexture(CubemapResolution, CubemapResolution, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear) { enableRandomWrite = true, useMipMap = false, autoGenerateMips = false, anisoLevel = 0, dimension = TextureDimension.Tex2DArray, volumeDepth = 6 * ConvolutionLayerCount }; _fakeCubemapArrayRenderTexture.Create(); _environmentMap = new Cubemap(CubemapResolution, TextureFormat.RGBAHalf, false) { hideFlags = HideFlags.HideAndDontSave, wrapMode = TextureWrapMode.Clamp, anisoLevel = 0 }; _convolutedEnvironmentMapArray = new CubemapArray(CubemapResolution, ConvolutionLayerCount, TextureFormat.RGBAHalf, false) { hideFlags = HideFlags.HideAndDontSave, wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Trilinear, anisoLevel = 0 }; _sphereTracingFKernels = InitComputeKernels(SphereTracingShader, _targetResolution, 1, "SphereTracingFPassH", "SphereTracingFPassM", "SphereTracingFPassL"); _sphereTracingKKernels = InitComputeKernels(SphereTracingShader, _targetResolution, 1, "SphereTracingKPassH", "SphereTracingKPassM", "SphereTracingKPassL"); _sphereTracingDownSamplerKernels = InitComputeKernels(SphereTracingDownSampler, AmbientOcclusionDrt.Resolution, 2, "SphereTracingDownSampleH", "SphereTracingDownSampleM", "SphereTracingDownSampleL"); _sphereTracingAoKernels = InitComputeKernels(AmbientOcclusionShader, AmbientOcclusionDrt.Resolution, 1, "AmbientOcclusionH", "AmbientOcclusionM", "AmbientOcclusionL"); _sphereTracingAoUpSamplerKernels = InitComputeKernels(AmbientOcclusionUpSampler, _targetResolution, 2, "AmbientOcclusionUpSampleH", "AmbientOcclusionUpSampleM", "AmbientOcclusionUpSampleL"); _horizontalBilateralFilterKernels = InitComputeKernels(BilateralFilterShader, _targetResolution, 2, "AOHorizontalH", "AOHorizontalM", "AOHorizontalL"); _verticalBilateralFilterKernels = InitComputeKernels(BilateralFilterShader, _targetResolution, 2, "AOVerticalH", "AOVerticalM", "AOVerticalL"); _deferredKernels = InitComputeKernels(DeferredShader, _targetResolution, 1, "DeferredH", "DeferredM", "DeferredL"); var environmentMapResolution = new Resolution { width = CubemapResolution, height = CubemapResolution }; //With 6 z Dispatch groups, one for each side of cubemap _environmentMapRendererKernels = InitComputeKernels(EnvironmentMapRenderer, environmentMapResolution, 6, "RenderEnvironmentMapH", "RenderEnvironmentMapM", "RenderEnvironmentMapL"); //With 6 * ConvolutionLayerCount z Dispatch groups. One for each side of cubemap per convoluted cubemap _environmentMapConvolutionKernels = InitComputeKernels(EnvironmentMapConvolution, environmentMapResolution, 6 * ConvolutionLayerCount, "ConvoluteEnvironmentMapH", "ConvoluteEnvironmentMapM", "ConvoluteEnvironmentMapL"); GenerateAmbientOcclusionSamples(); InitLights(); InitMaterials(); InitMatrices(); SetShaderPropertiesOnce(); //At least we have to render environmentmap one RenderEnvironmentMap(); }
public override void OnImportAsset(AssetImportContext ctx) { var width = 64; var mipmapEnabled = true; var textureFormat = TextureFormat.RGBA32; var srgbTexture = true; // Mark all input textures as dependency to the CubemapArray. // This causes the CubemapArray to get re-generated when any input texture changes or when the build target changed. for (var n = 0; n < m_Cubemaps.Count; ++n) { var source = m_Cubemaps[n]; if (source != null) { var path = AssetDatabase.GetAssetPath(source); #if UNITY_2020_1_OR_NEWER ctx.DependsOnArtifact(path); #else ctx.DependsOnSourceAsset(path); #endif } } #if !UNITY_2020_1_OR_NEWER // This value is not really used in this importer, // but getting the build target here will add a dependency to the current active buildtarget. // Because DependsOnArtifact does not exist in 2019.4, adding this dependency on top of the DependsOnSourceAsset // will force a re-import when the target platform changes in case it would have impacted any texture this importer depends on. var buildTarget = ctx.selectedBuildTarget; #endif // Check if the input textures are valid to be used to build the texture array. var isValid = Verify(ctx, false); if (isValid) { // Use the texture assigned to the first slice as "master". // This means all other textures have to use same settings as the master texture. var sourceTexture = m_Cubemaps[0]; width = sourceTexture.width; textureFormat = sourceTexture.format; var sourceTexturePath = AssetDatabase.GetAssetPath(sourceTexture); var textureImporter = (TextureImporter)AssetImporter.GetAtPath(sourceTexturePath); mipmapEnabled = textureImporter.mipmapEnabled; srgbTexture = textureImporter.sRGBTexture; } CubemapArray cubemapArray; try { // Create the texture array. // When the texture array asset is being created, there are no input textures added yet, // thus we do Max(1, Count) to make sure to add at least 1 slice. cubemapArray = new CubemapArray(width, Mathf.Max(1, m_Cubemaps.Count), textureFormat, mipmapEnabled, !srgbTexture); } catch (System.Exception e) { Debug.LogException(e); ctx.LogImportError($"Import failed '{ctx.assetPath}'.", ctx.mainObject); isValid = false; textureFormat = TextureFormat.RGBA32; cubemapArray = new CubemapArray(width, Mathf.Max(1, m_Cubemaps.Count), textureFormat, mipmapEnabled, !srgbTexture); } cubemapArray.wrapMode = m_WrapMode; cubemapArray.filterMode = m_FilterMode; cubemapArray.anisoLevel = m_AnisoLevel; if (isValid) { // If everything is valid, copy source textures over to the texture array. for (int face = 0; face < 6; face++) { for (var n = 0; n < m_Cubemaps.Count; ++n) { var source = m_Cubemaps[n]; Graphics.CopyTexture(source, face, cubemapArray, face + (n * 6)); } } } else { // If there is any error, copy a magenta colored texture into every slice. // I was thinking to only make the invalid slice magenta, but then it's way less obvious that // something isn't right with the texture array. Thus I mark the entire texture array as broken. var errorTexture = new Cubemap(width, textureFormat, mipmapEnabled); try { for (var face = 0; face < 6; ++face) { var errorPixels = errorTexture.GetPixels((CubemapFace)face); for (var n = 0; n < errorPixels.Length; ++n) { errorPixels[n] = Color.magenta; } errorTexture.SetPixels(errorPixels, (CubemapFace)face); } errorTexture.Apply(); for (int face = 0; face < 6; face++) { for (var n = 0; n < cubemapArray.cubemapCount; ++n) { Graphics.CopyTexture(errorTexture, face, cubemapArray, face + (n * 6)); } } } finally { DestroyImmediate(errorTexture); } } // this should have been named "MainAsset" to be conform with Unity, but changing it now // would break all existing CubemapArray assets, so we don't touch it. cubemapArray.Apply(false, !m_IsReadable); ctx.AddObjectToAsset("CubemapArray", cubemapArray); ctx.SetMainObject(cubemapArray); if (!isValid) { // Run the verify step again, but this time we have the main object asset. // Console logs should ping the asset, but they don't in 2019.3 beta, bug? Verify(ctx, true); } }