Esempio n. 1
0
        public PmxModel(RenderToTexturePipeline pipeline, string filename)
        {
            _pipeline = pipeline;

            _model = new PmxModelData();
            using (var r = new BinaryReader(File.OpenRead(filename)))
            {
                _model.Read(r);
            }

            _textures = new Texture2D[_model.TextureFiles.Length];
            var path   = Path.GetDirectoryName(filename);
            var device = pipeline.Device;

            for (int i = 0; i < _textures.Length; ++i)
            {
                var src      = _model.TextureFiles[i];
                var fullName = Path.Combine(path, src);
                if (Path.GetExtension(src) == ".tga")
                {
                    using (var tga = TgaDecoder.FromFile(fullName))
                    {
                        _textures[i] = device.CreateTexture2D(tga);
                    }
                }
                else
                {
                    using (var textureFile = File.OpenRead(fullName))
                    {
                        _textures[i] = device.CreateTexture2D(textureFile);
                    }
                }
            }

            var bufferProcesser = pipeline.CreateVertexDataProcessor <Vertex>();

            _vertexBuffers = new VertexBuffer[_model.MaterialArray.Length];
            for (int i = 0, indexOffset = 0; i < _model.MaterialArray.Length; ++i)
            {
                var mat        = _model.MaterialArray[i];
                var vertexData = new Vertex[mat.FaceCount];
                for (int j = 0; j < mat.FaceCount; ++j)
                {
                    var index  = _model.VertexIndices[indexOffset + j];
                    var vertex = _model.VertexArray[Math.Abs(index)];
                    vertexData[j].Position = vertex.Pos;
                    vertexData[j].TexCoord = vertex.Uv;
                    vertexData[j].Normal   = vertex.Normal;
                }
                indexOffset      += mat.FaceCount;
                _vertexBuffers[i] = bufferProcesser.CreateImmutableBuffer(vertexData);
            }

            _isTransparent = new bool[_vertexBuffers.Length];
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string filename;

            using (var dialog = new OpenFileDialog {
                Filter = "*.pmx|*.pmx"
            })
            {
                if (dialog.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }
                filename = dialog.FileName;
            }

            var form = new Form
            {
                ClientSize = new Size(800, 600),
                Text       = "Pmx Viewer",
            };
            var device = LightDevice.Create(form);

            var renderToTexturePipeline = new RenderToTexturePipeline(device);
            var deferredTarget          = new DeferredTarget(device);
            var deferredRendering1      = new DeferredRenderer(device, "Deferred1.fx",
                                                               new[] { deferredTarget.ColorData, deferredTarget.NormalData, deferredTarget.DepthData }, false);
            var deferredRendering2 = new DeferredRenderer(device, "Deferred2.fx",
                                                          new[] { deferredTarget.NormalData, deferredTarget.DepthData }, true);

            deferredRendering2.ClearColor = Color.Black.WithAlpha(0);

            var model = new PmxModel(renderToTexturePipeline, filename);

            model.HideMaterial(29); //Hide the shadow of hair. We don't have diffuse color in shader.
            model.SetTransparent(26);
            var camera = new Camera(form);

            form.Show();
            form.Activate();
            var frameCounter = new FrameCounter();

            frameCounter.Start();

            device.RunMultithreadLoop(delegate()
            {
                var time = frameCounter.NextFrame() / 1000;

                camera.Update();

                renderToTexturePipeline.View = camera.GetViewMatrix().Transpose();
                renderToTexturePipeline.UpdateConstants();

                deferredTarget.ClearAll();
                deferredTarget.ApplyDeferred();
                renderToTexturePipeline.ApplyDeferred();
                model.DrawSolid();

                deferredRendering1.ClearAll();
                deferredRendering1.Render();

                deferredTarget.ClearDeferred();
                deferredTarget.ApplyForward();
                renderToTexturePipeline.ApplyForward();
                model.DrawTransparent();

                deferredRendering2.Render();

                device.Present(true);
            });
        }