public override bool Prepare(MegaModContext mc)
    {
        switch (EAxis)
        {
        case MegaEffectAxis.X:  doX = true;             doY = false;    break;

        case MegaEffectAxis.Y:  doX = false;    doY = true;             break;

        case MegaEffectAxis.XY: doX = true;             doY = true;             break;
        }

        mat = Matrix4x4.identity;
        switch (axis)
        {
        case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f);  l = bbox.max[0] - bbox.min[0];  break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); l = bbox.max[2] - bbox.min[2];  break;

        case MegaAxis.Z: l = bbox.max[1] - bbox.min[1]; break;
        }

        if (l != 0.0f)
        {
            ovl = 1.0f / l;
        }

        MegaMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir);

        SetAxis(mat);
        SetK(amount, crv);

        return(true);
    }
예제 #2
0
    void Calc()
    {
        //if ( from > 0.0f)	from = 0.0f;
        //if ( to < 0.0f )	to = 0.0f;

        tm    = transform.worldToLocalMatrix;
        invtm = tm.inverse;

        mat = Matrix4x4.identity;

        switch (axis)
        {
        case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;

        case MegaAxis.Z: break;
        }

        MegaMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir);
        SetAxis(mat);

        CalcR(axis, Mathf.Deg2Rad * -angle);

        if (doRegion)
        {
            doRegion = false;
            float len = to - from;
            float rat1, rat2;

            if (len == 0.0f)
            {
                rat1 = rat2 = 1.0f;
            }
            else
            {
                rat1 = to / len;
                rat2 = from / len;
            }

            Vector3 pt;
            tmAbove = Matrix4x4.identity;
            MegaMatrix.Translate(ref tmAbove, 0.0f, -to, 0.0f);
            MegaMatrix.RotateZ(ref tmAbove, -Mathf.Deg2Rad * angle * rat1);
            MegaMatrix.Translate(ref tmAbove, 0.0f, to, 0.0f);
            pt = new Vector3(0.0f, to, 0.0f);
            MegaMatrix.Translate(ref tmAbove, tm.MultiplyPoint3x4(Map(0, invtm.MultiplyPoint3x4(pt))) - pt);

            tmBelow = Matrix4x4.identity;
            MegaMatrix.Translate(ref tmBelow, 0.0f, -from, 0.0f);
            MegaMatrix.RotateZ(ref tmBelow, -Mathf.Deg2Rad * angle * rat2);
            MegaMatrix.Translate(ref tmBelow, 0.0f, from, 0.0f);
            pt = new Vector3(0.0f, from, 0.0f);
            MegaMatrix.Translate(ref tmBelow, tm.MultiplyPoint3x4(Map(0, invtm.MultiplyPoint3x4(pt))) - pt);

            doRegion = true;
        }
    }
    public void SetTM1()
    {
        tm = Matrix4x4.identity;

        MegaMatrix.RotateZ(ref tm, -gizmoRot.z * Mathf.Deg2Rad);
        MegaMatrix.RotateY(ref tm, -gizmoRot.y * Mathf.Deg2Rad);
        MegaMatrix.RotateX(ref tm, -gizmoRot.x * Mathf.Deg2Rad);

        MegaMatrix.SetTrans(ref tm, gizmoPos + Offset);

        invtm = tm.inverse;
    }
예제 #4
0
    public override bool Prepare(MegaModContext mc)
    {
        if (from > 0.0f)
        {
            from = 0.0f;
        }

        if (to < 0.0f)
        {
            to = 0.0f;
        }

        mat = Matrix4x4.identity;

        switch (axis)
        {
        case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f);  break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;

        case MegaAxis.Z: break;
        }

        MegaMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir);
        SetAxis(mat);

        float len = 0.0f;

        if (!doRegion)
        {
            switch (axis)
            {
            case MegaAxis.X: len = bbox.max.x - bbox.min.x; break;

            case MegaAxis.Z: len = bbox.max.y - bbox.min.y; break;

            case MegaAxis.Y: len = bbox.max.z - bbox.min.z; break;
            }
        }
        else
        {
            len = to - from;
        }

        if (len == 0.0f)
        {
            len = 0.000001f;
        }

        amountOverLength = amount / len;
        return(true);
    }
