static int _CreateClothSkinningCoefficient(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 0);
        ClothSkinningCoefficient obj = new ClothSkinningCoefficient();

        LuaScriptMgr.PushValue(L, obj);
        return(1);
    }
Пример #2
0
        private float GetCoefficient(ClothSkinningCoefficient coefficient)
        {
            DrawMode drawMode = this.drawMode;

            if (drawMode != DrawMode.MaxDistance)
            {
                if (drawMode == DrawMode.CollisionSphereDistance)
                {
                    return(coefficient.collisionSphereDistance);
                }
                return(0f);
            }
            return(coefficient.maxDistance);
        }
        public void SetAllClothContraints()
        {
            Debug.Log("Setting All Cloth Constraints");

            if (m_Cloth == null)
            {
                Debug.LogError("No Cloth component found!");
                return;
            }

            ClothSkinningCoefficient[] newConstraints = new ClothSkinningCoefficient[m_Cloth.coefficients.Length];
            for (int i = 0; i < m_Cloth.coefficients.Length; i++)
            {
                newConstraints [i].maxDistance             = distance;
                newConstraints [i].collisionSphereDistance = penetration;
            }
            m_Cloth.coefficients = newConstraints;
        }
Пример #4
0
    /// <summary>
    /// ClothのMaxDistanceを自動で設定する
    /// </summary>
    /// <param name="cloth"></param>
    /// <returns></returns>
    ClothSkinningCoefficient[] SetAutoWeight(Cloth cloth, int mode)
    {
        float maxH = float.MinValue;
        float minH = float.MaxValue;

        foreach (Vector3 v in cloth.vertices)
        {
            if (v.z > maxH)
            {
                maxH = v.z;
            }
            if (v.z < minH)
            {
                minH = v.z;
            }
        }

        maxH -= minH;

        Debug.Log("max=" + maxH + "min=" + minH);
        ClothSkinningCoefficient[] CSC = new ClothSkinningCoefficient[cloth.coefficients.Length];

        maxH -= (maxH * offset);

        for (int i = 0; i < cloth.coefficients.Length; i++)
        {
            float h = cloth.vertices[i].z;

            float w = 0f;
            switch (mode)
            {
            case 0: { w = Mathf.SmoothStep(maxClothDistance, 0f, Mathf.Abs((h - minH) / maxH)); } break;

            case 1: { w = Mathf.InverseLerp(maxClothDistance, 0f, Mathf.Abs((h - minH) / maxH)); } break;

            default: { w = Mathf.Lerp(maxClothDistance, 0f, Mathf.Abs((h - minH) / maxH)); } break;
            }

            CSC[i].maxDistance             = w;
            CSC[i].collisionSphereDistance = 0f;
        }

        return(CSC);
    }
    static int get_collisionSphereRadius(IntPtr L)
    {
        object o = LuaScriptMgr.GetLuaObject(L, 1);

        if (o == null)
        {
            LuaTypes types = LuaDLL.lua_type(L, 1);

            if (types == LuaTypes.LUA_TTABLE)
            {
                LuaDLL.luaL_error(L, "unknown member name collisionSphereRadius");
            }
            else
            {
                LuaDLL.luaL_error(L, "attempt to index collisionSphereRadius on a nil value");
            }
        }

        ClothSkinningCoefficient obj = (ClothSkinningCoefficient)o;

        LuaScriptMgr.Push(L, obj.collisionSphereRadius);
        return(1);
    }
    static int get_maxDistance(IntPtr L)
    {
        object o = LuaScriptMgr.GetLuaObject(L, 1);

        if (o == null)
        {
            LuaTypes types = LuaDLL.lua_type(L, 1);

            if (types == LuaTypes.LUA_TTABLE)
            {
                LuaDLL.luaL_error(L, "unknown member name maxDistance");
            }
            else
            {
                LuaDLL.luaL_error(L, "attempt to index maxDistance on a nil value");
            }
        }

        ClothSkinningCoefficient obj = (ClothSkinningCoefficient)o;

        LuaScriptMgr.Push(L, obj.maxDistance);
        return(1);
    }
    static int set_collisionSphereDistance(IntPtr L)
    {
        object o = LuaScriptMgr.GetLuaObject(L, 1);

        if (o == null)
        {
            LuaTypes types = LuaDLL.lua_type(L, 1);

            if (types == LuaTypes.LUA_TTABLE)
            {
                LuaDLL.luaL_error(L, "unknown member name collisionSphereDistance");
            }
            else
            {
                LuaDLL.luaL_error(L, "attempt to index collisionSphereDistance on a nil value");
            }
        }

        ClothSkinningCoefficient obj = (ClothSkinningCoefficient)o;

        obj.collisionSphereDistance = (float)LuaScriptMgr.GetNumber(L, 3);
        LuaScriptMgr.SetValueObject(L, 1, obj);
        return(0);
    }
