コード例 #1
0
ファイル: tool_build.py プロジェクト: carlhuth/Sprytile
    def build_preview(self, context, scene, ray_origin, ray_vector):
        obj = context.object
        data = scene.sprytile_data

        grid_id = obj.sprytile_gridid
        target_grid = sprytile_utils.get_grid(context, grid_id)

        target_img = sprytile_utils.get_grid_texture(obj, target_grid)
        if target_img is None:
            return

        up_vector, right_vector, plane_normal = sprytile_utils.get_current_grid_vectors(
            scene, False)

        # Raycast to the virtual grid
        face_position, x_vector, y_vector, plane_cursor = sprytile_utils.raycast_grid(
            scene, context, up_vector, right_vector, plane_normal, ray_origin,
            ray_vector)

        if face_position is None:
            return

        preview_verts = self.modal.get_build_vertices(face_position, x_vector,
                                                      y_vector, up_vector,
                                                      right_vector)

        # Get the center of the preview verts
        vtx_center = Vector((0, 0, 0))
        for vtx in preview_verts:
            vtx_center += vtx
        vtx_center /= len(preview_verts)

        rotate_normal = plane_normal

        rotation = Quaternion(rotate_normal, data.mesh_rotate)
        up_vector = rotation * up_vector
        right_vector = rotation * right_vector

        up_vector.normalize()
        right_vector.normalize()

        tile_xy = (target_grid.tile_selection[0],
                   target_grid.tile_selection[1])
        preview_uvs = sprytile_uv.get_uv_positions(data, target_img.size,
                                                   target_grid, up_vector,
                                                   right_vector, tile_xy,
                                                   preview_verts, vtx_center)

        self.modal.set_preview_data(preview_verts, preview_uvs)
