terraingen.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:RandTerrainPy 作者: jackromo 项目源码 文件源码
def _get_noise_at(self, x, y):
        """Get perlin noise at a point in terrain.

        Does this by choosing a random gradient vector for each grid corner (done at initialization)
        and taking their dot products with the displacement vectors to each point in the grid.
        The generated values are then interpolated between based on distance to each corner from the desired point.

        Args:
            x (int): X coordinate of requested point.
            y (int): Y coordinate of requested point.

        Returns:
            float: Height of point on terrain, between 0 and 1 inclusive.

        """
        grid_x = x / float(self._square_len)    # X value within grid of gradient vectors
        grid_y = y / float(self._square_len)    # Y value within grid of gradient vectors
        left_x, right_x, upper_y, lower_y = self._get_corners(grid_x, grid_y)
        x_weight = grid_x - left_x
        y_weight = grid_y - upper_y
        # ul = upper left, lr = lower right, etc.
        ul_influence_val = self._get_influence_val(left_x, upper_y, grid_x, grid_y)
        ur_influence_val = self._get_influence_val(right_x, upper_y, grid_x, grid_y)
        ll_influence_val = self._get_influence_val(left_x, lower_y, grid_x, grid_y)
        lr_influence_val = self._get_influence_val(right_x, lower_y, grid_x, grid_y)
        # Interpolate between top two and bottom two influence vals, then interpolate between them using y_weight
        upper_influence_val = self._interpolate_between(ul_influence_val, ur_influence_val, x_weight)
        lower_influence_val = self._interpolate_between(ll_influence_val, lr_influence_val, x_weight)
        interpolated_val = self._interpolate_between(upper_influence_val, lower_influence_val, y_weight)
        # Normalize interpolated_val to be between 0 and 1, return as height
        # Can range from 0.5 to -0.5, add 0.5 to achieve proper result
        height = interpolated_val + 0.5
        # Some margin of error, ensure is still between 0 and 1
        return round(height) if not 0 <= height <= 1 else height
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号