// Use this for initialization void Start() { SceneParamater.InitLightmapSetting(); for (int i = 0; i < LightmapSettings.lightmaps.Length; ++i) { var lightmap = new RenderTexture(LightMapSize, LightMapSize, 24, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear) { enableRandomWrite = true }; lightmap.Create(); _lightmaps.Add(lightmap); } var boxColliders = GameObject.FindObjectsOfType <MeshRenderer>().Select(x => x.bounds).ToList(); _sceneAABB = boxColliders[0]; foreach (var obj in boxColliders) { _sceneAABB.Encapsulate(obj); } _gBuffer = new UnityLmGBuffer(CollectGeomertyShader, LightMapSize); _gBuffer.PrepareGeometry(false); ImprefectShadowMap = new ISM_TS(ShadowMapSize, _gBuffer); //塞入lumel的信息 _irradianceBuffer = new ComputeBuffer(1, sizeof(float) * 4); UpdateLightInfo(); _polygonLightBuffer = new ComputeBuffer(_polygonLight.Count, sizeof(float) * 3); }
public void IrradianceUseComputeShader(ComputeShader indirectLightPassCS, UnityLmGBuffer gBuffer, RenderTexture shadowmap, PolygonLight polygonLight, RenderTexture rt, Vector2 farPlane, int lightmapIndex, int lightmapSize, int shadowmapSize, ShadowMapParams shadowFactor) { //rt.Clear(); int mainKernel = indirectLightPassCS.FindKernel("CSMain"); _gBuffer.SetComputeShaderData(indirectLightPassCS, mainKernel, lightmapIndex); //塞入polygonlight 多边形的信息 ComputeBuffer polygonLightBuffer = new ComputeBuffer(_polygonLight.Count, sizeof(float) * 3); //polygonLightBuffer.SetData(polygonLight.PolygonHull.ToArray()); polygonLightBuffer.SetData(_polygonLight.ToArray()); indirectLightPassCS.SetBuffer(mainKernel, "_PolygonHull", polygonLightBuffer); indirectLightPassCS.SetVector("_PolygonCenter", polygonLight.Center); indirectLightPassCS.SetVector("_PolygonNormal", polygonLight.Normal); indirectLightPassCS.SetVector("_PolygonTotalFlux", polygonLight.SumPower); indirectLightPassCS.SetInt("_PolygonVertexCount", _polygonLight.Count); indirectLightPassCS.SetFloat("_ShadowBias", shadowFactor.ShadowBias); indirectLightPassCS.SetVector("_FarPlane", farPlane); indirectLightPassCS.SetInt("_LightMapSize", lightmapSize); indirectLightPassCS.SetInt("_ShadowMapSize", shadowmapSize); indirectLightPassCS.SetInt("_IsmIndexOffset", 0); indirectLightPassCS.SetFloat("_ShadowSoftness", shadowFactor.ShadowSoftness); indirectLightPassCS.SetFloat("_BiasFade", shadowFactor.BiasFade); //塞入最后返回的RenderTexture indirectLightPassCS.SetTexture(mainKernel, "_LightMap", rt); indirectLightPassCS.SetTexture(mainKernel, "_Ism", shadowmap); // ComputeBuffer outputPosBuffer = new ComputeBuffer(100*1000, sizeof(float)); // IndirectLightPassCS.SetBuffer(mainKernel, "_OutputGBufferPos", outputPosBuffer); indirectLightPassCS.Dispatch(mainKernel, lightmapSize / 32, lightmapSize / 32, 1); //dispose polygonLightBuffer.Dispose(); }
public void IrradianceUseComputeShader(ComputeShader indirectLightPassCS, UnityLmGBuffer gBuffer, RenderTexture shadowmap, PolygonLight polygonLight, RenderTexture rt, Vector2 farPlane, int lightmapIndex, ShadowMapParams shadowFactor) { //rt.Clear(); int mainKernel = indirectLightPassCS.FindKernel("CSMain"); indirectLightPassCS.SetTexture(mainKernel, "_NormalTex", gBuffer.NormalTex[lightmapIndex]); indirectLightPassCS.SetTexture(mainKernel, "_PositionTex", gBuffer.PositionTex[lightmapIndex]); Matrix4x4 matrix = gBuffer.RenderCameraVPInverse[lightmapIndex]; //转换成行优先? float[] matrixFloats = { matrix[0, 0], matrix[1, 0], matrix[2, 0], matrix[3, 0], matrix[0, 1], matrix[1, 1], matrix[2, 1], matrix[3, 1], matrix[0, 2], matrix[1, 2], matrix[2, 2], matrix[3, 2], matrix[0, 3], matrix[1, 3], matrix[2, 3], matrix[3, 3] }; float[] matrixFloats1 = { matrix[0, 0], matrix[0, 1], matrix[0, 2], matrix[0, 3], matrix[1, 0], matrix[1, 1], matrix[1, 2], matrix[1, 3], matrix[2, 0], matrix[2, 1], matrix[2, 2], matrix[2, 3], matrix[3, 0], matrix[3, 1], matrix[3, 2], matrix[3, 3] }; indirectLightPassCS.SetFloats("_GBufferCameraVPInverse", matrixFloats); indirectLightPassCS.SetVector("_GBufferCameraPos", gBuffer.RenderCameraPos[lightmapIndex]); //塞入polygonlight 多边形的信息 ComputeBuffer polygonLightBuffer = new ComputeBuffer(_polygonLight.Count, sizeof(float) * 3); //polygonLightBuffer.SetData(polygonLight.PolygonHull.ToArray()); polygonLightBuffer.SetData(_polygonLight.ToArray()); indirectLightPassCS.SetBuffer(mainKernel, "_PolygonHull", polygonLightBuffer); indirectLightPassCS.SetVector("_PolygonCenter", polygonLight.Center); indirectLightPassCS.SetVector("_PolygonNormal", polygonLight.Normal); indirectLightPassCS.SetVector("_PolygonTotalFlux", polygonLight.SumPower); indirectLightPassCS.SetInt("_PolygonVertexCount", _polygonLight.Count); indirectLightPassCS.SetFloat("_ShadowBias", shadowFactor.ShadowBias); indirectLightPassCS.SetVector("_FarPlane", farPlane); indirectLightPassCS.SetInt("_LightMapSize", LightMapSize); indirectLightPassCS.SetInt("_ShadowMapSize", ShadowMapSize); indirectLightPassCS.SetInt("_IsmCount", MaxVPLPerIsm); indirectLightPassCS.SetInt("_IsmIndexOffset", 0); indirectLightPassCS.SetFloat("_ShadowSoftness", shadowFactor.ShadowSoftness); indirectLightPassCS.SetFloat("_BiasFade", shadowFactor.BiasFade); //塞入最后返回的RenderTexture indirectLightPassCS.SetTexture(mainKernel, "_LightMap", rt); indirectLightPassCS.SetTexture(mainKernel, "_Ism", ISM); // ComputeBuffer outputPosBuffer = new ComputeBuffer(100*1000, sizeof(float)); // IndirectLightPassCS.SetBuffer(mainKernel, "_OutputGBufferPos", outputPosBuffer); indirectLightPassCS.Dispatch(mainKernel, LightMapSize / 32, LightMapSize / 32, 1); //dispose polygonLightBuffer.Dispose(); }
public void Init() { var unityAPIInit = UnityAPICaller.Instance; _geometryBuffer?.Release(); _gi?.Release(); SceneParamater.InitLightmapSetting(); Debug.Log("GI Init Begin"); var meshRenderers = GameObject.FindObjectsOfType <MeshRenderer>(); //gen mesh collider /* * foreach (var go in meshRenderers) * { * if (go.GetComponent<MeshCollider>() == null) * { * go.gameObject.AddComponent<MeshCollider>(); * } * }*/ Debug.LogWarning("Has Added MeshCollider"); var boxColliders = meshRenderers.Select(x => x.bounds).ToList(); var sceneAABB = boxColliders[0]; foreach (var obj in boxColliders) { sceneAABB.Encapsulate(obj); } _geometryBuffer = new UnityLmGBuffer(CollectGeomerty, LightMapSize); _geometryBuffer.PrepareGeometry(SaveGBufferTexture); ImprefectShadowMap ism; switch (ISMType) { case ISMGenType.TS: ism = new ISM_TS(ShadowMapSize, _geometryBuffer); break; case ISMGenType.CS: ism = new ISM_CS(IsmPass, ShadowMapSize, _geometryBuffer); break; default: throw new ArgumentOutOfRangeException(); } _gi = new VplWithIsm(_geometryBuffer, ism, LightMapSize, TotalSampleCount, Bounce, RenderTextureFormat.ARGBFloat, DirectLightPass, IndirectLightPass, AccumlatePass, SpeID); _gi.EncodeRgbmCS = EncodeRgbm; _gi.DirectLightWeight = DirectWight; _gi.IndirectLightWeight = IndirectWight; _gi.BuildFinish = false; _gi.Init(); List <GILightBase> lights = new List <GILightBase>(); lights.AddRange(FindObjectsOfType <GIPointLight>()); lights.AddRange(FindObjectsOfType <GIAreaLight>()); lights.AddRange(FindObjectsOfType <GIDirectionalLight>()); _gi.EmitVPL(lights); //1.准备G-buffer //3.聚类成polygonlight _gi.Cluster2Polygon(); ShadowMapParams param = new ShadowMapParams(); param.ShadowSlopeFactor = SceneParamater.ShadowSlopeFactor; param.ShadowBias = SceneParamater.ShadowBias; _gi.ShadowMapParam = param; _gi.BuildLightMap(BuildDirectLight, BuildIndirectLight); Debug.Log("GI Finish"); }
public void Init() { var unityAPIInit = UnityAPICaller.Instance; _geometryBuffer?.Release(); _gi?.Release(); SceneParamater.InitLightmapSetting(); Debug.Log("GI Init Begin"); var meshRenderers = GameObject.FindObjectsOfType <MeshRenderer>(); //gen mesh collider /* * foreach (var go in meshRenderers) * { * if (go.GetComponent<MeshCollider>() == null) * { * go.gameObject.AddComponent<MeshCollider>(); * } * }*/ Debug.LogWarning("Has Added MeshCollider"); var boxColliders = meshRenderers.Select(x => x.bounds).ToList(); var sceneAABB = boxColliders[0]; foreach (var obj in boxColliders) { sceneAABB.Encapsulate(obj); } _geometryBuffer = new UnityLmGBuffer(CollectGeomerty, LightMapSize); _geometryBuffer.PrepareGeometry(SaveGBufferTexture); _gi = new VplUseUnityLm(_geometryBuffer, LightMapSize, ShadowmapSize, TotalSampleCount, Bounce, RenderTextureFormat.ARGBFloat, DirectLightPass, IndirectLightPass, AccumlatePass); _gi.EncodeRgbmCS = EncodeRgbm; _gi.DirectLightWeight = DirectWight; _gi.IndirectLightWeight = IndirectWight; _gi.ShadowSlopeFactor = SceneParamater.ShadowSlopeFactor; _gi.ShadowBias = SceneParamater.ShadowBias; _gi.BuildFinish = false; _gi.Init(); List <GILightBase> lights = new List <GILightBase>(); lights.AddRange(FindObjectsOfType <GIPointLight>()); lights.AddRange(FindObjectsOfType <GIAreaLight>()); lights.AddRange(FindObjectsOfType <GIDirectionalLight>()); _gi.EmitVPL(lights); //1.准备G-buffer //3.聚类成polygonlight _gi.Cluster2Polygon(); // ThreadPool.QueueUserWorkItem((state) => // { _gi.BuildLightMap(BuildDirectLight, BuildIndirectLight); Debug.Log("GI Init Finish"); // }); }