Пример #1
0
    // Update is called once per frame
    void Update()
    {
        if (_mesh == null)
        {
            return;
        }

        //仮
        if (Input.GetKeyDown(KeyCode.W))
        {
            _isConstantWidth = !_isConstantWidth;
        }

        _mouse.Update();

        int num = _mesh.vertexCount / 2;//頂点の数、最大数

        //if( _mouse.positions.Count<=num)return;
        if (_mouse.GetLength() <= num)
        {
            return;
        }

        var forward = Camera.main.transform.forward;


        var v = _mesh.vertices;

        //長さを調整する
        float lenRatio = brushLengthRatio;//0.1f;//0.5f;
        int   vertNum  = Mathf.FloorToInt(num * lenRatio);

        for (int i = 0; i < vertNum; i++)
        {
            var r = 1f - (float)i / (float)(vertNum - 1);

            //var rot = _mouse.rotations[i] + _offsetRot*Mathf.Deg2Rad;
            var pos = _mouse.GetPoint(i);//_mouse.positions[i];

            //random値を追加
            //pos.x += 0.3f * (Random.value - 0.5f);
            //pos.y += 0.3f * (Random.value - 0.5f);

            //ブラシの太さ、瞬間の速度で太さが変えている
            var vv = _mouse.velocity.magnitude * 8f;
            if (vv > 1f)
            {
                vv = 1f;
            }
            var w = vv * _brushWidth * Mathf.Sin(r * Mathf.PI) * brushWidthRatio;

            //一定かどうか
            if (_isConstantWidth)
            {
                w = _brushWidth * brushWidthRatio;
            }

            //奇跡の方向
            Vector3 current = _mouse.GetPoint(i);     //_mouse.positions[ i ];
            Vector3 next    = _mouse.GetPoint(i + 1); //_mouse.positions[ i+1 ];
            Vector3 dir     = current - next;

            if (_isNejireFix)
            {
                dir.x = Mathf.Abs(dir.x);
                dir.y = Mathf.Abs(dir.y);
                dir.z = Mathf.Abs(dir.z);
            }

            //カメラの正面ベクトルと、方向ベクトルに直行するベクトルを取得
            var     dir1 = Vector3.Cross(forward, dir).normalized;
            var     dir2 = -Vector3.Cross(forward, dir).normalized;
            Vector3 v1   = dir1 * w;
            Vector3 v2   = dir2 * w;

            //ちょっとねじれないように処理を入れてみるテスト
            //var yy1 = pos.y + v1.y;//,pos.y + v2.y );
            //var yy2 = pos.y + v2.y;

            //if(isNejireFix){
            //    yy1 = Mathf.Min( pos.y + v1.y,pos.y + v2.y );
            //    yy2 = Mathf.Max( pos.y + v1.y,pos.y + v2.y );
            //}

            //長さを

            v[i + 0].x = pos.x + v1.x;
            v[i + 0].y = pos.y + v1.y;
            v[i + 0].z = pos.z + v1.z;

            v[i + num].x = pos.x + v2.x;
            v[i + num].y = pos.y + v2.y;
            v[i + num].z = pos.z + v2.z;
        }


        if (isBoxel)
        {
            for (int i = 0; i < num; i++)
            {
                //if( i<num*0.5f ){

                var boxel  = _boxel * (1f - brushWidthRatio); // * (1f+_mouse.velocity.magnitude*30f);//0.5f;
                var nobasu = _mouse.velocity.magnitude * 30f + 1f;

                v[i + 0].x = Mathf.Floor(v[i + 0].x * boxel) / boxel;
                v[i + 0].y = Mathf.Floor(v[i + 0].y * boxel) / boxel;
                v[i + 0].z = Mathf.Floor(v[i + 0].z * boxel) / boxel;

                v[i + num].x = (Mathf.Ceil(v[i + num].x * boxel)) / boxel;
                v[i + num].y = (Mathf.Ceil(v[i + num].y * boxel)) / boxel;
                v[i + num].z = (Mathf.Ceil(v[i + num].z * boxel)) / boxel;
                //v[i+num].z = v[i+0].z;//(Mathf.Ceil( v[i+num].z * boxel ) ) / boxel;

                //}
            }
        }

        //消す
        var lastPos = _mouse.GetPoint(vertNum - 2);// _mouse.positions[vertNum-2];

        for (int i = vertNum - 1; i < num; i++)
        {
            var pos = lastPos;
            v[i + 0].x   = pos.x;
            v[i + 0].y   = pos.y;
            v[i + 0].z   = pos.z;
            v[i + num].x = pos.x;
            v[i + num].y = pos.y;
            v[i + num].z = pos.z;
        }

        _zRad         += _zRadSpd;
        _mesh.vertices = v;
        if (_frame % 2 == 0 && !isBoxel)
        {
            _mesh.RecalculateNormals();
            _mesh.RecalculateTangents();
        }

        int idx = 0;//Mathf.FloorToInt(v.Length/2f);

        screenPos    = RectTransformUtility.WorldToScreenPoint(Camera.main, v[idx]);
        screenPos.x /= Screen.width;
        screenPos.y /= Screen.height;

        //色の取得
        Color col = Color.red;

        if (colorTex)
        {
            /*
             * var ww = colorTex.width;
             * var hh = colorTex.height;
             * col = colorTex.GetPixel(
             *  Mathf.FloorToInt( screenPos.x * ww * 0.999f ),
             *  Mathf.FloorToInt( screenPos.y * hh * 0.999f )
             * );
             * col.a = 0.5f;//_alpha;
             */
            _property.SetTexture("_ColorTex", colorTex);
        }

        _property.SetVector("_Color", col);

        _property.SetVector("_EmissionColor", col * 0.5f);//暗くなりすぎないように
        _renderer.SetPropertyBlock(_property);

        _frame++;
    }