Пример #8
0
    public void OnWizardCreate()
    {
        if (m_source == null)
        {
            Debug.LogError("Source is null");
            return;
        }

        if (m_dest == null)
        {
            Debug.LogError("Dest is null");
            return;
        }

        var vertColours = m_source.sharedMesh.colors;

        if (vertColours.Length == 0)
        {
            Debug.LogError("Mesh: " + m_source.sharedMesh.name + " has no vert colours");
            return;
        }

        var bakedSkin = new Mesh();

        m_source.BakeMesh(bakedSkin);

        var coefficients = new ClothSkinningCoefficient[m_dest.coefficients.Length];

        var vertexLookup = new Dictionary <int, List <Vertex3Hashed> >();

        var skinTransformNoScale = Matrix4x4.TRS(m_source.transform.position, m_source.transform.rotation, Vector3.one);

        for (int index = 0; index < bakedSkin.vertices.Length; index++)
        {
            var skinVertWorldSpace = skinTransformNoScale.MultiplyPoint3x4(bakedSkin.vertices[index]); //m_source.transform.TransformPoint(bakedSkin.vertices[index]);

            var vertex3Hashed = new Vertex3Hashed(skinVertWorldSpace, bakedSkin.colors[index], m_cellSize);

            List <Vertex3Hashed> cellList;
            if (!vertexLookup.TryGetValue(vertex3Hashed.GetHashCode(), out cellList))
            {
                vertexLookup.Add(vertex3Hashed.GetHashCode(), new List <Vertex3Hashed>(1)
                {
                    vertex3Hashed
                });
            }
            else
            {
                cellList.Add(vertex3Hashed);
            }
        }

        var destSkin = m_dest.GetComponent <SkinnedMeshRenderer>();
        var rootBone = destSkin.rootBone != null ? destSkin.rootBone : destSkin.transform;
        var rootBoneTransformNoScale = Matrix4x4.TRS(rootBone.position, rootBone.rotation, Vector3.one);

        for (var i = 0; i < m_dest.coefficients.Length; ++i)
        {
            //Find nearest vert in source mesh
            var closestVertDistance = float.MaxValue;
            var closestVertColour   = Color.black;

            var clothVertWorldSpace = rootBoneTransformNoScale.MultiplyPoint3x4(m_dest.vertices[i]);

            //Check the cube of 27 cells around me
            var hashList = new Vertex3Hashed(clothVertWorldSpace, Color.black, m_cellSize).GetSurroundingHashCodes(1);

            foreach (var hash in hashList)
            {
                List <Vertex3Hashed> cellList;
                if (vertexLookup.TryGetValue(hash, out cellList))
                {
                    foreach (var vertex3Hashed in cellList)
                    {
                        var dist = Vector3.Distance(clothVertWorldSpace, vertex3Hashed.Vector);

                        if (dist < closestVertDistance)
                        {
                            closestVertColour   = vertex3Hashed.Color;
                            closestVertDistance = dist;
                        }
                    }
                }
            }

            float maxDistance;
            switch (m_distanceChannel)
            {
            default:
            case Channel.Red:
                maxDistance = closestVertColour.r;
                break;

            case Channel.Green:
                maxDistance = closestVertColour.g;
                break;

            case Channel.Blue:
                maxDistance = closestVertColour.b;
                break;

            case Channel.Alpha:
                maxDistance = closestVertColour.a;
                break;
            }

            if (m_distanceChannelInvert)
            {
                maxDistance = 1f - maxDistance;
            }

            maxDistance = m_distanceMultiplier * maxDistance;

            coefficients[i].maxDistance             = maxDistance;
            coefficients[i].collisionSphereDistance = float.MaxValue;
        }

        m_dest.coefficients = coefficients;
    }
