예제 #1
0
        public RenderObjectUnity(AnimatorEditor editor, HashSet<string> meshNames)
        {
            HighlightSubmesh = new HashSet<int>();
            highlightMaterial = new SlimDX.Direct3D9.Material();
            highlightMaterial.Ambient = new Color4(1, 1, 1, 1);
            highlightMaterial.Diffuse = new Color4(1, 0, 1, 0);

            this.device = Gui.Renderer.Device;
            this.tweeningVertDec = new VertexDeclaration(this.device, TweeningWithoutNormalsVertexBufferFormat.ThreeStreams);

            Textures = new Texture[editor.Textures.Count];
            Materials = new SlimDX.Direct3D9.Material[editor.Materials.Count];

            rootFrame = CreateHierarchy(editor, meshNames, device, out meshFrames);

            AnimationController = new AnimationController(numFrames, 30, 30, 1);
            Frame.RegisterNamedMatrices(rootFrame, AnimationController);

            if (meshFrames.Count > 0)
            {
                Bounds = meshFrames[0].Bounds;
                for (int i = 1; i < meshFrames.Count; i++)
                {
                    Bounds = BoundingBox.Merge(Bounds, meshFrames[i].Bounds);
                }
            }
            else
            {
                Bounds = new BoundingBox();
            }
        }
예제 #2
0
        protected override void OnInitialize()
        {
            Physics = new Physics();
            Physics.World.SetInternalTickCallback(PickingPreTickCallback, this, true);

            light = new Light();
            light.Type = LightType.Point;
            light.Range = 75;
            light.Position = new Vector3(10, 25, 0);
            light.Diffuse = Color.LemonChiffon;
            light.Attenuation0 = 1.0f;

            softBodyMaterial = new SlimDX.Direct3D9.Material();
            softBodyMaterial.Diffuse = Color.White;
            softBodyMaterial.Ambient = new Color4(Ambient);

            Fps.Text = "Move using mouse and WASD+shift\n" +
                "F3 - Toggle debug\n" +
                "F11 - Toggle fullscreen\n" +
                "Space - Shoot box\n\n" +
                "B - Previous Demo\n" +
                "N - Next Demo";

            Freelook.SetEyeTarget(eye, target);

            base.OnInitialize();
        }
예제 #3
0
        protected override void OnInitialize()
        {
            Physics = new Physics();
            Physics.World.SetInternalTickCallback(PickingPreTickCallback, this, true);

            light              = new Light();
            light.Type         = LightType.Point;
            light.Range        = 75;
            light.Position     = new Vector3(10, 25, 0);
            light.Diffuse      = Color.LemonChiffon;
            light.Attenuation0 = 1.0f;

            softBodyMaterial         = new SlimDX.Direct3D9.Material();
            softBodyMaterial.Diffuse = Color.White;
            softBodyMaterial.Ambient = new Color4(Ambient);

            Fps.Text = "Move using mouse and WASD+shift\n" +
                       "F3 - Toggle debug\n" +
                       "F11 - Toggle fullscreen\n" +
                       "Space - Shoot box\n\n" +
                       "B - Previous Demo\n" +
                       "N - Next Demo";

            Freelook.SetEyeTarget(eye, target);

            base.OnInitialize();
        }
예제 #4
0
        public static short[] DecompressIndices(short[] indices, int start, int count)
        {
            bool  dir = false;
            short tempx;
            short tempy;
            short tempz;

            short[] s***e = new short[50000];
            int     m     = start;
            int     s     = 0;

            do
            {
                tempx = indices[m];
                tempy = indices[m + 1];
                tempz = indices[m + 2];


                if (tempx != tempy && tempx != tempz && tempy != tempz)
                {
                    if (dir == false)
                    {
                        s***e[s]     = tempx;
                        s***e[s + 1] = tempy;
                        s***e[s + 2] = tempz;
                        s           += 3;

                        dir = true;
                    }
                    else
                    {
                        s***e[s]     = tempx;
                        s***e[s + 1] = tempz;
                        s***e[s + 2] = tempy;
                        s           += 3;
                        dir          = false;
                    }
                    m += 1;
                }
                else
                {
                    if (dir == true)
                    {
                        dir = false;
                    }
                    else
                    {
                        dir = true;
                    }

                    m += 1;
                }
            }while (m < start + count - 2);
            short[] uncompressedindices = new short[s];
            Array.Copy(s***e, uncompressedindices, s);
            return(uncompressedindices);
        }
