private ShapeKey KeyGUI (ShapeKey shapeKey)
	{
		EditorGUILayout.LabelField ("Shape key " + shapeKey.label, EditorStyles.boldLabel);
		
		shapeKey.label = EditorGUILayout.TextField ("Key label:", shapeKey.label);
		shapeKey.index = EditorGUILayout.IntField ("BlendShape index:", shapeKey.index);
		return shapeKey;
	}
Exemple #2
0
    private List <ShapeKey> AllKeysGUI(List <ShapeKey> shapeKeys)
    {
        EditorGUILayout.LabelField("Shape keys", EditorStyles.boldLabel);

        foreach (ShapeKey shapeKey in shapeKeys)
        {
            EditorGUILayout.BeginHorizontal();

            string buttonLabel = shapeKey.ID + ": ";
            if (shapeKey.label == "")
            {
                buttonLabel += "(Untitled)";
            }
            else
            {
                buttonLabel += shapeKey.label;
            }

            bool buttonOn = false;
            if (selectedKey == shapeKey)
            {
                buttonOn = true;
            }

            if (GUILayout.Toggle(buttonOn, buttonLabel, "Button"))
            {
                if (selectedKey != shapeKey)
                {
                    selectedKey = shapeKey;
                }
            }

            if (GUILayout.Button("-", GUILayout.Width(20f), GUILayout.Height(15f)))
            {
                shapeKeys.Remove(shapeKey);
                selectedKey = null;
                break;
            }

            EditorGUILayout.EndHorizontal();
        }

        if (GUILayout.Button("Create new shape key"))
        {
            ShapeKey newShapeKey = new ShapeKey(GetIDArray(shapeKeys));
            shapeKeys.Add(newShapeKey);
            selectedKey = newShapeKey;
        }

        return(shapeKeys);
    }
Exemple #3
0
        void BuildShapeKeys(Transform skTrans, ConfigNode keyNode)
        {
            int keyCount = 0;

            if (keyNode != null)
            {
                keyCount = keyNode.values.Count;
            }
            var keys = new ShapeKey[keyCount];

            for (int i = 0; i < keys.Length; i++)
            {
                var value = keyNode.values[i];
                keys[i].name = value.name;
                float.TryParse(value.value, out keys[i].weight);
            }
            GameObject go  = skTrans.gameObject;
            var        smr = go.GetComponent <SkinnedMeshRenderer> ();

            if (!smr)
            {
                var mf = go.GetComponent <MeshFilter> ();
                var r  = go.GetComponent <MeshRenderer> ();
                if (!mf || !r)
                {
                    Debug.Log("[DeltaMu] missing shared mesh or renderer");
                    Debug.Log($"[DeltaMu] '{mf}' '{r}'");
                    return;
                }
                smr                = go.AddComponent <SkinnedMeshRenderer> ();
                smr.sharedMesh     = BuildShapeKeys(mf.sharedMesh, keys);
                smr.sharedMaterial = r.sharedMaterial;
                r.sharedMaterial   = null;
                Destroy(mf);
                Destroy(r);
            }
            else
            {
                smr.sharedMesh = BuildShapeKeys(smr.sharedMesh, keys);
            }
            Debug.Log($"[DeltaMu] {smr.sharedMesh.blendShapeCount}");
        }
	private ShapeGroup GroupGUI (ShapeGroup shapeGroup)
	{
		EditorGUILayout.Space ();
		
		EditorGUILayout.BeginVertical ("Button");
		EditorGUILayout.LabelField ("Shape group " + shapeGroup.label, EditorStyles.boldLabel);
		
		shapeGroup.label = EditorGUILayout.TextField ("Group label:", shapeGroup.label);
		
		shapeGroup.shapeKeys = AllKeysGUI (shapeGroup.shapeKeys);
		
		if (selectedKey != null && shapeGroup.shapeKeys.Contains (selectedKey))
		{
			selectedKey = KeyGUI (selectedKey);
		}
		
		EditorGUILayout.EndVertical ();
		
		return shapeGroup;
	}