Пример #9
0
 public static void ConvertData(ref ClothSkinningCoefficient source, ref Vector2 dest)
 {
     dest.x = source.collisionSphereDistance;
     dest.y = source.maxDistance;
 }
Пример #10
0
 public static void ConvertData(ref Vector2 source, ref ClothSkinningCoefficient dest)
 {
     dest.collisionSphereDistance = source.x;
     dest.maxDistance             = source.y;
 }
Пример #11
0
        private void SetClothWeightMap()
        {
            if (m_ClothHelper.clothWeightMap == null)
            {
                Debug.Log("No clothWeightMap set!");
                return;
            }

            SkinnedMeshRenderer renderer = m_ClothHelper.gameObject.GetComponent <SkinnedMeshRenderer> ();
            Cloth cloth = m_ClothHelper.gameObject.GetComponent <Cloth> ();

            if (renderer == null)
            {
                Debug.Log("No skinnedMeshRenderer found!");
                return;
            }

            if (cloth == null)
            {
                Debug.LogError("No Cloth component found!");
                return;
            }

            //m_ClothHelper.SetAllClothContraints ();
            Debug.Log("Number of Contraints: " + cloth.coefficients.Length.ToString());


            m_ClothHelper.clothVerts.Clear();
            for (int i = 0; i < cloth.vertices.Length; i++)
            {
                Vector3 key = cloth.vertices [i];
                if (renderer.rootBone != null)
                {
                    //
                    //key = key + renderer.rootBone.localPosition;
                    //key = RotatePointAroundPivot (key, new Vector3 (), renderer.rootBone.eulerAngles);
                    key = renderer.rootBone.InverseTransformPoint(key);

                    //
                }

                if (!m_ClothHelper.clothVerts.ContainsKey(key))
                {
                    m_ClothHelper.clothVerts.Add(key, i);
                }
            }

            int matchCount = 0;

            ClothSkinningCoefficient[] newConstraints = new ClothSkinningCoefficient[cloth.coefficients.Length];

            for (int i = 0; i < renderer.sharedMesh.vertexCount; i++)
            {
                EditorUtility.DisplayProgressBar("Painting Cloth Weights", "", (float)(i / renderer.sharedMesh.vertexCount));

                Vector3 rVertex = renderer.sharedMesh.vertices [i];

                if (m_ClothHelper.clothVerts.ContainsKey(rVertex))
                {
                    matchCount++;
                    int clothIndex = m_ClothHelper.clothVerts[rVertex];

                    int   x = (int)(renderer.sharedMesh.uv [i].x * m_ClothHelper.clothWeightMap.width);
                    int   y = (int)(renderer.sharedMesh.uv [i].y * m_ClothHelper.clothWeightMap.height);
                    Color c = m_ClothHelper.clothWeightMap.GetPixel(x, y);

                    float distWeight        = c.r * m_ClothHelper.distanceMax;
                    float penetrationWeight = c.g * m_ClothHelper.penetrationMax;

                    newConstraints [clothIndex].maxDistance             = distWeight;
                    newConstraints [clothIndex].collisionSphereDistance = penetrationWeight;
                }
            }
            Debug.Log(matchCount + " Vertices matched!");
            cloth.coefficients = newConstraints;

            EditorUtility.ClearProgressBar();
        }