예제 #1
0
    void Update()
    {
        if (texture == null)
        {
            texture = GrassManipulationUtility.GetGrassTexture(transform, false);
        }

        Color[] pixels = texture.GetPixels();

        for (int i = 0; i < pixels.Length; i++)
        {
            pixels[i] = Color.Lerp(pixels[i], originalColor, regenerationSpeed * Time.deltaTime);
        }

        texture.SetPixels(pixels);

        //Search for texture updater, which prevents multiply applys per frame (for multiple displacers)
        TextureUpdater updater = GetComponent <TextureUpdater>();

        if (updater == null)
        {
            updater = gameObject.AddComponent <TextureUpdater>();
            updater.targetTexture = texture;
        }

        updater.RequestTextureUpdate();
    }
예제 #2
0
    public void ApplyBrush(Vector3 hitPoint)
    {
        RaycastHit hit;
        Vector2    texForward, texRight;

        if (!GrassManipulationUtility.GetWorldToTextureSpaceMatrix(new Ray(hitPoint + Vector3.up * 1000, Vector3.down),
                                                                   EPSILON, GrassCollider, out hit, out texForward, out texRight))
        {
            return;
        }

        Vector2 texCoord = hit.textureCoord;

        //Convert the world space radius to a pixel radius in texture space. This requires square textures.
        int pixelRadius = (int)(Size * texForward.magnitude * Texture.width);

        //Calculate the pixel coordinates of the point where the raycast hit the texture.
        Vector2 mid = new Vector2(texCoord.x * Texture.width, texCoord.y * Texture.height);

        //Calculate the pixel area where the texture will be changed
        int targetStartX = (int)(mid.x - pixelRadius);
        int targetStartY = (int)(mid.y - pixelRadius);
        int startX       = Mathf.Clamp(targetStartX, 0, Texture.width);
        int startY       = Mathf.Clamp(targetStartY, 0, Texture.height);
        int width        = Mathf.Min(targetStartX + pixelRadius * 2, Texture.width) - targetStartX;
        int height       = Mathf.Min(targetStartY + pixelRadius * 2, Texture.height) - targetStartY;

        mid -= new Vector2(startX, startY);

        //Get pixels
        Color[] pixels = Texture.GetPixels(startX, startY, width, height);

        //Iterate trough all pixels
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                int     index = y * width + x;
                Vector2 uv    = ((new Vector2(x, y) - mid) / pixelRadius) * 0.5f + new Vector2(0.5f, 0.5f);
                pixels[index] = ApplyBrushToPixel(pixels[index], uv);
            }
        }

        //Save pixels and apply them to the texture
        Texture.SetPixels(startX, startY, width, height, pixels);
        Texture.Apply();
    }