/*  Old version, stores columns of the weight table
         * public void AddWeightData(SceneNodeContainer snc, PolygonObject polyOb, CAWeightTag weightTag, IEnumerable<int> range)
         * {
         *  if (weightTag == null || polyOb == null)
         *      return;
         *
         *  List<JointWeightColumn> weightMap = new List<JointWeightColumn>();
         *  List<float4x4> bindingMatrices = new List<float4x4>();
         *  for (int i = 0; i < weightTag.GetJointCount(); i++)
         *  {
         *      JointWeightColumn jointWeightWrapper = new JointWeightColumn()
         *      {
         *          JointWeights = new List<double>()
         *      };
         *
         *      foreach (int j in range)
         *      {
         *          using (CPolygon poly = polyOb.GetPolygonAt(j))
         *          {
         *              jointWeightWrapper.JointWeights.Add(weightTag.GetWeight(i, poly.a));
         *              jointWeightWrapper.JointWeights.Add(weightTag.GetWeight(i, poly.b));
         *              jointWeightWrapper.JointWeights.Add(weightTag.GetWeight(i, poly.c));
         *
         *              double3 c = polyOb.GetPointAt(poly.c);
         *              double3 d = polyOb.GetPointAt(poly.d);
         *
         *              if (c != d)
         *              {
         *                  // The Polyogon is not a triangle, but a quad. Add the second triangle.
         *                  jointWeightWrapper.JointWeights.Add(weightTag.GetWeight(i, poly.d));
         *              }
         *          }
         *      }
         *      weightMap.Add(jointWeightWrapper);
         *
         *      // Add Binding Matrix
         *      JointRestState jointRestState = weightTag.GetJointRestState(i);
         *      float4x4 mat = (float4x4)(jointRestState.m_oMi * weightTag.GetGeomMg());
         *      bindingMatrices.Add(mat);
         *  }
         *
         *  _weightObjects.Add(new WeightObject()
         *  {
         *      SceneNodeContainer = snc,
         *      WeightTag = weightTag,
         *      WeightMap = weightMap,
         *      BindingMatrices = bindingMatrices
         *  });
         * }
         * */


        public void CreateWeightMap()
        {
            foreach (WeightObject wObject in _weightObjects)
            {
                WeightComponent wComponent = new WeightComponent();
                wComponent.Name   = wObject.WeightTag.GetName();
                wComponent.Joints = new List <SceneNodeContainer>();
                for (int i = 0; i < wObject.WeightTag.GetJointCount(); i++)
                {
                    CAJointObject      joint = wObject.WeightTag.GetJoint(i, _doc) as CAJointObject;
                    SceneNodeContainer jointSnc;
                    if (_jointObjects.TryGetValue(joint.RefUID(), out jointSnc))
                    {
                        wComponent.Joints.Add(jointSnc);
                    }
                }
                wComponent.WeightMap       = wObject.WeightMap;
                wComponent.BindingMatrices = wObject.BindingMatrices;
                int inxMesh = wObject.SceneNodeContainer.GetIndexOf <MeshComponent>();
                if (inxMesh == -1)
                {
                    inxMesh = 0;
                }
                wObject.SceneNodeContainer.Components.Insert(inxMesh, wComponent);
            }
        }
        /*
        struct SurfaceOutput {
            half3 Albedo;
            half3 Normal;
            half3 Emission;
            half Specular;
            half Gloss;
            half Alpha;
        };
        */

        public ShaderCodeBuilder(MaterialComponent mc, MeshComponent mesh, WeightComponent wc = null)
        {
            if (wc != null)
            {
                _hasWeightMap = true;
                _nBones = wc.Joints.Count;
            }
            else
            {
                _nBones = 0;
            }

            float f1 = 1;
            f1.GetType();
            _normalizeNormals = true;
            if (mesh != null)
                AnalyzeMesh(mesh);
            else
            {
                _hasVertices = _hasNormals = _hasUVs = true;
            }
            AnalyzeMaterial(mc);

            StringBuilder vs = new StringBuilder();
            MeshInputDeclarations(vs);
            MatrixDeclarations(vs);
            VSBody(vs);
            _vs = vs.ToString();

            StringBuilder ps = new StringBuilder();
            PixelInputDeclarations(ps);
            PSBody(ps);
            _ps = ps.ToString();
        }
        private ShaderEffect MakeMaterial(MaterialComponent mc)
        {
            WeightComponent   wc  = CurrentNode.GetWeights();
            ShaderCodeBuilder scb = new ShaderCodeBuilder(mc, null, wc); // TODO, CurrentNode.GetWeights() != null);
            var effectParameters  = AssembleEffectParamers(mc, scb);

            ShaderEffect ret = new ShaderEffect(new []
            {
                new EffectPassDeclaration()
                {
                    VS = scb.VS,
                    //VS = VsBones,
                    PS       = scb.PS,
                    StateSet = new RenderStateSet()
                    {
                        ZEnable          = true,
                        AlphaBlendEnable = false
                    }
                }
            },
                                                effectParameters
                                                );

            return(ret);
        }
 public void RenderWeight(WeightComponent weight)
 {
     float4x4[] boneArray = new float4x4[weight.Joints.Count()];
     for (int i = 0; i < weight.Joints.Count(); i++)
     {
         float4x4 tmp = weight.BindingMatrices[i];
         boneArray[i] = _boneMap[weight.Joints[i]] * tmp;
     }
     _rc.Bones = boneArray;
 }
