Ejemplo n.º 1
0
        //private async void Awake()
        private void Start()
        {
            var md = new MotionDataInNative();

            md.ConvertFrom(MotionClipData);
            //	md.TransformKeyDataToWorld_();



            // メッシュの加工

            var newmesh = new Mesh();

            newmesh.vertices    = meshSetting.mesh.vertices;
            newmesh.boneWeights = meshSetting.mesh.boneWeights;
            newmesh.bindposes   = meshSetting.mesh.bindposes;
            newmesh.normals     = meshSetting.mesh.normals;
            newmesh.uv          = meshSetting.mesh.uv;
            newmesh.triangles   = meshSetting.mesh.triangles;
            meshSetting.mesh    = newmesh.ConvertForMotionable(useUvChannel: 1, MotionClipData);


            var bindposes = meshSetting.mesh.bindposes;

            meshSetting.material.SetMatrixArray("mts", bindposes);
            meshSetting.material.SetInt("boneLength", bindposes.Length);

            meshSetting.material.SetVectorArray("positions", new Vector4 [1023]);
            meshSetting.material.SetVectorArray("rotations", new Vector4 [1023]);



            // アーキタイプ

            var em = World.Active.GetOrCreateManager <EntityManager>();

            var streamArch = em.CreateArchetype(
                typeof(StreamInitialLabel),
                typeof(StreamKeyShiftData),
                typeof(StreamNearKeysCacheData),
                typeof(StreamInterpolatedData)
                );
            var boneArch = em.CreateArchetype(
                typeof(DrawBoneTargetLabel),
                typeof(BoneEntityLinkData),
                typeof(BoneIndexData),
                typeof(BonePostureData)
                );
            var motionArch = em.CreateArchetype(
                typeof(MotionInitializeData),
                typeof(MotionComponentData)
                );

            var rendererArch = em.CreateArchetype(
                typeof(MeshInstanceRenderer)
                );


            // レンダラ

            var rent = em.CreateEntity(rendererArch);

            em.SetSharedComponentData(rent, meshSetting);

            var drawOrderId     = 0;
            var boneSerialIndex = 0;


            //	Enumerable.Range(0,num).ForEach( x => create( Random.Range( 0, 15 ) ) );//.AddTo( gameObject );
            Observable.EveryGameObjectUpdate().Take(num).Subscribe(x => create(Random.Range(0, 15))).AddTo(gameObject);
            //	motionIndex.Subscribe( x => create(x) ).AddTo( gameObject );


            void create(int motionIndex)
            {
                var ma = md.CreateAccessor(motionIndex);



                // ボーン&ストリーム

                var    nowTime = Time.time;
                Entity boneTop;

                using (var boneEntities = new NativeArray <Entity>(ma.boneLength, Allocator.Temp))
                    using (var streamEntities = new NativeArray <Entity>(ma.boneLength * 2, Allocator.Temp))
                    {
                        em.CreateEntity(boneArch, boneEntities);
                        em.CreateEntity(streamArch, streamEntities);

                        boneTop = boneEntities[0];

                        Enumerable.Zip(boneEntities, streamEntities.Buffer(2), (bone, streams) => (bone, streams))
                        .ForEach((ents, ibone) =>
                        {
                            var parentIndex  = ma.GetParentBoneIndex(ibone);
                            var siblingIndex = ibone + 1;

                            em.SetComponentData(ents.bone,
                                                new BoneEntityLinkData
                            {
                                Self    = ents.bone,
                                Sibling = siblingIndex <ma.boneLength?boneEntities[siblingIndex] : Entity.Null,
                                                        Parent = parentIndex> -1 ? boneEntities[parentIndex] : Entity.Null,
                                positionEntity = ents.streams[0],
                                rotationEntity = ents.streams[1],
                                ParentIndex    = parentIndex,                                         //
                            }
                                                );
                            em.SetComponentData(ents.bone,
                                                new BoneIndexData
                            {
                                Index       = ibone,
                                ParentIndex = parentIndex,
                            }
                                                );
                            em.SetComponentData(ents.bone,
                                                new DrawBoneTargetLabel
                            {
                                drawingOrder = drawOrderId,
                                boneIndex    = ibone,
                            }
                                                );
                            //em.SetComponentData( ents.bone,
                            //	new BonePostureData
                            //	{
                            //		boneSerialIndex = boneSerialIndex++,//
                            //		boneIndex = ibone,
                            //		parentBoneIndex = parentIndex//
                            //	}
                            //);

                            //	em.AddComponentData( ents.streams[0], new StreamInitialData() );
                            //	if( ibone != 0 ) em.AddComponentData( ents.streams[0], new StreamInitialDataFor1pos() );

                            createStreamSectionEntity(KeyStreamSection.positions, 0.1f);
                            createStreamSectionEntity(KeyStreamSection.rotations, 0.1f);

                            void createStreamSectionEntity(KeyStreamSection streamSection, float timeScale)
                            {
                                //	em.AddComponentData( ents.streams[(int)streamSection], new StreamInitialData() );
                                em.SetComponentData(ents.streams[(int)streamSection],
                                                    new StreamKeyShiftData
                                {
                                    Self = ents.streams[(int)streamSection],
                                    Keys = ma.GetStreamSlice(ibone, streamSection).Stream
                                }
                                                    );
                                em.SetComponentData(ents.streams[(int)streamSection],
                                                    new StreamNearKeysCacheData
                                {
                                    TimeLength = ma.TimeLength,
                                    TimeScale  = 0.1f
                                }
                                                    );
                            }
                        });
                    }


                // モーション

                var motionEntity = em.CreateEntity(motionArch);

                //	em.AddComponent( motionEntity, typeof(MotionInitializeData) );

                em.AddComponentData <DrawTargetLabel>(motionEntity,
                                                      new DrawTargetLabel
                {
                    orderId = drawOrderId++
                }
                                                      );
                em.SetComponentData <MotionComponentData>(motionEntity,
                                                          new MotionComponentData
                {
                    MotionIndex  = motionIndex,
                    DataAccessor = ma,
                    Self         = motionEntity,
                    BoneTop      = boneTop
                }
                                                          );
                em.SetComponentData <MotionInitializeData>(motionEntity,
                                                           new MotionInitializeData
                {
                    Self        = motionEntity,
                    BoneArche   = boneArch,
                    StreamArche = streamArch
                }
                                                           );
            }

            this.motionData = md;

            //await this.OnDestroyAsObservable();
        }