public void NewShader() { MDL0Node model = ((MDL0Node)_resource); if (model._shadGroup == null) { MDL0GroupNode g = model._shadGroup; if (g == null) { model.AddChild(g = new MDL0GroupNode(MDLResourceType.Shaders), true); model._shadGroup = g; model._shadList = g.Children; } } if (model._shadList != null && model._matList != null && model._shadList.Count < model._matList.Count) { MDL0ShaderNode shader = new MDL0ShaderNode(); model._shadGroup.AddChild(shader); shader.Default(); shader.Rebuild(true); BaseWrapper b = FindResource(shader, true); if (b != null) { b.EnsureVisible(); } } }
public MDL0ShaderNode NewShader() { MDL0Node model = (MDL0Node)_resource; if (model._shadGroup == null) { MDL0GroupNode g = model._shadGroup; if (g == null) { model.AddChild(g = new MDL0GroupNode(MDLResourceType.Shaders), true); model._shadGroup = g; model._shadList = g.Children; } } if (model._shadList != null && model._matList != null && model._shadList.Count < model._matList.Count) { MDL0ShaderNode shader = new MDL0ShaderNode(); model._shadGroup.AddChild(shader); shader.Default(); shader.Rebuild(true); BaseWrapper b = FindResource(shader, true); b?.EnsureVisible(); return(shader); } MessageBox.Show("Shader could not be added. Make sure that you do not have more shaders than materials", "Error"); return(null); }
public IModel ImportModel(string filePath, ImportType type) { IModel model = null; ModelType = type; BoneType = ModelType == ImportType.MDL0 ? typeof(MDL0BoneNode) : null; //TransformMatrix = Matrix.TransformMatrix(_importOptions._modifyScale, _importOptions._modifyRotation, new Vector3()); switch (type) { case ImportType.MDL0: MDL0Node m = new MDL0Node() { _name = Path.GetFileNameWithoutExtension(filePath), _version = _importOptions._modelVersion.Clamp(8, 11) }; if (_importOptions._setOrigPath) { m._originalPath = filePath; } m.BeginImport(); model = m; break; } CurrentModel = model; Error = "There was a problem reading the model."; using (DecoderShell shell = DecoderShell.Import(filePath)) try { Error = "There was a problem reading texture entries."; //Extract images, removing duplicates foreach (ImageEntry img in shell._images) { string name = img._path != null? Path.GetFileNameWithoutExtension(img._path) : img._name != null ? img._name : img._id; switch (type) { case ImportType.MDL0: img._node = ((MDL0Node)model).FindOrCreateTexture(name); break; } } Error = "There was a problem creating a default shader."; //Create a shader ResourceNode shader = null; switch (type) { case ImportType.MDL0: MDL0Node m = (MDL0Node)model; MDL0ShaderNode shadNode = new MDL0ShaderNode() { _ref0 = 0, _ref1 = -1, _ref2 = -1, _ref3 = -1, _ref4 = -1, _ref5 = -1, _ref6 = -1, _ref7 = -1, }; shadNode._parent = m._shadGroup; m._shadList.Add(shadNode); switch (_importOptions._mdlType) { case ImportOptions.MDLType.Character: for (int i = 0; i < 3; i++) { switch (i) { case 0: shadNode.AddChild(new MDL0TEVStageNode(0x28F8AF, 0x08F2F0, 0, TevKColorSel.ConstantColor0_RGB, TevKAlphaSel.ConstantColor0_Alpha, TexMapID.TexMap0, TexCoordID.TexCoord0, ColorSelChan.LightChannel0, true)); break; case 1: shadNode.AddChild(new MDL0TEVStageNode(0x08FEB0, 0x081FF0, 0, TevKColorSel.ConstantColor1_RGB, TevKAlphaSel.ConstantColor0_Alpha, TexMapID.TexMap7, TexCoordID.TexCoord7, ColorSelChan.LightChannel0, false)); break; case 2: shadNode.AddChild(new MDL0TEVStageNode(0x0806EF, 0x081FF0, 0, TevKColorSel.ConstantColor0_RGB, TevKAlphaSel.ConstantColor0_Alpha, TexMapID.TexMap7, TexCoordID.TexCoord7, ColorSelChan.Zero, false)); break; } } break; case ImportOptions.MDLType.Stage: shadNode.AddChild(new MDL0TEVStageNode(0x28F8AF, 0x08F2F0, 0, TevKColorSel.ConstantColor0_RGB, TevKAlphaSel.ConstantColor0_Alpha, TexMapID.TexMap0, TexCoordID.TexCoord0, ColorSelChan.LightChannel0, true)); break; } shader = shadNode; break; } Error = "There was a problem extracting materials."; //Extract materials foreach (MaterialEntry mat in shell._materials) { List <ImageEntry> imgEntries = new List <ImageEntry>(); //Find effect if (mat._effect != null) { foreach (EffectEntry eff in shell._effects) { if (eff._id == mat._effect) //Attach textures and effects to material { if (eff._shader != null) { foreach (LightEffectEntry l in eff._shader._effects) { if (l._type == LightEffectType.diffuse && l._texture != null) { string path = l._texture; foreach (EffectNewParam p in eff._newParams) { if (p._sid == l._texture) { path = p._sampler2D._url; if (!String.IsNullOrEmpty(p._sampler2D._source)) { foreach (EffectNewParam p2 in eff._newParams) { if (p2._sid == p._sampler2D._source) { path = p2._path; } } } } } foreach (ImageEntry img in shell._images) { if (img._id == path) { imgEntries.Add(img); break; } } } } } } } } switch (type) { case ImportType.MDL0: MDL0MaterialNode matNode = new MDL0MaterialNode(); MDL0Node m = (MDL0Node)model; matNode._parent = m._matGroup; m._matList.Add(matNode); matNode._name = mat._name != null ? mat._name : mat._id; matNode.ShaderNode = shader as MDL0ShaderNode; mat._node = matNode; matNode._cull = _importOptions._culling; foreach (ImageEntry img in imgEntries) { MDL0MaterialRefNode mr = new MDL0MaterialRefNode(); (mr._texture = img._node as MDL0TextureNode)._references.Add(mr); mr._name = mr._texture.Name; matNode._children.Add(mr); mr._parent = matNode; mr._minFltr = mr._magFltr = 1; mr._uWrap = mr._vWrap = (int)_importOptions._wrap; } break; } } Say("Extracting scenes..."); List <ObjectInfo> _objects = new List <ObjectInfo>(); ResourceNode boneGroup = null; switch (type) { case ImportType.MDL0: boneGroup = ((MDL0Node)model)._boneGroup; break; } //Extract bones and objects and create bone tree foreach (SceneEntry scene in shell._scenes) { foreach (NodeEntry node in scene._nodes) { EnumNode(node, boneGroup, scene, model, shell, _objects, TransformMatrix, Matrix.Identity); } } //Add root bone if there are no bones if (boneGroup.Children.Count == 0) { switch (type) { case ImportType.MDL0: MDL0BoneNode bone = new MDL0BoneNode(); bone.Scale = new Vector3(1); bone.RecalcBindState(false, false); bone._name = "TopN"; TempRootBone = bone; break; } } //Create objects foreach (ObjectInfo obj in _objects) { NodeEntry node = obj._node; string w = obj._weighted ? "" : "un"; string w2 = obj._weighted ? "\nOne or more vertices may not be weighted correctly." : ""; string n = node._name != null ? node._name : node._id; Error = String.Format("There was a problem decoding {0}weighted primitives for the object {1}.{2}", w, n, w2); Say(String.Format("Decoding {0}weighted primitives for {1}...", w, n)); obj.Initialize(model, shell); } //Finish switch (type) { case ImportType.MDL0: MDL0Node mdl0 = (MDL0Node)model; if (TempRootBone != null) { mdl0._boneGroup._children.Add(TempRootBone); TempRootBone._parent = mdl0._boneGroup; } FinishMDL0(mdl0); break; } } #if !DEBUG catch (Exception x) { MessageBox.Show("Cannot continue importing this model.\n" + Error + "\n\nException:\n" + x.ToString()); model = null; Close(); } #endif finally { //Clean up the mess we've made GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); } CurrentModel = null; Error = null; return(model); }
public static string GeneratePixelShader(MDL0ObjectNode obj) { Reset(); MDL0MaterialNode mat = obj.UsableMaterialNode; MDL0ShaderNode shader = mat.ShaderNode; //w("#version 330\n"); foreach (MDL0MaterialRefNode r in mat.Children) { w("uniform sampler2D Texture{0};", r.Index); } for (int i = 0; i < obj._uvSet.Length; i++) { if (obj._uvSet[i] != null) { w("in vec2 UVSet{0};", i); } } for (int i = 0; i < obj._colorSet.Length; i++) { if (obj._colorSet[i] != null) { w("in vec2 ColorSet{0};", i); } } w("out vec4 out_color;\n"); //w("uniform vec4 C1Amb;\n"); //w("uniform vec4 C2Amb;\n"); //w("uniform vec4 C1Mat;\n"); //w("uniform vec4 C2Mat;\n"); Start(); foreach (MDL0MaterialRefNode r in mat.Children) { if (r.TextureCoordId >= 0) { w("vec4 tex{0}col = texture2D(Texture{0}, UVSet{1}.st);\n", r.Index, r.TextureCoordId); } } //w("vec4 creg0 = vec4(0.0, 0.0, 0.0, 0.0);\n"); //w("vec4 creg1 = vec4(0.0, 0.0, 0.0, 0.0);\n"); //w("vec4 creg2 = vec4(0.0, 0.0, 0.0, 0.0);\n"); //w("vec4 prev = vec4(0.0, 0.0, 0.0, 0.0);\n"); //foreach (TEVStage stage in shader.Children) // if (stage.Index < mat.ActiveShaderStages) // w(stage.Write(mat, obj)); // else break; //if (shader._stages > 0) //{ // w("prev.rgb = {0};\n", tevCOutputTable[(int)((TEVStage)shader.Children[shader.Children.Count - 1]).ColorRegister]); // w("prev.a = {0};\n", tevAOutputTable[(int)((TEVStage)shader.Children[shader.Children.Count - 1]).AlphaRegister]); //} w("out_color = tex0col;"); Finish(); return(tempShader); }
public static string GenerateVertexShader(MDL0ObjectNode obj) { Reset(); MDL0MaterialNode mat = obj.UsableMaterialNode; MDL0ShaderNode shader = mat.ShaderNode; //w("#version 330\n"); bool[] data = new bool[12]; for (int i = 0; i < 12; i++) { data[i] = obj._manager._faceData[i] != null; } if (data[0]) { w("in vec3 Position;"); } if (data[1]) { w("in vec3 Normal;"); } for (int i = 0; i < 2; i++) { if (data[i + 2]) { w("in vec4 Color{0};", i); } } for (int i = 0; i < 8; i++) { if (data[i + 4]) { w("in vec2 UV{0};", i); } } w("uniform mat4 projection_matrix;"); w("uniform mat4 modelview_matrix;"); for (int i = 0; i < obj._uvSet.Length; i++) { if (obj._uvSet[i] != null) { w("out vec2 UVSet{0};", i); } } for (int i = 0; i < obj._colorSet.Length; i++) { if (obj._colorSet[i] != null) { w("out vec4 ColorSet{0};", i); } } Start(); w("gl_Position = projection_matrix * modelview_matrix * vec4(Position, 1.0);"); //w("gl_Normal = vec4(Normal, 1.0);\n"); for (int i = 0; i < obj._uvSet.Length; i++) { if (obj._uvSet[i] != null) { w("UVSet{0} = UV{0};", i); } } for (int i = 0; i < obj._colorSet.Length; i++) { if (obj._colorSet[i] != null) { w("ColorSet{0} = Color{0};", i); } } Finish(); return(tempShader); }
public override ResourceNode Duplicate(bool changeName) { if (_resource.Parent == null) { return(null); } string tempPath = Path.GetTempFileName(); _resource.Export(tempPath); // Initialize node in a way that will not cause crashes ResourceNode rNode2; switch (Resource.ResourceFileType) { case ResourceType.MDL0Color: rNode2 = new MDL0ColorNode(); break; case ResourceType.MDL0UV: rNode2 = new MDL0UVNode(); break; case ResourceType.MDL0Material: rNode2 = new MDL0MaterialNode(); break; case ResourceType.MDL0Shader: rNode2 = new MDL0ShaderNode(); break; case ResourceType.MDL0Texture: rNode2 = new MDL0TextureNode(); break; case ResourceType.MDL0Normal: rNode2 = new MDL0NormalNode(); break; case ResourceType.MDL0Bone: rNode2 = new MDL0BoneNode(); break; case ResourceType.MDL0Vertex: rNode2 = new MDL0VertexNode(); break; default: throw new NotSupportedException("Unsupported type for MDL0 Duplication"); } rNode2._name = _resource.Name; rNode2.Replace(tempPath); if (rNode2 == null) { MessageBox.Show("The node could not be duplicated correctly.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } // Remove the node from the parent temporarily rNode2.Remove(); // Set the name programatically (based on Windows' implementation) int index = _resource.Index; int n = 0; if (changeName) { while (_resource.Parent.FindChildrenByName(rNode2.Name).Length >= 1) { // Get the last index of the last duplicated node in order to place it after that one index = Math.Max(index, _resource.Parent.FindChildrenByName(rNode2.Name).Last().Index); // Set the name based on the number of duplicate nodes found rNode2.Name = $"{_resource.Name} ({++n})"; } } // Place the node in the same containing parent, after the last duplicated node. _resource.Parent.InsertChild(rNode2, true, index + 1); // Update name again in order to refresh things that need refreshing when name is updated rNode2.OnRenamed(); return(rNode2); }