Exemplo n.º 1
0
 public BrushTypeSphere()
 {
     _inst = this;
 }
Exemplo n.º 2
0
        void Paint()
        {
            RaycastHit hit;

            // bool anyHits = false;
            //bool anyRecivers = false;
            var texturesNeedUpdate = new List <ImageData>();

            for (int i = 0; i < shoots; i++)
            {
                if (Physics.Raycast(new Ray(transform.position, transform.forward + transform.right * Random.Range(-spread, spread) + transform.up * Random.Range(-spread, spread)), out hit))
                {
                    var recivers             = hit.transform.GetComponentsInParent <PaintingReciever>();
                    PaintingReciever reciver = null;
                    //  Debug.Log("Hit");
                    if (recivers.Length > 0)
                    {
                        var submesh = 0;
                        reciver = recivers[0];

                        // IF FEW SUBMESHES
                        if (hit.collider.GetType() == typeof(MeshCollider))
                        {
                            submesh = ((MeshCollider)hit.collider).sharedMesh.GetSubmeshNumber(hit.triangleIndex);

                            if (recivers.Length > 1)
                            {
                                var mats = reciver.Rendy.materials;

                                var material = mats[submesh % mats.Length];

                                reciver = null;

                                foreach (var r in recivers)
                                {
                                    if (r.Material == material)
                                    {
                                        reciver = r;
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            submesh = reciver.materialIndex;
                        }

                        // ACTUAL PAINTING

                        if (reciver != null)
                        {
                            var tex = reciver.GetTexture();
                            if (tex != null)
                            {
                                var rendTex = (reciver.texture.GetType() == typeof(RenderTexture)) ? (RenderTexture)reciver.texture : null;

                                // WORLD SPACE BRUSH

                                if (rendTex != null)
                                {
                                    var st = new StrokeVector(hit.point)
                                    {
                                        unRepeatedUV = hit.collider.GetType() == typeof(MeshCollider) ?
                                                       (reciver.useTexcoord2 ? hit.textureCoord2 : hit.textureCoord).Floor() : reciver.meshUVoffset,

                                        useTexcoord2 = reciver.useTexcoord2
                                    };

                                    if (reciver.type == PaintingReciever.RendererType.Skinned && reciver.skinnedMeshRenderer != null)
                                    {
                                        BrushTypeSphere.Paint(rendTex, reciver.gameObject, reciver.skinnedMeshRenderer, brush, st, submesh);
                                    }
                                    else if (reciver.type == PaintingReciever.RendererType.regular && reciver.meshFilter != null)
                                    {
                                        var mat = reciver.Material;
                                        if (mat != null && mat.IsAtlased())
                                        {
                                            BrushTypeSphere.PaintAtlased(rendTex, reciver.gameObject,
                                                                         reciver.originalMesh ? reciver.originalMesh : reciver.meshFilter.sharedMesh, brush, st, new List <int> {
                                                submesh
                                            }, (int)mat.GetFloat(PainterDataAndConfig.atlasedTexturesInARow));
                                        }
                                        else
                                        {
                                            BrushTypeSphere.Paint(rendTex, reciver.gameObject,
                                                                  reciver.originalMesh ? reciver.originalMesh : reciver.meshFilter.sharedMesh, brush, st, new List <int> {
                                                submesh
                                            });
                                        }
                                    }
                                }
                                // TEXTURE SPACE BRUSH
                                else if (reciver.texture.GetType() == typeof(Texture2D))
                                {
                                    if (hit.collider.GetType() != typeof(MeshCollider))
                                    {
                                        Debug.Log("Can't get UV coordinates from a Non-Mesh Collider");
                                    }

                                    Blit_Functions.Paint(reciver.useTexcoord2 ? hit.textureCoord2 : hit.textureCoord, 1, (Texture2D)reciver.texture, Vector2.zero, Vector2.one, brush, null);
                                    var id = reciver.texture.GetImgData();

                                    if (!texturesNeedUpdate.Contains(id))
                                    {
                                        texturesNeedUpdate.Add(id);
                                    }
                                }
                                else
                                {
                                    Debug.Log(reciver.gameObject.name + " doesn't have any combination of paintable things setup on his PainterReciver.");
                                }
                            }
                        }
                    }
                }
            }

            foreach (var t in texturesNeedUpdate)
            {
                t.SetAndApply(true);                                   // True for Mipmaps. Best to disable mipmaps on textures and set to false
            }
            //Not to waste performance, don't SetAndApply after each edit, but at the end of the frame (LateUpdate maybe) and only if texture was changed.
            //Mip maps will slow things down, so best is to disable them.


            //   if (!anyHits) Debug.Log("No hits");
            // else if (!anyRecivers) Debug.Log("Attach PaintingReciever script to objects you want to Paint on.");
        }