Esempio n. 1
0
        /// <summary>
        /// Initializes the surface, called by the genetor to pass parameters
        /// </summary>
        public void Initialize(int level, Vector3 start, Vector3 end, Vector3 topRight, Vector3 bottomLeft, Planet planet, Planet.SurfacePosition sp, Surface parent, int sx, int sy)
        {
            // save reference
            this.planet = planet;
            this.surfacePosition = sp;
            this.parent = parent;
            this.subX = sx;
            this.subY = sy;

            // surface start and end points
            this.topleft = start;
            this.bottomright = end;
            this.topright = topRight;
            this.bottomleft = bottomLeft;

            // lod subdivision level
            lodLevel = level;

            // modifier starting point
            modifierStartX = 0;
            modifierStartY = 0;
            if(lodLevel == 0) {
            modifierResolution = planet.modifierResolution / (float)planet.subdivisions;
            }
            else {
            modifierResolution = parent.modifierResolution / 2f;
            modifierStartX += parent.modifierStartX;
            modifierStartY += parent.modifierStartY;
            }

            modifierStartX += subX * modifierResolution;
            modifierStartY += subY * modifierResolution;

            modifierMultiplier = modifierResolution / planet.meshResolution;

            // uv
            uvStartX = 0f;
            uvStartY = 0f;
            if(lodLevel == 0) {
            uvResolution = 1f / planet.subdivisions;
            }
            else {
            uvResolution = parent.uvResolution / 2f;
            uvStartX += parent.uvStartX;
            uvStartY += parent.uvStartY;
            }
            uvStartX += subX * uvResolution;
            uvStartY += subY * uvResolution;

            // corners
            subSurfaces = new List<Surface>();
            if(planet.useLod) {
            /*topLeftCorner = SperifyPoint(topleft) * planet.radius;
            bottomRightCorner = SperifyPoint(bottomright) * planet.radius;

            middlePoint = SperifyPoint((start + end) / 2f) * planet.radius;

            topRightCorner = SperifyPoint(topright) * planet.radius;
            bottomLeftCorner = SperifyPoint(bottomleft) * planet.radius;*/

            // TOP LEFT
            topLeftCorner = SperifyPoint(topleft);
            float displacement = planet.Terrain.module.GetValue(topLeftCorner);
            float rx = modifierStartX + 0 * modifierMultiplier;
            float cy = modifierStartY + 0 * modifierMultiplier;
            if(planet.useBicubicInterpolation)
                displacement += planet.GetBicubicInterpolatedModifierAt(rx, cy, surfacePosition);
            else
                displacement += planet.GetBilinearInterpolatedModifierAt(rx, cy, surfacePosition);
            topLeftCorner += topLeftCorner * displacement * planet.heightVariation;
            topLeftCorner *= planet.radius;

            // BOTTOM RIGHT
            bottomRightCorner = SperifyPoint(bottomright);
            displacement = planet.Terrain.module.GetValue(bottomRightCorner);
            rx = modifierStartX + planet.meshResolution * modifierMultiplier;
            cy = modifierStartY + planet.meshResolution * modifierMultiplier;
            if(planet.useBicubicInterpolation)
                displacement += planet.GetBicubicInterpolatedModifierAt(rx, cy, surfacePosition);
            else
                displacement += planet.GetBilinearInterpolatedModifierAt(rx, cy, surfacePosition);
            bottomRightCorner += bottomRightCorner * displacement * planet.heightVariation;
            bottomRightCorner *= planet.radius;

            // MIDDLE POINT
            middlePoint = SperifyPoint((start + end) / 2f);
            displacement = planet.Terrain.module.GetValue(middlePoint);
            rx = modifierStartX + (planet.meshResolution/2) * modifierMultiplier;
            cy = modifierStartY + (planet.meshResolution/2) * modifierMultiplier;
            if(planet.useBicubicInterpolation)
                displacement += planet.GetBicubicInterpolatedModifierAt(rx, cy, surfacePosition);
            else
                displacement += planet.GetBilinearInterpolatedModifierAt(rx, cy, surfacePosition);
            middlePoint += middlePoint * displacement * planet.heightVariation;
            middlePoint *= planet.radius;

            // TOP RIGHT
            topRightCorner = SperifyPoint(topright);
            displacement = planet.Terrain.module.GetValue(topRightCorner);
            rx = modifierStartX + planet.meshResolution * modifierMultiplier;
            cy = modifierStartY + 0 * modifierMultiplier;
            if(planet.useBicubicInterpolation)
                displacement += planet.GetBicubicInterpolatedModifierAt(rx, cy, surfacePosition);
            else
                displacement += planet.GetBilinearInterpolatedModifierAt(rx, cy, surfacePosition);
            topRightCorner += topRightCorner * displacement * planet.heightVariation;
            topRightCorner *= planet.radius;

            // BOTTOM LEFT
            bottomLeftCorner = SperifyPoint(bottomleft);
            displacement = planet.Terrain.module.GetValue(bottomLeftCorner);
            rx = modifierStartX + planet.meshResolution * modifierMultiplier;
            cy = modifierStartY + 0 * modifierMultiplier;
            if(planet.useBicubicInterpolation)
                displacement += planet.GetBicubicInterpolatedModifierAt(rx, cy, surfacePosition);
            else
                displacement += planet.GetBilinearInterpolatedModifierAt(rx, cy, surfacePosition);
            bottomLeftCorner += bottomLeftCorner * displacement * planet.heightVariation;
            bottomLeftCorner *= planet.radius;

            /*Transform sphere = (Transform)Resources.Load("Sphere", typeof(Transform));
            Transform newSphere = (Transform)Instantiate(sphere, planet.transform.TransformPoint(topLeftCorner), Quaternion.identity);
            newSphere.parent = transform;
            newSphere = (Transform)Instantiate(sphere, bottomRightCorner, Quaternion.identity);
            newSphere.parent = transform;
            newSphere = (Transform)Instantiate(sphere, middlePoint, Quaternion.identity);
            newSphere.parent = transform;
            newSphere = (Transform)Instantiate(sphere, topRightCorner, Quaternion.identity);
            newSphere.parent = transform;
            newSphere = (Transform)Instantiate(sphere, bottomLeftCorner, Quaternion.identity);
            newSphere.parent = transform;*/
            }

            // mesh
            if(mesh == null) {
            // create mesh filter
            MeshFilter meshFilter = (MeshFilter)gameObject.AddComponent(typeof(MeshFilter));
            mesh = meshFilter.sharedMesh = new Mesh();

            // create mesh renderer
            gameObject.AddComponent(typeof(MeshRenderer));
            renderer.castShadows = true;
            renderer.receiveShadows = true;
            renderer.enabled = true;
            }
        }
