public static RenderTexture RenderRCSMPerPixel(Texture2D src, int steps) { RenderTexture[] blitTextures = new RenderTexture[2]; for (int i = 0; i < 2; i++) { blitTextures[i] = WizardUtils.NewRenderTexture(src); } var material = new Material(Shader.Find("Hidden/RCSMWizard/PerPixel")); material.SetFloat("_Steps", steps); Graphics.Blit(src, blitTextures[0], material, 0); Graphics.Blit(src, blitTextures[1], material, 0); int target = 1; for (int x = 0; x < src.width; x++) { for (int y = 0; y < src.height; y++) { target ^= 1; material.SetVector("_PixelParams", new Vector4(src.width, src.height, x - src.width / 2, y - src.height / 2)); material.SetTexture("_BufferTex", blitTextures[target ^ 1]); Graphics.Blit(src, blitTextures[target], material, 1); } } blitTextures[target ^ 1].Release(); return(blitTextures[target]); }
public static RenderTexture RenderRCSMFloodJump(Texture2D src, int steps) { RenderTexture[] blitTextures = new RenderTexture[2]; for (int i = 0; i < 2; i++) { blitTextures[i] = WizardUtils.NewRenderTexture(src); } var material = new Material(Shader.Find("Hidden/RCSMWizard/JumpFlood")); material.SetFloat("_Steps", steps); material.SetVector( "_PixelParams", new Vector4( src.width, src.height, 0, 0 ) ); Graphics.Blit(src, blitTextures[0], material, 2); Graphics.Blit(src, blitTextures[1], material, 2); int target = 0; int jump = 2; material.SetVector( "_PixelParams", new Vector4( blitTextures[target].width, blitTextures[target].height, Vector2.Distance(Vector2.zero, new Vector2(blitTextures[target].width, blitTextures[target].height)), jump ) ); int size = Mathf.Min(src.width, src.height); while (size > 1) { target ^= 1; jump <<= 1; size >>= 1; material.SetVector( "_PixelParams", new Vector4( blitTextures[target].width, blitTextures[target].height, 0, jump ) ); Graphics.Blit(blitTextures[target ^ 1], blitTextures[target], material, 1); } target ^= 1; Graphics.Blit(blitTextures[target ^ 1], blitTextures[target], material, 3); blitTextures[target ^ 1].Release(); return(blitTextures[target]); }
public static RenderTexture PackRCSM(Texture src, Texture cone, Texture normal) { RenderTexture dst = WizardUtils.NewRenderTexture(src); var material = new Material(Shader.Find("Hidden/RCSMWizard/Pack")); material.SetVector("_PixelParams", new Vector4(src.width, src.height, 0, 0)); material.SetTexture("_Cone", cone); material.SetTexture("_Normal", normal); Graphics.Blit(src, dst, material, 0); return(dst); }
public static RenderTexture ImportNormal(Texture src) { if (src == null) { src = Texture2D.normalTexture; } RenderTexture dst = WizardUtils.NewRenderTexture(src); var material = new Material(Shader.Find("Hidden/RCSMWizard/Normal")); material.SetVector("_PixelParams", new Vector4(src.width, src.height, 0, 0)); Graphics.Blit(src, dst, material, 1); return(dst); }
public static RenderTexture RenderNormal(Texture2D src) { RenderTexture dst = WizardUtils.NewRenderTexture(src); var material = new Material(Shader.Find("Hidden/RCSMWizard/Normal")); int s = Mathf.Max(src.width, src.height); int f = 1; while (s >= 256) { s >>= 1; f <<= 1; } material.SetVector("_PixelParams", new Vector4(src.width, src.height, f, 0)); Graphics.Blit(src, dst, material, 0); return(dst); }
public static Texture2D RenderSDF(Texture2D source, int distance, float treshold, int upsampling, System.Enum channel, System.Enum mode) { Material material = null; material = new Material(Shader.Find("Hidden/SDFWizard/JumpFlood")); RenderTexture[] blitTextures = new RenderTexture[2]; int maxDim = Mathf.Max(source.width, source.height); while (maxDim << upsampling > 2048) { upsampling--; } upsampling = Mathf.Max(0, upsampling); for (int i = 0; i < 2; i++) { blitTextures[i] = WizardUtils.NewRenderTexture(source, upsampling); } int target = 0; int jump = 2; material.SetVector( "_PixelParams", new Vector4( blitTextures[target].width, blitTextures[target].height, Vector2.Distance(Vector2.zero, new Vector2(blitTextures[target].width, blitTextures[target].height)), jump ) ); if (source.wrapMode == TextureWrapMode.Repeat) { material.EnableKeyword("__REPEAT"); } else { material.DisableKeyword("__REPEAT"); } var channelDict = new Dictionary <System.Enum, string>() { { WizardUtils.ColorChannel.Red, "__RED" }, { WizardUtils.ColorChannel.Green, "__GREEN" }, { WizardUtils.ColorChannel.Blue, "__BLUE" }, { WizardUtils.ColorChannel.Alpha, "__ALPHA" }, { WizardUtils.ColorChannel.mixRGB, "__MIX" } }; string channelKey; if (channelDict.TryGetValue(channel, out channelKey)) { material.EnableKeyword(channelKey); } material.SetFloat("_Treshold", treshold); var exoTexture = new RenderTexture(blitTextures[target]); Graphics.Blit(source, exoTexture, material, 3); Graphics.Blit(exoTexture, blitTextures[target], material, 0); int size = Mathf.Min(source.width, source.height); while (size > 1) { target ^= 1; jump <<= 1; size >>= 1; material.SetVector( "_PixelParams", new Vector4( blitTextures[target].width, blitTextures[target].height, Vector2.Distance(Vector2.zero, new Vector2(blitTextures[target].width, blitTextures[target].height)), jump ) ); Graphics.Blit(blitTextures[target ^ 1], blitTextures[target], material, 1); } target ^= 1; Graphics.Blit(exoTexture, blitTextures[target], material, 4); Graphics.Blit(blitTextures[target ^ 1], exoTexture); size = Mathf.Min(source.width, source.height); jump = 2; target ^= 1; Graphics.Blit(blitTextures[target ^ 1], blitTextures[target], material, 0); while (size > 1) { target ^= 1; jump <<= 1; size >>= 1; material.SetVector( "_PixelParams", new Vector4( blitTextures[target].width, blitTextures[target].height, Vector2.Distance(Vector2.zero, new Vector2(blitTextures[target].width, blitTextures[target].height)), jump ) ); Graphics.Blit(blitTextures[target ^ 1], blitTextures[target], material, 1); } target ^= 1; var outTexture = new RenderTexture( source.width, source.height, 8, RenderTextureFormat.ARGB32 ); var modeDict = new Dictionary <System.Enum, string>() { { SDFConverter.RenderingMode.ContourUV, "__MODE_UV_CONTOUR" }, { SDFConverter.RenderingMode.ContourRGB, "__MODE_RGB_CONTOUR" }, { SDFConverter.RenderingMode.DistanceOnly, "__MODE_DIST" }, { SDFConverter.RenderingMode.NearestUV, "__MODE_UV" }, { SDFConverter.RenderingMode.RGBDistance, "__MODE_RGB_DIST" }, { SDFConverter.RenderingMode.RGBOnly, "__MODE_RGB" }, }; string modeKey; if (modeDict.TryGetValue(mode, out modeKey)) { material.EnableKeyword(modeKey); } material.SetFloat("_Distance", distance << upsampling); material.SetTexture("_ExoTex", exoTexture); material.SetTexture("_EndoTex", blitTextures[target ^ 1]); Graphics.Blit(source, outTexture, material, 2); Texture2D outTexture2D = WizardUtils.RenderTextureToTexture2D(outTexture); RenderTexture.active = null; outTexture.Release(); exoTexture.Release(); blitTextures[0].Release(); blitTextures[1].Release(); return(outTexture2D); }