public static Texture2D CreateShadermap(Material targetMaterial, int intersectionStyle, int waveStyle, int heightmapStyle, bool useCompression = false, Texture2D customIntersectionTex = null) { StylizedWaterResources r = StylizedWaterResources.Instance; //Set compression setting TexturePacker.useCompression = useCompression; Texture2D intersectionTex; if (customIntersectionTex) { intersectionTex = customIntersectionTex; } else { intersectionTex = r.intersectionStyles[intersectionStyle]; } Texture2D surfaceHighlightTex = r.waveStyles[waveStyle]; Texture2D heightmapTex = r.heightmapStyles[heightmapStyle]; Texture2D shadermap = new Texture2D(resolution, resolution, TextureFormat.RGB24, true) { name = "_shadermap", //Prefix and suffix to be appended upon saving anisoLevel = 2, filterMode = FilterMode.Bilinear, wrapMode = TextureWrapMode.Repeat }; RenderTexture rt = new RenderTexture(resolution, resolution, 0); RenderTexture.active = rt; //Constants ShaderMapRenderMat.SetTexture("_RedInput", surfaceHighlightTex); ShaderMapRenderMat.SetTexture("_GreenInput", heightmapTex); ShaderMapRenderMat.SetTexture("_BlueInput", intersectionTex); //ShaderMapRenderMat.SetTexture("_AlphaInput", null); //Pack textures on GPU Graphics.Blit(null, rt, ShaderMapRenderMat); //Copy result into texture shadermap.ReadPixels(new Rect(0, 0, shadermap.width, shadermap.height), 0, 0); shadermap.Apply(); //Cleanup RenderTexture.active = null; shadermap = SaveAndGetTexture(targetMaterial, shadermap); return(shadermap); }
public static Texture2D RenderNormalMap(Material targetMaterial, int waveStyle, bool useCompression = false, Texture2D customNormalTex = null) { StylizedWaterResources r = StylizedWaterResources.Instance; //If a custom texture is assigned, return that if (customNormalTex) { return(customNormalTex); } //Set compression setting TexturePacker.useCompression = useCompression; Texture2D heightmap = r.waveStyles[waveStyle]; Texture2D normalTexture = new Texture2D(resolution, resolution, TextureFormat.ARGB32, true) { name = "_normal", filterMode = FilterMode.Trilinear, wrapMode = TextureWrapMode.Repeat }; RenderTexture rt = new RenderTexture(resolution, resolution, 0); RenderTexture.active = rt; //Constants NormalRenderMat.SetFloat("_Offset", NORMALOFFSET); NormalRenderMat.SetFloat("_Strength", NORMALSTRENGTH); //Convert heightmap to normal on GPU Graphics.Blit(heightmap, rt, NormalRenderMat); //Copy result into texture normalTexture.ReadPixels(new Rect(0, 0, normalTexture.width, normalTexture.height), 0, 0); normalTexture.Apply(); //Cleanup RenderTexture.active = null; normalTexture = SaveAndGetTexture(targetMaterial, normalTexture); return(normalTexture); }