Exemple #5
0
    private ShapeGroup GroupGUI(ShapeGroup shapeGroup)
    {
        EditorGUILayout.Space();

        EditorGUILayout.BeginVertical("Button");
        EditorGUILayout.LabelField("Shape group " + shapeGroup.label, EditorStyles.boldLabel);

        shapeGroup.label = EditorGUILayout.TextField("Group label:", shapeGroup.label);

        shapeGroup.shapeKeys = AllKeysGUI(shapeGroup.shapeKeys);

        if (selectedKey != null && shapeGroup.shapeKeys.Contains(selectedKey))
        {
            selectedKey = KeyGUI(selectedKey);
        }

        EditorGUILayout.EndVertical();

        return(shapeGroup);
    }
Exemple #6
0
        public NearestPointResolver(Animator avatarRoot, ShapeKey key, bool isGenericMode)
        {
            this.avatarRoot = avatarRoot;
            this.key        = key;
            groups          = new BoneGroup[key.bodyLines.Count];
            for (var i = 0; i < key.bodyLines.Count; i++)
            {
                var b = key.bodyLines[i];
                switch (b.bones.Count)
                {
                case 2:
                {
                    var b0 = b.GetBoneTransform(0, avatarRoot, isGenericMode).position;
                    var b1 = b.GetBoneTransform(1, avatarRoot, isGenericMode).position;
                    groups[i] = new BoneGroup2(
                        Vector3.Lerp(b0, b1, key.startMargin),
                        Vector3.Lerp(b0, b1, 1f - key.endMargin),
                        key.isLeaf
                        );
                    break;
                }

                case 3:
                {
                    var b0 = b.GetBoneTransform(0, avatarRoot, isGenericMode).position;
                    var b1 = b.GetBoneTransform(1, avatarRoot, isGenericMode).position;
                    var b2 = b.GetBoneTransform(2, avatarRoot, isGenericMode).position;
                    groups[i] = new BoneGroup3(
                        Vector3.Lerp(b0, b1, key.startMargin * 2f),
                        b1,
                        Vector3.Lerp(b1, b2, 1f - key.endMargin * 2f)
                        );
                    break;
                }

                default:
                    Debug.Log("Body line must have 2 or 3 bones");
                    break;
                }
            }
        }
Exemple #7
0
    //called everytime when model or submodel will change (visual will somehow changes)
    public void OnVisualChanged()
    {
        shapeKeyList.Clear();

        SkinnedMeshRenderer[] sMeshArray = baseEntity.GetComponentsInChildren <SkinnedMeshRenderer>();
        for (int i = 0; i < sMeshArray.Length; i++)
        {
            for (int b = 0; b < sMeshArray[i].sharedMesh.blendShapeCount; b++)
            {
                string   keyName  = sMeshArray[i].sharedMesh.GetBlendShapeName(b);
                ShapeKey foundKey = shapeKeyList.Find(x => x.GetKeyName() == keyName);

                Debug.Assert(foundKey == null, "There is already shapeKey with name '" + keyName + "' (Collision between '" +
                             (foundKey == null ? "invalid!" : foundKey.Identify()) + "' and '" + sMeshArray[i].transform.name + "')!");

                ShapeKey tempKey = new ShapeKey(keyName, b, sMeshArray[i]);
                shapeKeyList.Add(tempKey);
            }
        }
        animator = baseEntity.GetComponentInChildren <Animator>();
    }
        public static void CopyProperties(SerializedProperty property, ShapeKey value)
        {
            property.FindPropertyRelative("enable").boolValue           = value.enable;
            property.FindPropertyRelative("name").stringValue           = value.name;
            property.FindPropertyRelative("radius").floatValue          = value.radius;
            property.FindPropertyRelative("startMargin").floatValue     = value.startMargin;
            property.FindPropertyRelative("endMargin").floatValue       = value.endMargin;
            property.FindPropertyRelative("isLeaf").boolValue           = value.isLeaf;
            property.FindPropertyRelative("shape").animationCurveValue  = value.shape;
            property.FindPropertyRelative("addNormal").floatValue       = value.addNormal;
            property.FindPropertyRelative("removeThreshold").floatValue = value.removeThreshold;
            property.FindPropertyRelative("gizmoColor").colorValue      = value.gizmoColor;
            var bodyLines = property.FindPropertyRelative("bodyLines");

            bodyLines.arraySize = value.bodyLines.Count;
            for (var i = 0; i < bodyLines.arraySize; i++)
            {
                var bodyLine = bodyLines.GetArrayElementAtIndex(i);
                bodyLine.FindPropertyRelative("_index").intValue          = i;
                bodyLine.FindPropertyRelative("_isGenericMode").boolValue = value._isGenericMode;
                BodyLineDrawer.CopyProperties(bodyLine, value.bodyLines[i]);
            }
        }
