Ejemplo n.º 1
0
    public static float GetAltitude(EnvironmentGenome genome, float x, float z)
    {
        if (!genome.terrainGenome.useAltitude)
        {
            return(0f);
        }
        float altitude       = 0f;
        float distMultiplier = 0.02f;
        float dist           = new Vector2(x, z).magnitude *distMultiplier;

        float amplitude = 60f;
        float total     = 0f;

        for (int i = 0; i < genome.terrainGenome.terrainWaves.Length; i++)
        {
            float height = NoisePrime.Simplex3D(new Vector3(x, genome.terrainGenome.terrainWaves[i].z, z), genome.terrainGenome.terrainWaves[i].x * Mathf.Pow(2f, i)).value *(amplitude * genome.terrainGenome.terrainWaves[i].y / Mathf.Pow(2f, i) * dist);
            total += height;
        }
        altitude = total / genome.terrainGenome.terrainWaves.Length;

        // StartPositions!!!
        for (int j = 0; j < genome.agentStartPositionsList.Count; j++)
        {
            float distToSpawn = (new Vector2(x, z) - new Vector2(genome.agentStartPositionsList[j].agentStartPosition.x, genome.agentStartPositionsList[j].agentStartPosition.z)).magnitude;
            if (distToSpawn < 12f)
            {
                altitude = Mathf.Lerp(0f, altitude, Mathf.Max(distToSpawn - 4f, 0f) / 8f);
            }
        }

        return(altitude);

        // OLD!!!!

        /*
         *
         * float total = 0f;
         * for (int i = 0; i < genome.terrainGenome.terrainWaves.Length; i++) {
         *  if (i % 2 == 0) {
         *      total += Mathf.Sin(x * genome.terrainGenome.terrainWaves[i].x + genome.terrainGenome.terrainWaves[i].z) * genome.terrainGenome.terrainWaves[i].y / genome.terrainGenome.terrainWaves[i].x; // divide by frequency
         *  }
         *  else {
         *      total += Mathf.Sin(z * genome.terrainGenome.terrainWaves[i].x + genome.terrainGenome.terrainWaves[i].z) * genome.terrainGenome.terrainWaves[i].y / genome.terrainGenome.terrainWaves[i].x;
         *  }
         * }
         * float height = total / genome.terrainGenome.terrainWaves.Length;
         * float distToSpawn = new Vector2(x, z).magnitude;
         * if (distToSpawn < 12f) {
         *  height = Mathf.Lerp(0f, height, Mathf.Max(distToSpawn - 4f, 0f) / 8f);
         * }
         * return height;*/
    }
    private void UpdateSkyTangents()
    {
        for (int i = 0; i < strokeBackgroundArray.Length; i++)
        {
            //background1[i].y = Mathf.Abs(background1[i].y);
            NoiseSample noiseSample = NoisePrime.Simplex3D(strokeBackgroundArray[i].pos + skyNoiseOffset, skyNoiseFrequency);
            //float size = Vector3.Dot(noiseSample.derivative.normalized, strokeBackgroundArray[i].normal);
            noiseSample.value = noiseSample.value * 0.5f + 0.5f;
            //strokeBackgroundArray[i].pos = background1[i];
            float       colorNoiseFrequency = 1000f;
            NoiseSample colorNoiseSample    = NoisePrime.Simplex3D(strokeBackgroundArray[i].pos, skyNoiseFrequency * colorNoiseFrequency);
            float       colorNoise          = (colorNoiseSample.value * 0.5f + 0.5f);

            Color color = skyColorGradient.Evaluate(Vector3.Dot(strokeBackgroundArray[i].pos.normalized, Vector3.up));
            strokeBackgroundArray[i].col = new Vector3(Mathf.Lerp(color.r, colorNoise, skyColorNoise), Mathf.Lerp(color.g, colorNoise, skyColorNoise), Mathf.Lerp(color.b, colorNoise, skyColorNoise));
            //strokeBackgroundArray[i].col = new Vector3(noiseSample.value, noiseSample.value, noiseSample.value);
            //strokeBackgroundArray[i].normal = -background1[i].normalized;
            //Vector3 cross = Vector3.Cross(strokeBackgroundArray[i].normal, noiseSample.derivative);
            strokeBackgroundArray[i].tangent = Vector3.Cross(strokeBackgroundArray[i].normal, noiseSample.derivative);
            //strokeBackgroundArray[i].prevPos = background1[i];

            //size = (1.0f - Mathf.Abs(size));  //0 = 1,
            strokeBackgroundArray[i].dimensions = new Vector2(1f, 1f);
        }
        skyNoiseOffset += skyNoiseScroll * Time.deltaTime;
        backgroundStrokesBuffer.SetData(strokeBackgroundArray);


        /*float groundPos = -5f;
         * for (int i = 0; i < strokeTerrainArray.Length; i++) {
         *  //strokeTerrainArray[i].pos = UnityEngine.Random.insideUnitSphere * groundSpreadExponent;
         *  //strokeTerrainArray[i].pos *= strokeTerrainArray[i].pos.sqrMagnitude;
         *  strokeTerrainArray[i].pos.y = groundPos;  // start at groundHeight
         *  NoiseSample noiseSample = NoisePrime.Simplex3D(new Vector3(strokeTerrainArray[i].pos.x, 0f, strokeTerrainArray[i].pos.z), groundNoiseFrequency);
         *  float heightOffset = noiseSample.value * groundNoiseAmplitude;
         *  strokeTerrainArray[i].pos.y = groundPos + heightOffset;
         *  //strokeTerrainArray[i].pos = terrain1[i];
         *  Color color = terrainColorGradient.Evaluate(noiseSample.value * 0.5f + 0.5f);
         *  strokeTerrainArray[i].col = new Vector3(color.r, color.g, color.b);
         *  //Vector3 preTangent = noiseSample.derivative;
         *  //preTangent.y *= groundNoiseAmplitude;
         *
         *  //strokeTerrainArray[i].normal = new Vector3(-noiseSample.derivative.x, 1f, -noiseSample.derivative.z);
         *  //Vector3 cross = Vector3.Cross(strokeBackgroundArray[i].normal, noiseSample.derivative);
         *  strokeTerrainArray[i].tangent = Vector3.Cross(strokeTerrainArray[i].normal, noiseSample.derivative);
         *  strokeTerrainArray[i].dimensions = new Vector2(1f, 1f);
         *
         * }
         * terrainStrokesBuffer.SetData(strokeTerrainArray);
         */
    }
    void InitializeOnStartup()
    {
        fullscreenQuadMesh = new Mesh();

        // Create Materials
        canvasMaterial                 = new Material(canvasShader);
        paintMaterial                  = new Material(paintShader);
        brushstrokeGessoMaterial       = new Material(brushGessoShader);
        brushstrokeBackgroundMaterial  = new Material(brushWorldSpaceShader);
        brushstrokeTerrainMaterial     = new Material(brushTerrainShader);
        brushstrokeCritterMaterial     = new Material(brushCritterShader);
        brushstrokeDecorationsMaterial = new Material(brushWorldSpaceShader);

        // GESSO BUFFER:
        int gessoRes = 16;

        Vector3[] gesso1 = PointCloudSphericalShell.GetPointsSphericalShell(50f, gessoRes, 1f);
        Vector3[] gesso2 = PointCloudSphericalShell.GetPointsSphericalShell(55f, gessoRes, 1f);
        Vector3[] gesso3 = PointCloudSphericalShell.GetPointsSphericalShell(60f, gessoRes, 1f);
        strokeGessoArray = new strokeStruct[gesso1.Length + gesso2.Length + gesso3.Length];
        for (int i = 0; i < gesso1.Length; i++)
        {
            strokeGessoArray[i].pos    = gesso1[i];
            strokeGessoArray[i].col    = new Vector3(1f, 1f, 1f);
            strokeGessoArray[i].normal = -gesso1[i].normalized;
            strokeGessoArray[i + gesso1.Length].pos    = gesso2[i];
            strokeGessoArray[i + gesso1.Length].col    = new Vector3(1f, 1f, 1f);
            strokeGessoArray[i + gesso1.Length].normal = -gesso2[i].normalized;
            strokeGessoArray[i + gesso1.Length + gesso2.Length].pos    = gesso3[i];
            strokeGessoArray[i + gesso1.Length + gesso2.Length].col    = new Vector3(1f, 1f, 1f);
            strokeGessoArray[i + gesso1.Length + gesso2.Length].normal = -gesso3[i].normalized;
        }
        gessoStrokesBuffer = new ComputeBuffer(strokeGessoArray.Length, sizeof(float) * (3 + 3 + 3 + 3 + 3 + 2) + sizeof(int) * 1);  // col=3f, pos=3f, nml=3f, tan=3f, prevP=3f, dim=2f, type=1i
        gessoStrokesBuffer.SetData(strokeGessoArray);

        // BACKGROUND BUFFER:
        int backgroundRes = 24;

        Vector3[] background1 = PointCloudSphericalShell.GetPointsSphericalShell(100f, backgroundRes, 1f);
        Vector3[] background2 = PointCloudSphericalShell.GetPointsSphericalShell(110f, backgroundRes, 1f);
        Vector3[] background3 = PointCloudSphericalShell.GetPointsSphericalShell(120f, backgroundRes, 1f);
        strokeBackgroundArray = new strokeStruct[background1.Length + background2.Length + background3.Length];

        //float groundPos = -10f;
        for (int i = 0; i < background1.Length; i++)
        {
            float colorNoiseFrequency = 1000f;

            background1[i].y = Mathf.Abs(background1[i].y) + terrainAltitude;
            NoiseSample colorNoiseSample = NoisePrime.Simplex3D(background1[i], skyNoiseFrequency * colorNoiseFrequency);
            float       colorNoise       = colorNoiseSample.value * 0.5f + 0.5f;
            NoiseSample noiseSample      = NoisePrime.Simplex3D(background1[i], skyNoiseFrequency);
            strokeBackgroundArray[i].pos    = background1[i];
            strokeBackgroundArray[i].col    = new Vector3(colorNoise, colorNoise, colorNoise);
            strokeBackgroundArray[i].normal = -background1[i].normalized;
            //Vector3 cross = Vector3.Cross(strokeBackgroundArray[i].normal, noiseSample.derivative);
            strokeBackgroundArray[i].tangent    = Vector3.Cross(strokeBackgroundArray[i].normal, noiseSample.derivative);
            strokeBackgroundArray[i].prevPos    = background1[i];
            strokeBackgroundArray[i].dimensions = new Vector2(1f, noiseSample.derivative.magnitude);


            background2[i].y = Mathf.Abs(background2[i].y) + terrainAltitude;
            colorNoiseSample = NoisePrime.Simplex3D(background1[i], skyNoiseFrequency * colorNoiseFrequency);
            colorNoise       = colorNoiseSample.value * 0.5f + 0.5f;
            noiseSample      = NoisePrime.Simplex3D(background2[i], skyNoiseFrequency);
            strokeBackgroundArray[i + background1.Length].pos    = background2[i];
            strokeBackgroundArray[i + background1.Length].col    = new Vector3(colorNoise, colorNoise, colorNoise);
            strokeBackgroundArray[i + background1.Length].normal = -background2[i].normalized;
            //strokeBackgroundArray[i + background1.Length].normal = -background2[i].normalized;
            //cross = Vector3.Cross(strokeBackgroundArray[i].normal, noiseSample.derivative);
            strokeBackgroundArray[i + background1.Length].tangent    = Vector3.Cross(strokeBackgroundArray[i + background1.Length].normal, noiseSample.derivative);
            strokeBackgroundArray[i + background1.Length].prevPos    = background2[i];
            strokeBackgroundArray[i + background1.Length].dimensions = new Vector2(1f, noiseSample.derivative.magnitude);

            background3[i].y = Mathf.Abs(background3[i].y) + terrainAltitude;
            colorNoiseSample = NoisePrime.Simplex3D(background1[i], skyNoiseFrequency * colorNoiseFrequency);
            colorNoise       = colorNoiseSample.value * 0.5f + 0.5f;
            noiseSample      = NoisePrime.Simplex3D(background3[i], skyNoiseFrequency);
            strokeBackgroundArray[i + background1.Length + background2.Length].pos    = background3[i];
            strokeBackgroundArray[i + background1.Length + background2.Length].col    = new Vector3(colorNoise, colorNoise, colorNoise);
            strokeBackgroundArray[i + background1.Length + background2.Length].normal = -background1[i].normalized;
            //cross = Vector3.Cross(strokeBackgroundArray[i].normal, noiseSample.derivative);
            strokeBackgroundArray[i + background1.Length + background2.Length].tangent    = Vector3.Cross(strokeBackgroundArray[i + background1.Length + background2.Length].normal, noiseSample.derivative);
            strokeBackgroundArray[i + background1.Length + background2.Length].prevPos    = background3[i];
            strokeBackgroundArray[i + background1.Length + background2.Length].dimensions = new Vector2(1f, noiseSample.derivative.magnitude);
        }
        backgroundStrokesBuffer = new ComputeBuffer(strokeBackgroundArray.Length, sizeof(float) * (3 + 3 + 3 + 3 + 3 + 2) + sizeof(int) * 1);
        backgroundStrokesBuffer.SetData(strokeBackgroundArray);

        /*// TERRAIN BUFFER:
         * int terrainCount = 20000;
         * Vector3[] terrain1 = new Vector3[terrainCount]; //PointCloudSphericalShell.GetPointsSphericalShell(2f, backgroundRes, 1f);
         * strokeTerrainArray = new strokeStruct[terrain1.Length];
         *
         * float groundPos = -5f;
         * for (int i = 0; i < strokeTerrainArray.Length; i++) {
         *  terrain1[i] = UnityEngine.Random.insideUnitSphere * groundSpreadExponent;
         *  //terrain1[i] *= terrain1[i].sqrMagnitude;
         *  terrain1[i].y = groundPos;  // start at groundHeight
         *  NoiseSample noiseSample = NoisePrime.Simplex3D(new Vector3(terrain1[i].x, 0f, terrain1[i].z), groundNoiseFrequency);
         *  float heightOffset = noiseSample.value * groundNoiseAmplitude;
         *  terrain1[i].y += heightOffset;
         *  strokeTerrainArray[i].pos = terrain1[i];
         *  Color color = terrainColorGradient.Evaluate(noiseSample.value * 0.5f + 0.5f);
         *  strokeTerrainArray[i].col = new Vector3(color.r, color.g, color.b);
         *  Vector3 preTangent = noiseSample.derivative;
         *  preTangent.y *= groundNoiseAmplitude;
         *  strokeTerrainArray[i].tangent = preTangent.normalized;
         *  strokeTerrainArray[i].normal = new Vector3(-noiseSample.derivative.x, 1f, -noiseSample.derivative.z).normalized;
         *  //Vector3 cross = Vector3.Cross(strokeBackgroundArray[i].normal, noiseSample.derivative);
         *  strokeTerrainArray[i].prevPos = strokeTerrainArray[i].prevPos;
         *  strokeTerrainArray[i].dimensions = new Vector2(1f, 1f);
         *
         * }
         * terrainStrokesBuffer = new ComputeBuffer(strokeTerrainArray.Length, sizeof(float) * (3 + 3 + 3 + 3 + 3 + 2) + sizeof(int) * 1);
         * terrainStrokesBuffer.SetData(strokeTerrainArray);
         */

        InitTerrain();

        //Create quad buffer for brushtroke billboard
        quadPointsBuffer = new ComputeBuffer(6, sizeof(float) * 3);
        quadPointsBuffer.SetData(new[] {
            new Vector3(-0.5f, 0.5f),
            new Vector3(0.5f, 0.5f),
            new Vector3(0.5f, -0.5f),
            new Vector3(0.5f, -0.5f),
            new Vector3(-0.5f, -0.5f),
            new Vector3(-0.5f, 0.5f)
        });

        // Create master commandBuffer that makes the magic happen
        cmdBuffer      = new CommandBuffer();
        cmdBuffer.name = "cmdBuffer";
    }