Ejemplo n.º 1
0
 static Command()
 {
     cachedMaterial = P3dPaintableManager.BuildMaterial("Hidden/Paint in 3D/Sphere Blur");
 }
        private void Bake(RenderTexture renderTexture, int width, int height, int scale)
        {
            if (bakedPixels == null)
            {
                bakedPixels = new List <bool>();
            }
            else
            {
                bakedPixels.Clear();
            }

            bakedMesh = mesh;
            bakedSize = new Vector2Int(width, height);

            if (cachedMaterial == null)
            {
                cachedMaterial = P3dPaintableManager.BuildMaterial("Hidden/Paint in 3D/White");
            }

            switch (PaintableTexture.Channel)
            {
            case P3dChannel.UV: cachedMaterial.SetVector(P3dShader._Channel, new Vector4(1.0f, 0.0f, 0.0f, 0.0f)); break;

            case P3dChannel.UV2: cachedMaterial.SetVector(P3dShader._Channel, new Vector4(0.0f, 1.0f, 0.0f, 0.0f)); break;

            case P3dChannel.UV3: cachedMaterial.SetVector(P3dShader._Channel, new Vector4(0.0f, 0.0f, 1.0f, 0.0f)); break;

            case P3dChannel.UV4: cachedMaterial.SetVector(P3dShader._Channel, new Vector4(0.0f, 0.0f, 0.0f, 1.0f)); break;
            }

            // Write to temp RenderTexture
            var oldActive = RenderTexture.active;

            RenderTexture.active = renderTexture;

            GL.Clear(true, true, Color.black);

            cachedMaterial.SetPass(0);

            Graphics.DrawMeshNow(mesh, Matrix4x4.identity, PaintableTexture.Slot.Index);

            RenderTexture.active = oldActive;

            // Get readable copy
            var readable = P3dHelper.GetReadableCopy(renderTexture);

            // Run through pixels to count total and build binary mask
            bakedPixels.Capacity = width * height;

            total = 0;

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    if (readable.GetPixel(x, y).r > 0.5f)
                    {
                        bakedPixels.Add(true);

                        total += scale;
                    }
                    else
                    {
                        bakedPixels.Add(false);
                    }
                }
            }

            // Clean up
            P3dHelper.Destroy(readable);
        }
        private void Bake(RenderTexture temporary, int width, int height, RenderTextureFormat format, int scale)
        {
            if (bakedPixels == null)
            {
                bakedPixels = new List <bool>();
            }
            else
            {
                bakedPixels.Clear();
            }

            baked     = true;
            bakedMesh = mesh;
            bakedSize = new Vector2Int(width, height);

            if (cachedMaterial == null)
            {
                cachedMaterial = P3dPaintableManager.BuildMaterial("Hidden/Paint in 3D/White");
            }

            cachedMaterial.SetVector(P3dShader._Channel, P3dHelper.IndexToVector((int)PaintableTexture.Channel));

            // Write to temp RenderTexture
            var oldActive     = RenderTexture.active;
            var renderTexture = temporary;

            if (temporary == null)
            {
                var desc = new RenderTextureDescriptor(width, height, format, 0);

                renderTexture = P3dHelper.GetRenderTexture(desc);
            }

            RenderTexture.active = renderTexture;

            GL.Clear(true, true, Color.black);

            cachedMaterial.SetPass(0);

            Graphics.DrawMeshNow(mesh, Matrix4x4.identity, PaintableTexture.Slot.Index);

            RenderTexture.active = oldActive;

            // Get readable copy
            var readable = P3dHelper.GetReadableCopy(renderTexture);

            if (temporary == null)
            {
                P3dHelper.ReleaseRenderTexture(renderTexture);
            }

            // Run through pixels to count total and build binary mask
            bakedPixels.Capacity = width * height;

            total = 0;

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    if (readable.GetPixel(x, y).r > 0.5f)
                    {
                        bakedPixels.Add(true);

                        total += scale;
                    }
                    else
                    {
                        bakedPixels.Add(false);
                    }
                }
            }

            // Clean up
            P3dHelper.Destroy(readable);
        }
        private void BakeOverlap()
        {
            var desc          = new RenderTextureDescriptor(1024, 1024, RenderTextureFormat.ARGB32, 0);
            var renderTexture = P3dHelper.GetRenderTexture(desc);

            if (overlapMaterial == null)
            {
                overlapMaterial = P3dPaintableManager.BuildMaterial("Hidden/Paint in 3D/Overlap");
            }

            overlapMaterial.SetVector(P3dShader._Channel, P3dHelper.IndexToVector(coord));

            var oldActive = RenderTexture.active;

            RenderTexture.active = renderTexture;

            GL.Clear(true, true, Color.black);

            overlapMaterial.SetPass(0);

            Graphics.DrawMeshNow(mesh, Matrix4x4.identity, submesh);

            RenderTexture.active = oldActive;

            overlapTex = P3dHelper.GetReadableCopy(renderTexture);

            P3dHelper.ReleaseRenderTexture(renderTexture);

            var utilizationCount = 0;
            var overlapCount     = 0;

            for (var y = 0; y < overlapTex.height; y++)
            {
                for (var x = 0; x < overlapTex.width; x++)
                {
                    var pixel = overlapTex.GetPixel(x, y);

                    if (pixel.r > 0.0f)
                    {
                        if (pixel.r > 1.5 / 255.0f)
                        {
                            pixel = Color.red;

                            overlapCount += 1;
                        }
                        else
                        {
                            pixel = Color.gray;
                        }

                        utilizationCount += 1;

                        overlapTex.SetPixel(x, y, pixel);
                    }
                }
            }

            var total = overlapTex.width * overlapTex.height * 0.01f;

            utilizationPercent = utilizationCount / total;
            overlapPercent     = overlapCount / total;

            overlapTex.Apply();
        }
Ejemplo n.º 5
0
 static Command()
 {
     cachedMaterial = P3dPaintableManager.BuildMaterial("Hidden/Paint in 3D/Replace");
 }