// 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;
                    }
                }
            }
        }
Beispiel #2
0
        // Use this for initialization
        void Start()
        {
            Mesh     mesh     = BaseMeshLibrary.CreateDefault();
            Material material = new Material(Shader.Find("Instanced/instancemeshdefault_Transparent"));

            material.enableInstancing = true;

            imesher = new MeshInstancer();

            imesher.Initialize(64, 64, override_mesh: BaseMeshLibrary.CreatePlane2Sides(), override_material: BaseMaterialLibrary.CreateDefault(), max_parent_depth: 1, num_skeleton_bones: 1);
            var keys = Texture2MeshType.Textures2MeshType(this.Textures, mesh, material);

            foreach (var key in keys)
            {
                imesher.AddNewMeshType(key.mesh_key, key.material_key);
            }

            imesher.FrustumCamera = frustum_culling ? GameObject.Find("Main Camera").GetComponent <Camera>() : null;

            var udat = keys.Where(x => x.material_key.mainTexture.name == "clear3").First();

            InstanceData d1 = new InstanceData(imesher.GetMeshType(udat.mesh_key, udat.material_key));

            d1.props_color32 = new Color32(0, 0, 255, 90);
            d1.position      = new Vector3(0.5f, 0, 0);
            d1.rotation      = Quaternion.Euler(-90, 0, 0);
            d1.props_color32 = Color.blue;
            imesher.Initialize(ref d1);
            imesher.Append(ref d1);

            InstanceData d2 = new InstanceData(imesher.GetMeshType(udat.mesh_key, udat.material_key));

            d2.props_color32 = new Color32(0, 0, 255, 90);
            d2.position      = new Vector3(0, 0, 0);
            d2.rotation      = Quaternion.Euler(-90, 0, 0);
            d2.props_color32 = Color.blue;
            imesher.Initialize(ref d2);
            imesher.Append(ref d2);

            InstanceData d3 = new InstanceData(imesher.GetMeshType(udat.mesh_key, udat.material_key));

            d3.props_color32 = new Color32(0, 0, 255, 90);
            d3.position      = new Vector3(-0.5f, 0, 0);
            d3.rotation      = Quaternion.Euler(-90, 0, 0);
            d3.props_color32 = Color.blue;
            imesher.Initialize(ref d3);
            imesher.Append(ref d3);
        }