Esempio n. 1
0
        public override void Initialize(ActorBaseComponent actorBaseComponent)
        {
            base.Initialize(actorBaseComponent);

            ActorComponent actorComponent = actorBaseComponent as ActorComponent;

            m_SortingOrderOffset = actorComponent.SortingOrder;

            ActorImage imageNode = m_ActorNode as ActorImage;

            // Attach the skinned bone nodes to the bones of the skinned renderer.
            if (imageNode.IsSkinned)
            {
                SkinnedMeshRenderer skinnedRenderer = gameObject.GetComponent <SkinnedMeshRenderer>();

                Transform[] transforms = new Transform[imageNode.ConnectedBoneCount + 1];
                transforms[0] = actorComponent.DefaultBone.transform;
                int idx = 1;
                foreach (ActorImage.BoneConnection bc in imageNode.BoneConnections)
                {
                    ActorNodeComponent boneComponent = actorComponent.Nodes[bc.m_BoneIdx];
                    transforms[idx] = boneComponent.gameObject.transform;
                    idx++;
                }
                skinnedRenderer.bones = transforms;
            }
        }
Esempio n. 2
0
        public virtual void Initialize(ActorBaseComponent actorComponent)
        {
            m_ActorComponent = actorComponent;

            ActorNodeComponent parentComponent = actorComponent.Nodes[m_ActorNode.ParentIdx];

            if (parentComponent == this)
            {
                // This is the root.
                // If the parent is self, we've reached root.
                gameObject.transform.parent = actorComponent.gameObject.transform;
            }
            else
            {
                gameObject.transform.parent = parentComponent.gameObject.transform;
            }
            UpdateTransform();
        }
Esempio n. 3
0
        public void InitializeFromAsset(ActorAsset actorAsset)
        {
            HideFlags hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild | HideFlags.DontUnloadUnusedAsset | HideFlags.HideInHierarchy | HideFlags.HideInInspector;

            m_ActorAsset = actorAsset;

            if (!actorAsset.Loaded && !actorAsset.Load())
            {
#if UNITY_EDITOR
                Debug.Log("Bad actorAsset referenced by Actor2D.");
#endif
                return;
            }

            RemoveNodes();

            m_ActorInstance = new Actor();
            m_ActorInstance.Copy(m_ActorAsset.Actor);
            OnActorInstanced();

            // Instance actor bones first as our image nodes need to know about them if they are skinned.
            {
                //ActorNode[] allNodes = m_ActorInstance.AllNodes;
                IList <Nima.ActorComponent> actorComponents = m_ActorInstance.Components;
                m_Nodes = new ActorNodeComponent[actorComponents.Count];

                int imgNodeIdx = 0;
                for (int i = 0; i < actorComponents.Count; i++)
                {
                    Nima.ActorComponent ac = actorComponents[i];
                    ActorNode           an = ac as ActorNode;
                    if (an == null)
                    {
                        continue;
                    }
                    GameObject go = null;
                    ActorImage ai = an as ActorImage;
                    if (ai != null)
                    {
                        ActorNodeComponent nodeComponent = MakeImageNodeComponent(imgNodeIdx, ai);
                        nodeComponent.Node = ai;
                        go         = nodeComponent.gameObject;
                        m_Nodes[i] = nodeComponent;
                        imgNodeIdx++;
                    }
                    else
                    {
                        ActorCollider actorCollider = an as ActorCollider;
                        if (actorCollider != null && InstanceColliders)
                        {
                            go = new GameObject(an.Name, typeof(ActorColliderComponent));
                            ActorColliderComponent colliderComponent = go.GetComponent <ActorColliderComponent>();
                            colliderComponent.Node = actorCollider;
                            m_Nodes[i]             = colliderComponent;
                        }
                        // else
                        // {
                        //  go = new GameObject(an.Name, typeof(ActorNodeComponent));

                        //  ActorNodeComponent nodeComponent = go.GetComponent<ActorNodeComponent>();
                        //  nodeComponent.Node = an;
                        //  m_Nodes[i] = nodeComponent;
                        // }
                    }

                    if (go != null)
                    {
                        go.hideFlags = hideFlags;
                    }
                }
                // After they are all created, initialize them.
                for (int i = 0; i < m_Nodes.Length; i++)
                {
                    ActorNodeComponent nodeComponent = m_Nodes[i];
                    if (nodeComponent != null)
                    {
                        nodeComponent.Initialize(this);
                    }
                }
            }

            OnActorInitialized();

#if UNITY_EDITOR
            LateUpdate();
            UpdateEditorBounds();
#endif
        }
