Esempio n. 1
0
 public bool LoadVolume(int index)
 {
     if (isLoading)
     {
         return(false);
     }
     if (index < 0 || index >= resources.allVolume.Count)
     {
         return(false);
     }
     isLoading = true;
     IrradianceResources.Volume data = resources.allVolume[index];
     currentIrr = new LoadedIrradiance
     {
         resolution         = data.resolution,
         position           = data.position,
         localToWorld       = data.localToWorld,
         renderTextureIndex = loadedIrradiance.Length
     };
     targetPath     = data.path;
     currentTexture = new CoeffTexture(data.resolution);
     len            = (int)(data.resolution.x * data.resolution.y * data.resolution.z * 9);
     if (coeff != null && coeff.count < len)
     {
         coeff.Dispose();
         coeff = new ComputeBuffer(len, 12);
     }
     if (coeff == null)
     {
         coeff = new ComputeBuffer(len, 12);
     }
     LoadingThread.AddCommand((obj) =>
     {
         var controller = (IrradianceVolumeController)obj;
         controller.LoadVolumeAsync();
     }, this);
     return(true);
 }
        public IEnumerator BakeLightmap()
        {
            RenderTextureDescriptor texArrayDescriptor = new RenderTextureDescriptor
            {
                autoGenerateMips   = false,
                bindMS             = false,
                colorFormat        = RenderTextureFormat.ARGB32,
                depthBufferBits    = 16,
                dimension          = TextureDimension.Tex2DArray,
                enableRandomWrite  = false,
                height             = RESOLUTION,
                width              = RESOLUTION,
                memoryless         = RenderTextureMemoryless.None,
                msaaSamples        = 1,
                shadowSamplingMode = ShadowSamplingMode.None,
                sRGB        = false,
                useMipMap   = true,
                volumeDepth = 6,
                vrUsage     = VRTextureUsage.None
            };

            RenderTexture rt = RenderTexture.GetTemporary(texArrayDescriptor); rt.filterMode = FilterMode.Trilinear;

            rt.Create();
            texArrayDescriptor.volumeDepth = 1;
            texArrayDescriptor.dimension   = TextureDimension.Tex2D;
            RenderTexture tempRT = RenderTexture.GetTemporary(texArrayDescriptor);

            tempRT.Create();
            ComputeShader          shader = resources.shaders.probeCoeffShader;
            Action <CommandBuffer> func   = (cb) =>
            {
                cb.SetComputeBufferParam(shader, 0, "_CoeffTemp", coeffTemp);
                cb.SetComputeBufferParam(shader, 1, "_CoeffTemp", coeffTemp);
                cb.SetComputeBufferParam(shader, 1, "_Coeff", coeff);
                cb.SetComputeTextureParam(shader, 0, "_SourceCubemap", rt);
                cb.SetGlobalVector("_Tex3DSize", new Vector4(probeCount.x + 0.01f, probeCount.y + 0.01f, probeCount.z + 0.01f));
                cb.SetGlobalVector("_SHSize", transform.localScale);
                cb.SetGlobalVector("_LeftDownBack", transform.position - transform.localScale * 0.5f);
            };

            RenderPipeline.ExecuteBufferAtFrameEnding(func);
            yield return(null);

            yield return(null);

            int target = probeCount.x * probeCount.y * probeCount.z;

            for (int x = 0; x < probeCount.x; ++x)
            {
                for (int y = 0; y < probeCount.y; ++y)
                {
                    for (int z = 0; z < probeCount.z; ++z)
                    {
                        BakeMap(int3(x, y, z), rt, tempRT);
                        cbuffer.GenerateMips(rt);
                        cbuffer.SetComputeIntParam(shader, "_OffsetIndex", PipelineFunctions.DownDimension(int3(x, y, z), probeCount.xy));
                        cbuffer.DispatchCompute(shader, 0, RESOLUTION / 32, RESOLUTION / 32, 6);
                        cbuffer.DispatchCompute(shader, 1, 1, 1, 1);
                        yield return(null);
                    }
                }
            }
            isRendering = false;
            yield return(null);

            isRendered = true;
            byte[] byteArray = new byte[coeff.count * coeff.stride];
            coeff.GetData(byteArray);
            string path = "Assets/BinaryData/Irradiance/" + volumeName + ".mpipe";

            File.WriteAllBytes(path, byteArray);
            float4x4 localToWorld = transform.localToWorldMatrix;

            IrradianceResources.Volume volume = new IrradianceResources.Volume
            {
                position     = transform.position,
                localToWorld = float3x3(localToWorld.c0.xyz, localToWorld.c1.xyz, localToWorld.c2.xyz),
                resolution   = (uint3)probeCount,
                volumeName   = volumeName,
                path         = path
            };
            Debug.Log(volume.volumeName);
            saveTarget.allVolume[indexInList] = volume;
            EditorUtility.SetDirty(saveTarget);
            RenderTexture.ReleaseTemporary(rt);
            RenderTexture.ReleaseTemporary(tempRT);
            yield return(null);

            Dispose();
        }