Exemple #1
0
    public void BuildChunk(EnvironmentGenome genome, Vector2 position, Vector2 scale, Material mat)
    {
        GameObject chunkGO = new GameObject("chunkMesh");

        /*Debug.Log("BuildChunk position: " + position.ToString() + ", scale: " + scale.ToString()
         + ", west: " + thisWest.ToString()
         + ", north: " + thisNorth.ToString()
         + ", east: " + thisEast.ToString()
         + ", south: " + thisSouth.ToString());*/
        //TerrainConstructorGPU constructor = new TerrainConstructorGPU();
        Mesh groundMesh = TerrainConstructorGPU.GetTerrainMesh(this.resolutionX, this.resolutionZ, position.x, position.y, scale.x * 2f, scale.y * 2);

        chunkGO.AddComponent <MeshFilter>().sharedMesh = groundMesh;
        this.mesh = groundMesh;
        chunkGO.AddComponent <MeshRenderer>().material = mat;
        chunkGO.transform.parent        = gameObject.transform;
        chunkGO.transform.localPosition = new Vector3(0f, 0f, 0f);
    }
Exemple #2
0
    /*public Mesh DeformMesh(Mesh mesh) {
     *  Mesh deformedMesh = new Mesh();
     *
     *  List<Vector3> verts = new List<Vector3>();
     *  for(int i = 0; i < mesh.vertices.Length; i++) {
     *      float randomExtrude = UnityEngine.Random.Range(-1f, 1f) * 0.05f;
     *      verts.Add(mesh.vertices[i] + mesh.normals[i] * randomExtrude);
     *  }
     *  deformedMesh.SetVertices(verts);
     *  deformedMesh.triangles = mesh.triangles;
     *  deformedMesh.normals = mesh.normals;
     *  deformedMesh.RecalculateNormals();
     *  return deformedMesh;
     * }*/

    public void CreateCollisionAndGameplayContent(EnvironmentGenome genome)
    {
        GameObject environmentGameplayGO = new GameObject("environmentGameplay");

        environmentGameplay = environmentGameplayGO.AddComponent <EnvironmentGameplay>();
        environmentGameplay.transform.parent        = gameObject.transform;
        environmentGameplay.transform.localPosition = new Vector3(0f, 0f, 0f);

        // Construct Ground Physics Material:
        PhysicMaterial noFriction = new PhysicMaterial();

        noFriction.dynamicFriction = 0f;
        noFriction.staticFriction  = 0f;
        noFriction.frictionCombine = PhysicMaterialCombine.Minimum;

        if (genome.useTerrain)
        {
            environmentGameplay.groundCollision = new GameObject("ground");

            //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
            // Need to set Terrain RenderTextureCascade and some other data before getting Collision Mesh:
            //TerrainConstructorGPU.GenerateTerrainTexturesFromGenome();
            // INITIALIZE TERRAIN-COMPUTE::
            //TerrainConstructorGPU.terrainDisplayMaterial = mat;
            TerrainConstructorGPU.GenerateTerrainTexturesFromGenome(genome, false);

            Mesh topology = TerrainConstructorGPU.GetTerrainMesh(32, 32, 0f, 0f, Challenge.GetChallengeArenaBounds(genome.challengeType).x, Challenge.GetChallengeArenaBounds(genome.challengeType).z);
            //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

            //Mesh topology = TerrainConstructor.GetTerrainMesh(genome, 32, 32, 0f, 0f, Challenge.GetChallengeArenaBounds(genome.challengeType).x, Challenge.GetChallengeArenaBounds(genome.challengeType).z);
            environmentGameplay.groundCollision.AddComponent <MeshCollider>().sharedMesh = topology;
            environmentGameplay.groundCollision.transform.parent        = environmentGameplay.gameObject.transform;
            environmentGameplay.groundCollision.transform.localPosition = new Vector3(0f, 0f, 0f);
            //ground.GetComponent<Collider>().material = noFriction;
        }

        //=============WALLS===========
        environmentGameplay.arenaWalls = new List <GameObject>();

        GameObject northWall = GameObject.CreatePrimitive(PrimitiveType.Quad);

        northWall.transform.parent        = environmentGameplay.gameObject.transform;
        northWall.transform.localPosition = new Vector3(0f, 0f, genome.arenaBounds.z * 0.5f);
        northWall.transform.localScale    = new Vector3(genome.arenaBounds.x, genome.arenaBounds.y, genome.arenaBounds.z);
        northWall.GetComponent <MeshRenderer>().enabled = false;
        northWall.GetComponent <Collider>().material    = noFriction;
        northWall.tag = "hazard";
        environmentGameplay.arenaWalls.Add(northWall);

        GameObject southWall = GameObject.CreatePrimitive(PrimitiveType.Quad);

        southWall.transform.parent        = environmentGameplay.gameObject.transform;
        southWall.transform.localPosition = new Vector3(0f, 0f, -genome.arenaBounds.z * 0.5f);
        southWall.transform.localScale    = new Vector3(genome.arenaBounds.x, genome.arenaBounds.y, genome.arenaBounds.z);
        southWall.transform.rotation      = Quaternion.Euler(0f, 180f, 0f);
        southWall.GetComponent <MeshRenderer>().enabled = false;
        southWall.GetComponent <Collider>().material    = noFriction;
        southWall.tag = "hazard";
        environmentGameplay.arenaWalls.Add(southWall);

        GameObject eastWall = GameObject.CreatePrimitive(PrimitiveType.Quad);

        eastWall.transform.parent        = environmentGameplay.gameObject.transform;
        eastWall.transform.localPosition = new Vector3(genome.arenaBounds.x * 0.5f, 0f, 0f);
        eastWall.transform.localScale    = new Vector3(genome.arenaBounds.z, genome.arenaBounds.y, genome.arenaBounds.z);
        eastWall.transform.rotation      = Quaternion.Euler(0f, 90f, 0f);
        eastWall.GetComponent <MeshRenderer>().enabled = false;
        eastWall.GetComponent <Collider>().material    = noFriction;
        eastWall.tag = "hazard";
        environmentGameplay.arenaWalls.Add(eastWall);

        GameObject westWall = GameObject.CreatePrimitive(PrimitiveType.Quad);

        westWall.transform.parent        = environmentGameplay.gameObject.transform;
        westWall.transform.localPosition = new Vector3(-genome.arenaBounds.x * 0.5f, 0f, 0f);
        westWall.transform.localScale    = new Vector3(genome.arenaBounds.z, genome.arenaBounds.y, genome.arenaBounds.z);
        westWall.transform.rotation      = Quaternion.Euler(0f, -90f, 0f);
        westWall.GetComponent <MeshRenderer>().enabled = false;
        westWall.GetComponent <Collider>().material    = noFriction;
        westWall.tag = "hazard";
        environmentGameplay.arenaWalls.Add(westWall);

        GameObject ceiling = GameObject.CreatePrimitive(PrimitiveType.Quad);

        ceiling.transform.parent        = environmentGameplay.gameObject.transform;
        ceiling.transform.localPosition = new Vector3(0f, genome.arenaBounds.y * 0.5f, 0f);
        ceiling.transform.localScale    = new Vector3(genome.arenaBounds.z, genome.arenaBounds.y, 1f);
        ceiling.transform.rotation      = Quaternion.Euler(-90f, 0f, 0f);
        ceiling.GetComponent <MeshRenderer>().enabled = false;
        ceiling.GetComponent <Collider>().material    = noFriction;
        ceiling.tag = "hazard";
        environmentGameplay.arenaWalls.Add(ceiling);

        // ======================================================================

        // Game-Required Modules:
        // Target !!!
        if (genome.useTargetColumn)
        {
            GameObject targetColumnGO = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
            targetColumnGO.transform.parent  = environmentGameplay.gameObject.transform;
            environmentGameplay.targetColumn = targetColumnGO.AddComponent <TargetColumn>();
            environmentGameplay.targetColumn.GetComponent <MeshRenderer>().enabled = false; // hide
            environmentGameplay.targetColumn.Initialize(genome.targetColumnGenome, genome);
        }

        // Obstacles:
        if (genome.useBasicObstacles)
        {
            environmentGameplay.obstacles = new List <GameObject>();
            for (int i = 0; i < genome.basicObstaclesGenome.obstaclePositions.Length; i++)
            {
                GameObject obstacle = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
                obstacle.transform.parent = environmentGameplay.gameObject.transform;
                //float x = genome.basicObstaclesGenome.obstaclePositions[i].x * genome.arenaBounds.x - genome.arenaBounds.x * 0.5f;
                //float z = genome.basicObstaclesGenome.obstaclePositions[i].y * genome.arenaBounds.z - genome.arenaBounds.z * 0.5f;
                float x = genome.basicObstaclesGenome.obstaclePositions[i].x * genome.arenaBounds.x - genome.arenaBounds.x * 0.5f;
                float z = genome.basicObstaclesGenome.obstaclePositions[i].y * genome.arenaBounds.z - genome.arenaBounds.z * 0.5f;
                if (genome.useTargetColumn)
                {
                    float distToTarget = (new Vector2(environmentGameplay.targetColumn.transform.localPosition.x, environmentGameplay.targetColumn.transform.localPosition.z) - new Vector2(x, z)).magnitude;
                    if (distToTarget < genome.basicObstaclesGenome.obstacleScales[i] * 0.6f)
                    {
                        obstacle.SetActive(false);
                    }
                }

                float y = TerrainConstructorGPU.GetAltitude(x, z);// + 0.5f;
                obstacle.transform.localScale               = new Vector3(genome.basicObstaclesGenome.obstacleScales[i], genome.basicObstaclesGenome.obstacleScales[i], genome.basicObstaclesGenome.obstacleScales[i]);
                obstacle.transform.localPosition            = new Vector3(x, y, z);
                obstacle.GetComponent <Collider>().material = noFriction;
                obstacle.tag = "hazard";
                obstacle.GetComponent <MeshRenderer>().enabled = false; // hide
                environmentGameplay.obstacles.Add(obstacle);
            }
        }

        // Atmosphere (WIND) !!!
        if (genome.useAtmosphere)
        {
            environmentGameplay.atmosphere = new Atmosphere();
            environmentGameplay.atmosphere.Initialize(genome.atmosphereGenome);
        }

        // Set Genome's prefab environment:
        genome.gameplayPrefab = environmentGameplay;
    }