예제 #1
0
        public static Rect drawTexture(Rect drawRect, Texture2D texture, AspectType aspectType, float angle)
        {
            angle %= 360;

            if (angle == 0)
            {
                return(drawTexture(drawRect, texture, aspectType));
            }
            else
            {
                angle = 360 - angle;

                int w  = texture.width,
                    h  = texture.height,
                    s  = (int)(Math.Sqrt(w * w + h * h)),
                    sf = Math.Max(w, h);

                Texture2D targetTexture;
                Rect      useRect = getUseRect(drawRect, texture, aspectType);

                if (targetTextures.ContainsKey(texture))
                {
                    targetTexture = targetTextures[texture];

                    float pad = (s - sf) / 2f;
                    float xO = pad, yO = pad, f = (1f * sf) / s, xF = f / X_COUNT, yF = f / Y_COUNT;

                    if (angle > 270)
                    {
                        angle = angle - 360;
                    }
                    else if (angle > 90)
                    {
                        xF   *= -1;
                        angle = 180 - angle;
                    }

                    if (xF < 0)
                    {
                        xO += sf;
                    }

                    int i = angleToIndex(angle);
                    int xamt, yamt;
                    xamt = i % X_COUNT;
                    yamt = (i - xamt) / X_COUNT;

                    xO += xamt * s;
                    yO += yamt * s;

                    float x0, y0;
                    x0 = xO / (s * X_COUNT);
                    y0 = yO / (s * Y_COUNT);

                    Rect coords;
                    coords = new Rect(x0, y0, xF, yF);

                    GUIX.drawTexture(useRect, targetTexture, coords);
                }
                else
                {
                    RenderTexture rotateTexture = new RenderTexture(s * X_COUNT, s * Y_COUNT, 0);
                    targetTextures[texture] = targetTexture = new Texture2D(s * X_COUNT, s * Y_COUNT);

                    RenderTexture.active = rotateTexture;

                    GL.Clear(true, true, CrhcConstants.COLOR_TRANSPARENT);

                    GUIX.undoAllActions();

                    GL.PushMatrix();
                    GL.LoadOrtho();
                    GL.LoadPixelMatrix(0, s * X_COUNT, s * Y_COUNT, 0);

                    for (int y = 0; y < Y_COUNT; y++)
                    {
                        for (int x = 0; x < X_COUNT; x++)
                        {
                            Rect rect = new Rect(new Vector2(-sf / 2, -sf / 2), new Vector2(sf, sf));

                            float ang = indexToAngle(y * X_COUNT + x);

                            GL.PushMatrix();
                            GL.MultMatrix(Matrix4x4.TRS(new Vector3(s * x + s / 2, s * y + s / 2, 0), Quaternion.Euler(0, 0, ang), Vector3.one));
                            Graphics.DrawTexture(rect, texture);
                            GL.PopMatrix();
                        }
                    }

                    GL.PopMatrix();

                    targetTexture.ReadPixels(new Rect(0, 0, s * X_COUNT, s * Y_COUNT), 0, 0);
                    targetTexture.Apply();
                    GUIX.redoAllActions();

                    RenderTexture.active = null;

                    rotateTexture.Release();
                    GameObject.Destroy(rotateTexture);
                }

                return(useRect);
            }
        }
예제 #2
0
    private IEnumerator createTextureCoroutine(int iW, int iH)
    {
        if (waveformTexture == null)
        {
            waveformTexture = new Texture2D(iW, iH);

            AudioClip clip;
            audioSource      = AppRunner.get().AddComponent <AudioSource>();
            audioSource.clip = clip = audioClip.getResource();

            float[] samples = new float[clip.samples * clip.channels];
            clip.GetData(samples, 0);

            float resolution = samples.Length / iW;

            float[] waveForm = new float[iW];

            float maxAmp = 0;

            int steps = 0;

            /*for (int i = 0; i < waveForm.Length; i++) {
             *  waveForm[i] = 0;
             *
             *  for (int ii = 0; ii < resolution; ii++) {
             *      waveForm[i] += Mathf.Abs(samples[(int)((i * resolution) + ii)]);
             *  }
             *
             *  waveForm[i] /= resolution;
             *
             *  maxAmp = Math.Max(maxAmp, Math.Abs(waveForm[i]));
             *
             *  if (steps++ % stepsPerFrame == 0) {
             *      progress = .5f * i / waveForm.Length;
             *      yield return null;
             *  }
             * }
             *
             * // Generate texture.
             * for (int x = 0; x < iW; x++) {
             *  float amp = waveForm[x] / maxAmp;
             *
             *  for (int y = 0; y < iH; y++) {
             *      float relAmp = 2 * (1f * y / iH - .5f);
             *
             *      if (Math.Abs(relAmp) <= Math.Abs(amp)) {
             *          waveformTexture.SetPixel(x, y, CrhcConstants.COLOR_GRAY_DARK);
             *      }
             *      else {
             *          waveformTexture.SetPixel(x, y, CrhcConstants.COLOR_TRANSPARENT);
             *      }
             *  }
             *
             *  if (steps++ % stepsPerFrame == 0) {
             *      progress = .5f + .5f * x / iW;
             *      yield return null;
             *  }
             * }
             *
             * waveformTexture.Apply();
             * hasWaveformTexture = true;*/

            int   dither     = 80;
            float desolution = resolution / dither;

            for (int i = 0; i < waveForm.Length; i++)
            {
                waveForm[i] = 0;

                /*for (int ii = 0; ii < resolution; ii++) {
                 *  waveForm[i] += Mathf.Abs(samples[(int)((i * resolution) + ii)]);
                 * }
                 *
                 * waveForm[i] /= resolution;*/

                for (int ii = 0; ii < resolution; ii += dither)
                {
                    waveForm[i] += Mathf.Abs(samples[(int)((i * resolution) + ii)]);
                }

                waveForm[i] /= desolution;

                maxAmp = Math.Max(maxAmp, Math.Abs(waveForm[i]));

                /*if (steps++ % stepsPerFrame == 0) {
                 *  progress = .5f * i / waveForm.Length;
                 *  yield return null;
                 * }*/
            }

            RenderTexture rotateTexture = new RenderTexture(iW, iH, 0);
            RenderTexture.active = rotateTexture;

            AppRunner.getMaterial().SetPass(0);

            GL.Clear(true, true, CrhcConstants.COLOR_TRANSPARENT);

            GUIX.undoAllActions();
            GL.PushMatrix();
            GL.LoadOrtho();
            GL.LoadPixelMatrix(0, iW, iH, 0);

            float y = iH / 2f, d = 0;

            GL.Begin(GL.LINES);
            GL.Color(CrhcConstants.COLOR_GRAY_DARK);
            for (int x = 0; x < iW; x++)
            {
                float amp = waveForm[x] / maxAmp;
                GL.Vertex3(x, y - amp * y, d);
                GL.Vertex3(x, y + amp * y, d);
            }
            GL.End();

            GL.PopMatrix();

            waveformTexture.ReadPixels(new Rect(0, 0, iW, iH), 0, 0);
            waveformTexture.Apply();
            GUIX.redoAllActions();

            RenderTexture.active = null;

            rotateTexture.Release();
            GameObject.Destroy(rotateTexture);

            hasWaveformTexture = true;
        }

        yield return(null);
    }