コード例 #1
0
    // //Display.Length will always be 1 in the Editor. We dont have an implementation of display detection in editor as it works different. We are using Editor windows, not actual displays.
    // //You can still use multiple display in the editor you just dont need to activate the displays like you do in a release.

    // //https://stackoverflow.com/questions/43066541/unity-multiple-displays-not-working

    // void InitializeCameras()
    // {


    //     // multiple camera tutorial: http://blog.theknightsofunity.com/using-multiple-unity-cameras-why-this-may-be-important/
    //     //Did you notice that the default Unity 5 scene camera clears buffers to Skybox?
    //     ////This is Main Camera in the scene
    //     //Camera m_MainCamera;
    //     //This is the second Camera and is assigned in inspector
    //     // public Camera m_CameraTwo;

    //     // Finding multiple cameras: https://answers.unity.com/questions/15801/finding-cameras.html
    //     //(1) In your code use the following sysntax:
    //     //Camera myCamera = GameObject.FindWithTag("MainCamera").GetComponent <> Camera > ();
    //     //
    //     // (2): 1) Simply drag a reference of the desired camera to a variable of type Camera in your script.

    //     //2) use Camera.main if the camera you want is the only active one right now.

    //     //3) If you have multiple cameras named uniquely,
    //     //check foreach (Camera c in Camera.allCameras) and  c.gameObject.name == "DesiredCamera"
    //     //then that is the camera you want.
    //     //.ner, far, fieldOfView
    //     // //Start the Camera field of view at 60
    //     //m_FieldOfView = 60.0f; This is the vertical field of view;
    //     // .aspect = width / height
    //     //.name


    //     // Script inherits from MonoBehavior => Behavior => Component => object
    //     // Camera component inherits from Behavior => Component => object
    //     // Behaviours are Components that can be enabled or disabled.
    //     // For example, Rigidbody cannot be enabled/disabled. This is why it inherits from the Component class instead of Behaviour.
    //     // MonoBehaviour is the base class from which every Unity script derives.
    //     //MonoBehaviour:
    //     //The most important thing to note about MonoBehaviour is that you need it when you have to use corutines, Invoking,
    //     //or any Unity callback functions such as physics OnCollisionEnter function, Start, OnEnable, OnDisable, etc.
    //     //MonoBehaviour inherits from Behaviour so that your scripts can be enabled / disabled.
    //     //Note that Behaviour and Component are used by Unity for internal stuff. You should not try to inherit your script from these.


    // Use this for initialization
    void Start()
    {
        if (_boids == null)
        {
            Debug.LogError("_boids component is not set in the inspector");
            EditorApplication.Exit(0);
            //Application.Quit();
        }

        //BoidsNum = GetComponent<SimpleBoids>().BoidsNum;
        //GroundMaxCorner = GetComponent<SimpleBoids>().GroundMaxCorner;
        //GroundMinCorner = GetComponent<SimpleBoids>().GroundMinCorner;

        //CeilingMaxCorner = GetComponent<SimpleBoids>().CeilingMaxCorner;
        //CeilingMinCorner = GetComponent<SimpleBoids>().CeilingMinCorner;

        // check if the global component object is defined
        if (_instanceMaterial == null)
        {
            Debug.LogError("The global Variable _instanceMaterial is not  defined in Inspector");
            EditorApplication.Exit(0);
        }

        GroundMaxCorner = _boids.GroundMaxCorner;
        GroundMinCorner = _boids.GroundMinCorner;

        CeilingMaxCorner = _boids.CeilingMaxCorner;
        CeilingMinCorner = _boids.CeilingMinCorner;


        //Screen.SetResolution( sum of width of all displays, height, false)


        // InitializeCameras();



        // Create Vector2 vertices
        float unitRadius = 1f;                                            // radius = 1m

        Vector2[] vertices = Triangulator.FindPointsOnCircle(unitRadius); // 2D circle on xz plane

        // Use the triangulator to get indices for creating triangles
        Triangulator tr = new Triangulator(vertices);

        indices = tr.Triangulate();

        vertices3D = new Vector3[vertices.Length];

        for (int i = 0; i < vertices.Length; i++)
        {
            vertices3D[i] = new Vector3(vertices[i].x, 0.0f, vertices[i].y);
        }

        _instanceMeshCircle = new Mesh();

        _instanceMeshCircle.vertices  = vertices3D;
        _instanceMeshCircle.triangles = indices;
        _instanceMeshCircle.RecalculateNormals();
        _instanceMeshCircle.RecalculateBounds();



        // for debugging

        // _instanceMesh = GetComponent < Mesh > (); // get the Mesh component for the current gameboject

        _argsBuffer = new ComputeBuffer(
            1,
            _args.Length * sizeof(uint),
            ComputeBufferType.IndirectArguments
            );


        //https://answers.unity.com/questions/979080/how-to-pass-an-array-to-a-shader.html
        //_instanceMaterial.SetFloatArray("GroundMaxCorner", GroundMaxCornerF);
        //_instanceMaterial.SetFloatArray("GroundMinCorner", GroundMinCornerF);

        // _instanceMaterial.SetFloatArray("CeilingMaxCorner", CeilingMaxCornerF);
        // _instanceMaterial.SetFloatArray("CeilingMinCorner", CeilingMinCornerF);

        // // Shader vectors are always Vector4s.
        // But the value here is converted to a Vector3.
        //Vector3 value = Vector3.one;
        //Renderer renderer = GetComponent<Renderer>();
        //renderer.material.SetVector("_SomeVariable", value);

        // Use SetVector() rather than setFloatArray
        _instanceMaterial.SetVector("GroundMaxCorner", GroundMaxCorner);
        _instanceMaterial.SetVector("GroundMinCorner", GroundMinCorner);

        _instanceMaterial.SetVector("CeilingMaxCorner", CeilingMaxCorner);
        _instanceMaterial.SetVector("CeilingMinCorner", CeilingMinCorner);

        // _instanceMaterial.SetFloat("Use3DBoids", Use3DBoids);

        _instanceMaterial.SetBuffer("_BoidBuffer", _boids.BoidBuffer); // _boids.BoidBuffer is ceated in SimpleBoids.cs
    } // Start()