public static DrawModel FromScene(String uid, BlenderScene scene, string fileSearchPath) { var drawSys = DrawSystem.GetInstance(); var drawRepository = drawSys.ResourceRepository; var d3d = drawSys.D3D; var model = new DrawModel(uid); bool hasBone = BlenderUtil.GetLengthOf(scene.NodeList[0].BoneArray) > 0;// boneArray is set to the first node var aabb = Aabb.Invalid(); foreach (var n in scene.NodeList) { if (n.MaterialData.Type == MaterialBase.MaterialTypes.Marker) { // marker material is used as 'Marker' continue; } if (n.Vertics.Count() == 0) { // empty vertex list continue; } // Build a vertex buffer(s) var vertices1 = n.Vertics .Select(v => new _VertexCommon() { Position = v.Position, Normal = v.Normal, Texcoord = v.Texcoord }).ToArray(); var vertices2 = n.Vertics .Select(v => v.Tangent).ToArray(); // update aabb foreach (var v in vertices1) { aabb.ExtendByPoint(MathUtil.ToVector3(v.Position)); } var node = new Node(); node.Material = n.MaterialData; node.IsDebug = false; node.HasBone = hasBone; if (node.HasBone) { // if model has bone, we create a bone vertex info var vertices3 = n.Vertics .Select(v => { Debug.Assert(BlenderUtil.GetLengthOf(v.BoneIndices) == BlenderUtil.GetLengthOf(v.BoneWeights), "both of bone index and bone weight must be matched"); //Debug.Assert(BlenderUtil.GetLengthOf(v.BoneWeights) <= _VertexBoneWeight.MAX_COUNT, "length of bone weight is over :" + BlenderUtil.GetLengthOf(v.BoneWeights)); Debug.Assert(BlenderUtil.GetLengthOf(v.BoneWeights) != 0, "no bone entry"); var tmp = new _VertexBoneWeight() { Index0 = BlenderUtil.GetLengthOf(v.BoneIndices) > 0 ? v.BoneIndices[0] : 0, Weight0 = BlenderUtil.GetLengthOf(v.BoneWeights) > 0 ? v.BoneWeights[0] : 0.0f, Index1 = BlenderUtil.GetLengthOf(v.BoneIndices) > 1 ? v.BoneIndices[1] : 0, Weight1 = BlenderUtil.GetLengthOf(v.BoneWeights) > 1 ? v.BoneWeights[1] : 0.0f, Index2 = BlenderUtil.GetLengthOf(v.BoneIndices) > 2 ? v.BoneIndices[2] : 0, Weight2 = BlenderUtil.GetLengthOf(v.BoneWeights) > 2 ? v.BoneWeights[2] : 0.0f, Index3 = BlenderUtil.GetLengthOf(v.BoneIndices) > 3 ? v.BoneIndices[3] : 0, Weight3 = BlenderUtil.GetLengthOf(v.BoneWeights) > 3 ? v.BoneWeights[3] : 0.0f, }; float sumWeight = tmp.Weight0 + tmp.Weight1 + tmp.Weight2 + tmp.Weight3; tmp.Weight0 /= sumWeight; tmp.Weight1 /= sumWeight; tmp.Weight2 /= sumWeight; tmp.Weight3 /= sumWeight; return(tmp); }).ToArray(); node.Mesh = DrawUtil.CreateMeshData(d3d, PrimitiveTopology.TriangleList, vertices1, vertices2, vertices3); } else { node.Mesh = DrawUtil.CreateMeshData(d3d, PrimitiveTopology.TriangleList, vertices1, vertices2); } // add dispoable /* * foreach (var buf in node.Mesh.Buffers) * { * model._AddDisposable(buf.Buffer); * } */ // create skeleton if (model.m_boneArray == null && n.BoneArray != null) { model.m_boneArray = (DrawSystem.BoneData[])n.BoneArray.Clone(); } // load new texture foreach (var texInfo in n.TextureInfos.Values) { if (!drawRepository.Contains(texInfo.Name)) { var tex = TextureView.FromFile(texInfo.Name, drawSys.D3D, Path.Combine(fileSearchPath, texInfo.Name)); drawRepository.AddResource(tex); } } // copy textures from cache foreach (DrawSystem.TextureTypes textureType in Enum.GetValues(typeof(DrawSystem.TextureTypes))) { if (n.TextureInfos.ContainsKey(textureType)) { node.Material.SetTextureData( textureType, new DrawSystem.TextureData { Resource = drawRepository.FindResource <TextureView>(n.TextureInfos[textureType].Name), UvScale = n.TextureInfos[textureType].UvScale, }); } } model.m_nodeList.Add(node); } model.m_bb = aabb; return(model); }
public Scene(Device device, SwapChain swapChain, Panel renderTarget, HmdDevice hmd, bool bStereoRendering, int multiThreadCount) { var drawSys = DrawSystem.GetInstance(); var mapSys = MapSystem.GetInstance(); // load textures var textures = new List <TextureView>(new[] { TextureView.FromFile("dot", drawSys.D3D, "Image/dot.png"), TextureView.FromFile("floor", drawSys.D3D, "Image/floor.jpg"), }); var numTextures = new DrawSystem.TextureData[10]; for (int i = 0; i < 10; ++i) { var name = String.Format("number_{0}", i); numTextures[i] = new DrawSystem.TextureData { Resource = TextureView.FromFile(name, drawSys.D3D, String.Format("Image/{0}.png", name)), UvScale = Vector2.One, }; } textures.AddRange(numTextures.Select(item => item.Resource)); foreach (var tex in textures) { drawSys.ResourceRepository.AddResource(tex); } // create number entity m_fps = new FpsCounter(); m_numberEntity = new NumberEntity(new NumberEntity.InitParam() { Dot = new DrawSystem.TextureData { Resource = drawSys.ResourceRepository.FindResource <TextureView>("dot"), UvScale = Vector2.One, }, Numbers = numTextures, Layout = Matrix.RotationYawPitchRoll(1.0f, -1.5f, MathUtil.PI) * Matrix.Translation(-1.5f, 2.5f, -4.5f) }); // create map mapSys.LoadResource(); mapSys.CreateMap("Level/l9000.tmx"); // create player m_player = new PlayerEntity(); ChrSystem.GetInstance().Player = m_player; m_multiThreadCount = multiThreadCount; m_taskList = new List <Task>(m_multiThreadCount); m_taskResultList = new List <CommandList>(m_multiThreadCount); // other settings #if DEBUG CameraSystem.GetInstance().ActivateCamera(CameraSystem.FollowEntityCameraName); //CameraSystem.GetInstance().ActivateCamera(CameraSystem.FixedCameraName); //CameraSystem.GetInstance().ActivateCamera(CameraSystem.FreeCameraName); #else CameraSystem.GetInstance().ActivateCamera(CameraSystem.FollowEntityCameraName); #endif // DEBUG }