Under investigation in this paper is a (3+1)-dimensional generalized wave equation, which is the second equation in the Kadomtsev–Petviashvili hierarchy. Linear partial differential conditions presented in this paper are different from the previous study. Pfaffian and extended Pfaffian solutions with a real function φ(y) have been obtained based on these linear partial differential conditions, where y is the scaled space coordinate, and compared with the solutions obtained in the previous articles, the advantage of our ones is that φ(y) could represent an inhomogeneous medium to influence or control the shape of waves. Specific solutions, namely the first- and second-order exact solutions from the Pfaffian and extended Pfaffian solutions are discussed. Discussions indicate that the first- and second-order exact solutions are of the same shape as the corresponding one- and two-soliton solutions on the x−t or z−t plane but of the different shape on the y−t plane.