public BufferPyramid( ComputeShader colorPyramidCS, ComputeShader depthPyramidCS, GPUCopy gpuCopy, TexturePadding texturePadding ) { m_ColorPyramidCS = colorPyramidCS; m_ColorPyramidKernel = m_ColorPyramidCS.FindKernel("KMain"); m_DepthPyramidCS = depthPyramidCS; m_GPUCopy = gpuCopy; m_DepthKernels = new int[] { m_DepthPyramidCS.FindKernel("KDepthDownSample8"), m_DepthPyramidCS.FindKernel("KDepthDownSample1") }; m_TexturePadding = texturePadding; }
Texture2D BakeToTexture(PaintJob[] jobs, int w, int h) { RenderTexture rt = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGB32); Texture2D tex = new Texture2D(w, h, TextureFormat.ARGB32, false); tex.wrapMode = TextureWrapMode.Clamp; RenderTexture.active = rt; Material m = new Material(Shader.Find("Hidden/VertexPainterPro_Preview")); m.SetInt("_flowVisualization", (int)0); m.SetInt("_tab", (int)0); m.SetInt("_flowTarget", (int)0); m.SetInt("_channel", (int)brushMode); m.SetVector("_uvRange", new Vector2(0, 1)); var CamObj = new GameObject("BakeTexture Camera"); CamObj.transform.position = new Vector3(0, 0, -1); var camera = CamObj.AddComponent <Camera>(); camera.clearFlags = CameraClearFlags.Color; camera.backgroundColor = Color.black; camera.orthographic = true; camera.orthographicSize = 1; camera.cullingMask = 1 << 7; camera.targetTexture = rt; camera.allowMSAA = false; foreach (PaintJob job in jobs) { Mesh source = job.meshFilter.sharedMesh; Mesh mesh = Object.Instantiate(source); List <Vector3> srcVertices = new List <Vector3>(); source.GetVertices(srcVertices); List <Vector4> srcUV0 = new List <Vector4>(); source.GetUVs(0, srcUV0); List <Color> srcColors = new List <Color>(); source.GetColors(srcColors); for (int i = 0; i < job.verts.Length; ++i) { // Vertex Vector4 uv = Vector4.zero; if (job.stream.uv0 != null && job.stream.uv0.Count == job.verts.Length) { uv = job.stream.uv0[i]; } else if (srcUV0 != null && srcUV0.Count == job.verts.Length) { uv = srcUV0[i]; } uv *= 2; uv -= Vector4.one; srcVertices[i] = new Vector3(uv.x, uv.y, 0); // Color if (job.stream.colors != null && job.stream.colors.Length == job.verts.Length) { srcColors[i] = job.stream.colors[i]; } } mesh.SetVertices(srcVertices); mesh.SetColors(srcColors); var bound = mesh.bounds; bound.center = Vector3.zero; bound.size = Vector3.one; mesh.bounds = bound; Graphics.DrawMesh(mesh, Matrix4x4.identity, m, 7); camera.Render(); } tex.ReadPixels(new Rect(0, 0, w, h), 0, 0); tex.Apply(); if (padMode) { var pad = new TexturePadding(tex); pad.Padding((w + h) / 50); } RenderTexture.active = null; camera.targetTexture = null; Object.DestroyImmediate(CamObj); Object.DestroyImmediate(m); RenderTexture.ReleaseTemporary(rt); return(tex); }