//============================================================ public override void DrawRenderable(FDxRegion region, FDxRenderable renderable, FDxEffect effect) { // 设置顶点缓冲 FDxGeometry geometry = renderable as FDxGeometry; effect.NativeEffect.GetVariableByName("_modelMatrix").AsMatrix().SetMatrix(renderable.Matrix.Native); effect.NativeEffect.GetVariableByName("_viewMatrix").AsMatrix().SetMatrix(region.Camera.Matrix.Native); effect.NativeEffect.GetVariableByName("_projectionMatrix").AsMatrix().SetMatrix(region.Camera.Viewport.Matrix); effect.NativeEffect.GetVariableByName("_cameraPosition").AsVector().Set(new Vector3(region.Camera.Position.X, region.Camera.Position.Y, region.Camera.Position.Z)); effect.NativeEffect.GetVariableByName("_lightPosition").AsVector().Set(new Vector3(region.LightDirectional.Position.X, region.LightDirectional.Position.Y, region.LightDirectional.Position.Z)); effect.NativeEffect.GetVariableByName("_lightDirection").AsVector().Set(new Vector3(region.LightDirectional.Camera.Direction.X, region.LightDirectional.Camera.Direction.Y, region.LightDirectional.Camera.Direction.Z)); _device.NativeDevice.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; _device.NativeDevice.InputAssembler.SetVertexBuffers(0, renderable.VertexBuffer.NativeBuffers); _device.NativeDevice.InputAssembler.SetIndexBuffer(renderable.FaceBuffer.NativeBuffer, Format.R32_UInt, 0); int textureCount = geometry.Material.Textures.Count; for (int i = 0; i < textureCount; i++) { FDxTexture texture = geometry.Material.Textures[i]; if (null != texture) { string textureName = "_texture" + RString.FirstUpper(texture.TypeName); effect.NativeEffect.GetVariableByName(textureName).AsResource().SetResource(texture.NativeResource); } } effect.NativePass.Apply(_device.NativeDevice); _device.NativeDevice.DrawIndexed(renderable.FaceBuffer.count, 0, 0); }
//============================================================ public FDxBufferedTexture ExportTexture(string importFileName) { // 获得图片大小 Bitmap bmp = new Bitmap(importFileName); _size.Set(bmp.Width, bmp.Height); bmp.Dispose(); // 生成目标 Form form = new Form(); FDxDevice3D device = RDxCore.Adapter.CreateDevice(form.Handle, _size.Width, _size.Height); device.ModeWireFrame = false; // 设置几何体 FDxRegion region = new FDxRegion(); // 渲染几何体 FDxTechnique technique = RDxCore.TechniqueConsole.Get(device, "growing"); FDrGrowingGeometry geometry = new FDrGrowingGeometry(); geometry.Device = device; geometry.LoadResource(); // 绘制结果 region.Renderables.Push(geometry); // 设置目标 FDxBufferedTexture texture = new FDxBufferedTexture(); texture.Device = device; texture.Create(_size.Width, _size.Height); // 绘制目标 FDxTexture originTexture = new FDxTexture(); originTexture.Device = device; originTexture.LoadFile(importFileName); geometry.Material.Textures.Push(originTexture); // 绘制目标 FDxTexture importTexture = new FDxTexture(); importTexture.Device = device; importTexture.LoadFile(importFileName); geometry.Material.Textures.Push(importTexture); MemoryStream stream = new MemoryStream(); for (int n = 0; n < _borderCount; n++) { device.SetRenderTarget(texture); // 绘制目标 technique.Draw(region); device.SetRenderTarget(); // 复制渲染目标为开始 //importTexture.CopyFrom(texture); texture.SaveStream(stream); importTexture.LoadStream(stream); } // 保存内容 return(texture); }
//============================================================ // <T>绘制区域内所有节点集合。</T> // // @param p:region 区域 //============================================================ public override void DrawRenderable(FDxRegion region, FDxRenderable renderable, FDxEffect effect) { // 设置顶点缓冲 FDxGeometry geometry = renderable as FDxGeometry; _device.NativeDevice.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; _device.NativeDevice.InputAssembler.SetVertexBuffers(0, renderable.VertexBuffer.NativeBuffers); _device.NativeDevice.InputAssembler.SetIndexBuffer(renderable.FaceBuffer.NativeBuffer, Format.R32_UInt, 0); FDxTexture orignTexture = renderable.Material.Textures[0]; FDxTexture texture = renderable.Material.Textures[1]; effect.NativeEffect.GetVariableByName("_textureSize").AsVector().Set(new Vector2(texture.Size.Width, texture.Size.Height)); effect.NativeEffect.GetVariableByName("_textureOrigin").AsResource().SetResource(orignTexture.NativeResource); effect.NativeEffect.GetVariableByName("_textureColor").AsResource().SetResource(texture.NativeResource); effect.NativePass.Apply(_device.NativeDevice); _device.NativeDevice.DrawIndexed(renderable.FaceBuffer.count, 0, 0); }
//============================================================ public override void DrawRenderable(FDxRegion region, FDxRenderable renderable, FDxEffect effect) { if (renderable.IsUi) { return; } // 设置输入数据缓冲 _device.NativeDevice.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; _device.NativeDevice.InputAssembler.SetVertexBuffers(0, renderable.VertexBuffer.NativeBuffers); _device.NativeDevice.InputAssembler.SetIndexBuffer(renderable.FaceBuffer.NativeBuffer, Format.R32_UInt, 0); // 设置渲染对象参数 Vector4 flags = new Vector4(renderable.IsSelected ? 1 : 0, renderable.IsUi ? 1 : 0, 0, 0); // 设置渲染对象参数 FDxGeometry geometry = renderable as FDxGeometry; FDxMaterial material = geometry.Material; effect.NativeEffect.GetVariableByName("_flags").AsVector().Set(new Vector4(renderable.Id, 0, 0, 0)); effect.NativeEffect.GetVariableByName("_status").AsVector().Set(flags); effect.SetMatrix("_modelMatrix", renderable.ModelMatrix); effect.SetNativeMatrix("_viewMatrix", region.Camera.Matrix.Native); effect.SetNativeMatrix("_projectionMatrix", region.Camera.Viewport.Matrix); effect.NativeEffect.GetVariableByName("_projectionMatrix").AsMatrix().SetMatrix(region.Camera.Viewport.Matrix); effect.NativeEffect.GetVariableByName("_cameraPosition").AsVector().Set(new Vector3(region.Camera.Position.X, region.Camera.Position.Y, region.Camera.Position.Z)); effect.NativeEffect.GetVariableByName("_lightPosition").AsVector().Set(new Vector3(region.LightDirectional.Position.X, region.LightDirectional.Position.Y, region.LightDirectional.Position.Z)); effect.NativeEffect.GetVariableByName("_lightDirection").AsVector().Set(new Vector3(region.LightDirectional.Camera.Direction.X, region.LightDirectional.Camera.Direction.Y, region.LightDirectional.Camera.Direction.Z)); // 设置渲染材质参数 effect.NativeEffect.GetVariableByName("_ambientColor").AsVector().Set(new Vector4(material.AmbientColor.R, material.AmbientColor.G, material.AmbientColor.B, material.AmbientColor.A)); effect.NativeEffect.GetVariableByName("_diffuseColor").AsVector().Set(new Vector4(material.DiffuseColor.R, material.DiffuseColor.G, material.DiffuseColor.B, material.DiffuseColor.A)); effect.NativeEffect.GetVariableByName("_specularColor").AsVector().Set(new Vector4(material.SpecularColor.R, material.SpecularColor.G, material.SpecularColor.B, material.SpecularColor.A)); // 设置纹理列表 int textureCount = geometry.Material.Textures.Count; for (int i = 0; i < textureCount; i++) { FDxTexture texture = geometry.Material.Textures[i]; if (null != texture) { string textureName = "_texture" + RString.FirstUpper(texture.TypeName); effect.NativeEffect.GetVariableByName(textureName).AsResource().SetResource(texture.NativeResource); } } // 提交效果设置 effect.NativePass.Apply(_device.NativeDevice); // 绘制几何体 _device.NativeDevice.DrawIndexed(renderable.FaceBuffer.count, 0, 0); }