private CompositeFixture(CompositeShape wrappedCompositeShape, CompositeMaterial wrappedCompositeMaterial, Matrix4x4 realParentPose, FixtureDescriptor descriptor) { _root = false; _realParentPose = realParentPose; _pose = descriptor.Pose; _realPose = GMath.mul(_pose, _realParentPose); UserData = descriptor.UserData; _wrappedCompositeMaterial = wrappedCompositeMaterial; _wrappedCompositeShape = wrappedCompositeShape; FixtureFactory = new CompositeFixtureFixtureFactory(this); }
public CompositeFixture(DR.RigidBody wrappedRigidBody, FixtureDescriptor descriptor) { _root = true; _pose = descriptor.Pose; _realPose = _pose; _wrappedCompositeMaterial = new CompositeMaterial(); wrappedRigidBody.Material = _wrappedCompositeMaterial; _wrappedCompositeShape = new CompositeShape(); wrappedRigidBody.Shape = _wrappedCompositeShape; UserData = descriptor.UserData; FixtureFactory = new CompositeFixtureFixtureFactory(this); }
public CompositeMaterial2Sample(Microsoft.Xna.Framework.Game game) : base(game) { // Add basic force effects. Simulation.ForceEffects.Add(new Gravity()); Simulation.ForceEffects.Add(new Damping()); // Add a ground plane. RigidBody groundPlane = new RigidBody(new PlaneShape(new Vector3(0, 1, 0.25f).Normalized, 0)) { Name = "GroundPlane", // Names are not required but helpful for debugging. MotionType = MotionType.Static, }; // Adjust the coefficients of friction of the ground plane. ((UniformMaterial)groundPlane.Material).DynamicFriction = 0.5f; ((UniformMaterial)groundPlane.Material).StaticFriction = 0.5f; Simulation.RigidBodies.Add(groundPlane); // Prepare two materials: a slippery material and a rough material. UniformMaterial slipperyMaterial = new UniformMaterial { DynamicFriction = 0.001f, StaticFriction = 0.001f, }; UniformMaterial roughMaterial = new UniformMaterial { DynamicFriction = 1, StaticFriction = 1, }; // Create a rigid body that consists of multiple shapes: Two boxes and a cylinder between them. CompositeShape compositeShape = new CompositeShape(); compositeShape.Children.Add(new GeometricObject(new BoxShape(1f, 1f, 1f), new Pose(new Vector3(1.5f, 0f, 0f)))); compositeShape.Children.Add(new GeometricObject(new BoxShape(1f, 1, 1f), new Pose(new Vector3(-1.5f, 0f, 0f)))); compositeShape.Children.Add(new GeometricObject(new CylinderShape(0.1f, 2), new Pose(Matrix.CreateRotationZ(ConstantsF.PiOver2)))); // A CompositeMaterial is used to assign a different material to each shape. CompositeMaterial compositeMaterial = new CompositeMaterial(); compositeMaterial.Materials.Add(roughMaterial); // Assign the rough material to the first box. compositeMaterial.Materials.Add(slipperyMaterial); // Assign the slippery material to the second box. compositeMaterial.Materials.Add(null); // Use whatever is default for the handle between the boxes. RigidBody body = new RigidBody(compositeShape, null, compositeMaterial) { Pose = new Pose(new Vector3(0, 2.2f, -5)), }; Simulation.RigidBodies.Add(body); }
private void OnRenderImage(RenderTexture source, RenderTexture destination) { if (_stencilBuffer == null) { Graphics.Blit(source, destination); return; } //创建两个降采样纹理来模糊图像 int width = source.width / DownSampleFactor; int height = source.height / DownSampleFactor; RenderTexture buffer1 = RenderTexture.GetTemporary(width, height, StencilZBufferDepth, RenderTextureFormat.ARGB32); RenderTexture buffer2 = RenderTexture.GetTemporary(width, height, StencilZBufferDepth, RenderTextureFormat.ARGB32); //将纹理降采样、模糊处理后存入buffer1 DownSample4x(_stencilBuffer, buffer1); //循环迭代模糊处理图像 bool oddEven = true; for (int i = 0; i < BlurIterations; i++) { if (oddEven) { FourTapCone(buffer1, buffer2, i); } else { FourTapCone(buffer2, buffer1, i); } oddEven = !oddEven; } //使用合成Shader,合成最终输出图像 CompositeMaterial.SetTexture("_StencilTex", _stencilBuffer); CompositeMaterial.SetTexture("_BlurTex", oddEven ? buffer1 : buffer2); Graphics.Blit(source, destination, CompositeMaterial); //清理缓存 RenderTexture.ReleaseTemporary(buffer1); RenderTexture.ReleaseTemporary(buffer2); if (_stencilBuffer != null) { RenderTexture.ReleaseTemporary(_stencilBuffer); _stencilBuffer = null; } }
public CompositeMaterialSample(Microsoft.Xna.Framework.Game game) : base(game) { // Add basic force effects. Simulation.ForceEffects.Add(new Gravity()); Simulation.ForceEffects.Add(new Damping()); // ----- Create a simple height field. // The height data consists of height samples with a resolution of 20 entries per dimension. // (Height samples are stored in a 1-dimensional array.) var numberOfSamplesX = 20; var numberOfSamplesZ = 20; var samples = new float[numberOfSamplesX * numberOfSamplesZ]; for (int z = 0; z < numberOfSamplesZ; z++) { for (int x = 0; x < numberOfSamplesX; x++) { // Set the y values (height). samples[z * numberOfSamplesX + x] = 20 - z; } } // Set the size of the height field in world space. (WidthX/Z determine the extent // of the height field but not the resolution of the height samples.) float widthX = 40; float widthZ = 40; // The origin is at (-20, -20) to center the height field at the world space origin. float originX = -20; float originZ = -20; // Create the height field shape. HeightField heightField = new HeightField( originX, originZ, widthX, widthZ, samples, numberOfSamplesX, numberOfSamplesZ); RigidBody ground = new RigidBody(heightField) { Pose = new Pose(new Vector3F(0, -10, 0)), MotionType = MotionType.Static, }; Simulation.RigidBodies.Add(ground); // Assign two different materials to the height field. // A rough material (high friction) should be used for the left cells of the height field. UniformMaterial roughMaterial = new UniformMaterial { DynamicFriction = 1, StaticFriction = 1, }; // A slippery material (low friction) should be used for the right cells of the height field. UniformMaterial slipperyMaterial = new UniformMaterial { DynamicFriction = 0, StaticFriction = 0, }; // Use a CompositeMaterial two assign the materials to the features of the height field. CompositeMaterial compositeMaterial = new CompositeMaterial(); // A "feature" of a height field is a triangle: // The height field is triangulated. Each cell consists of two triangles. The triangles are // numbered from left-to-right and top-to-bottom. // (For more information: See the description of HeightField.) // Loop over the cells. // (If the resolution is 20, we have 20 height values in one row. Between these height // values are 19 cells.) for (int z = 0; z < numberOfSamplesZ - 1; z++) { for (int x = 0; x < numberOfSamplesX - 1; x++) { // Assign the rough material to the left cells and the slippery material to the // right cells. if (x < numberOfSamplesX / 2) { // Each cell contains 2 triangles, therefore we have to add 2 entries to the // CompositeMaterial. compositeMaterial.Materials.Add(roughMaterial); compositeMaterial.Materials.Add(roughMaterial); } else { compositeMaterial.Materials.Add(slipperyMaterial); compositeMaterial.Materials.Add(slipperyMaterial); } } } ground.Material = compositeMaterial; // Create a few boxes on the height field. // The left boxes will roll or stop on the height field because of the high friction. // The right boxes will slide down because of the low friction. BoxShape boxShape = new BoxShape(1, 1, 1); for (int i = 0; i < 10; i++) { RigidBody body = new RigidBody(boxShape, null, roughMaterial) { Pose = new Pose(new Vector3F(-19 + i * 4, 10, -10)), }; Simulation.RigidBodies.Add(body); } }
public CompositeMaterialSample(Microsoft.Xna.Framework.Game game) : base(game) { // Add basic force effects. Simulation.ForceEffects.Add(new Gravity()); Simulation.ForceEffects.Add(new Damping()); // ----- Create a simple height field. const int resolution = 20; HeightField heightField = new HeightField(); // Set the size of the height field in world space. (WidthX/Z determine the extent // of the height field but not the resolution.) heightField.WidthX = 40; heightField.WidthZ = 40; // Create the height field data. // The size of the array determines the resolution of the height field. heightField.Array = new float[resolution, resolution]; for (int x = 0; x < heightField.Array.GetLength(0); x++) { for (int z = 0; z < heightField.Array.GetLength(1); z++) { // Set the y value (height) at the given index. heightField.Array[x, z] = 20 - z; } } RigidBody ground = new RigidBody(heightField) { Pose = new Pose(new Vector3F(-20, -10, -20f)), MotionType = MotionType.Static, }; Simulation.RigidBodies.Add(ground); // Assign two different materials to the height field. // A rough material (high friction) should be used for the left cells of the height field. UniformMaterial roughMaterial = new UniformMaterial { DynamicFriction = 1, StaticFriction = 1, }; // A slippery material (low friction) should be used for the right cells of the height field. UniformMaterial slipperyMaterial = new UniformMaterial { DynamicFriction = 0, StaticFriction = 0, }; // Use a CompositeMaterial two assign the materials to the features of the height field. CompositeMaterial compositeMaterial = new CompositeMaterial(); // A "feature" of a height field is a triangle: // The height field is triangulated. Each cell consists of two triangles. The triangles are // numbered from left-to-right and top-to-bottom. // (For more information: See the description of HeightField.) // Loop over the cells. // (If the resolution is 20, we have 20 height values in one row. Between these height // values are 19 cells.) for (int z = 0; z < resolution - 1; z++) { for (int x = 0; x < resolution - 1; x++) { // Assign the rough material to the left cells and the slippery material to the // right cells. if (x < resolution / 2) { // Each cell contains 2 triangles, therefore we have to add 2 entries to the // CompositeMaterial. compositeMaterial.Materials.Add(roughMaterial); compositeMaterial.Materials.Add(roughMaterial); } else { compositeMaterial.Materials.Add(slipperyMaterial); compositeMaterial.Materials.Add(slipperyMaterial); } } } ground.Material = compositeMaterial; // Create a few boxes on the height field. // The left boxes will roll or stop on the height field because of the high friction. // The right boxes will slide down because of the low friction. BoxShape boxShape = new BoxShape(1, 1, 1); for (int i = 0; i < 10; i++) { RigidBody body = new RigidBody(boxShape, null, roughMaterial) { Pose = new Pose(new Vector3F(-19 + i * 4, 10, -10)), }; Simulation.RigidBodies.Add(body); } }
// Called by the camera to apply the image effect void OnRenderImage(RenderTexture source, RenderTexture destination) { if (m_RenderSolidColorCmdBuf != null && SolidColorMaterial && CutoffMaterial && CompositeMaterial) { int rtW = source.width / m_DownSample; int rtH = source.height / m_DownSample; // Step 1. Draw Solid Color Target Object(使用 Command Buffer 代替之前的那个附加相机) SolidColorMaterial.SetColor("_Color", m_OutlineColor); if (m_SolidColorRT == null) { m_SolidColorRT = RenderTexture.GetTemporary(rtW, rtH, 0); m_SolidColorRT.filterMode = FilterMode.Bilinear; } Graphics.SetRenderTarget(m_SolidColorRT); Graphics.ExecuteCommandBuffer(m_RenderSolidColorCmdBuf); // Step 2. Copy to buffer2 if (m_Buffer2 == null) { m_Buffer2 = RenderTexture.GetTemporary(rtW, rtH, 0); m_Buffer2.filterMode = FilterMode.Bilinear; } Graphics.Blit(m_SolidColorRT, m_Buffer2); // Step 3. Blur if (m_Buffer3 == null) { m_Buffer3 = RenderTexture.GetTemporary(rtW, rtH, 0); m_Buffer3.filterMode = FilterMode.Bilinear; } bool oddEven = true; for (int i = 0; i < m_Iterations; i++) { if (oddEven) { FourTapCone(m_Buffer2, m_Buffer3, i); } else { FourTapCone(m_Buffer3, m_Buffer2, i); } oddEven = !oddEven; } RenderTexture blurBuffer = null; // 引用,指向 blur 的结果 buffer if (!oddEven) { blurBuffer = m_Buffer3; } else { blurBuffer = m_Buffer2; } // Step 4. 用 cutoffMaterial 进行 blurBuffer - solidColorRT 操作,得到线框 CutoffMaterial.SetTexture("_BlurredTex", blurBuffer); if (m_Buffer4 == null) { m_Buffer4 = RenderTexture.GetTemporary(rtW, rtH, 0); m_Buffer4.filterMode = FilterMode.Bilinear; } Graphics.Blit(m_SolidColorRT, m_Buffer4, CutoffMaterial); // Step 5.用 compositeMaterial 进行 原图 + 线框操作 CompositeMaterial.SetTexture("_SrcTex", source); CompositeMaterial.SetFloat("_OutlineStrength", m_OutlineStrength); Graphics.Blit(m_Buffer4, destination, CompositeMaterial); } else { Graphics.Blit(source, destination); } }