예제 #5
0
        public static void Convert(string source, string dest)
        {
            FileStream fs = File.Open(source, FileMode.Open, FileAccess.Read);

            DX.Mesh mesh = DX.Mesh.FromStream(device, fs, DX.MeshFlags.Managed);

            DX.ExtendedMaterial[] mats    = mesh.GetMaterials();
            Material[][]          outMats = new Material[mats.Length][];
            for (int i = 0; i < mats.Length; i++)
            {
                outMats[i]    = new Material[1];
                outMats[i][0] = new Material(null);

                DX.Material mat = mats[i].MaterialD3D;

                SlimDX.Color4 clr = mat.Ambient;

                outMats[i][0].Ambient = new Color4F(clr.Alpha, clr.Red, clr.Green, clr.Blue);

                clr = mat.Diffuse;
                outMats[i][0].Diffuse = new Color4F(clr.Alpha, clr.Red, clr.Green, clr.Blue);

                clr = mat.Specular;
                outMats[i][0].Specular = new Color4F(clr.Alpha, clr.Red, clr.Green, clr.Blue);

                outMats[i][0].SetTextureFile(0, mats[i].TextureFileName);
            }


            string name = Path.GetFileNameWithoutExtension(source);



            //EditableMesh outMesh = new EditableMesh(name, mesh, outMats);


            MeshData data = new MeshData((RenderSystem)null);


            data.Name      = name;
            data.Materials = outMats;

            BuildFromMesh(mesh, data, outMats);


            EditableModel outMdl = new EditableModel();

            //mesh.Dispose();

            outMdl.Entities = new MeshData[] { data };

            //TransformAnimation transAnim = new TransformAnimation(1);
            //transAnim.Nodes[0].Transforms = new Matrix[1] { Matrix.Identity };

            //outMdl.SetTransformAnimInst(new TransformAnimationInstance(transAnim));
            EditableModel.ToFile(outMdl, dest);
        }
예제 #6
0
파일: SoftDemo.cs 프로젝트: diqost/bullet
        protected override void OnInitialize()
        {
            Physics = new Physics();
            Physics.World.SetInternalTickCallback(PickingPreTickCallback, this, true);

            softBodyMaterial         = new SlimDX.Direct3D9.Material();
            softBodyMaterial.Diffuse = Color.White;
            softBodyMaterial.Ambient = new Color4(Ambient);

            Fps.Text = "Move using mouse and WASD+shift\n" +
                       "F3 - Toggle debug\n" +
                       "F11 - Toggle fullscreen\n" +
                       "Space - Shoot box\n\n" +
                       "B - Previous Demo\n" +
                       "N - Next Demo";

            Freelook.SetEyeTarget(eye, target);

            base.OnInitialize();
        }
예제 #7
0
        public override void Initialize()
        {
            currentFormWindowState = Form.WindowState;
            Form.Resize           += (o, args) =>
            {
                if (Form.WindowState != currentFormWindowState)
                {
                    if (togglingFullScreen == false)
                    {
                        HandleResize(o, args);
                    }
                }

                currentFormWindowState = Form.WindowState;
            };

            Form.ResizeBegin += (o, args) => { formIsResizing = true; };
            Form.ResizeEnd   += (o, args) =>
            {
                formIsResizing = false;
                HandleResize(o, args);
            };

            Form.Closed += (o, args) => { isFormClosed = true; };


            Width            = 1024;
            Height           = 768;
            FullScreenWidth  = Screen.PrimaryScreen.Bounds.Width;
            FullScreenHeight = Screen.PrimaryScreen.Bounds.Height;
            NearPlane        = 1.0f;
            FarPlane         = 200f;
            Ambient          = Color.Gray.ToArgb();

            OnInitializeDevice();

            ActiveMaterial         = new Material();
            ActiveMaterial.Diffuse = Color.Orange;
            ActiveMaterial.Ambient = new Color4(Ambient);

            PassiveMaterial         = new Material();
            PassiveMaterial.Diffuse = Color.Red;
            PassiveMaterial.Ambient = new Color4(Ambient);

            GroundMaterial         = new Material();
            GroundMaterial.Diffuse = Color.Green;
            GroundMaterial.Ambient = new Color4(Ambient);

            SoftBodyMaterial         = new Material();
            SoftBodyMaterial.Diffuse = Color.White;
            SoftBodyMaterial.Ambient = new Color4(Ambient);

            light              = new Light();
            light.Type         = LightType.Point;
            light.Range        = 70;
            light.Position     = new Vector3(10, 25, 10);
            light.Diffuse      = Color.LemonChiffon;
            light.Attenuation0 = 1.0f;

            Info         = new InfoText(Device);
            _meshFactory = new MeshFactory(this);
            MeshFactory  = _meshFactory;
        }
