/// <summary> /// Set parameters, this overload sets all material parameters too. /// </summary> public virtual void SetParametersOptimized(Material setMat) { // No need to set world matrix, will be done later in mesh rendering // in the MeshRenderManager. All the rest is set with help of the // SetParametersOptimizedGeneral above. // Only update ambient, diffuse, specular and the textures, the rest // will not change for a material change in MeshRenderManager. ambientColor.SetValue(setMat.ambientColor.ToVector4()); diffuseColor.SetValue(setMat.diffuseColor.ToVector4()); specularColor.SetValue(setMat.specularColor.ToVector4()); if (setMat.diffuseTexture != null) diffuseTexture.SetValue(setMat.diffuseTexture.XnaTexture); if (setMat.normalTexture != null) normalTexture.SetValue(setMat.normalTexture.XnaTexture); }
/// <summary> /// Update parameters /// </summary> public override void SetParameters(Material setMat) { // Can't set parameters if loading failed! if (xnaEffect == null) return; /*already set, don't change! shadowNearPlane = 1.0f; shadowFarPlane = 28; virtualLightDistance = 24; virtualVisibleRange = 23.5f; */ if (BaseGame.CanUsePS20) { compareDepthBias = 0.00075f;//0.00025f; shadowMapDepthBiasValue = 0.00075f;//0.00025f; } // if (BaseGame.CanUsePS20) else { // Ps 1.1 isn't as percise! compareDepthBias = 0.0025f; shadowMapDepthBiasValue = 0.002f; } // else base.SetParameters(setMat); // Set all extra parameters for this shader depthBias.SetValue(compareDepthBias); shadowMapDepthBias.SetValue(shadowMapDepthBiasValue); shadowMapTexelSize.SetValue( new Vector2(texelWidth, texelHeight)); nearPlane.SetValue(shadowNearPlane); farPlane.SetValue(shadowFarPlane); }
/// <summary> /// Set parameters, this overload sets all material parameters too. /// </summary> public virtual void SetParameters(Material setMat) { if (worldViewProj != null) worldViewProj.SetValue(BaseGame.WorldViewProjectionMatrix); if (viewProj != null) viewProj.SetValue(BaseGame.ViewProjectionMatrix); if (world != null) world.SetValue(BaseGame.WorldMatrix); if (viewInverse != null) viewInverse.SetValue(BaseGame.InverseViewMatrix); if (lightDir != null) lightDir.SetValue(BaseGame.LightDirection); // Set all material properties if (setMat != null) { AmbientColor = setMat.ambientColor; DiffuseColor = setMat.diffuseColor; SpecularColor = setMat.specularColor; SpecularPower = setMat.specularPower; DiffuseTexture = setMat.diffuseTexture; NormalTexture = setMat.normalTexture; } // if (setMat) }
/// <summary> /// Render /// </summary> /// <param name="setMat">Set matrix</param> /// <param name="techniqueName">Technique name</param> /// <param name="renderDelegate">Render delegate</param> public void Render(Material setMat, string techniqueName, BaseGame.RenderDelegate renderDelegate) { SetParameters(setMat); /*will become important later in the book. // Can we do the requested technique? // For graphic cards not supporting ps2.0, fall back to ps1.1 if (BaseGame.CanUsePS20 == false && techniqueName.EndsWith("20")) // Use same technique without the 20 ending! techniqueName = techniqueName.Substring(0, techniqueName.Length - 2); */ // Start shader xnaEffect.CurrentTechnique = xnaEffect.Techniques[techniqueName]; xnaEffect.Begin(SaveStateMode.None); // Render all passes (usually just one) //foreach (EffectPass pass in effect.CurrentTechnique.Passes) for (int num = 0; num < xnaEffect.CurrentTechnique.Passes.Count; num++) { EffectPass pass = xnaEffect.CurrentTechnique.Passes[num]; pass.Begin(); renderDelegate(); pass.End(); } // foreach (pass) // End shader xnaEffect.End(); }
/// <summary> /// Add model mesh part with the used effect to our sortedMeshes list. /// Neither the model mesh part nor the effect is directly used, /// we will extract all data from the model and only render the /// index and vertex buffers later. /// The model mesh part must use the TangentVertex format. /// </summary> /// <param name="vertexBuffer">Vertex buffer</param> /// <param name="indexBuffer">Index buffer</param> /// <param name="part">Part</param> /// <param name="effect">Effect</param> /// <returns>Renderable mesh</returns> public RenderableMesh Add(VertexBuffer vertexBuffer, IndexBuffer indexBuffer, ModelMeshPart part, Effect effect) { string techniqueName = effect.CurrentTechnique.Name; // Does this technique already exists? MeshesPerMaterialPerTechniques foundList = null; //obs: foreach (MeshesPerMaterialPerTechniques list in sortedMeshes) for (int listNum = 0; listNum < sortedMeshes.Count; listNum++) { MeshesPerMaterialPerTechniques list = sortedMeshes[listNum]; if (list.technique != null && list.technique.Name == techniqueName) { foundList = list; break; } // if (list.technique.Name) } // for (listNum) // Did not found list? Create new one if (foundList == null) { EffectTechnique technique = ShaderEffect.normalMapping.GetTechnique(techniqueName); // Make sure we always have a valid technique if (technique == null) { if (BaseGame.CanUsePS20) techniqueName = "Diffuse20";//"Specular20"; else techniqueName = "Diffuse";//"Specular"; technique = ShaderEffect.normalMapping.GetTechnique(techniqueName); } // if foundList = new MeshesPerMaterialPerTechniques(technique); sortedMeshes.Add(foundList); } // if (foundList) // Create new material from the current effect parameters. // This will create duplicate materials if the same material is used // multiple times, we check this later. Material material = new Material(effect); // Search for material inside foundList. //obs: foreach (MeshesPerMaterial innerList in foundList.meshesPerMaterials) for (int innerListNum = 0; innerListNum < foundList.meshesPerMaterials.Count; innerListNum++) { MeshesPerMaterial innerList = foundList.meshesPerMaterials[innerListNum]; // Check if this is the same material and we can use it instead. // For our purposes it is sufficiant if we check textures and colors. if (innerList.material.diffuseTexture == material.diffuseTexture && innerList.material.normalTexture == material.normalTexture && innerList.material.ambientColor == material.ambientColor && innerList.material.diffuseColor == material.diffuseColor && innerList.material.specularColor == material.specularColor && innerList.material.specularPower == material.specularPower) { // Reuse this material and quit this search material = innerList.material; break; } // if (innerList.material.diffuseTexture) } // foreach (innerList) // Build new RenderableMesh object RenderableMesh mesh = new RenderableMesh( vertexBuffer, indexBuffer, material, foundList.technique, part.VertexDeclaration, part.StreamOffset, part.VertexStride, part.BaseVertex, part.NumVertices, part.StartIndex, part.PrimitiveCount); foundList.Add(mesh); return mesh; }
/// <summary> /// Create renderable mesh /// </summary> /// <param name="setVertexBuffer">Set vertex buffer</param> /// <param name="setIndexBuffer">Set index buffer</param> /// <param name="setMaterial">Set material</param> /// <param name="setUsedTechnique">Set used technique</param> /// <param name="setWorldParameter">Set world parameter</param> /// <param name="setVertexDeclaration">Set vertex declaration</param> /// <param name="setStreamOffset">Set stream offset</param> /// <param name="setVertexStride">Set vertex stride</param> /// <param name="setBaseVertex">Set base vertex</param> /// <param name="setNumVertices">Set number vertices</param> /// <param name="setStartIndex">Set start index</param> /// <param name="setPrimitiveCount">Set primitive count</param> public RenderableMesh(VertexBuffer setVertexBuffer, IndexBuffer setIndexBuffer, Material setMaterial, EffectTechnique setUsedTechnique, //EffectParameter setWorldParameter, VertexDeclaration setVertexDeclaration, int setStreamOffset, int setVertexStride, int setBaseVertex, int setNumVertices, int setStartIndex, int setPrimitiveCount) { vertexBuffer = setVertexBuffer; indexBuffer = setIndexBuffer; material = setMaterial; usedTechnique = setUsedTechnique; vertexDeclaration = setVertexDeclaration; streamOffset = setStreamOffset; vertexStride = setVertexStride; baseVertex = setBaseVertex; numVertices = setNumVertices; startIndex = setStartIndex; primitiveCount = setPrimitiveCount; }
/// <summary> /// Create meshes per material for the setMaterial. /// </summary> /// <param name="setMaterial">Set material</param> public MeshesPerMaterial(Material setMaterial) { material = setMaterial; }