예제 #5
0
    void Calc()
    {
        tm    = transform.worldToLocalMatrix;
        invtm = tm.inverse;

        mat = Matrix4x4.identity;

        tm1    = tm;
        invtm1 = invtm;

        switch (axis)
        {
        case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;

        case MegaAxis.Z: break;
        }

        MegaMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir);
        SetAxis(mat);

        mat = Matrix4x4.identity;

        switch (axis1)
        {
        case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;

        case MegaAxis.Z: break;
        }

        MegaMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir1);
        //SetAxis(mat);
        Matrix4x4 itm = mat.inverse;

        tm1    = mat * tm1;
        invtm1 = invtm1 * itm;

        r = -radius;

        if (linkRadii)
        {
            r1 = -radius;
        }
        else
        {
            r1 = -radius1;
        }
    }
예제 #6
0
    public void SetTM1()
    {
        tm = Matrix4x4.identity;
        //Quaternion rot = Quaternion.Euler(-gizmoRot);

        MegaMatrix.RotateZ(ref tm, -gizmoRot.z * Mathf.Deg2Rad);
        MegaMatrix.RotateY(ref tm, -gizmoRot.y * Mathf.Deg2Rad);
        MegaMatrix.RotateX(ref tm, -gizmoRot.x * Mathf.Deg2Rad);

        MegaMatrix.SetTrans(ref tm, gizmoPos + Offset);

        //tm.SetTRS(gizmoPos + Offset, rot, gizmoScale);
        invtm = tm.inverse;
    }
예제 #7
0
    public Matrix4x4 GetMatrix()
    {
        Matrix4x4 tm = Matrix4x4.identity;

        switch (axis)
        {
        case MegaAxis.X: MegaMatrix.RotateY(ref tm, Mathf.PI * 0.5f); break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref tm, Mathf.PI * 0.5f); break;

        case MegaAxis.Z: break;                 //Matrix.RotateY(ref tm, Mathf.PI * 0.5f); break;
        }

        return(tm);
    }