コード例 #2
0
    def build_preview(self, context, scene, ray_origin, ray_vector):
        obj = context.object
        data = scene.sprytile_data

        grid_id = obj.sprytile_gridid
        target_grid = sprytile_utils.get_grid(context, grid_id)

        # Reset can build flag
        self.can_build = False

        target_img = sprytile_utils.get_grid_texture(obj, target_grid)
        if target_img is None:
            self.modal.clear_preview_data()
            return

        # If building on base layer, get from current virtual grid
        up_vector, right_vector, plane_normal = sprytile_utils.get_current_grid_vectors(
            scene, False)
        # Building on decal layer, get from face under mouse
        if data.work_layer == 'DECAL_1' and data.lock_normal is False:
            location, hit_normal, face_index, distance = self.modal.raycast_object(
                context.object, ray_origin, ray_vector)
            # For decals, if not hitting the object don't draw preview
            if hit_normal is None:
                self.modal.clear_preview_data()
                return

            # Do a coplanar check between hit location and cursor
            grid_origin = scene.cursor_location.copy()
            grid_origin += hit_normal * data.mesh_decal_offset

            check_coplanar = distance_point_to_plane(location, grid_origin,
                                                     hit_normal)
            check_coplanar = abs(check_coplanar) < 0.05
            if check_coplanar is False:
                self.modal.clear_preview_data()
                return

            face_up, face_right = self.modal.get_face_up_vector(
                context, face_index, 0.4, bias_right=True)
            if face_up is not None and face_right is not None:
                plane_normal = hit_normal
                up_vector = face_up
                right_vector = face_right
            else:
                self.modal.clear_preview_data()
                return

        rotation = Quaternion(plane_normal, data.mesh_rotate)

        up_vector = rotation * up_vector
        right_vector = rotation * right_vector

        # Raycast to the virtual grid
        face_position, x_vector, y_vector, plane_cursor = sprytile_utils.raycast_grid(
            scene, context, up_vector, right_vector, plane_normal, ray_origin,
            ray_vector)

        if face_position is None:
            self.modal.clear_preview_data()
            return

        # Passed can build checks, set flag to true
        self.can_build = True

        offset_tile_id, offset_grid, coord_min, coord_max = sprytile_utils.get_grid_area(
            target_grid.tile_selection[2], target_grid.tile_selection[3],
            data.uv_flip_x, data.uv_flip_y)

        grid_no_spacing = sprytile_utils.grid_no_spacing(target_grid)
        # No spacing in grid, automatically join the preview together
        if grid_no_spacing:
            origin_coord = face_position + coord_min[0] * x_vector + coord_min[
                1] * y_vector

            size_x = (coord_max[0] - coord_min[0]) + 1
            size_y = (coord_max[1] - coord_min[1]) + 1

            size_x *= target_grid.grid[0]
            size_y *= target_grid.grid[1]

            x_vector *= size_x / target_grid.grid[0]
            y_vector *= size_y / target_grid.grid[1]

            preview_verts = self.modal.get_build_vertices(
                origin_coord, x_vector, y_vector, up_vector, right_vector)
            vtx_center = Vector((0, 0, 0))
            for vtx in preview_verts:
                vtx_center += vtx
            vtx_center /= len(preview_verts)

            origin_xy = (target_grid.tile_selection[0],
                         target_grid.tile_selection[1])

            preview_uvs = sprytile_uv.get_uv_pos_size(
                data, target_img.size, target_grid, origin_xy, size_x, size_y,
                up_vector, right_vector, preview_verts, vtx_center)
            self.modal.set_preview_data(preview_verts, preview_uvs)
            return

        # Spaced grids need to be tiled
        preview_verts = []
        preview_uvs = []
        for i in range(len(offset_tile_id)):
            grid_offset = offset_grid[i]
            tile_offset = offset_tile_id[i]

            x_offset = x_vector * grid_offset[0]
            y_offset = y_vector * grid_offset[1]

            coord_position = face_position + x_offset + y_offset
            coord_verts = self.modal.get_build_vertices(
                coord_position, x_vector, y_vector, up_vector, right_vector)
            # Get the center of the preview verts
            vtx_center = Vector((0, 0, 0))
            for vtx in coord_verts:
                vtx_center += vtx
            vtx_center /= len(coord_verts)

            # Calculate the tile with offset
            tile_xy = (target_grid.tile_selection[0] + tile_offset[0],
                       target_grid.tile_selection[1] + tile_offset[1])

            coord_uvs = sprytile_uv.get_uv_positions(data, target_img.size,
                                                     target_grid, up_vector,
                                                     right_vector, tile_xy,
                                                     coord_verts, vtx_center)

            preview_verts.extend(coord_verts)
            preview_uvs.extend(coord_uvs)

        self.modal.set_preview_data(preview_verts, preview_uvs)
