Explorar o código

Added PhysicsMaterial and PhysicsMesh to C#

BearishSun %!s(int64=10) %!d(string=hai) anos
pai
achega
e5382bda87

+ 10 - 2
BansheeCore/Include/BsCommonTypes.h

@@ -346,8 +346,16 @@ namespace BansheeEngine
 	/** Valid types of a mesh used for physics. */
 	/** Valid types of a mesh used for physics. */
 	enum class PhysicsMeshType
 	enum class PhysicsMeshType
 	{
 	{
-		Triangle, /** A regular triangle mesh. */
-		Convex /** Mesh representing a convex shape. */
+		/** 
+		 * A regular triangle mesh. Mesh can be of arbitrary size but cannot be used for triggers and non-kinematic 
+         * objects. Occurs a significantly larger performance impact than convex meshes.
+		 */
+		Triangle,
+		/** 
+		 * Mesh representing a convex shape. Mesh will not have more than 256 vertices. Occurs a significantly lower
+		 * performance impact than triangle meshes.
+		 */
+		Convex
 	};
 	};
 
 
 	/**	Texture addressing mode, per component. */
 	/**	Texture addressing mode, per component. */

+ 76 - 0
MBansheeEditor/Inspectors/PhysicsMaterialInspector.cs

@@ -0,0 +1,76 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+using System.Collections.Generic;
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    /// <summary>
+    /// Renders an inspector for the <see cref="PhysicsMaterial"/> resource.
+    /// </summary>
+    [CustomInspector(typeof(PhysicsMaterial))]
+    internal class PhysicsMaterialInspector : Inspector
+    {
+        private GUIFloatField staticFrictionField;
+        private GUIFloatField dynamicFrictionField;
+        private GUIFloatField restitutionField;
+
+        /// <inheritdoc/>
+        protected internal override void Initialize()
+        {
+            BuildGUI();
+        }
+
+        /// <inheritdoc/>
+        protected internal override InspectableState Refresh()
+        {
+            PhysicsMaterial material = InspectedObject as PhysicsMaterial;
+            if (material == null)
+                return InspectableState.NotModified;
+
+            staticFrictionField.Value = material.StaticFriction;
+            dynamicFrictionField.Value = material.DynamicFriction;
+            restitutionField.Value = material.Restitution;
+
+            return InspectableState.NotModified;
+        }
+
+        /// <summary>
+        /// Recreates all the GUI elements used by this inspector.
+        /// </summary>
+        private void BuildGUI()
+        {
+            Layout.Clear();
+
+            PhysicsMaterial material = InspectedObject as PhysicsMaterial;
+            if (material == null)
+                return;
+
+            staticFrictionField = new GUIFloatField(new LocEdString("Static friction"));
+            dynamicFrictionField = new GUIFloatField(new LocEdString("Dynamic friction"));
+            restitutionField = new GUIFloatField(new LocEdString("Restitution"));
+
+            staticFrictionField.OnChanged += x =>
+            {
+                material.StaticFriction = x;
+                EditorApplication.SetDirty(material);
+            };
+
+            dynamicFrictionField.OnChanged += x =>
+            {
+                material.DynamicFriction = x;
+                EditorApplication.SetDirty(material);
+            };
+
+            restitutionField.OnChanged += x =>
+            {
+                material.Restitution = x;
+                EditorApplication.SetDirty(material);
+            };
+            
+            Layout.AddElement(staticFrictionField);
+            Layout.AddElement(dynamicFrictionField);
+            Layout.AddElement(restitutionField);
+        }
+    }
+}

+ 25 - 0
MBansheeEditor/Inspectors/PhysicsMeshInspector.cs

@@ -0,0 +1,25 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    /// <summary>
+    /// Renders an inspector for the <see cref="PhysicsMesh"/> resource.
+    /// </summary>
+    [CustomInspector(typeof(PhysicsMesh))]
+    internal class PhysicsMeshInspector : Inspector
+    {
+        /// <inheritdoc/>
+        protected internal override void Initialize()
+        {
+            // No GUI for physics mesh resource
+        }
+
+        /// <inheritdoc/>
+        protected internal override InspectableState Refresh()
+        {
+            return InspectableState.NotModified;
+        }
+    }
+}

+ 2 - 0
MBansheeEditor/MBansheeEditor.csproj

@@ -48,6 +48,8 @@
     <Compile Include="ColorPicker.cs" />
     <Compile Include="ColorPicker.cs" />
     <Compile Include="EditorPersistentData.cs" />
     <Compile Include="EditorPersistentData.cs" />
     <Compile Include="Inspectors\GUIWidgetInspector.cs" />
     <Compile Include="Inspectors\GUIWidgetInspector.cs" />
+    <Compile Include="Inspectors\PhysicsMaterialInspector.cs" />
+    <Compile Include="Inspectors\PhysicsMeshInspector.cs" />
     <Compile Include="LogWindow.cs" />
     <Compile Include="LogWindow.cs" />
     <Compile Include="DefaultSize.cs" />
     <Compile Include="DefaultSize.cs" />
     <Compile Include="DialogBox.cs" />
     <Compile Include="DialogBox.cs" />

+ 2 - 0
MBansheeEngine/MBansheeEngine.csproj