예제 #8
0
    public override bool Prepare(float decay)
    {
        tm    = transform.worldToLocalMatrix;
        invtm = tm.inverse;

        switch (EAxis)
        {
        case MegaEffectAxis.X: doX = true; doY = false; break;

        case MegaEffectAxis.Y: doX = false; doY = true; break;

        case MegaEffectAxis.XY: doX = true; doY = true; break;
        }

        mat = Matrix4x4.identity;
        switch (axis)
        {
        case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); l = Width; break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); l = Length; break;

        case MegaAxis.Z: l = Height; break;
        }

        MegaMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir);

        SetAxis(mat);
        SetK(amount, crv);

        totaldecay = Decay + decay;
        if (totaldecay < 0.0f)
        {
            totaldecay = 0.0f;
        }
        return(true);
    }
    public override bool Prepare(float decay)
    {
        tm    = transform.worldToLocalMatrix;
        invtm = tm.inverse;

        if (from > 0.0f)
        {
            from = 0.0f;
        }

        if (to < 0.0f)
        {
            to = 0.0f;
        }

        mat = Matrix4x4.identity;

        switch (axis)
        {
        case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;

        case MegaAxis.Z: break;
        }

        MegaMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir);
        SetAxis(mat);

        float len = 0.0f;

        if (!doRegion)
        {
            switch (axis)
            {
            case MegaAxis.X: len = Width; break;

            case MegaAxis.Z: len = Height; break;

            case MegaAxis.Y: len = Length; break;
            }
        }
        else
        {
            len = to - from;
        }

        if (len == 0.0f)
        {
            len = 0.000001f;
        }

        amountOverLength = amount / len;

        totaldecay = Decay + decay;
        if (totaldecay < 0.0f)
        {
            totaldecay = 0.0f;
        }

        return(true);
    }
    public override int BuildMesh(MegaShapeLoft loft, int triindex)
    {
        MegaShape path = layerPath;

        if (tangent < 0.1f)
        {
            tangent = 0.1f;
        }

        //if ( snaptopath )
        //{
        //mat = path.transform.worldToLocalMatrix * transform.localToWorldMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        //mat = transform.localToWorldMatrix * layerPath.transform.worldToLocalMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        //mat = layerPath.transform.worldToLocalMatrix;	// * transform.localToWorldMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        //}
        //else
        //{
        mat = Matrix4x4.identity;                       //path.transform.localToWorldMatrix;
        //}
        //mat = Matrix4x4.identity;	//transform.worldToLocalMatrix;
        //mat = transform.localToWorldMatrix * path.transform.worldToLocalMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        //mat = path.transform.worldToLocalMatrix * transform.localToWorldMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;

        tm = Matrix4x4.identity;

        switch (axis)
        {
        case MegaAxis.X: MegaMatrix.RotateY(ref tm, -Mathf.PI * 0.5f); break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref tm, -Mathf.PI * 0.5f); break;

        case MegaAxis.Z: break;
        }

        meshtm = Matrix4x4.identity;
        MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot);

        meshrot = Quaternion.Euler(rot);
        tw      = meshrot;

        pathtm.SetTRS(offPath, Quaternion.Euler(rotPath), sclPath);
        float off    = 0.0f;
        int   trioff = 0;

        int vi = 0;
        int fi = 0;

        Vector3 sploff = Vector3.zero;

        if (snap)
        {
            sploff = path.splines[0].knots[0].p - path.splines[curve].knots[0].p;
        }

        int ax = (int)axis;

        float palpha = Start;           // * 0.01f;

        if (startObj != null && StartEnabled)
        {
            Vector3 sscl = StartScale * GlobalScale;
            Vector3 soff = Vector3.Scale(StartOff, sscl);

            off -= startObj.bounds.min[(int)axis] * sscl[ax];
            for (int i = 0; i < sverts.Length; i++)
            {
                Vector3 p = sverts[i];

                p             = Deform(p, path, palpha, off, sscl, RemoveDof, soff, sploff);            // + sploff;
                loftverts[vi] = p;
                loftuvs[vi++] = suvs[i];
            }

            for (int i = 0; i < stris.Length; i++)
            {
                lofttris[fi++] = stris[i] + triindex;
            }

            off   += startObj.bounds.max[(int)axis] * sscl[ax];
            off   += StartGap;
            trioff = vi;
        }

        if (mainObj != null && MainEnabled)
        {
            float mw = mainObj.bounds.size[(int)axis];

            Vector3 mscl = MainScale * GlobalScale;
            Vector3 moff = Vector3.Scale(MainOff, mscl);

            off -= mainObj.bounds.min[(int)axis] * mscl[ax];
            mw  *= mscl[(int)axis];

            for (int r = 0; r < repeat; r++)
            {
                for (int i = 0; i < mverts.Length; i++)
                {
                    Vector3 p = mverts[i];
                    p             = Deform(p, path, palpha, off, mscl, RemoveDof, moff, sploff);                // + sploff;
                    loftverts[vi] = p;
                    loftuvs[vi++] = muvs[i];
                }

                for (int i = 0; i < mtris.Length; i++)
                {
                    lofttris[fi++] = mtris[i] + trioff + triindex;
                }

                off   += mw;
                off   += Gap;
                trioff = vi;
            }

            off -= Gap;
            off += (mainObj.bounds.max[(int)axis] * mscl[ax]) - mw;
        }

        if (endObj != null && EndEnabled)
        {
            Vector3 escl = EndScale * GlobalScale;
            Vector3 eoff = Vector3.Scale(EndOff, escl);

            off -= endObj.bounds.min[(int)axis] * escl[ax];
            off += EndGap;

            for (int i = 0; i < everts.Length; i++)
            {
                Vector3 p = everts[i];
                p             = Deform(p, path, palpha, off, escl, RemoveDof, eoff, sploff);            // + sploff;
                loftverts[vi] = p;
                loftuvs[vi++] = euvs[i];
            }

            for (int i = 0; i < etris.Length; i++)
            {
                lofttris[fi++] = etris[i] + trioff + triindex;
            }

            trioff += everts.Length;
        }

        if (conform)
        {
            CalcBounds(loftverts);
            DoConform(loft, loftverts);
        }

        return(triindex);
    }
