diff options
Diffstat (limited to 'NW4RTools/ColladaWriter.cs')
-rw-r--r-- | NW4RTools/ColladaWriter.cs | 122 |
1 files changed, 120 insertions, 2 deletions
diff --git a/NW4RTools/ColladaWriter.cs b/NW4RTools/ColladaWriter.cs index b7c039a..f0f5b19 100644 --- a/NW4RTools/ColladaWriter.cs +++ b/NW4RTools/ColladaWriter.cs @@ -69,7 +69,7 @@ namespace NW4RTools { img.name = "Texture-" + texName; img.id = img.name; - img.Item = texName + ".png"; + img.Item = "./images/" + texName + ".png"; ImageList.Add(img); } @@ -193,7 +193,7 @@ namespace NW4RTools { } // Apply shapes to nodes - foreach (var kv in CurrentModel.Shapes) { + /*foreach (var kv in CurrentModel.Shapes) { Shape shape = kv.Value; Node origNode = CurrentModel.Nodes[CurrentModel.MatrixIDtoNodeID[shape.MatrixID]]; node cNode = NodeDefs[origNode]; @@ -211,8 +211,107 @@ namespace NW4RTools { cNode.instance_geometry = geoArrayCopy; // TODO: Add material handling, I'll probably have to parse DrawOpa/DrawXlu for this... + }*/ + + // WARNING: THIS NEEDS REFACTORING + + int drawID = 0; + + foreach (var insn in CurrentModel.Bytecode["DrawOpa"].Instructions) { + if (insn is ByteCode.DrawShapeInstruction) { + var dsInsn = insn as ByteCode.DrawShapeInstruction; + + Shape shape = CurrentModel.Shapes[dsInsn.ShapeID]; + string shapeName = CurrentModel.Shapes.GetKeyForIndex(dsInsn.ShapeID); + + Node origNode = CurrentModel.Nodes[dsInsn.NodeID]; + node cNode = NodeDefs[origNode]; + + Material mat = CurrentModel.Materials[dsInsn.MaterialID]; + string matName = CurrentModel.Materials.GetKeyForIndex(dsInsn.MaterialID); + + var newGeoEntry = new instance_geometry(); + newGeoEntry.name = String.Format("DrawOpa{0}-{1}", drawID, shapeName); + newGeoEntry.url = String.Format("#{0}-lib", shapeName); + + // now add the material + var bindMaterial = newGeoEntry.bind_material = new bind_material(); + bindMaterial.technique_common = new instance_material[1]; + var matTechnique = bindMaterial.technique_common[0] = new instance_material(); + + // constant marker so that I don't have to set a unique material name in each primitive + // it doesn't matter, since each geometry instance only uses one material anyway + matTechnique.symbol = "NW4R_MATERIAL"; + matTechnique.target = "#Material-" + matName; + + matTechnique.bind_vertex_input = new instance_materialBind_vertex_input[1]; + matTechnique.bind_vertex_input[0] = new instance_materialBind_vertex_input(); + matTechnique.bind_vertex_input[0].semantic = "CHANNEL1"; + matTechnique.bind_vertex_input[0].input_semantic = "TEXCOORD"; + matTechnique.bind_vertex_input[0].input_set = 0; + + // ok, now add the instance_geometry into the node + instance_geometry[] geoArrayCopy = cNode.instance_geometry; + if (geoArrayCopy == null) + geoArrayCopy = new instance_geometry[1]; + else + Array.Resize<instance_geometry>(ref geoArrayCopy, geoArrayCopy.Length + 1); + geoArrayCopy[geoArrayCopy.Length - 1] = newGeoEntry; + cNode.instance_geometry = geoArrayCopy; + + drawID++; + } + } + + drawID = 0; + + foreach (var insn in CurrentModel.Bytecode["DrawXlu"].Instructions) { + if (insn is ByteCode.DrawShapeInstruction) { + var dsInsn = insn as ByteCode.DrawShapeInstruction; + + Shape shape = CurrentModel.Shapes[dsInsn.ShapeID]; + string shapeName = CurrentModel.Shapes.GetKeyForIndex(dsInsn.ShapeID); + + Node origNode = CurrentModel.Nodes[dsInsn.NodeID]; + node cNode = NodeDefs[origNode]; + + Material mat = CurrentModel.Materials[dsInsn.MaterialID]; + string matName = CurrentModel.Materials.GetKeyForIndex(dsInsn.MaterialID); + + var newGeoEntry = new instance_geometry(); + newGeoEntry.name = String.Format("DrawXlu{0}-{1}", drawID, shapeName); + newGeoEntry.url = String.Format("#{0}-lib", shapeName); + + // now add the material + var bindMaterial = newGeoEntry.bind_material = new bind_material(); + bindMaterial.technique_common = new instance_material[1]; + var matTechnique = bindMaterial.technique_common[0] = new instance_material(); + + // constant marker so that I don't have to set a unique material name in each primitive + // it doesn't matter, since each geometry instance only uses one material anyway + matTechnique.symbol = "NW4R_MATERIAL"; + matTechnique.target = "#Material-" + matName; + + matTechnique.bind_vertex_input = new instance_materialBind_vertex_input[1]; + matTechnique.bind_vertex_input[0] = new instance_materialBind_vertex_input(); + matTechnique.bind_vertex_input[0].semantic = "CHANNEL1"; + matTechnique.bind_vertex_input[0].input_semantic = "TEXCOORD"; + matTechnique.bind_vertex_input[0].input_set = 0; + + // ok, now add the instance_geometry into the node + instance_geometry[] geoArrayCopy = cNode.instance_geometry; + if (geoArrayCopy == null) + geoArrayCopy = new instance_geometry[1]; + else + Array.Resize<instance_geometry>(ref geoArrayCopy, geoArrayCopy.Length + 1); + geoArrayCopy[geoArrayCopy.Length - 1] = newGeoEntry; + cNode.instance_geometry = geoArrayCopy; + + drawID++; + } } + /*foreach (var kv in CurrentModel.Shapes) { var thisNode = new node(); @@ -277,10 +376,27 @@ namespace NW4RTools { sampler2d.source = "Surface-" + mat.TextureInfos[0].TextureName; + samplerParam.sid = "Sampler-" + mat.TextureInfos[0].TextureName; samplerParam.ItemElementName = ItemChoiceType.sampler2D; samplerParam.Item = sampler2d; profile.Items[1] = samplerParam; + // now make a technique + // should I really use blinn...? + profile.technique = new effectFx_profile_abstractProfile_COMMONTechnique(); + profile.technique.sid = "common"; + + var pShader = new effectFx_profile_abstractProfile_COMMONTechniquePhong(); + pShader.diffuse = new common_color_or_texture_type(); + + var diffuseTex = new common_color_or_texture_typeTexture(); + diffuseTex.texture = "Sampler-" + mat.TextureInfos[0].TextureName; + diffuseTex.texcoord = "CHANNEL1"; + + pShader.diffuse.Item = diffuseTex; + + profile.technique.Item = pShader; + return eff; } @@ -529,6 +645,7 @@ namespace NW4RTools { switch (prim) { case PrimitiveType.Triangles: var pTri = new triangles(); + pTri.material = "NW4R_MATERIAL"; pTri.count = (ulong)(vtxCount / 3); // should be 1? dunno pTri.input = inputArray; @@ -567,6 +684,7 @@ namespace NW4RTools { // If any tristrips were found, add them! if (triStrips.Count > 0) { var pTriStrips = new tristrips(); + pTriStrips.material = "NW4R_MATERIAL"; pTriStrips.input = inputArray; pTriStrips.count = (ulong)triStrips.Count; pTriStrips.p = triStrips.ToArray(); |