Ejemplo n.º 1
0
        void Start()
        {
            this.instances = new GPUMesh[this.N];
            this.paths     = new Path[this.N];

            // Initialize GPU Instancer
            this.m = new MeshInstancer();
            this.m.Initialize(pathCount: 4);
            this.p = new PathArrayHelper(this.m);

            // Add all mesh types to GPU Instancer
            foreach (var a in this.asteroids)
            {
                this.m.AddGPUMeshType(a);
            }

            // Do stuff
            for (int i = 0; i < N; i++)
            {
                var mesh = asteroids[Random.Range(0, asteroids.Count)].MeshTypes[0];
                instances[i] = new GPUMesh(mesh, this.m);
                var size = Random.Range(1f, ScaleRange);
                instances[i].mesh.scale = Vector3.one * size;
                instances[i].SetRadius(4f * size); // set large enough radius so model doesnt get culled to early
                instances[i].Initialize();

                var path = GetNewPath();                 // create new path
                instances[i].mesh.SetPath(path, this.m); // assign path to instance
                paths[i] = path;

                instances[i].Update();
            }
        }
Ejemplo n.º 2
0
        public bool Update(List <Transform> points, PathArrayHelper p, MeshInstancer m, bool force_update = false)
        {
            if (points.Count != this._cached_path.Length)
            {
                throw new System.Exception("Error, please dont change the path length. Make a new path instead.");
            }

            bool dirty = this._cached_path.loop != this.loop || this._cached_path.PathCompletionTime != this.completion_time || this._cached_path.avg_path != this.do_avg ||
                         this._cached_path.AvgWeight != this.avg_weight || this._cached_path.smoothing != this.smoothing || this._cached_path.use_constants != this.use_constants || this._cached_path.yaw_only != this.yaw_only;

            this._cached_path.loop      = this.loop; this._cached_path.avg_path = this.do_avg; this._cached_path.smoothing = this.smoothing;
            this._cached_path.AvgWeight = this.avg_weight; this._cached_path.PathCompletionTime = this.completion_time; this._cached_path.yaw_only = this.yaw_only;
            this._cached_path.SetUseConstants(this.use_constants, m);

            if (dirty || force_update)
            {
                var start_index = p.StartIndexOfPath(this._cached_path); // get index of path in paths array
                for (int i = 0; i < points.Count; i++)                   // set the path points for instances to follow
                {
                    p.path[start_index + i] = points[i].position;
                }
                p.AutoCalcPathUpAndT(this._cached_path, this.path_up_rotation_modifier); // recalc up and T
            }

            return(dirty || force_update);
        }