예제 #11
0
    public override void BuildMesh(MegaRope rope)
    {
        BuildObjectLinks(rope);
#if false
        float len = rope.RopeLength;

        float linklen = (linkOff1.y - linkOff.y) * linkScale.y;         //Length - linkOff.y
        int   lc      = (int)(len / linklen);

        if (lc != linkcount)
        {
            InitLinkMesh(rope);
        }

        int vi = 0;

        mat = Matrix4x4.identity;

        Matrix4x4 linkrot1 = Matrix4x4.identity;
        Matrix4x4 linkrot2 = Matrix4x4.identity;

        MegaMatrix.RotateY(ref linkrot2, Mathf.PI * 0.5f);

        MegaMatrix.RotateY(ref linkrot1, Mathf.PI * 0.5f);
        MegaMatrix.RotateZ(ref linkrot1, Mathf.PI * 0.5f);

        MegaMatrix.Scale(ref linkrot1, linkScale, false);
        MegaMatrix.Scale(ref linkrot2, linkScale, false);

        // Last off1 becomes next link pos
        // Again uvs and tris are a one off, its just verts that change
        float lastalpha = 0.0f;
        for (int i = 0; i < linkcount; i++)
        {
            //Debug.Log("alpha " + alpha);
            if (LinkMat)
            {
                float alpha = (float)(i + 1) / (float)linkcount;
                wtm       = GetLinkMat(alpha, lastalpha, rope);
                lastalpha = alpha;
            }
            else
            {
                float alpha = (float)(i) / (float)linkcount;
                wtm = rope.GetDeformMat(alpha);                 // Different mat needed, not current vel bu vel to the next point
            }

            if ((i & 1) == 1)
            {
                wtm = wtm * linkrot1;
            }
            else
            {
                wtm = wtm * linkrot2;
            }

            for (int v = 0; v < lverts.Length; v++)
            {
                verts[vi + v] = wtm.MultiplyPoint3x4(lverts[v]);
            }

            vi += lverts.Length;
        }

        rope.mesh.vertices = verts;
        rope.mesh.RecalculateBounds();
        rope.mesh.RecalculateNormals();
#endif
    }
    public override int BuildMesh(MegaShapeLoft loft, int triindex)
    {
        if (layerPath == null)
        {
            return(triindex);
        }

        LayerLength = 1.0f / layerPath.splines[curve].length;

        if (tangent < 0.1f)
        {
            tangent = 0.1f;
        }

        //mat = loft.transform.localToWorldMatrix;
        mat = transform.localToWorldMatrix * layerPath.transform.worldToLocalMatrix;            // * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        //mat = surfaceLoft.transform.localToWorldMatrix * transform.worldToLocalMatrix;

        tm = Matrix4x4.identity;

        switch (axis)
        {
        case MegaAxis.X: MegaMatrix.RotateY(ref tm, -Mathf.PI * 0.5f); break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref tm, -Mathf.PI * 0.5f); break;

        case MegaAxis.Z: break;
        }

        //meshtm = Matrix4x4.identity;
        //MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot);

        meshrot = Quaternion.Euler(rot);
        tw      = meshrot;

        //meshrot = Quaternion.Euler(rot);
        int trioff = 0;

        int vi = 0;
        int fi = 0;

        pathtm.SetTRS(offPath, Quaternion.Euler(rotPath), sclPath);

        Matrix4x4 omat = Matrix4x4.identity;

