The real and imaginary parts of the complex refractive index of SixNyHz have been calculated from first principles. Optical spectra for reflectivity, absorption coefficient, energy-loss function (ELF), and refractive index were obtained. The results for Si3N4 are in agreement with the available theoretical and experimental results. To understand the electron energy loss mechanism in Si-rich silicon nitride, the influence of the Si/N ratio, the positions of the access Si atoms, and H in and on the surface of the ELF have been investigated. It has been found that all defects, such as dangling bonds in the bulk and surfaces, increase the intensity of the ELF in the low energy range (below 10 eV). H in the bulk and on the surface has a healing effect, which can reduce the intensity of the loss peaks by saturating the dangling bonds. Electronic structure analysis has confirmed the origin of the changes in the ELF. It has demonstrated that the changes in ELF are not only affected by the composition but also by the microstructures of the materials. The results can be used to tailor the optical properties, in this case the ELF of Si-rich Si3N4, which is essential for secondary electron emission applications.