Ejemplo n.º 3
0
        // Start is called before the first frame update
        void Start()
        {
            List <TextureUVAnimation> anims = new List <TextureUVAnimation>();

            anims.Add(MakeGPUTextureAnim(this.RunBack));
            anims.Add(MakeGPUTextureAnim(this.RunForward));
            anims.Add(MakeGPUTextureAnim(this.RunLeft));
            anims.Add(MakeGPUTextureAnim(this.RunRight));

            var tlib = new TextureAnimationLibrary(anims);                                                                         // make texture animation library object.. This will just combine all the above lists into a single buffer

            this.m = new MeshInstancer();                                                                                          // create & initialize mesh instancer
            this.m.Initialize(override_mesh: BaseMeshLibrary.CreatePlane2Sides(), override_material: this.material, pathCount: 4); // override default cube with a two-sided plane that uses uv cutout shader

            this.m.SetAllTextureAnimations(tlib);                                                                                  // set all texture animations on GPU

            // initialize paths that the instances will follow
            this.p = new PathArrayHelper(this.m);

            // Create instances
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    // specify instance data
                    var random_anim_id = anims[Random.Range(0, anims.Count)].animation_id;
                    InstanceData <InstanceProperties> dat = new InstanceData <InstanceProperties>(this.m.Default);
                    dat.position = new Vector3(i, 0, j);
                    dat.rotation = Quaternion.Euler(-90, 0, 0);
                    dat.props_IsTextureAnimation = true;
                    dat.props_animationID        = random_anim_id;
                    dat.props_AnimationSpeed     = Random.Range(0.5f, 2.0f);
                    dat.props_AnimationSeconds   = Random.Range(0, 1.0f);

                    // create & assign path
                    Path path = new Path(path_length: 4, m: this.m, use_constants: true);
                    this.p.InitializePath(ref path);
                    this.p.SetPathConstants(path, new Vector3(0, 0, Random.Range(-10.0f, 10.0f)), new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f)), dat.rotation, dat.position);
                    this.p.UpdatePath(ref path);
                    dat.SetPath(path, this.m);

                    // Initialize & send instance to gpu
                    this.m.Initialize(ref dat);
                    this.m.Append(ref dat);

                    // Make a unity gameobject follow the first instance.. Just for testing CPU instance transform approximation
                    if (i == 0 && j == 0)
                    {
                        this.follow_obj      = GameObject.CreatePrimitive(PrimitiveType.Cube);
                        this.follow_obj.name = "Calculated Object Transform";
                        this.follow_obj.transform.localScale = Vector3.one * 0.25f;
                        this.follow_obj.transform.position   = dat.position;
                        this.follow_obj.transform.rotation   = dat.rotation;
                        this.follow_instance = dat;
                        this.follow_path     = path;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        // Use this for initialization
        void Awake()
        {
            //initialize
            buffer  = new InstanceData[instanceCountsq * instanceCountsq];
            imesher = new MeshInstancer();
            imesher.Initialize(max_parent_depth: 2, pathCount: 4);

            this.p = new PathArrayHelper(this.imesher);

            // load all the textures
            var keys = Texture2MeshType.Textures2MeshType(this.Textures, imesher.Default.shared_mesh, imesher.Default.shared_material);

            // create all the mesh types that we can use
            foreach (var key in keys)
            {
                imesher.AddNewMeshType(key.mesh_key, key.material_key);
            }

            //make parent object
            parent          = new InstanceData(imesher.Default);
            parent.position = position;
            parent.scale    = scale;
            parent.rotation = Quaternion.Euler(rotation);
            buffer[0]       = parent;

            float v        = this.VelocityRange;
            var   rangemax = keys.Count;

            for (int i = 0; i < instanceCountsq; i++)
            {
                for (int j = 0; j < instanceCountsq; j++)
                {
                    if (i == 0 && j == 0)
                    {
                        continue;                                                                                                                    // skip parent
                    }
                    var tkey = Random.Range(1, rangemax);                                                                                            // get random texture
                    var newt = new InstanceData(imesher.GetMeshType(keys[tkey].mesh_key, keys[tkey].material_key));                                  // make instance data

                    newt.position      = new Vector3(i, 0, j);                                                                                       // assign position of instance
                    newt.parentID      = 0;                                                                                                          // parent to instance at index 0
                    newt.props_color32 = new Color32((byte)Random.Range(0, 256), (byte)Random.Range(0, 256), (byte)Random.Range(0, 256), (byte)255); // set random color

                    // create & assign path
                    Path path = new Path(path_length: 4, m: this.imesher, use_constants: true);
                    this.p.InitializePath(ref path);
                    this.p.SetPathConstants(path, new Vector3(Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f)), new Vector3(Random.Range(-v, v), Random.Range(-v, v), Random.Range(-v, v)), newt.rotation, newt.position);
                    this.p.UpdatePath(ref path);
                    newt.SetPath(path, this.imesher);

                    buffer[i * instanceCountsq + j] = newt; // assign in buffer
                }
            }

            imesher.InitializeSet(buffer);
            imesher.AppendMany(buffer);
            parent = buffer[0];
        }
Ejemplo n.º 5
0
        // Start is called before the first frame update
        void Start()
        {
            this.m = new MeshInstancer();
            this.m.Initialize(pathCount: Mathf.Max(path1.Count, Mathf.Max(path2.Count, path3.Count)));
            this.p = new PathArrayHelper(this.m);

            InitInstance(ref this.instance1, this.path1, ref this.path1_props, Color.red);
            InitInstance(ref this.instance2, this.path2, ref this.path2_props, Color.green);
            InitInstance(ref this.instance3, this.path3, ref this.path3_props, Color.blue);
        }