#if UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019
        Random.InitState(Seed);
#else
        Random.seed = Seed;
#endif

        Vector3 pivot;
        Vector3 scl = Vector3.zero;

        Vector3 sclmin = Vector3.Scale(scaleRangeMin, scale);
        Vector3 sclmax = Vector3.Scale(scaleRangeMax, scale);

        Vector3 sploff = Vector3.zero;
        if (snap)
        {
            sploff = layerPath.splines[0].knots[0].p - layerPath.splines[curve].knots[0].p;
        }

        float a = 0.0f;
        for (int r = 0; r < Count; r++)
        {
            scl.x = (scale.x + Mathf.Lerp(sclmin.x, sclmax.x, Random.value)) * GlobalScale;
            scl.y = (scale.y + Mathf.Lerp(sclmin.y, sclmax.y, Random.value)) * GlobalScale;
            scl.z = (scale.z + Mathf.Lerp(sclmin.z, sclmax.z, Random.value)) * GlobalScale;

            if (useDensity)
            {
                a = FindScatterAlpha();
            }
            else
            {
                a = Random.value;
            }

            float alpha = start + (a * length) + Alpha;

            Vector3 rt = rot + (((Random.value - 0.5f) * 2.0f) * rotRange);
            meshrot = Quaternion.Euler(rt);

            //if ( CalcUp )
            pivot = Deform(layerPath, alpha, 0.0f, scl, RemoveDof, Vector3.zero, out omat, sploff);
            //else
            //	pivot = Deform(layerPath, alpha, 0.0f, scl, 0.0f, Vector3.zero, out omat, sploff);

            Vector3 pp = Vector3.zero;

            for (int i = 0; i < sverts.Length; i++)
            {
                pp.x = sverts[i].x + Offset.x;
                pp.y = sverts[i].y + Offset.y;
                pp.z = sverts[i].z + Offset.z;

                pp = omat.MultiplyPoint3x4(pp);
                loftverts[vi].x = pp.x + pivot.x;
                loftverts[vi].y = pp.y + pivot.y;
                loftverts[vi].z = pp.z + pivot.z;

                loftuvs[vi++] = suvs[i];
            }

            for (int i = 0; i < stris.Length; i++)
            {
                lofttris[fi++] = stris[i] + trioff + triindex;
            }

            trioff = vi;
        }

        if (conform)
        {
            CalcBounds(loftverts);
            DoConform(loft, loftverts);
        }

        return(triindex);
    }
예제 #13
0
    public override int BuildMesh(MegaShapeLoft loft, int triindex)
    {
        MegaLoftLayerSimple layer = (MegaLoftLayerSimple)surfaceLoft.Layers[surfaceLayer];

        LayerLength = 1.0f / layer.GetLength(surfaceLoft);
        if (tangent < 0.1f)
        {
            tangent = 0.1f;
        }

        //mat = surfaceLoft.transform.localToWorldMatrix;
        //mat = transform.localToWorldMatrix * surfaceLoft.transform.worldToLocalMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        mat = surfaceLoft.transform.localToWorldMatrix * transform.worldToLocalMatrix;

        tm = Matrix4x4.identity;

        switch (axis)
        {
        case MegaAxis.X: MegaMatrix.RotateY(ref tm, -Mathf.PI * 0.5f); break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref tm, -Mathf.PI * 0.5f); break;

        case MegaAxis.Z: break;
        }

        meshrot = Quaternion.Euler(rot);
        int trioff = 0;

        int vi = 0;

        Matrix4x4 omat = Matrix4x4.identity;

#if UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019
        Random.InitState(Seed);
#else
        Random.seed = Seed;