Exemple #5
0
        /// <summary>
        /// If we have a MaterialLightComponent this constructor is called.
        /// </summary>
        /// <param name="mlc">The MaterialLightComponent</param>
        /// <param name="mesh">The Mesh</param>
        /// <param name="wc">WeightCompoennt for animations</param>
        public LegacyShaderCodeBuilder(MaterialLightComponent mlc, Mesh mesh, WeightComponent wc = null)
        {
            // Check WC
            if (wc != null)
            {
                _hasWeightMap = true;
                _nBones       = wc.Joints.Count;
            }
            else
            {
                _nBones = 0;
            }

            //float f1 = 1;
            //var type = f1.GetType();
            _normalizeNormals = true;

            // Check for mesh
            if (mesh != null)
            {
                AnalyzeMesh(mesh);
            }
            else
            {
                _hasVertices = _hasNormals = _hasUVs = true;
            }

            // Analyze the Material
            AnalyzeMaterial(mlc);


            // VS
            StringBuilder vs = new StringBuilder();

            MeshInputDeclarations(vs);
            MatrixDeclarations(vs);
            VSBody(vs);
            _vs = vs.ToString();

            // PS
            StringBuilder ps = new StringBuilder();

            PixelInputDeclarations(ps);
            PSCustomBody(ps, mlc);
            _ps = ps.ToString();
        }
Exemple #6
0
        public void ConVWeight(WeightComponent weight)
        {
            // check if we have bones
            if (_boneContainers.Count >= 1)
            {
                if (weight.Joints == null) // initialize joint container
                {
                    weight.Joints = new List <SceneNodeContainer>();
                }

                // set all bones found until this WeightComponent
                while (_boneContainers.Count != 0)
                {
                    weight.Joints.Add(_boneContainers.Pop());
                }
            }

            _currentNode.Components.Add(weight);
        }