Esempio n. 4
0
        protected override ActorNodeComponent MakeImageNodeComponent(int imageNodeIndex, ActorImage actorImage)
        {
            if (actorImage.VertexCount == 0)
            {
                GameObject         go            = new GameObject(actorImage.Name, typeof(ActorNodeComponent));
                ActorNodeComponent nodeComponent = go.GetComponent <ActorNodeComponent>();
                return(nodeComponent);
            }
            else
            {
                Mesh mesh     = m_ActorAsset.GetMesh(imageNodeIndex);
                bool hasBones = actorImage.ConnectedBoneCount > 0;

                GameObject go = hasBones ?
                                new GameObject(actorImage.Name, typeof(SkinnedMeshRenderer), typeof(ActorImageComponent)) :
                                new GameObject(actorImage.Name, typeof(MeshFilter), typeof(MeshRenderer), typeof(ActorImageComponent));


                ActorImageComponent actorImageComponent = go.GetComponent <ActorImageComponent>();
                // Clone the vertex array alway right now so we can update opacity
                // In future we could check if this node animates opacity as we did for vertex deform
                //if(actorImage.DoesAnimationVertexDeform)
                {
                    // Clone the vertex array if we deform.
                    Mesh clonedMesh = new Mesh();
                    clonedMesh.vertices    = (Vector3[])mesh.vertices.Clone();
                    clonedMesh.uv          = mesh.uv;
                    clonedMesh.boneWeights = mesh.boneWeights;
                    clonedMesh.bindposes   = mesh.bindposes;
                    clonedMesh.triangles   = mesh.triangles;
                    clonedMesh.colors32    = (UnityEngine.Color32[])mesh.colors32.Clone();
                    clonedMesh.RecalculateNormals();
                    clonedMesh.MarkDynamic();
                    mesh = clonedMesh;
                }
                if (hasBones)
                {
                    Mesh skinnedMesh = new Mesh();
                    skinnedMesh.vertices    = mesh.vertices;
                    skinnedMesh.uv          = mesh.uv;
                    skinnedMesh.boneWeights = mesh.boneWeights;
                    skinnedMesh.triangles   = mesh.triangles;
                    skinnedMesh.colors32    = (UnityEngine.Color32[])mesh.colors32.Clone();
                    skinnedMesh.bindposes   = mesh.bindposes;

                    go.GetComponent <SkinnedMeshRenderer>().sharedMesh = skinnedMesh;
                }
                else
                {
                    MeshFilter meshFilter = go.GetComponent <MeshFilter>();
                    meshFilter.sharedMesh = mesh;
                }

                Renderer renderer = go.GetComponent <Renderer>();

                Material material = m_ActorAsset.GetMaterial(actorImage.TextureIndex);
                renderer.sortingLayerID = m_SortingLayerID;

                switch (actorImage.BlendMode)
                {
                case BlendModes.Screen:
                {
                    Material overrideMaterial = new Material(Shader.Find("Nima/Screen"));
                    overrideMaterial.mainTexture = material.mainTexture;
                    material = overrideMaterial;
                    break;
                }

                case BlendModes.Additive:
                {
                    Material overrideMaterial = new Material(Shader.Find("Nima/Additive"));
                    overrideMaterial.mainTexture = material.mainTexture;
                    material = overrideMaterial;
                    break;
                }

                case BlendModes.Multiply:
                {
                    Material overrideMaterial = new Material(Shader.Find("Nima/Multiply"));
                    overrideMaterial.mainTexture = material.mainTexture;
                    material = overrideMaterial;
                    break;
                }

                default:
                {
                    /*Material overrideMaterial = new Material(Shader.Find("Nima/Normal"));
                     * overrideMaterial.mainTexture = material.mainTexture;
                     * material = overrideMaterial;*/
                    break;
                }
                }

                renderer.sharedMaterial = material;
                return(actorImageComponent);
            }
        }
        protected override ActorNodeComponent MakeImageNodeComponent(int imageNodeIndex, ActorImage actorImage)
        {
            GameObject         gameObject    = new GameObject(actorImage.Name, typeof(ActorNodeComponent));
            ActorNodeComponent nodeComponent = gameObject.GetComponent <ActorNodeComponent>();

            Mesh mesh     = m_ActorAsset.GetMesh(imageNodeIndex);
            bool hasBones = actorImage.ConnectedBoneCount > 0;

            GameObject go = new GameObject(actorImage.Name, typeof(RectTransform), typeof(CanvasRenderer), typeof(ActorCanvasImageComponent));

            ActorCanvasImageComponent actorImageComponent = go.GetComponent <ActorCanvasImageComponent>();

            actorImageComponent.Node = actorImage;
            go.transform.SetParent(m_DrawOrderRoot.transform, false);
            m_ImageComponents.Add(actorImageComponent);

            // Clone the vertex array alway right now so we can update opacity
            Mesh clonedMesh = new Mesh();

            clonedMesh.vertices  = (Vector3[])mesh.vertices.Clone();
            clonedMesh.uv        = mesh.uv;
            clonedMesh.triangles = mesh.triangles;
            clonedMesh.colors32  = (UnityEngine.Color32[])mesh.colors32.Clone();
            clonedMesh.RecalculateNormals();
            clonedMesh.MarkDynamic();
            mesh = clonedMesh;

            Material material = m_ActorAsset.GetMaterial(actorImage.TextureIndex);

            switch (actorImage.BlendMode)
            {
            case BlendModes.Screen:
            {
                Material overrideMaterial = new Material(Shader.Find("Nima/Screen"));
                overrideMaterial.mainTexture = material.mainTexture;
                material = overrideMaterial;
                break;
            }

            case BlendModes.Additive:
            {
                Material overrideMaterial = new Material(Shader.Find("Nima/Additive"));
                overrideMaterial.mainTexture = material.mainTexture;
                material = overrideMaterial;
                break;
            }

            case BlendModes.Multiply:
            {
                Material overrideMaterial = new Material(Shader.Find("Nima/Multiply"));
                overrideMaterial.mainTexture = material.mainTexture;
                material = overrideMaterial;
                break;
            }

            default:
            {
                /*	Material overrideMaterial = new Material(Shader.Find("Nima/Normal"));
                 *      //Material overrideMaterial = new Material(Shader.Find("Transparent/Diffuse"));
                 *      overrideMaterial.color = Color.white;
                 *      overrideMaterial.mainTexture = material.mainTexture;
                 *      material = overrideMaterial;*/
                break;
            }
            }

            actorImageComponent.m_Mesh     = mesh;
            actorImageComponent.m_Material = material;

            return(nodeComponent);
        }