private void SetTextureUniforms(Shader shader, GenericMaterial previousMaterial) { int textureIndex = InitialTextureUnit; for (int i = 0; i < textureUniformNames.Count; i++) { var name = textureUniformNames[i]; var value = textureValues[i]; // If the same texture is bound to this texture unit, we don't need to set it again. bool shouldSkipTextureSet = previousMaterial != null && i < previousMaterial.textureUniformNames.Count && previousMaterial.textureUniformNames[i] == name && previousMaterial.textureValues[i] == value; if (!shouldSkipTextureSet) { shader.SetTexture(name, value, textureIndex); } // Even if the textures are the same, the bound sampler may be different. if (samplerValues[i] != null) { samplerValues[i].Bind(textureIndex); } textureIndex++; } }
public void SetMaterialUniforms(Shader shader, GenericMaterial previousMaterial) { // TODO: Rework default texture creation. if (defaultTextures == null) { defaultTextures = new Resources.DefaultTextures(); } if (genericMaterial == null) { genericMaterial = Material.CreateGenericMaterial(); } genericMaterial.SetShaderUniforms(shader, previousMaterial); if (uniformBlock == null) { uniformBlock = new UniformBlock(shader, "MaterialParams") { BlockBinding = 1 }; Material.AddMaterialParams(uniformBlock); } // This needs to be updated more than once. Material.AddDebugParams(uniformBlock); uniformBlock.BindBlock(shader); }
/// <summary> /// Sets uniform values for all the added uniform values. /// Redundant setting of texture uniform is skipped to improve performance. /// </summary> /// <param name="shader">The shader whose uniforms will be set</param> /// <param name="previousMaterial">The previous material used to set this shader's uniforms</param> public void SetShaderUniforms(Shader shader, GenericMaterial previousMaterial = null) { SetIntUniforms(shader); SetFloatUniforms(shader); SetVec2Uniforms(shader); SetVec3Uniforms(shader); SetVec4Uniforms(shader); SetMat4Uniforms(shader); SetTextureUniforms(shader, previousMaterial); }