@@ -109,6 +109,8 @@
     <Compile Include="MeshData.cs" />
     <Compile Include="MeshData.cs" />
     <Compile Include="MissingComponent.cs" />
     <Compile Include="MissingComponent.cs" />
     <Compile Include="PathEx.cs" />
     <Compile Include="PathEx.cs" />
+    <Compile Include="PhysicsMaterial.cs" />
+    <Compile Include="PhysicsMesh.cs" />
     <Compile Include="PixelData.cs" />
     <Compile Include="PixelData.cs" />
     <Compile Include="PixelUtility.cs" />
     <Compile Include="PixelUtility.cs" />
     <Compile Include="PlainText.cs" />
     <Compile Include="PlainText.cs" />

+ 87 - 0
MBansheeEngine/PhysicsMaterial.cs

@@ -0,0 +1,87 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+using System;
+using System.Runtime.CompilerServices;
+
+namespace BansheeEngine
+{
+    /// <summary>
+    /// Material that controls how two physical objects interact with each other. Materials of both objects are used during
+    /// their interaction and their combined values are used.
+    /// </summary>
+    public class PhysicsMaterial : Resource
+    {
+        /// <summary>
+        /// Constructor for internal use by the runtime.
+        /// </summary>
+        private PhysicsMaterial(int dummy)
+        { }
+
+        /// <summary>
+        /// Creates a brand new physics material.
+        /// </summary>
+        /// <param name="staticFriction">Controls friction when two in-contact objects are not moving lateral to each other.
+        ///                              </param>
+        /// <param name="dynamicFriction">Controls friction when two in-contact objects are moving lateral to each other.
+        ///                               </param>
+        /// <param name="restitution">Controls "bounciness" of an object during a collision. Value of 1 means the collision
+        /// is elastic, and value of 0 means the value is inelastic.</param>
+        public PhysicsMaterial(float staticFriction = 0.0f, float dynamicFriction = 0.0f, float restitution = 0.0f)
+        {
+            Internal_CreateInstance(this, staticFriction, dynamicFriction, restitution);
+        }
+
+        /// <summary>
+        /// Controls friction when two in-contact objects are not moving lateral to each other (e.g. how difficult is to 
+        /// get an object moving from a static state while it is in contact other object(s)).
+        /// </summary>
+        public float StaticFriction
+        {
+            get { return Internal_GetStaticFriction(mCachedPtr); }
+            set { Internal_SetStaticFriction(mCachedPtr, value); }
+        }
+
+
+        /// <summary>
+        /// Controls friction when two in-contact objects are moving lateral to each other (e.g. how quickly does an object
+        /// slow down when sliding along another object).
+        /// </summary>
+        public float DynamicFriction
+        {
+            get { return Internal_GetDynamicFriction(mCachedPtr); }
+            set { Internal_SetDynamicFriction(mCachedPtr, value); }
+        }
+
+        /// <summary>
+        /// Controls "bounciness" of an object during a collision. Value of 1 means the collision is elastic, and value
+        /// of 0 means the value is inelastic.
+        /// </summary>
+        public float Restitution
+        {
+            get { return Internal_GetRestitution(mCachedPtr); }
+            set { Internal_SetRestitution(mCachedPtr, value); }
+        }
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_CreateInstance(PhysicsMaterial instance, float staticFriction,
+            float dynamicFriction, float restitution);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern float Internal_GetStaticFriction(IntPtr thisPtr);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_SetStaticFriction(IntPtr thisPtr, float value);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern float Internal_GetDynamicFriction(IntPtr thisPtr);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_SetDynamicFriction(IntPtr thisPtr, float value);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern float Internal_GetRestitution(IntPtr thisPtr);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_SetRestitution(IntPtr thisPtr, float value);
+    }
+}

+ 59 - 0
MBansheeEngine/PhysicsMesh.cs

@@ -0,0 +1,59 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+using System;
+using System.Runtime.CompilerServices;
+
+namespace BansheeEngine
+{
+    /// <summary>
+    /// Mesh that is used purely for collision purposes and not rendering. For example as a collider or a trigger.
+    /// </summary>
+    public class PhysicsMesh : Resource
+    {
+        /// <summary>
+        /// Constructor for internal use by the runtime.
+        /// </summary>
+        private PhysicsMesh()
+        { }
+
+        /// <summary>
+        /// Retrieves the vertex and index data of the mesh.
+        /// </summary>
+        public MeshData MeshData
+        {
+            get { return Internal_GetMeshData(mCachedPtr); }
+        }
+
+        /// <summary>
+        /// Returns the type of the mesh.
+        /// </summary>
+        public PhysicsMeshType MeshType
+        {
+            get { return (PhysicsMeshType)Internal_GetMeshType(mCachedPtr); }
+        }
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern MeshData Internal_GetMeshData(IntPtr thisPtr);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern int Internal_GetMeshType(IntPtr thisPtr);
+    }
+
+    /// <summary>
+    /// Valid types of a mesh used for physics.
+    /// </summary>
+    public enum PhysicsMeshType
+    {
+        /// <summary>
+        /// A regular triangle mesh. Mesh can be of arbitrary size but cannot be used for triggers and non-kinematic 
+        /// objects. Occurs a significantly larger performance impact than convex meshes.
+        /// </summary>
+        Triangle,
+        /// <summary>
+        /// Mesh representing a convex shape. Mesh will not have more than 256 vertices. Occurs a significantly lower
+        /// performance impact than triangle meshes.
+        /// </summary>
+        Convex
+
+    }
+}