Exemple #7
0
        public LegacyShaderCodeBuilder(MaterialComponent mc, Mesh mesh, WeightComponent wc = null)
        {
            if (wc != null)
            {
                _hasWeightMap = true;
                _nBones       = wc.Joints.Count;
            }
            else
            {
                _nBones = 0;
            }

            //float f1 = 1;
            //var type = f1.GetType();
            _normalizeNormals = true;
            if (mesh != null)
            {
                AnalyzeMesh(mesh);
            }
            else
            {
                _hasVertices = _hasNormals = _hasUVs = true;
            }
            AnalyzeMaterial(mc);

            //TODO: Use/switch lightning method to build PS & VS

            StringBuilder vs = new StringBuilder();

            MeshInputDeclarations(vs);
            MatrixDeclarations(vs);
            VSBody(vs);
            _vs = vs.ToString();

            StringBuilder ps = new StringBuilder();

            PixelInputDeclarations(ps);
            PSBody(ps);
            _ps = ps.ToString();
        }
        public Mesh MakeMesh(MeshComponent mc)
        {
            WeightComponent wc = CurrentNode.GetWeights();
            Mesh            rm;

            if (wc == null)
            {
                rm = new Mesh()
                {
                    Colors    = null,
                    Normals   = mc.Normals,
                    UVs       = mc.UVs,
                    Vertices  = mc.Vertices,
                    Triangles = mc.Triangles
                };
            }
            else // Create Mesh with weightdata
            {
                float4[] boneWeights = new float4[wc.WeightMap.Count];
                float4[] boneIndices = new float4[wc.WeightMap.Count];

                // Iterate over the vertices
                for (int iVert = 0; iVert < wc.WeightMap.Count; iVert++)
                {
                    VertexWeightList vwl = wc.WeightMap[iVert];

                    // Security guard. Sometimes a vertex has no weight. This should be fixed in the model. But
                    // let's just not crash here. Instead of having a completely unweighted vertex, bind it to
                    // the root bone (index 0).
                    if (vwl == null)
                    {
                        vwl = new VertexWeightList();
                    }
                    if (vwl.VertexWeights == null)
                    {
                        vwl.VertexWeights = new List <VertexWeight>(new[] { new VertexWeight {
                                                                                JointIndex = 0, Weight = 1.0f
                                                                            } });
                    }
                    int nJoints = System.Math.Min(4, vwl.VertexWeights.Count);
                    for (int iJoint = 0; iJoint < nJoints; iJoint++)
                    {
                        // boneWeights[iVert][iJoint] = vwl.VertexWeights[iJoint].Weight;
                        // boneIndices[iVert][iJoint] = vwl.VertexWeights[iJoint].JointIndex;
                        // JSIL cannot handle float4 indexer. Map [0..3] to [x..z] by hand
                        switch (iJoint)
                        {
                        case 0:
                            boneWeights[iVert].x = vwl.VertexWeights[iJoint].Weight;
                            boneIndices[iVert].x = vwl.VertexWeights[iJoint].JointIndex;
                            break;

                        case 1:
                            boneWeights[iVert].y = vwl.VertexWeights[iJoint].Weight;
                            boneIndices[iVert].y = vwl.VertexWeights[iJoint].JointIndex;
                            break;

                        case 2:
                            boneWeights[iVert].z = vwl.VertexWeights[iJoint].Weight;
                            boneIndices[iVert].z = vwl.VertexWeights[iJoint].JointIndex;
                            break;

                        case 3:
                            boneWeights[iVert].w = vwl.VertexWeights[iJoint].Weight;
                            boneIndices[iVert].w = vwl.VertexWeights[iJoint].JointIndex;
                            break;
                        }
                    }
                    boneWeights[iVert].Normalize1();
                }

                rm = new Mesh()
                {
                    Colors      = null,
                    Normals     = mc.Normals,
                    UVs         = mc.UVs,
                    BoneIndices = boneIndices,
                    BoneWeights = boneWeights,
                    Vertices    = mc.Vertices,
                    Triangles   = mc.Triangles
                };



                /*
                 * // invert weightmap to handle it easier
                 * float[,] invertedWeightMap = new float[wc.WeightMap[0].JointWeights.Count, wc.Joints.Count];
                 * for (int i = 0; i < wc.WeightMap.Count; i++)
                 * {
                 *  for (int j = 0; j < wc.WeightMap[i].JointWeights.Count; j++)
                 *  {
                 *      invertedWeightMap[j, i] = (float) wc.WeightMap[i].JointWeights[j];
                 *  }
                 * }
                 *
                 * float4[] boneWeights = new float4[invertedWeightMap.GetLength(0)];
                 * float4[] boneIndices = new float4[invertedWeightMap.GetLength(0)];
                 *
                 * // Contents of the invertedWeightMap:
                 * // ----------------------------------
                 * // Imagine the weight table as seen in 3d modelling programs, i.e. cinema4d;
                 * // wij are values in the range between 0..1 and specify to which percentage
                 * // the vertex (i) is controlled by the bone (j).
                 * //
                 * //            bone 0   bone 1   bone 2   bone 3   ....  -> indexed by j
                 * // vertex 0:   w00      w01      w02      w03
                 * // vertex 1:   w10      w11      w12      w13
                 * // vertex 2:   w20      w21      w22      w23
                 * // vertex 3:   w30      w31      w32      w33
                 * //   ...
                 * //  indexed
                 * //   by i
                 *
                 * // Iterate over the vertices
                 * for (int iVert = 0; iVert < invertedWeightMap.GetLength(0); iVert++)
                 * {
                 *  boneWeights[iVert] = new float4(0, 0, 0, 0);
                 *  boneIndices[iVert] = new float4(0, 0, 0, 0);
                 *
                 *  var tempDictionary = new Dictionary<int, float>();
                 *
                 *  // For the given vertex i, see which bones control us
                 *  for (int j = 0; j < invertedWeightMap.GetLength(1); j++)
                 *  {
                 *      if (j < 4)
                 *      {
                 *          tempDictionary.Add(j, invertedWeightMap[iVert, j]);
                 *      }
                 *      else
                 *      {
                 *          float tmpWeight = invertedWeightMap[iVert, j];
                 *          var keyAndValue = tempDictionary.OrderBy(kvp => kvp.Value).First();
                 *          if (tmpWeight > keyAndValue.Value)
                 *          {
                 *              tempDictionary.Remove(keyAndValue.Key);
                 *              tempDictionary.Add(j, tmpWeight);
                 *          }
                 *      }
                 *  }
                 *
                 *  if (tempDictionary.Count != 0)
                 *  {
                 *      var keyValuePair = tempDictionary.First();
                 *      boneIndices[iVert].x = keyValuePair.Key;
                 *      boneWeights[iVert].x = keyValuePair.Value;
                 *      tempDictionary.Remove(keyValuePair.Key);
                 *  }
                 *  if (tempDictionary.Count != 0)
                 *  {
                 *      var keyValuePair = tempDictionary.First();
                 *      boneIndices[iVert].y = keyValuePair.Key;
                 *      boneWeights[iVert].y = keyValuePair.Value;
                 *      tempDictionary.Remove(keyValuePair.Key);
                 *  }
                 *  if (tempDictionary.Count != 0)
                 *  {
                 *      var keyValuePair = tempDictionary.First();
                 *      boneIndices[iVert].z = keyValuePair.Key;
                 *      boneWeights[iVert].z = keyValuePair.Value;
                 *      tempDictionary.Remove(keyValuePair.Key);
                 *  }
                 *  if (tempDictionary.Count != 0)
                 *  {
                 *      var keyValuePair = tempDictionary.First();
                 *      boneIndices[iVert].w = keyValuePair.Key;
                 *      boneWeights[iVert].w = keyValuePair.Value;
                 *      tempDictionary.Remove(keyValuePair.Key);
                 *  }
                 *
                 *  boneWeights[iVert].Normalize1();
                 * }
                 *
                 * rm = new Mesh()
                 * {
                 *  Colors = null,
                 *  Normals = mc.Normals,
                 *  UVs = mc.UVs,
                 *  BoneIndices = boneIndices,
                 *  BoneWeights = boneWeights,
                 *  Vertices = mc.Vertices,
                 *  Triangles = mc.Triangles
                 * };
                 */
            }


            return(rm);
        }