Exemple #9
0
    private float shapeKeyStrengthAtTime(ShapeKey shapeKey, float time)
    {
        int frameForTime = ((int)Mathf.Floor((time / duration) * (float)(numberOfFrames - 1)));

        ShapeKeyFrameSequence f = (ShapeKeyFrameSequence)frames[shapeKey];

        if (f == null)
        {
            return(0);
        }

        if (frameForTime >= numberOfFrames - 1)
        {
            return(f.strength[numberOfFrames - 1]);
        }

        // Get the frame
        float l = f.strength[frameForTime];
        float h = f.strength[frameForTime + 1];
        float d = l - h;

        float lt = (float)frameForTime * frameDelay;
        float ht = ((float)frameForTime + 1.0f) * frameDelay;

        float dt = lt - ht;
        float s  = (time - lt) / dt;

        float r = l + (d * s);

        /*if (r < 0)
         *      r = 0;
         * if (r > 1)
         *      r = 1;
         */
        return(r);
    }
Exemple #10
0
        static void AddBoneKey(DietShaper p, ShapeKey key, ShapeKey exceptKey, Vector3[] posed, Vector3[] normals, Mesh result, List <Vector3> debugPoints)
        {
            if (!key.enable)
            {
                return;
            }
            key.shape.preWrapMode  = WrapMode.ClampForever;
            key.shape.postWrapMode = WrapMode.ClampForever;
            var vertices = new Vector3[p.sourceMesh.vertexCount];
            var w2l      = p.targetRenderer.worldToLocalMatrix;
            var avs      = p.avatarRoot.transform.lossyScale;
            var l2ws     = p.targetRenderer.localToWorldMatrix.lossyScale;
            var rescale  = new Vector3(l2ws.x / avs.x, l2ws.y / avs.y, l2ws.z / avs.z);
            // FIXME: SkinnedMeshRenderer の元々の(FBX内での)Scale を知りたい
            var rts         = p.targetRenderer.rootBone.lossyScale;
            var rtsInv      = new Vector3(1f / rts.x, 1f / rts.y, 1f / rts.z);
            var resolver    = new NearestPointResolver(p.avatarRoot, key, p.isGenericMode);
            var rNormal     = key.addNormal / avs.magnitude;
            var toBeRemoved = new bool[p.sourceMesh.vertexCount];

            for (var j = 0; j < p.sourceMesh.vertexCount; j++)
            {
                var v = posed[j];
                var(nearest, time, distance) = resolver.Resolve(v);
                var radius = key.radius; // Mathf.Lerp(key.startRadius, key.endRadius, time);
                if (radius < distance)
                {
                    continue;
                }
                debugPoints.Add(nearest);
                var w = w2l.MultiplyVector(nearest - v); // 結果の座標差分
                if (p.adjustScale)
                {
                    w.Scale(rescale);
                }
                var r = key.shape.Evaluate(time);

                // 法線をミックス
                if (0f < rNormal && 0f <= time && time <= 1f)
                {
                    var n  = -normals[j].normalized;               // 法線の逆方向
                    var nt = n - Vector3.Project(n, w.normalized); // nのwに対して垂直な成分
                    w += Vector3.Scale(nt * rNormal * (1f - r), rtsInv);
                }

                vertices[j]    = w;
                toBeRemoved[j] = r < key.removeThreshold;
            }
            if (key.removeThreshold < 1.0f)
            {
                result.AddBlendShapeFrame(key.name, 100f, vertices, null, null);
            }

            if (0.0f < key.removeThreshold)
            {
                // ポリゴン削除
                var srcTriangles = result.triangles;
                var k            = 0;
                var n            = srcTriangles.Length;
                var triangles    = new int[n];
                for (var i = 0; i < n; i += 3)
                {
                    var v0 = srcTriangles[i];
                    var v1 = srcTriangles[i + 1];
                    var v2 = srcTriangles[i + 2];
                    if (toBeRemoved[v0] && toBeRemoved[v1] && toBeRemoved[v2])
                    {
                        continue;
                    }
                    triangles[k++] = v0;
                    triangles[k++] = v1;
                    triangles[k++] = v2;
                }
                if (k < n)
                {
                    result.triangles = triangles.Take(k).ToArray();
                }
            }
        }
    private float shapeKeyStrengthAtTime(ShapeKey shapeKey, float time)
    {
        int frameForTime = ((int)Mathf.Floor((time / duration) * (float)(numberOfFrames - 1)));

        ShapeKeyFrameSequence f = (ShapeKeyFrameSequence)frames[shapeKey];

        if (f == null)
            return 0;

        if (frameForTime >= numberOfFrames - 1) {
            return f.strength[numberOfFrames - 1];
        }

        // Get the frame
        float l = f.strength[frameForTime];
        float h = f.strength[frameForTime + 1];
        float d = l - h;

        float lt = (float)frameForTime * frameDelay;
        float ht = ((float)frameForTime + 1.0f) * frameDelay;

        float dt = lt - ht;
        float s = (time - lt) / dt;

        float r = l + (d * s);
        /*if (r < 0)
            r = 0;
        if (r > 1)
            r = 1;
        */
        return r;
    }
	private List<ShapeGroup> AllGroupsGUI (List<ShapeGroup> shapeGroups)
	{
		EditorGUILayout.LabelField ("Shape groups", EditorStyles.boldLabel);
		
		foreach (ShapeGroup shapeGroup in shapeGroups)
		{
			EditorGUILayout.BeginHorizontal ();
			
			string buttonLabel = shapeGroup.ID + ": ";
			if (shapeGroup.label == "")
			{
				buttonLabel += "(Untitled)";	
			}
			else
			{
				buttonLabel += shapeGroup.label;
			}
			
			bool buttonOn = false;
			if (selectedGroup == shapeGroup)
			{
				buttonOn = true;
			}
			
			if (GUILayout.Toggle (buttonOn, buttonLabel, "Button"))
			{
				if (selectedGroup != shapeGroup)
				{
					selectedGroup = shapeGroup;
					selectedKey = null;
				}
			}
			
			if (GUILayout.Button ("-", GUILayout.Width (20f), GUILayout.Height (15f)))
			{
				shapeGroups.Remove (shapeGroup);
				selectedGroup = null;
				selectedKey = null;
				break;
			}
			
			EditorGUILayout.EndHorizontal ();
		}
		
		if (GUILayout.Button ("Create new shape group"))
		{
			ShapeGroup newShapeGroup = new ShapeGroup (GetIDArray (shapeGroups));
			shapeGroups.Add (newShapeGroup);
			selectedGroup = newShapeGroup;
			selectedKey = null;
		}
		
		return shapeGroups;
	}
    public void Awake()
    {
        textureLookup = new Hashtable ();
        for (int i = 0; i < textureReferences.Length; i++) {
            string name = textureReferences[i].name;
            if (name == "") {
                name = textureReferences[i].texture.name;
                textureReferences[i].name = name;
            }
            textureLookup.Add (textureReferences[i].name, textureReferences[i].texture);
        }
        gameObjectLookup = new Hashtable ();
        for (int i = 0; i < gameObjectReferences.Length; i++) {
            string name = gameObjectReferences[i].name;
            if (name == "") {
                name = gameObjectReferences[i].gameObject.name;
                gameObjectReferences[i].name = name;
            }
            gameObjectLookup.Add (gameObjectReferences[i].name, gameObjectReferences[i].gameObject);
        }
        string encodedString = dataFile.text;
        data = new JSONObject(encodedString);

        if (data.type == JSONObject.Type.OBJECT) {
            JSONObject shapeKeys = data["ShapeKeys"];
            if (shapeKeys.type == JSONObject.Type.OBJECT) {
                if (shapeKeys.list.Count > 0) {
                    ShapeKeys sk = gameObject.AddComponent<ShapeKeys>() as ShapeKeys;
                    ShapeKey[] shapekeyarray = new ShapeKey[shapeKeys.list.Count];
                    for (int i = 0; i < shapeKeys.list.Count; i++) {
                        JSONObject thisShapeKey = shapeKeys.list[i];
                        JSONObject scaleVector = thisShapeKey["Scale"];

                        ShapeKey s = new ShapeKey();
                        s.name = shapeKeys.keys[i];
                        s.meshObject = lookupGameObject(thisShapeKey["Object"].str);
                        s.image = lookupTexture(thisShapeKey["ImageFile"].str);
                        s.scale = new Vector3(scaleVector["x"].n, scaleVector["y"].n, scaleVector["z"].n);
                        shapekeyarray[i] = s;
                    }
                    sk.shapeKeys = shapekeyarray;
                    sk.InitialiseShapeKeys();
                }
            } else {
                Debug.Log ("shapeKeys: Should be an object!");
            }

            JSONObject animations = data["ShapeKeyAnimations"];
            if (animations.type == JSONObject.Type.OBJECT) {
                if (animations.list.Count > 0) {
                    ShapeKeyAnimations ska = gameObject.AddComponent<ShapeKeyAnimations>() as ShapeKeyAnimations;
                    ShapeKeyAnimation[] shapekeyanimarray = new ShapeKeyAnimation[animations.list.Count];
                    for (int i = 0; i < animations.list.Count; i++) {
                        JSONObject thisAnimation = animations.list[i];
                        JSONObject frames = thisAnimation["Frames"];
                        JSONObject keys = thisAnimation["ShapeKeys"];
                        //JSONObject style = thisShapeKey["Style"];
                        JSONObject startShape = thisAnimation["StartShape"];

                        ShapeKeyAnimation anim = new ShapeKeyAnimation();
                        anim.name = animations.keys[i];
                        anim.numberOfFrames = frames.list.Count;
                        anim.frameRate = thisAnimation["Framerate"].n;

                        Hashtable shapeKeyFrames = new Hashtable();
                        Hashtable startShapes = new Hashtable();

                        ShapeKey[] sks = new ShapeKey[keys.list.Count];
                        for (int j = 0; j < keys.list.Count; j++) {
                            ShapeKeys sk = gameObject.GetComponent<ShapeKeys>() as ShapeKeys;
                            ShapeKey shapekey = sk.findShapeKeyNamed(keys[j].str);
                            ShapeKeyFrameSequence sequence = new ShapeKeyFrameSequence();
                            sequence.strength = new float[frames.list.Count];
                            shapeKeyFrames.Add (shapekey, sequence);
                            sks[j] = shapekey;

                            startShapes.Add (shapekey, startShape[j].n);
                        }

                        for (int j = 0; j < frames.list.Count; j++) {
                            JSONObject frameData = frames[j];
                            for (int k = 0; k < frameData.list.Count;k++) {
                                JSONObject value = frameData[k];
                                ((ShapeKeyFrameSequence)shapeKeyFrames[sks[k]]).strength[j] = value.n;
                            }
                        }

                        anim.frames = shapeKeyFrames;
                        anim.startShapes = startShapes;
                        shapekeyanimarray[i] = anim;
                    }
                    ska.shapeKeyAnimations = shapekeyanimarray;
                    ska.InitialiseAnimations();
                }
            } else {
                Debug.Log ("animations: Should be an object!");
            }
        } else {
            Debug.Log ("data: Should be an object!");
        }
    }
    public void Awake()
    {
        textureLookup = new Hashtable();
        for (int i = 0; i < textureReferences.Length; i++)
        {
            string name = textureReferences[i].name;
            if (name == "")
            {
                name = textureReferences[i].texture.name;
                textureReferences[i].name = name;
            }
            textureLookup.Add(textureReferences[i].name, textureReferences[i].texture);
        }
        gameObjectLookup = new Hashtable();
        for (int i = 0; i < gameObjectReferences.Length; i++)
        {
            string name = gameObjectReferences[i].name;
            if (name == "")
            {
                name = gameObjectReferences[i].gameObject.name;
                gameObjectReferences[i].name = name;
            }
            gameObjectLookup.Add(gameObjectReferences[i].name, gameObjectReferences[i].gameObject);
        }
        string encodedString = dataFile.text;

        data = new JSONObject(encodedString);

        if (data.type == JSONObject.Type.OBJECT)
        {
            JSONObject shapeKeys = data["ShapeKeys"];
            if (shapeKeys.type == JSONObject.Type.OBJECT)
            {
                if (shapeKeys.list.Count > 0)
                {
                    ShapeKeys  sk            = gameObject.AddComponent <ShapeKeys>() as ShapeKeys;
                    ShapeKey[] shapekeyarray = new ShapeKey[shapeKeys.list.Count];
                    for (int i = 0; i < shapeKeys.list.Count; i++)
                    {
                        JSONObject thisShapeKey = shapeKeys.list[i];
                        JSONObject scaleVector  = thisShapeKey["Scale"];

                        ShapeKey s = new ShapeKey();
                        s.name           = shapeKeys.keys[i];
                        s.meshObject     = lookupGameObject(thisShapeKey["Object"].str);
                        s.image          = lookupTexture(thisShapeKey["ImageFile"].str);
                        s.scale          = new Vector3(scaleVector["x"].n, scaleVector["y"].n, scaleVector["z"].n);
                        shapekeyarray[i] = s;
                    }
                    sk.shapeKeys = shapekeyarray;
                    sk.InitialiseShapeKeys();
                }
            }
            else
            {
                Debug.Log("shapeKeys: Should be an object!");
            }

            JSONObject animations = data["ShapeKeyAnimations"];
            if (animations.type == JSONObject.Type.OBJECT)
            {
                if (animations.list.Count > 0)
                {
                    ShapeKeyAnimations  ska = gameObject.AddComponent <ShapeKeyAnimations>() as ShapeKeyAnimations;
                    ShapeKeyAnimation[] shapekeyanimarray = new ShapeKeyAnimation[animations.list.Count];
                    for (int i = 0; i < animations.list.Count; i++)
                    {
                        JSONObject thisAnimation = animations.list[i];
                        JSONObject frames        = thisAnimation["Frames"];
                        JSONObject keys          = thisAnimation["ShapeKeys"];
                        //JSONObject style = thisShapeKey["Style"];
                        JSONObject startShape = thisAnimation["StartShape"];

                        ShapeKeyAnimation anim = new ShapeKeyAnimation();
                        anim.name           = animations.keys[i];
                        anim.numberOfFrames = frames.list.Count;
                        anim.frameRate      = thisAnimation["Framerate"].n;

                        Hashtable shapeKeyFrames = new Hashtable();
                        Hashtable startShapes    = new Hashtable();

                        ShapeKey[] sks = new ShapeKey[keys.list.Count];
                        for (int j = 0; j < keys.list.Count; j++)
                        {
                            ShapeKeys             sk       = gameObject.GetComponent <ShapeKeys>() as ShapeKeys;
                            ShapeKey              shapekey = sk.findShapeKeyNamed(keys[j].str);
                            ShapeKeyFrameSequence sequence = new ShapeKeyFrameSequence();
                            sequence.strength = new float[frames.list.Count];
                            shapeKeyFrames.Add(shapekey, sequence);
                            sks[j] = shapekey;

                            startShapes.Add(shapekey, startShape[j].n);
                        }

                        for (int j = 0; j < frames.list.Count; j++)
                        {
                            JSONObject frameData = frames[j];
                            for (int k = 0; k < frameData.list.Count; k++)
                            {
                                JSONObject value = frameData[k];
                                ((ShapeKeyFrameSequence)shapeKeyFrames[sks[k]]).strength[j] = value.n;
                            }
                        }

                        anim.frames          = shapeKeyFrames;
                        anim.startShapes     = startShapes;
                        shapekeyanimarray[i] = anim;
                    }
                    ska.shapeKeyAnimations = shapekeyanimarray;
                    ska.InitialiseAnimations();
                }
            }
            else
            {
                Debug.Log("animations: Should be an object!");
            }
        }
        else
        {
            Debug.Log("data: Should be an object!");
        }
    }