Esempio n. 2
0
    /// <summary>
    /// Initializes the surface, called by the genetor to pass parameters
    /// </summary>
    public void Initialize(int level, Vector3 start, Vector3 end, Vector3 topRight, Vector3 bottomLeft, Planet planet, Planet.SurfacePosition sp, Surface parent, int sx, int sy)
    {
        // save reference
        this.planet          = planet;
        this.surfacePosition = sp;
        this.parent          = parent;
        this.subX            = sx;
        this.subY            = sy;

        // surface start and end points
        this.topleft     = start;
        this.bottomright = end;

        // lod subdivision level
        lodLevel = level;

        // modifier starting point
        modifierStartX = 0;
        modifierStartY = 0;
        if (lodLevel == 0)
        {
            modifierResolution = planet.modifierResolution / (float)planet.subdivisions;
        }
        else
        {
            modifierResolution = parent.modifierResolution / 2f;
            modifierStartX    += parent.modifierStartX;
            modifierStartY    += parent.modifierStartY;
        }

        modifierStartX += subX * modifierResolution;
        modifierStartY += subY * modifierResolution;

        modifierMultiplier = modifierResolution / planet.meshResolution;

        // uv
        uvStartX = 0f;
        uvStartY = 0f;
        if (lodLevel == 0)
        {
            uvResolution = 1f / planet.subdivisions;
        }
        else
        {
            uvResolution = parent.uvResolution / 2f;
            uvStartX    += parent.uvStartX;
            uvStartY    += parent.uvStartY;
        }
        uvStartX += subX * uvResolution;
        uvStartY += subY * uvResolution;

        // corners
        subSurfaces = new List <Surface>();
        if (planet.useLod)
        {
            topLeftCorner     = SperifyPoint(topleft) * planet.radius;
            bottomRightCorner = SperifyPoint(bottomright) * planet.radius;

            middlePoint = SperifyPoint(((start + end) / 2f)) * planet.radius;

            topRightCorner   = SperifyPoint(topRight) * planet.radius;
            bottomLeftCorner = SperifyPoint(bottomLeft) * planet.radius;
        }

        // mesh
        if (mesh == null)
        {
            // create mesh filter
            MeshFilter meshFilter = (MeshFilter)gameObject.AddComponent(typeof(MeshFilter));
            mesh = meshFilter.sharedMesh = new Mesh();

            // create mesh renderer
            gameObject.AddComponent(typeof(MeshRenderer));
            renderer.castShadows    = true;
            renderer.receiveShadows = true;
            renderer.enabled        = true;
        }
    }