コード例 #3
0
    def build_preview(self, context, scene, ray_origin, ray_vector):
        obj = context.object
        data = scene.sprytile_data

        grid_id = obj.sprytile_gridid
        target_grid = sprytile_utils.get_grid(context, grid_id)

        target_img = sprytile_utils.get_grid_texture(obj, target_grid)
        if target_img is None:
            return

        up_vector, right_vector, plane_normal = sprytile_utils.get_current_grid_vectors(scene, False)

        rotation = Quaternion(plane_normal, data.mesh_rotate)

        up_vector = rotation * up_vector
        right_vector = rotation * right_vector

        # Raycast to the virtual grid
        face_position, x_vector, y_vector, plane_cursor = sprytile_utils.raycast_grid(
            scene, context,
            up_vector, right_vector, plane_normal,
            ray_origin, ray_vector
        )

        if face_position is None:
            return

        offset_tile_id, offset_grid, coord_min, coord_max = sprytile_utils.get_grid_area(
                                                                    target_grid.tile_selection[2],
                                                                    target_grid.tile_selection[3],
                                                                    data.uv_flip_x,
                                                                    data.uv_flip_y)

        grid_no_spacing = sprytile_utils.grid_no_spacing(target_grid)
        # No spacing in grid, automatically join the preview together
        if grid_no_spacing:
            origin_coord = face_position + coord_min[0] * x_vector + coord_min[1] * y_vector

            size_x = (coord_max[0] - coord_min[0]) + 1
            size_y = (coord_max[1] - coord_min[1]) + 1

            size_x *= target_grid.grid[0]
            size_y *= target_grid.grid[1]

            x_vector *= size_x / target_grid.grid[0]
            y_vector *= size_y / target_grid.grid[1]

            preview_verts = self.modal.get_build_vertices(origin_coord,
                                                          x_vector, y_vector,
                                                          up_vector, right_vector)
            vtx_center = Vector((0, 0, 0))
            for vtx in preview_verts:
                vtx_center += vtx
            vtx_center /= len(preview_verts)

            origin_xy = (target_grid.tile_selection[0],
                         target_grid.tile_selection[1])

            preview_uvs = sprytile_uv.get_uv_pos_size(data, target_img.size, target_grid,
                                                      origin_xy, size_x, size_y,
                                                      up_vector, right_vector,
                                                      preview_verts, vtx_center)
            self.modal.set_preview_data(preview_verts, preview_uvs)
            return

        # Spaced grids need to be tiled
        preview_verts = []
        preview_uvs = []
        for i in range(len(offset_tile_id)):
            grid_offset = offset_grid[i]
            tile_offset = offset_tile_id[i]

            x_offset = x_vector * grid_offset[0]
            y_offset = y_vector * grid_offset[1]

            coord_position = face_position + x_offset + y_offset
            coord_verts = self.modal.get_build_vertices(coord_position, x_vector, y_vector,
                                                        up_vector, right_vector)
            # Get the center of the preview verts
            vtx_center = Vector((0, 0, 0))
            for vtx in coord_verts:
                vtx_center += vtx
            vtx_center /= len(coord_verts)

            # Calculate the tile with offset
            tile_xy = (target_grid.tile_selection[0] + tile_offset[0],
                       target_grid.tile_selection[1] + tile_offset[1])

            coord_uvs = sprytile_uv.get_uv_positions(data, target_img.size, target_grid,
                                                     up_vector, right_vector, tile_xy,
                                                     coord_verts, vtx_center)

            preview_verts.extend(coord_verts)
            preview_uvs.extend(coord_uvs)

        self.modal.set_preview_data(preview_verts, preview_uvs)
コード例 #4
0
    def build_preview(self, context, scene, ray_origin, ray_vector):
        obj = context.object
        data = scene.sprytile_data

        grid_id = obj.sprytile_gridid
        target_grid = sprytile_utils.get_grid(context, grid_id)

        target_img = sprytile_utils.get_grid_texture(obj, target_grid)
        if target_img is None:
            return

        up_vector, right_vector, plane_normal = sprytile_utils.get_current_grid_vectors(
            scene, False)

        # Raycast the object
        hit_loc, hit_normal, face_index, hit_dist = self.modal.raycast_object(
            obj, ray_origin, ray_vector)
        # Didn't hit a face, do nothing
        if face_index is None:
            self.modal.set_preview_data(None, None)
            return

        preview_verts = []

        face = self.modal.bmesh.faces[face_index]
        for loop in face.loops:
            vert = loop.vert
            preview_verts.append(context.object.matrix_world * vert.co)

        # Get the center of the preview verts
        vtx_center = Vector((0, 0, 0))
        for vtx in preview_verts:
            vtx_center += vtx
        vtx_center /= len(preview_verts)

        rotate_normal = plane_normal

        # Recalculate the rotation normal
        face_up, face_right = self.modal.get_face_up_vector(
            context, face_index)

        if face_up is not None and face_right is not None:
            rotate_normal = face_right.cross(face_up)

        if face_up is not None:
            up_vector = face_up
        if face_right is not None:
            right_vector = face_right

        rotation = Quaternion(rotate_normal, data.mesh_rotate)
        up_vector = rotation * up_vector
        right_vector = rotation * right_vector

        up_vector.normalize()
        right_vector.normalize()

        tile_xy = (target_grid.tile_selection[0],
                   target_grid.tile_selection[1])
        preview_uvs = sprytile_uv.get_uv_positions(data, target_img.size,
                                                   target_grid, up_vector,
                                                   right_vector, tile_xy,
                                                   preview_verts, vtx_center)

        self.modal.set_preview_data(preview_verts, preview_uvs)