Ejemplo n.º 6
0
        void Start()
        {
            // Initialize character mesh list
            int hierarchy_depth, skeleton_bone_count;
            var controllers = GPUSkinnedMeshComponent.PrepareControllers(characters, out hierarchy_depth, out skeleton_bone_count);

            // Initialize GPU Instancer
            this.m = new MeshInstancer();
            this.m.Initialize(max_parent_depth: hierarchy_depth + 2, num_skeleton_bones: skeleton_bone_count, pathCount: 2);
            this.p = new PathArrayHelper(this.m);

            // Add all animations to GPU buffer
            this.m.SetAllAnimations(controllers);

            // Add all character mesh types to GPU Instancer
            foreach (var character in this.characters)
            {
                this.m.AddGPUSkinnedMeshType(character);
            }

            // Do stuff
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    var mesh = characters[Random.Range(0, characters.Count)];
                    var anim = mesh.anim.namedAnimations["walk"];
                    instances[i, j] = new SkinnedMesh(mesh, this.m);
                    instances[i, j].mesh.position = new Vector3(i, 0, j);
                    instances[i, j].SetRadius(1.75f); // set large enough radius so model doesnt get culled to early
                    instances[i, j].Initialize();

                    instances[i, j].SetAnimation(anim, speed: 1.4f, start_time: Random.Range(0.0f, 1.0f)); // set animation

                    var path = GetNewPath();                                                               // create new path
                    instances[i, j].mesh.SetPath(path, this.m);                                            // assign path to instance
                    paths[i, j] = path;

                    instances[i, j].UpdateAll();
                }
            }

            // For testing purposes.. do bone visualization of [0,0] model
            bones_unity = new GameObject[instances[0, 0].skeleton.data.Length];
            for (int i = 0; i < bones_unity.Length; i++)
            {
                var obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
                obj.name       = "Calculated Bone Transform " + i.ToString();
                bones_unity[i] = obj;
            }
        }
Ejemplo n.º 7
0
        // Start is called before the first frame update
        void Start()
        {
            model1.anim.Initialize();
            model2.anim.Initialize();

            int hierarchy_depth = System.Math.Max(model1.anim.BoneHierarchyDepth, model2.anim.BoneHierarchyDepth) + 2; // maximum allowed hierarchy depth
            int bone_count      = System.Math.Max(model1.anim.BoneCount, model2.anim.BoneCount);                       // maximum allowed skeleton size in number of bones

            this.m = new MeshInstancer();
            this.m.Initialize(num_skeleton_bones: bone_count, max_parent_depth: hierarchy_depth, deltaBufferCount: 3, PropertyBufferMaxDeltaCount: 3, PathDeltaBufferSize: 3); // initialize meshinstancer (intentially picking terrible small delta buffer sizes for testing purposes)
            this.p = new PathArrayHelper(this.m);

            this.m.SetAllAnimations(new AnimationController[] { model1.anim, model2.anim }); // add all animations

            this.m.AddGPUSkinnedMeshType(model1);                                            // add each mesh type
            this.m.AddGPUSkinnedMeshType(model2);
        }
Ejemplo n.º 8
0
        // Start is called before the first frame update
        void Start()
        {
            List <TextureUVAnimation> anims = new List <TextureUVAnimation>();

            anims.Add(MakeGPUTextureAnim(this.Explosion));

            this.m = new MeshInstancer();
            this.m.Initialize(max_parent_depth: 1, pathCount: 2, override_mesh: BaseMeshLibrary.CreatePlane());
            this.p = new PathArrayHelper(this.m);

            var tlib = new TextureAnimationLibrary(anims);

            this.m.SetAllTextureAnimations(tlib);
            this.m.FrustumCamera = this.FrustumCamera;

            var explosion_mesh_type = this.m.AddNewMeshType(this.m.Default.shared_mesh, Instantiate(this.ExplosionMaterial));

            float f = 80.0f;

            for (int i = 0; i < N; i++)
            {
                this.instances[i]                          = new InstanceData <InstanceProperties>(explosion_mesh_type);
                this.instances[i].position                 = new Vector3(Random.Range(-f, f), Random.Range(-f, f), Random.Range(-f, f));
                this.instances[i].props_animationID        = tlib.Animations[0].animation_id;
                this.instances[i].props_IsTextureAnimation = true;
                this.instances[i].props_AnimationSeconds   = Random.Range(0, 1.0f);
                this.instances[i].props_AnimationSpeed     = 2.0f;

                var path = new Path(2, this.m, look_at_camera: true);                       // instruct instance to look at camera via path API
                this.p.InitializePath(ref path);
                this.p.SetPathStaticPosition(path, this.instances[i].position, Vector3.up); // just set a not-moving path
                this.p.UpdatePath(ref path);
                this.instances[i].SetPath(path, this.m);
                this.paths[i] = path;

                this.m.Initialize(ref this.instances[i]);
                this.m.Append(ref this.instances[i]);
            }
        }