//renderprobes
    //--for each probes position in probe array
    //----for each face of cubemap
    //------render face with Uvshader
    //----project cubemap to atlas
    void renderProbe()
    {
        Camera UVCapture = Capture.GetComponent <Camera>();
        List <CustomRenderTextureUpdateZone> zones = new List <CustomRenderTextureUpdateZone>();

        IndirectionProbeAtlas.GetUpdateZones(zones);
        //CustomRenderTextureUpdateZone[] Tile = zones.ToArray();

        //UVCapture.depth = cameraDepth;
        //UVCapture.cullingMask = cameraLayerMask;
        //UVCapture.nearClipPlane = cameraNearPlane;
        //UVCapture.farClipPlane = cameraFarPlane;
        //UVCapture.useOcclusionCulling = cameraUseOcclusion;

        UVCapture.SetReplacementShader(UV, "RenderType");
        UVCapture.backgroundColor = Color.blue;
        UVCapture.clearFlags      = CameraClearFlags.SolidColor;
        //public void SetReplacementShader(Shader shader, string replacementTag);
        int i = 0;

        foreach (GameObject probe in ProbeArray)
        {
            Capture.transform.position = probe.transform.position;
            Capture.transform.rotation = Quaternion.identity;
            UVCapture.RenderToCubemap(SceneCapture);
            //project cubemap to atlas -> in shader:
            //sample cubemap with perpixel octomaping 128 tile in atlas

            i++;
        }
        UVCapture.enabled = false;
    }
Example #2
0
    public static int GetUpdateZones(IntPtr l)
    {
        int result;

        try
        {
            CustomRenderTexture customRenderTexture = (CustomRenderTexture)LuaObject.checkSelf(l);
            List <CustomRenderTextureUpdateZone> updateZones;
            LuaObject.checkType <List <CustomRenderTextureUpdateZone> >(l, 2, out updateZones);
            customRenderTexture.GetUpdateZones(updateZones);
            LuaObject.pushValue(l, true);
            result = 1;
        }
        catch (Exception e)
        {
            result = LuaObject.error(l, e);
        }
        return(result);
    }
Example #3
0
    void setProbeCapture()
    {
        placeProbe();
        //set camera
        Capture = new GameObject("CaptureScene");
        Capture.AddComponent <Camera>();
        UVCapture = Capture.GetComponent <Camera>();
        UVCapture.SetReplacementShader(UV, "RenderType");
        //Set camera parameters
        UVCapture.backgroundColor = Color.blue;
        UVCapture.clearFlags      = CameraClearFlags.SolidColor;
        UVCapture.allowMSAA       = false;
        UVCapture.cullingMask     = 1 << 8;

        //UVCapture.targetTexture = SceneCapture;

        //atlas data
        zones = new List <CustomRenderTextureUpdateZone>();
        IndirectionProbeAtlas.GetUpdateZones(zones);
        updating = new CustomRenderTextureUpdateZone[1];
        IndirectionProbeAtlas.Initialize();
    }