예제 #8
0
        public override void SetSurfaceParams(ColorEx ambient, ColorEx diffuse, ColorEx specular,
            ColorEx emissive, Real shininess, TrackVertexColor tracking = TrackVertexColor.None)
        {
            // TODO: Cache color values to prune unneccessary setting

            // create a new material based on the supplied params
            var mat = new D3D.Material();
            mat.Diffuse = D3DHelper.ToColor( diffuse );
            mat.Ambient = D3DHelper.ToColor( ambient );
            mat.Specular = D3DHelper.ToColor( specular );
            mat.Emissive = D3DHelper.ToColor( emissive );
            mat.Power = shininess;

            // set the current material
            ActiveD3D9Device.Material = mat;

            if ( tracking != TrackVertexColor.None )
            {
                SetRenderState( RenderState.ColorVertex, true );
                SetRenderState( RenderState.AmbientMaterialSource, (int)( ( ( tracking & TrackVertexColor.Ambient ) != 0 ) ? ColorSource.Color1 : ColorSource.Material ) );
                SetRenderState( RenderState.DiffuseMaterialSource, (int)( ( ( tracking & TrackVertexColor.Diffuse ) != 0 ) ? ColorSource.Color1 : ColorSource.Material ) );
                SetRenderState( RenderState.SpecularMaterialSource, (int)( ( ( tracking & TrackVertexColor.Specular ) != 0 ) ? ColorSource.Color1 : ColorSource.Material ) );
                SetRenderState( RenderState.EmissiveMaterialSource, (int)( ( ( tracking & TrackVertexColor.Emissive ) != 0 ) ? ColorSource.Color1 : ColorSource.Material ) );
            }
            else
            {
                SetRenderState( RenderState.ColorVertex, false );
            }
        }
