public List <Material> GetMaterials() { List <Material> mats = new List <Material>(DAEMaterials.Count); for (int i = 0; i < DAEMaterials.Count; i++) { //phong var shdrst = new ShaderSettings(); var rddir = new RenderDirectives(); shdrst.HasSkeleton = true; shdrst.RecieveShadow = false; shdrst.AffectedByLight = true; rddir.CastShadow = false; rddir.HasEdges = true; shdrst.TextureDiffuse = true; shdrst.DiscardInvisible = true; shdrst.Ambient = true; var mat = new MaterialPMX(shdrst, rddir); mat.Name = DAEMaterials[i].Name; mat.UniformManager.Set("ambient_color", DAEMaterials[i].Ambient.Xyz); mat.SetTexture(DAEMaterials[i].DiffuseTexture, TextureType.Diffuse); mats.Add(mat); } return(mats); }
private MaterialPM(ShaderSettings shdrsett, RenderDirectives rdir, Shader shader) { ShaderSettings = shdrsett; RenderDirrectives = rdir; shaderProgram = shader; CreateShader(shaderProgram); }
static VisualComponent() { //create Quad mesh Vertex3D[] verts = new Vertex3D[] { new Vertex3D(new Vector2(0, 1), new Vector2(0, 0)), new Vertex3D(new Vector2(0, 0), new Vector2(0, 1)), new Vertex3D(new Vector2(1, 0), new Vector2(1, 1)), new Vertex3D(new Vector2(0, 1), new Vector2(0, 0)), new Vertex3D(new Vector2(1, 0), new Vector2(1, 1)), new Vertex3D(new Vector2(1, 1), new Vector2(1, 0)), }; defaultMesh = new Mesh(verts, new int[] { 0, 1, 2, 3, 4, 5 }); //load default material ShaderSettings ss = new ShaderSettings(); RenderDirectives rd = new RenderDirectives(); string path = "Toys.Resourses.shaders."; string vs = ShaderManager.ReadFromAssetStream(path + "UIElement.vsh"); string fs = ShaderManager.ReadFromAssetStream(path + "UIElement.fsh"); ss.TextureDiffuse = true; defaultMaterial = new MaterialCustom(ss, rd, vs, fs); defaultMaterial.Name = "Texture"; defaultTexture = Texture2D.LoadEmpty(); }
public MaterialCustom(ShaderSettings shdrsett, RenderDirectives rdir, string vertShader, string fragShader) : base() { ShaderSettings = shdrsett; RenderDirrectives = rdir; vs = vertShader; fs = fragShader; CreateShader(); }
void ReadMaterials() { reader.BaseStream.Position = 0x38; int matOffset = (int)reader.BaseStream.Position + reader.ReadInt32(); reader.BaseStream.Position = matOffset + 4; reader.BaseStream.Position += reader.ReadInt32(); int materialCount = reader.ReadInt32(); int[] offsets = new int[materialCount]; for (int i = 0; i < materialCount; i++) { offsets[i] = (int)reader.BaseStream.Position + reader.ReadInt32(); } foreach (var moffs in offsets) { reader.BaseStream.Position = moffs + 4; int MaterialNameTextOffset = (int)reader.BaseStream.Position + reader.ReadInt32(); reader.BaseStream.Position = MaterialNameTextOffset; string MaterialName = reader.readString(); reader.BaseStream.Position = moffs + 0x48; string MaterialFileName = reader.readString(); ShaderSettings sdrs = new ShaderSettings(); RenderDirectives rndd = new RenderDirectives(); sdrs.HasSkeleton = true; sdrs.TextureDiffuse = true; sdrs.TextureSpecular = true; sdrs.RecieveShadow = false; rndd.HasEdges = true; Material mat = new MaterialPM(sdrs, rndd); mat.Name = MaterialName; mat.Outline.EdgeScaler = 0.1f; materialTable.Add(MaterialName, mat); mat.UniformManager.Set("uv_scale", Vector4.One); //scale face shadow texture if (MaterialName.Contains("face")) { mat.UniformManager.Set("uv_scale", new Vector4(1, 1, 4, 4)); } try { ReadTexturesFromMaterial(MaterialName, mat); } catch (Exception e) { Console.WriteLine(e.Message); } } }
void ReadMaterial(Reader reader) { int materiaCount = reader.ReadInt32(); int offset = 0; mats = new Material[materiaCount]; for (int i = 0; i < materiaCount; i++) { ShaderSettings shdrs = new ShaderSettings(); RenderDirectives rndr = new RenderDirectives(); shdrs.HasSkeleton = true; shdrs.DiscardInvisible = true; shdrs.AffectedByLight = true; shdrs.DifuseColor = true; shdrs.Ambient = true; shdrs.SpecularColor = true; shdrs.TextureDiffuse = true; string name = reader.readString(); reader.readString(); //eng name Vector4 difColor = reader.readVector4(); //if (difColor.W == 0)//diffuse color // rndr.render = false; Vector3 specularColour = reader.readVector3(); //specular color float specularPower = reader.ReadSingle(); //specular //fix zero power bug if (specularPower == 0) { specularPower = 0.000001f; } Vector3 ambientColour = reader.readVector3(); //ambient color //setting values from flags var flags = new MaterialFlags(reader.ReadByte()); shdrs.RecieveShadow = flags.ReceiveShadow; shdrs.AffectedByLight = flags.ReceiveShadow; rndr.CastShadow = flags.CastShadow; rndr.HasEdges = flags.HasEdge; rndr.NoCull = flags.NoCull; var outln = new Outline(); outln.EdgeColour = reader.readVector4(); outln.EdgeScaler = reader.ReadSingle() * 0.03f; int difTexIndex = reader.readVal(header.GetTextureIndexSize); //sphericar texture for false light sources effect int envTexIndex = reader.readVal(header.GetTextureIndexSize); int envBlend = reader.ReadByte(); shdrs.EnvType = (EnvironmentMode)envBlend; Texture2D envTex = empty; if (envTexIndex != 255 && envBlend > 0) { if (textures[envTexIndex].Name != "def") { envTex = textures[envTexIndex]; } else { shdrs.EnvType = 0; } } else { shdrs.EnvType = 0; } byte toonType = reader.ReadByte(); Texture2D toon = empty; if (toonType == 0) { int text = reader.readVal(header.GetTextureIndexSize); if (text != 255) { shdrs.ToonShadow = true; textures[text].WrapMode = TextureWrapMode.ClampToEdge; toon = textures[text]; //toon.GetTextureType = TextureType.toon; } else { //disable shadowing if no toon texture shdrs.AffectedByLight = false; shdrs.RecieveShadow = false; } } else { byte toontex = reader.ReadByte(); toontex++; string texturePath = String.Format("Toys.Resourses.textures.PMX.toon{0}.bmp", toontex.ToString().PadLeft(2, '0')); Texture2D toonTex = ResourcesManager.GetResourse <Texture2D>(texturePath); if (toonTex == null) { var assembly = System.Reflection.IntrospectionExtensions.GetTypeInfo(typeof(Texture2D)).Assembly; using (Bitmap pic = new Bitmap(assembly.GetManifestResourceStream(texturePath))) { toonTex = new Texture2D(pic, TextureType.Toon, texturePath); } } toon = toonTex; shdrs.ToonShadow = true; } reader.readString(); int count = reader.ReadInt32(); Texture2D tex = empty; if (difTexIndex != 255) { tex = textures[difTexIndex]; } var mat = new MaterialPMX(shdrs, rndr); mat.Name = name; mat.Outline = outln; mat.SetTexture(tex, TextureType.Diffuse); mat.SetTexture(toon, TextureType.Toon); mat.SetTexture(envTex, TextureType.Sphere); mat.SpecularColour = specularColour; mat.Specular = specularPower; mat.DiffuseColor = difColor; mat.AmbientColour = ambientColour; mat.UniformManager.Set("specular_color", specularColour); mat.UniformManager.Set("ambient_color", ambientColour); mat.UniformManager.Set("specular_power", specularPower); mat.UniformManager.Set("diffuse_color", difColor); if (mat.DiffuseColor.W < 0.001f) { mat.RenderDirrectives.IsRendered = false; } //skip empty materials if (count == 0) { mat.RenderDirrectives.IsRendered = false; } mat.Offset = offset; mat.Count = count; mats[i] = mat; offset += count; } }
public MaterialPMX(ShaderSettings shdrsett, RenderDirectives rdir) : base() { ShaderSettings = shdrsett; RenderDirrectives = rdir; CreateShader(); }
void ReadMaterial(Reader reader) { int materialCount = reader.ReadInt32(); materialToonTable = new int[materialCount]; mats = new Material[materialCount]; int offset = 0; for (int i = 0; i < materialCount; i++) { var shdrs = new ShaderSettings(); var difColor = reader.readVector4(); var specularPower = reader.ReadSingle(); var specularColour = reader.readVector3(); var ambientColour = reader.readVector3(); materialToonTable[i] = reader.ReadByte(); bool hasEdge = (reader.ReadByte() > 0); int vertCount = reader.ReadInt32(); EnvironmentMode envMode = EnvironmentMode.None; Texture2D diffuseTexture = null; Texture2D sphereTexture = null; var pathList = reader.readStringLength(20); if (pathList.Contains('*')) { var texturePath = pathList.Split('*'); shdrs.EnvType = EnvironmentMode.None; for (int n = 0; n < texturePath.Length; n++) { string path = texturePath[n]; EnvironmentMode sphereMode = GetEnvType(path); if (sphereMode != 0) { sphereTexture = GetTexture(path); envMode = sphereMode; } else { diffuseTexture = GetTexture(path); } } } var outln = new Outline(); outln.EdgeScaler = 0.1f; shdrs.HasSkeleton = true; shdrs.DiscardInvisible = true; shdrs.AffectedByLight = true; shdrs.DifuseColor = true; shdrs.Ambient = true; shdrs.SpecularColor = true; shdrs.TextureDiffuse = true; shdrs.RecieveShadow = true; shdrs.EnvType = envMode; var rndr = new RenderDirectives(); rndr.HasEdges = hasEdge; var mat = new MaterialPMX(shdrs, rndr); mat.Name = string.Format("Material {0}", i); mat.Outline = outln; if (diffuseTexture) { mat.SetTexture(diffuseTexture, TextureType.Diffuse); } //mat.SetTexture(toon, TextureType.Toon); if (sphereTexture) { mat.SetTexture(sphereTexture, TextureType.Sphere); } mat.SpecularColour = specularColour; mat.Specular = specularPower; mat.DiffuseColor = difColor; mat.AmbientColour = ambientColour; mat.UniformManager.Set("specular_color", specularColour); mat.UniformManager.Set("ambient_color", ambientColour); mat.UniformManager.Set("specular_power", specularPower); mat.UniformManager.Set("diffuse_color", difColor); if (mat.DiffuseColor.W < 0.001f) { mat.RenderDirrectives.IsRendered = false; } //skip empty materials if (vertCount == 0) { mat.RenderDirrectives.IsRendered = false; } mat.Offset = offset; mat.Count = vertCount; mats[i] = mat; offset += vertCount; } }