예제 #1
0
    // do gaussian blur
    public void blur()
    {
        // blur selected layer or all
        tex    = Gaussian.FilterProcessImage(blurStrength, tex);
        pixels = tex.GetPixels();         // update pixels array also
        Selection.activeGameObject.GetComponent <Renderer>().sharedMaterial.SetTexture("_Mask", tex);

        isDirty = true;
        saveToDisk();
    }
예제 #2
0
    private Color[] brushPixels; // array of brush pixels



    // *** main splatmask creation function ***
    public void createSplatMask(int texResolution)
    {
        // take current game object (must be better way..), maybe this.gameObject

        GameObject go = Selection.activeGameObject;

        // check: we have selection?
        // check: has collider?

        // create texture

        /*
         *      if (realTimePreviewEnabled)
         *      {
         *              maskResolution = maskPreviewResolution;
         *      }
         */

        tex           = new Texture2D(texResolution, texResolution, TextureFormat.ARGB32, true);
        tex.hideFlags = HideFlags.DontSave;

        Bounds area = Selection.activeGameObject.GetComponent <Renderer>().bounds;

        //		Debug.Log("bounds:"+area.min+" to "+area.max+" , size:"+area.size.x);

        faceAngles = new float[texResolution, texResolution];
        pixels     = new Color[texResolution * texResolution];

        //		float pixelStep = area.size.x/maskResolution;
        float pixelStepX = area.size.x / texResolution;
        float pixelStepZ = area.size.z / texResolution;

        Mesh mesh = Selection.activeGameObject.GetComponent <MeshFilter>().sharedMesh;

        Vector3[] normals   = mesh.normals;
        int[]     triangles = mesh.triangles;

        RaycastHit hit;
        int        xx = 0;
        int        yy = 0;

        for (float z = area.min.z + pixelStepZ / 2; z < area.max.z; z += pixelStepZ)
        {
            xx = 0;
            for (float x = area.min.x + pixelStepX / 2; x < area.max.x; x += pixelStepX)
            {
                float r = 0;
                float g = 0;
                float b = 0;
                float a = 0;

                //Debug.DrawLine(new Vector3(x,100,y), new Vector3(x,0,y), Color.red, 5);

                //if (Physics.Raycast(new Vector3(x,200,y), -Vector3.up, out hit, 9999.0F))
                Ray ray = new Ray(new Vector3(x, area.max.y + 1, z), -Vector3.up);                        // FIXME: 9999 could be taken from maxheight
                if (Selection.activeGameObject.GetComponent <Collider>().Raycast(ray, out hit, 19999.0F)) // FIXME: 19999 could be taken from maxheight
                {
                    //					Debug.DrawRay(new Vector3(x,100,y), transform.TransformPoint(hit.point), Color.red, 5);
                    //Debug.DrawLine(new Vector3(x,100,y), hit.point, Color.blue, 5);

                    //Debug.DrawLine(new Vector3(x,100,y), new Vector3(x,0,y), Color.green, 5);

                    Vector3 n0                 = normals[triangles[hit.triangleIndex * 3 + 0]];
                    Vector3 n1                 = normals[triangles[hit.triangleIndex * 3 + 1]];
                    Vector3 n2                 = normals[triangles[hit.triangleIndex * 3 + 2]];
                    Vector3 baryCenter         = hit.barycentricCoordinate;
                    Vector3 interpolatedNormal = n0 * baryCenter.x + n1 * baryCenter.y + n2 * baryCenter.z;
                    interpolatedNormal = interpolatedNormal.normalized;
                    Transform hitTransform = hit.collider.transform;
                    interpolatedNormal = hitTransform.TransformDirection(interpolatedNormal);

                    //Debug.DrawRay(hit.point, interpolatedNormal*5, Color.yellow, 5);

                    //float distanceToGround = hit.distance;
                    //float a = Vector3.Angle(hit.normal, transform.forward);
                    float angle = Vector3.Angle(interpolatedNormal, Selection.activeGameObject.transform.forward);
                    //InverseLerp

                    faceAngles[xx, yy] = angle;


                    // get colors
                    if (angle <= flatRangeMax)
                    {
                        r = 1;
                    }

                    if (angle > flatRangeMax & angle <= mediumRangeMax)
                    {
                        g = 1;
                    }

                    if (angle > mediumRangeMax)
                    {
                        b = 1;
                    }

                    // a = road mask
                    //float p = Mathf.PerlinNoise(xx*0.01f,yy*0.01f);
                    //a = p>0.5f&&p<0.6f?a=1:a=0;

                    // check for obstacles around hitpoint
                    if (checkColliderObjects)
                    {
                        LayerMask  layerMask    = 1 << objCollidersLayer;                   //LayerMask.LayerToName(objCollidersLayer);
                        Collider[] hitColliders = Physics.OverlapSphere(hit.point, obstacleRadius, layerMask);
                        if (hitColliders.Length > 0)
                        {
                            a = 1;
                        }
                    }

                    pixels[xx + yy * texResolution] = new Color(r, g, b, a);
                }
                else
                {
                    // set a default color anyway to suport holes in the surface and to avoid black seams
                    pixels[xx + yy * texResolution] = new Color(1, 0, 0, 0);

                    //r=1;
                    //b=1;
                    //Debug.DrawLine(new Vector3(x,100,y), new Vector3(x,0,y), Color.yellow, 5);
                }
                // always increment, to support not rectangular surfaces
                xx++;

                //tex.SetPixel((int)x,(int)y,new Color(r,g,b,1));
                //Debug.Log((int)x+(int)y*(int)resolution);
                //Debug.Log("xx"+xx+" | "+xx+yy*(int)resolution);
            }
            yy++;
        }

        // ROAD , houses? mask

        tex.SetPixels(pixels);
        tex.Apply();

        // blurring
        if (doBlur)
        {
            // TODO: blur alpha?
            tex = Gaussian.FilterProcessImage(blurStrength, tex);
        }

        // apply to object?
        // check if has _mask?
        // if(m.HasProperty("_MainTex"))
        Selection.activeGameObject.GetComponent <Renderer>().sharedMaterial.SetTexture("_Mask", tex);
        //Selection.activeGameObject.renderer.material.SetTexture("_Mask", tex);

        // 0 - 90
        //Debug.Log("minmax:"+mina+"/"+maxa);

        // save texture
        if (saveFile)
        {
            isDirty = true;
            saveToDisk();
        }

        // assign texture?
        //		renderer.material.SetTexture("_Mask", tex);

        //DestroyImmediate(tex);
    }