예제 #9
0
        private AnimationFrame CreateFrame(Transform frame, AnimatorEditor editor, HashSet<string> extractFrames, HashSet<string> meshNames, Device device, List<AnimationFrame> meshFrames, Dictionary<string, Tuple<Matrix, Matrix>> extractMatrices)
        {
            AnimationFrame animationFrame = new AnimationFrame();
            animationFrame.Name = frame.GetTransformPath();
            animationFrame.TransformationMatrix = Matrix.Scaling(frame.m_LocalScale) * Matrix.RotationQuaternion(frame.m_LocalRotation) * Matrix.Translation(frame.m_LocalPosition);
            animationFrame.OriginalTransform = animationFrame.TransformationMatrix;
            animationFrame.CombinedTransform = extractMatrices[animationFrame.Name].Item1;

            if (meshNames.Contains(animationFrame.Name))
            {
                MeshRenderer meshR = frame.m_GameObject.instance.FindLinkedComponent(UnityClassID.SkinnedMeshRenderer);
                if (meshR == null)
                {
                    meshR = frame.m_GameObject.instance.FindLinkedComponent(UnityClassID.MeshRenderer);
                }
                if (meshR != null)
                {
                    Mesh mesh = Operations.GetMesh(meshR);
                    if (mesh != null)
                    {
                        SkinnedMeshRenderer smr = meshR as SkinnedMeshRenderer;
                        List<PPtr<Transform>> boneList = null;
                        string[] boneNames = null;
                        Matrix[] boneOffsets = null;
                        if (smr != null && smr.m_Bones.Count > 0)
                        {
                            boneList = smr.m_Bones;

                            int numBones = boneList.Count > 0 ? extractFrames.Count : 0;
                            boneNames = new string[numBones];
                            boneOffsets = new Matrix[numBones];
                            if (numBones > 0)
                            {
                                string[] extractArray = new string[numBones];
                                extractFrames.CopyTo(extractArray);
                                HashSet<string> extractCopy = new HashSet<string>(extractArray);
                                int invalidBones = 0;
                                for (int i = 0; i < boneList.Count; i++)
                                {
                                    Transform bone = boneList[i].instance;
                                    if (bone == null || bone.m_GameObject.instance == null || !extractCopy.Remove(bone.GetTransformPath()))
                                    {
                                        invalidBones++;
                                    }
                                    else if (i < numBones)
                                    {
                                        boneNames[i] = bone.GetTransformPath();
                                        boneOffsets[i] = Operations.Mirror(Matrix.Transpose(mesh.m_BindPose[i]));
                                    }
                                }
                                extractCopy.CopyTo(boneNames, boneList.Count - invalidBones);
                                for (int i = boneList.Count; i < extractFrames.Count; i++)
                                {
                                    boneOffsets[i] = extractMatrices[boneNames[i]].Item2;
                                }
                            }
                        }

                        AnimationMeshContainer[] meshContainers = new AnimationMeshContainer[mesh.m_SubMeshes.Count];
                        Vector3 min = new Vector3(Single.MaxValue);
                        Vector3 max = new Vector3(Single.MinValue);
                        Operations.vMesh vMesh = new Operations.vMesh(meshR, true, true);
                        for (int i = 0; i < mesh.m_SubMeshes.Count; i++)
                        {
                            Operations.vSubmesh submesh = vMesh.submeshes[i];
                            List<Operations.vFace> faceList = submesh.faceList;
                            List<Operations.vVertex> vertexList = submesh.vertexList;

                            SlimDX.Direct3D9.Mesh animationMesh = null;
                            PositionBlendWeightsIndexedColored[] normalLines = null;
                            try
                            {
                                animationMesh = new SlimDX.Direct3D9.Mesh(device, faceList.Count, vertexList.Count, MeshFlags.Managed, PositionBlendWeightsIndexedNormalTexturedColoured.Format);

                                using (DataStream indexStream = animationMesh.LockIndexBuffer(LockFlags.None))
                                {
                                    for (int j = 0; j < faceList.Count; j++)
                                    {
                                        ushort[] indices = faceList[j].index;
                                        indexStream.Write(indices[0]);
                                        indexStream.Write(indices[2]);
                                        indexStream.Write(indices[1]);
                                    }
                                    animationMesh.UnlockIndexBuffer();
                                }

                                FillVertexBuffer(animationMesh, vertexList, -1);

                                normalLines = new PositionBlendWeightsIndexedColored[vertexList.Count * 2];
                                for (int j = 0; j < vertexList.Count; j++)
                                {
                                    Operations.vVertex vertex = vertexList[j];

                                    byte[] bIdx;
                                    float[] bWeights;
                                    if (vertex.boneIndices != null)
                                    {
                                        bIdx = new byte[4] { (byte)vertex.boneIndices[0], (byte)vertex.boneIndices[1], (byte)vertex.boneIndices[2], (byte)vertex.boneIndices[3] };
                                        bWeights = vertex.weights;
                                    }
                                    else
                                    {
                                        bIdx = new byte[4];
                                        bWeights = new float[4];
                                    }
                                    normalLines[j * 2] = new PositionBlendWeightsIndexedColored(vertex.position, bWeights, bIdx, Color.Yellow.ToArgb());
                                    normalLines[(j * 2) + 1] = new PositionBlendWeightsIndexedColored(vertex.position + (vertex.normal / 64), bWeights, bIdx, Color.Yellow.ToArgb());

                                    min = Vector3.Minimize(min, vertex.position);
                                    max = Vector3.Maximize(max, vertex.position);
                                }
                            }
                            catch
                            {
                                Report.ReportLog("No display of submeshes with more than 64k vertices!");
                            }

                            AnimationMeshContainer meshContainer = new AnimationMeshContainer();
                            if (animationMesh != null)
                            {
                                meshContainer.Name = animationFrame.Name;
                                meshContainer.MeshData = new MeshData(animationMesh);
                                meshContainer.NormalLines = normalLines;
                            }
                            meshContainers[i] = meshContainer;

                            if (submesh.matList.Count > 0 && submesh.matList[0].instance != null)
                            {
                                Material mat = submesh.matList[0].instance;
                                int matIdx = editor.Materials.IndexOf(mat);
                                int texIdx;
                                if (!MatTexIndices.TryGetValue(matIdx, out texIdx))
                                {
                                    texIdx = -1;

                                    SlimDX.Direct3D9.Material materialD3D = new SlimDX.Direct3D9.Material();
                                    materialD3D.Ambient = GetColour(mat, "_SColor");
                                    materialD3D.Diffuse = GetColour(mat, "_Color");
                                    materialD3D.Emissive = GetColour(mat, "_ReflectColor");
                                    materialD3D.Specular = GetColour(mat, "_SpecColor");
                                    materialD3D.Power = GetFloat(mat, "_Shininess");
                                    Materials[matIdx] = materialD3D;

                                    Texture2D matTex = GetTexture(mat, "_MainTex");
                                    if (matTex != null)
                                    {
                                        texIdx = editor.Textures.IndexOf(matTex);
                                        if (Textures[texIdx] == null)
                                        {
                                            using (MemoryStream mem = new MemoryStream())
                                            {
                                                matTex.Export(mem);
                                                mem.Position = 0;
                                                ImportedTexture image = new ImportedTexture(mem, matTex.m_Name);
                                                Textures[texIdx] = Texture.FromMemory(device, image.Data);
                                            }
                                        }
                                    }

                                    MatTexIndices.Add(matIdx, texIdx);
                                }

                                meshContainer.MaterialIndex = matIdx;
                                meshContainer.TextureIndex = texIdx;
                            }
                        }

                        for (int i = 0; i < (meshContainers.Length - 1); i++)
                        {
                            meshContainers[i].NextMeshContainer = meshContainers[i + 1];
                        }
                        if (boneList != null)
                        {
                            for (int i = 0; i < meshContainers.Length; i++)
                            {
                                meshContainers[i].BoneNames = boneNames;
                                meshContainers[i].BoneOffsets = boneOffsets;
                                meshContainers[i].RealBones = boneList.Count;
                            }
                        }

                        Matrix mirrorCombined = Operations.Mirror(animationFrame.CombinedTransform);
                        min = Vector3.TransformCoordinate(min, mirrorCombined);
                        max = Vector3.TransformCoordinate(max, mirrorCombined);
                        animationFrame.Bounds = new BoundingBox(min, max);
                        animationFrame.MeshContainer = meshContainers[0];
                        meshFrames.Add(animationFrame);
                    }
                }
            }

            for (int i = 0; i < frame.Count; i++)
            {
                Transform child = frame[i];
                if (extractFrames.Contains(child.GetTransformPath()))
                {
                    AnimationFrame childAnimationFrame = CreateFrame(child, editor, extractFrames, meshNames, device, meshFrames, extractMatrices);
                    childAnimationFrame.Parent = animationFrame;
                    animationFrame.AppendChild(childAnimationFrame);
                }
            }

            numFrames++;
            return animationFrame;
        }
        public override void Initialize()
        {
            currentFormWindowState = Form.WindowState;
            Form.Resize += (o, args) =>
            {
                if (Form.WindowState != currentFormWindowState)
                {
                    if (togglingFullScreen == false)
                        HandleResize(o, args);
                }

                currentFormWindowState = Form.WindowState;
            };

            Form.ResizeBegin += (o, args) => { formIsResizing = true; };
            Form.ResizeEnd += (o, args) =>
            {
                formIsResizing = false;
                HandleResize(o, args);
            };

            Form.Closed += (o, args) => { isFormClosed = true; };


            Width = 1024;
            Height = 768;
            FullScreenWidth = Screen.PrimaryScreen.Bounds.Width;
            FullScreenHeight = Screen.PrimaryScreen.Bounds.Height;
            NearPlane = 1.0f;
            FarPlane = 200f;
            Ambient = Color.Gray.ToArgb();

            OnInitializeDevice();

            ActiveMaterial = new Material();
            ActiveMaterial.Diffuse = Color.Orange;
            ActiveMaterial.Ambient = new Color4(Ambient);

            PassiveMaterial = new Material();
            PassiveMaterial.Diffuse = Color.Red;
            PassiveMaterial.Ambient = new Color4(Ambient);

            GroundMaterial = new Material();
            GroundMaterial.Diffuse = Color.Green;
            GroundMaterial.Ambient = new Color4(Ambient);

            SoftBodyMaterial = new Material();
            SoftBodyMaterial.Diffuse = Color.White;
            SoftBodyMaterial.Ambient = new Color4(Ambient);

            light = new Light();
            light.Type = LightType.Point;
            light.Range = 70;
            light.Position = new Vector3(10, 25, 10);
            light.Diffuse = Color.LemonChiffon;
            light.Attenuation0 = 1.0f;

            Info = new InfoText(Device);
            _meshFactory = new MeshFactory(this);
            MeshFactory = _meshFactory;
        }