#endif

        Vector3 newup = Vector3.up;
        Vector3 pivot;
        Vector3 ps1;
        Vector3 scl    = Vector3.zero;
        Vector3 sclmin = Vector3.Scale(scaleRangeMin, scale);
        Vector3 sclmax = Vector3.Scale(scaleRangeMax, scale);

        float a = 0.0f;
        for (int r = 0; r < Count; r++)
        {
            scl.x = (scale.x + Mathf.Lerp(sclmin.x, sclmax.x, Random.value)) * GlobalScale;
            scl.y = (scale.y + Mathf.Lerp(sclmin.y, sclmax.y, Random.value)) * GlobalScale;
            scl.z = (scale.z + Mathf.Lerp(sclmin.z, sclmax.z, Random.value)) * GlobalScale;

            if (useDensity)
            {
                a = FindScatterAlpha();
            }
            else
            {
                a = Random.value;
            }

            float alpha  = start + (a * length) + Alpha;
            float calpha = cstart + (Random.value * clength) + CAlpha;

            Vector3 rt = rot + (((Random.value - 0.5f) * 2.0f) * rotRange);
            meshrot = Quaternion.Euler(rt);

            if (CalcUp)
            {
                pivot = layer.GetPos1(surfaceLoft, calpha, alpha, 0.001f, out ps1, out newup);

                Quaternion uprot = Quaternion.FromToRotation(Vector3.up, newup);
                tw = Quaternion.AngleAxis(180.0f, Vector3.forward) * uprot;

                Vector3 relativePos = ps1 - pivot;
                relativePos.y *= RemoveDof;

                Quaternion rotation = Quaternion.LookRotation(relativePos) * tw * meshrot;
                //wtm.SetTRS(Vector3.zero, rotation, scl);
                wtm.SetTRS(pivot, rotation, scl);

                omat = mat * wtm;
            }
            else
            {
                pivot = layer.GetPos1(surfaceLoft, calpha, alpha, 0.001f, out ps1, out newup);

                tw = Quaternion.AngleAxis(180.0f, Vector3.forward);

                Vector3 relativePos = ps1 - pivot;
                relativePos.y *= RemoveDof;

                Quaternion rotation = Quaternion.LookRotation(relativePos) * tw * meshrot;
                //wtm.SetTRS(Vector3.zero, rotation, scl);
                wtm.SetTRS(pivot, rotation, scl);

                omat = mat * wtm;
            }

            Vector3 pp = Vector3.zero;

            for (int i = 0; i < mverts.Length; i++)
            {
                pp.x = mverts[i].x + Offset.x;
                pp.y = mverts[i].y + Offset.y;
                pp.z = mverts[i].z + Offset.z;

                pp = omat.MultiplyPoint3x4(pp);
                loftverts[vi].x = pp.x;                 // + pivot.x;
                loftverts[vi].y = pp.y;                 // + pivot.y;
                loftverts[vi].z = pp.z;                 // + pivot.z;

                loftuvs[vi++] = muvs[i];
            }

            for (int i = 0; i < mainlofttris.Count; i++)
            {
                int toff = mainlofttris[i].offset;

                for (int t = 0; t < mainlofttris[i].sourcetris.Length; t++)
                {
                    mainlofttris[i].tris[toff++] = mainlofttris[i].sourcetris[t] + trioff + triindex;
                }

                mainlofttris[i].offset = toff;
            }

            trioff = vi;
        }

        return(triindex);
    }