Example #4
0
    public static void UpdateCustomRenderTexture(CommandBuffer cmd, CustomRenderTexture crt, int updateCount, int mipLevel = 0, MaterialPropertyBlock block = null)
    {
        // Prepare "self" texture for reading in the shader for double buffered custom textures
        RenderTexture textureSelf2D   = null;
        RenderTexture textureSelf3D   = null;
        RenderTexture textureSelfCube = null;

        if (crt.doubleBuffered)
        {
            if (crt.dimension == TextureDimension.Tex2D)
            {
                textureSelf2D = crt;
            }
            if (crt.dimension == TextureDimension.Cube)
            {
                textureSelfCube = crt;
            }
            if (crt.dimension == TextureDimension.Tex3D)
            {
                textureSelf3D = crt;
            }
        }

        if (crt.doubleBuffered)
        {
            // Update the internal double buffered render texture (resize / alloc / ect.)
            crt.EnsureDoubleBufferConsistency();
        }

        if (block == null)
        {
            block = new MaterialPropertyBlock();
        }

        // If the user didn't called the update on CRT, we still process it because it's realtime
        for (int i = 0; i < updateCount; i++)
        {
            // TODO: cache everything
            List <CustomRenderTextureUpdateZone> updateZones = new List <CustomRenderTextureUpdateZone>();
            crt.GetUpdateZones(updateZones);

            if (updateZones.Count == 0)
            {
                updateZones.Add(new CustomRenderTextureUpdateZone {
                    needSwap = false, updateZoneCenter = new Vector3(0.5f, 0.5f, 0.5f), updateZoneSize = Vector3.one, rotation = 0, passIndex = 0
                });
            }

            foreach (var zone in updateZones)
            {
                var zoneCenters          = updateZones.Select(z => new Vector4(z.updateZoneCenter.x, z.updateZoneCenter.y, z.updateZoneCenter.z, 0)).ToList();
                var zoneSizesAndRotation = updateZones.Select(z => new Vector4(z.updateZoneSize.x, z.updateZoneSize.y, z.updateZoneSize.z, z.rotation)).ToList();
                var zonePrimitiveIDs     = Enumerable.Range(0, updateZones.Count).Select(j => (float)j).ToList();// updateZones.Select(z => 0.0f).ToList();
                int sliceCount           = GetSliceCount(crt, mipLevel);

                // Copy all the slices in case the texture is double buffered
                if (zone.needSwap)
                {
                    var doubleBuffer = crt.GetDoubleBufferRenderTexture();
                    if (doubleBuffer != null)
                    {
                        // For now, it's just a copy, once we actually do the swap of pointer, be careful to reset the Active Render Texture
                        for (int sliceIndex = 0; sliceIndex < sliceCount; sliceIndex++)
                        {
                            cmd.CopyTexture(doubleBuffer, sliceIndex, crt, sliceIndex);
                        }
                    }
                }

                for (int slice = 0; slice < sliceCount; slice++)
                {
                    RenderTexture renderTexture = crt.doubleBuffered ? crt.GetDoubleBufferRenderTexture() : crt;
                    cmd.SetRenderTarget(renderTexture, mipLevel, (crt.dimension == TextureDimension.Cube) ? (CubemapFace)slice : CubemapFace.Unknown, (crt.dimension == TextureDimension.Tex3D) ? slice : 0);
                    cmd.SetViewport(new Rect(0, 0, Mathf.Max(1, crt.width >> mipLevel), Mathf.Max(1, crt.height >> mipLevel)));
                    block.SetVector(kCustomRenderTextureInfo, GetTextureInfos(crt, slice, mipLevel));
                    block.SetVector(kCustomRenderTextureParameters, GetTextureParameters(crt, slice, mipLevel));
                    block.SetFloat(kMipLevel, mipLevel);
                    if (textureSelf2D != null)
                    {
                        block.SetTexture(kSelf2D, textureSelf2D);
                    }
                    if (textureSelf3D != null)
                    {
                        block.SetTexture(kSelf3D, textureSelf3D);
                    }
                    if (textureSelfCube != null)
                    {
                        block.SetTexture(kSelfCube, textureSelfCube);
                    }

                    int passIndex = zone.passIndex == -1 ? 0 : zone.passIndex;

                    block.SetVectorArray(kUpdateDataCenters, zoneCenters);
                    block.SetVectorArray(kUpdateDataSizesAndRotation, zoneSizesAndRotation);
                    block.SetFloatArray(kUpdateDataPrimitiveIDs, zonePrimitiveIDs);

                    cmd.DrawProcedural(Matrix4x4.identity, crt.material, passIndex, MeshTopology.Triangles, 6 * updateZones.Count, 1, block);
                }
            }
        }
    }
    // Update one custom render texture.
    public static void UpdateCustomRenderTexture(CommandBuffer cmd, CustomRenderTexture crt)
    {
        bool firstPass = crt.updateCount == 0;

        // Handle initialization here too:
        if (crt.initializationMode == CustomRenderTextureUpdateMode.Realtime || needsInitialization.Contains(crt) || (firstPass && crt.initializationMode == CustomRenderTextureUpdateMode.OnLoad))
        {
            switch (crt.initializationSource)
            {
            case CustomRenderTextureInitializationSource.Material:
                // TODO
                break;

            case CustomRenderTextureInitializationSource.TextureAndColor:
                // TODO
                break;
            }
            needsInitialization.Remove(crt);
        }

        needsUpdate.TryGetValue(crt, out int updateCount);

        if (crt.material != null && (crt.updateMode == CustomRenderTextureUpdateMode.Realtime || updateCount > 0 || (firstPass && crt.updateMode == CustomRenderTextureUpdateMode.OnLoad)))
        {
            onBeforeCustomTextureUpdated?.Invoke(cmd, crt);

#if CUSTOM_TEXTURE_PROFILING
            customRenderTextureSamplers.TryGetValue(crt, out var sampler);
            if (sampler == null)
            {
                sampler = customRenderTextureSamplers[crt] = CustomSampler.Create($"{crt.name} - {crt.GetInstanceID()}", true);
                sampler.GetRecorder().enabled = true;
            }
            cmd.BeginSample(sampler);
#endif

            using (new ProfilingScope(cmd, new ProfilingSampler($"Update {crt.name}")))
            {
                // Prepare "self" texture for reading in the shader for double buffered custom textures
                RenderTexture textureSelf2D   = null;
                RenderTexture textureSelf3D   = null;
                RenderTexture textureSelfCube = null;
                if (crt.doubleBuffered)
                {
                    if (crt.dimension == TextureDimension.Tex2D)
                    {
                        textureSelf2D = crt;
                    }
                    if (crt.dimension == TextureDimension.Cube)
                    {
                        textureSelfCube = crt;
                    }
                    if (crt.dimension == TextureDimension.Tex3D)
                    {
                        textureSelf3D = crt;
                    }
                }

                if (crt.doubleBuffered)
                {
                    // Update the internal double buffered render texture (resize / alloc / ect.)
                    crt.EnsureDoubleBufferConsistency();
                }

                MaterialPropertyBlock block = new MaterialPropertyBlock();

                // If the user didn't called the update on CRT, we still process it because it's realtime
                updateCount = Mathf.Max(updateCount, 1);
                for (int i = 0; i < updateCount; i++)
                {
                    // TODO: cache everything
                    List <CustomRenderTextureUpdateZone> updateZones = new List <CustomRenderTextureUpdateZone>();
                    crt.GetUpdateZones(updateZones);

                    if (updateZones.Count == 0)
                    {
                        updateZones.Add(new CustomRenderTextureUpdateZone {
                            needSwap = false, updateZoneCenter = new Vector3(0.5f, 0.5f, 0.5f), updateZoneSize = Vector3.one, rotation = 0, passIndex = 0
                        });
                    }

                    foreach (var zone in updateZones)
                    // int sliceCount = GetSliceCount(crt);
                    // for (int slice = 0; slice < sliceCount; slice++)
                    {
                        var zoneCenters          = updateZones.Select(z => new Vector4(z.updateZoneCenter.x, z.updateZoneCenter.y, z.updateZoneCenter.z, 0)).ToList();
                        var zoneSizesAndRotation = updateZones.Select(z => new Vector4(z.updateZoneSize.x, z.updateZoneSize.y, z.updateZoneSize.z, z.rotation)).ToList();
                        var zonePrimitiveIDs     = Enumerable.Range(0, updateZones.Count).Select(j => (float)j).ToList();// updateZones.Select(z => 0.0f).ToList();
                        int sliceCount           = GetSliceCount(crt);

                        // Copy all the slices in case the texture is double buffered
                        if (zone.needSwap)
                        {
                            var doubleBuffer = crt.GetDoubleBufferRenderTexture();
                            if (doubleBuffer != null)
                            {
                                // For now, it's just a copy, once we actually do the swap of pointer, be careful to reset the Active Render Texture
                                for (int slice = 0; slice < sliceCount; slice++)
                                {
                                    cmd.CopyTexture(doubleBuffer, slice, crt, slice);
                                }
                            }
                        }

                        // foreach (var zone in updateZones)
                        for (int slice = 0; slice < sliceCount; slice++)
                        {
                            RenderTexture renderTexture = crt.doubleBuffered ? crt.GetDoubleBufferRenderTexture() : crt;
                            cmd.SetRenderTarget(renderTexture, 0, (crt.dimension == TextureDimension.Cube) ? (CubemapFace)slice : 0, (crt.dimension == TextureDimension.Tex3D) ? slice : 0);
                            cmd.SetViewport(new Rect(0, 0, crt.width, crt.height));
                            block.SetVector(kCustomRenderTextureInfo, GetTextureInfos(crt, slice));
                            block.SetVector(kCustomRenderTextureParameters, GetTextureParameters(crt, slice));
                            if (textureSelf2D != null)
                            {
                                block.SetTexture(kSelf2D, textureSelf2D);
                            }
                            if (textureSelf3D != null)
                            {
                                block.SetTexture(kSelf3D, textureSelf3D);
                            }
                            if (textureSelfCube != null)
                            {
                                block.SetTexture(kSelfCube, textureSelfCube);
                            }

                            int passIndex = zone.passIndex == -1 ? 0 : zone.passIndex;

                            block.SetVectorArray(kUpdateDataCenters, zoneCenters);
                            block.SetVectorArray(kUpdateDataSizesAndRotation, zoneSizesAndRotation);
                            block.SetFloatArray(kUpdateDataPrimitiveIDs, zonePrimitiveIDs);

                            cmd.DrawProcedural(Matrix4x4.identity, crt.material, passIndex, MeshTopology.Triangles, 6 * updateZones.Count, 1, block);
                        }
                    }
                }

                needsUpdate.Remove(crt);
            }

#if CUSTOM_TEXTURE_PROFILING
            cmd.EndSample(sampler);
#endif
            crt.IncrementUpdateCount();
        }
    }