예제 #14
0
    public override void BuildMesh(MegaRope rope)
    {
        float lengthuvtile = uvtiley * rope.RopeLength;

        Twist    = TwistPerUnit * rope.RopeLength;
        segments = (int)(rope.RopeLength * SegsPerUnit);

        float off = (rope.radius * 0.5f) + offset;

        float sradius = 0.0f;

        if (strands == 1)
        {
            off     = offset;
            sradius = rope.radius;
        }
        else
        {
            sradius = (rope.radius * 0.5f) + strandRadius;
        }

        BuildCrossSection(sradius);

        int vcount = ((segments + 1) * (sides + 1)) * strands;
        int tcount = ((sides * 2) * segments) * strands;

        if (cap)
        {
            vcount += ((sides + 1) * 2) * strands;
            tcount += (sides * 2) * strands;
        }

        if (verts == null || verts.Length != vcount)
        {
            verts = new Vector3[vcount];
        }

        bool builduvs = false;

        if (uvs == null || uvs.Length != vcount)
        {
            uvs      = new Vector2[vcount];
            tris     = new int[tcount * 3];
            builduvs = true;
        }

        //mat = Matrix4x4.identity;
        tm = Matrix4x4.identity;

        switch (rope.axis)
        {
        case MegaAxis.X: MegaMatrix.RotateY(ref tm, -Mathf.PI * 0.5f); break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref tm, -Mathf.PI * 0.5f); break;

        case MegaAxis.Z: break;
        }

        //switch ( rope.RopeUp )
        //{
        //	case MegaAxis.X: ropeup = Vector3.right; break;
        //	case MegaAxis.Y: ropeup = Vector3.up; break;
        //	case MegaAxis.Z: ropeup = Vector3.forward; break;
        //}
        // We only need to refresh the verts, tris and uvs are done once
        int vi = 0;
        int ti = 0;

        Vector2 uv   = Vector2.zero;
        Vector3 soff = Vector3.zero;

        //Vector3 up = Vector3.up;

        Vector3 T = Vector3.zero;
        Vector3 N = Vector3.zero;
        Vector3 B = Vector3.zero;

        for (int s = 0; s < strands; s++)
        {
            //rollingquat = Quaternion.identity;

            float ang = ((float)s / (float)strands) * Mathf.PI * 2.0f;

            soff.x = Mathf.Sin(ang) * off;
            soff.z = Mathf.Cos(ang) * off;
            //Matrix.SetTrans(ref tm, soff);

            int vo = vi;

            // Cap maybe needs to be submesh, at least needs seperate verts
            if (cap)
            {
                // Add slice at 0
                float alpha = 0.0f;
                wtm = rope.GetDeformMat(alpha);
                //wtm = rope.GetDeformMat(alpha, up);

                //float uvt = alpha * uvtwist;

                float tst = alpha * Twist * Mathf.PI * 2.0f;
                soff.x = Mathf.Sin(ang + tst) * off;
                soff.z = Mathf.Cos(ang + tst) * off;

                //int ovi = vi;

                for (int v = 0; v <= cross.Length; v++)
                {
                    Vector3 p = tm.MultiplyPoint3x4(cross[v % cross.Length] + soff);
                    verts[vi] = wtm.MultiplyPoint3x4(p);                        //cross[v]);

                    if (builduvs)
                    {
                        uv.y = 0.0f;                            //alpha * uvtiley;
                        uv.x = 0.0f;                            //(((float)v / (float)cross.Length) * uvtilex) + uvt;

                        uvs[vi++] = uv;
                    }
                    else
                    {
                        vi++;
                    }
                }

                //up = wtm.MultiplyPoint3x4(tm.MultiplyPoint3x4(cross[0])).normalized;

                if (builduvs)
                {
                    for (int sd = 1; sd < sides; sd++)
                    {
                        tris[ti++] = vo;
                        tris[ti++] = vo + sd + 1;
                        tris[ti++] = vo + sd;
                    }
                }

                vo = vi;

                // Other end
                alpha = 1.0f;
                wtm   = rope.GetDeformMat(alpha);

                //wtm = rope.CalcFrame(T, ref N, ref B);

                //uvt = alpha * uvtwist;

                tst    = alpha * Twist * Mathf.PI * 2.0f;
                soff.x = Mathf.Sin(ang + tst) * off;
                soff.z = Mathf.Cos(ang + tst) * off;

                for (int v = 0; v <= cross.Length; v++)
                {
                    Vector3 p = tm.MultiplyPoint3x4(cross[v % cross.Length] + soff);
                    verts[vi] = wtm.MultiplyPoint3x4(p);                        //cross[v]);

                    if (builduvs)
                    {
                        uv.y = 0.0f;                            //alpha * uvtiley;
                        uv.x = 0.0f;                            //(((float)v / (float)cross.Length) * uvtilex) + uvt;

                        uvs[vi++] = uv;
                    }
                    else
                    {
                        vi++;
                    }
                }

                if (builduvs)
                {
                    for (int sd = 1; sd < sides; sd++)
                    {
                        tris[ti++] = vo;
                        tris[ti++] = vo + sd;
                        tris[ti++] = vo + sd + 1;
                    }
                }
            }

            vo = vi;

            //wtm = rope.GetDeformMat(0.0f);

            for (int i = 0; i <= segments; i++)
            {
                float alpha = ((float)i / (float)segments);

                float uvt = alpha * uvtwist;

                float tst = (alpha * Twist * Mathf.PI * 2.0f);                  // + rollang;
                soff.x = Mathf.Sin(ang + tst) * off;
                soff.z = Mathf.Cos(ang + tst) * off;

                if (i == 0)
                {
                    wtm = rope.GetDeformMat(alpha);
                    T   = wtm.MultiplyPoint3x4(rope.Velocity(0.0f).normalized);

                    Vector3 cp = wtm.MultiplyPoint3x4(tm.MultiplyPoint3x4(cross[0]));
                    N = (cp - wtm.MultiplyPoint3x4(rope.Interp(0.0f))).normalized;
                    B = Vector3.Cross(T, N);
                }
                else
                {
                    Vector3 np = rope.Interp(alpha);                            //tm.MultiplyPoint3x4(rope.Interp(alpha));
                    T   = rope.Velocity(alpha).normalized;
                    wtm = rope.CalcFrame(T, ref N, ref B);
                    //wtm.SetRow(3, np);
                    MegaMatrix.SetTrans(ref wtm, np);
                }

                //wtm = rope.GetDeformMat(alpha);

                for (int v = 0; v <= cross.Length; v++)
                {
                    Vector3 p = tm.MultiplyPoint3x4(cross[v % cross.Length] + soff);
                    verts[vi] = wtm.MultiplyPoint3x4(p);                        //cross[v]);

                    //if ( true )	//builduvs )
                    {
                        uv.y = alpha * lengthuvtile;                            //uvtiley;
                        uv.x = (((float)v / (float)cross.Length) * uvtilex) + uvt;

                        uvs[vi++] = uv;
                    }
                    //else
                    //	vi++;
                }
                // Uv is - to 1 around and alpha along
            }

            if (builduvs)
            {
                int sc = sides + 1;
                for (int i = 0; i < segments; i++)
                {
                    for (int v = 0; v < cross.Length; v++)
                    {
                        tris[ti++] = (i * sc) + v + vo;
                        tris[ti++] = ((i + 1) * sc) + ((v + 1) % sc) + vo;
                        tris[ti++] = ((i + 1) * sc) + v + vo;

                        tris[ti++] = (i * sc) + v + vo;
                        tris[ti++] = (i * sc) + ((v + 1) % sc) + vo;
                        tris[ti++] = ((i + 1) * sc) + ((v + 1) % sc) + vo;
                    }
                }
            }
        }

        //Mesh mesh = MegaUtils.GetMesh(rope.gameObject);

        if (builduvs)
        {
            rope.mesh.Clear();
            rope.mesh.vertices  = verts;
            rope.mesh.uv        = uvs;
            rope.mesh.triangles = tris;
        }
        else
        {
            rope.mesh.vertices = verts;
            rope.mesh.uv       = uvs;
        }

        rope.mesh.RecalculateBounds();
        rope.mesh.RecalculateNormals();
        //MeshConstructor.BuildTangents(mesh);
    }