From 524edd4cc641fe168b2f0462e0caee3006f20f24 Mon Sep 17 00:00:00 2001 From: BobSong <605277374@qq.com> Date: Tue, 4 Nov 2025 23:33:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0rope=E7=BA=BF=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Rope/Examples/00_Main/Main.unity | 58 +++++++---------- Assets/Scripts/ThirdParty/Rope/Rope.cs | 64 +++++++++++++++++-- 2 files changed, 79 insertions(+), 43 deletions(-) diff --git a/Assets/Scripts/ThirdParty/Rope/Examples/00_Main/Main.unity b/Assets/Scripts/ThirdParty/Rope/Examples/00_Main/Main.unity index bbdc84149..27f932670 100644 --- a/Assets/Scripts/ThirdParty/Rope/Examples/00_Main/Main.unity +++ b/Assets/Scripts/ThirdParty/Rope/Examples/00_Main/Main.unity @@ -1184,11 +1184,9 @@ MonoBehaviour: isLoop: 0 material: {fileID: 2100000, guid: 189732c736fdb544f98524f96c34aff6, type: 2} shadowMode: 1 - customMesh: - mesh: {fileID: 0} - rotation: 90 - scale: {x: 1, y: 1, z: 1} - stretch: 0 + rendering: + useSimpleLineRenderer: 0 + simpleLineWidth: 0.02 spawnPoints: - x: 4 y: 0 @@ -2440,11 +2438,9 @@ MonoBehaviour: isLoop: 0 material: {fileID: 2100000, guid: 189732c736fdb544f98524f96c34aff6, type: 2} shadowMode: 1 - customMesh: - mesh: {fileID: 0} - rotation: 90 - scale: {x: 1, y: 1, z: 1} - stretch: 0 + rendering: + useSimpleLineRenderer: 0 + simpleLineWidth: 0.02 spawnPoints: - x: 0 y: 0 @@ -2578,11 +2574,9 @@ MonoBehaviour: isLoop: 0 material: {fileID: 2100000, guid: 189732c736fdb544f98524f96c34aff6, type: 2} shadowMode: 1 - customMesh: - mesh: {fileID: 0} - rotation: 90 - scale: {x: 1, y: 1, z: 1} - stretch: 0 + rendering: + useSimpleLineRenderer: 0 + simpleLineWidth: 0.02 spawnPoints: - x: 0 y: 0 @@ -3329,16 +3323,14 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 325b217b839086b4ca705834516bc0d5, type: 3} m_Name: m_EditorClassIdentifier: - radius: 0.04 + radius: 0.001 radialVertices: 6 isLoop: 0 material: {fileID: 2100000, guid: 85feedf64f0c16d4182fd4c33e342d2a, type: 2} shadowMode: 1 - customMesh: - mesh: {fileID: 4257529212018882713, guid: dc3ad6005b4f0514982b05eb88b9ed3f, type: 3} - rotation: 90 - scale: {x: 1, y: 1, z: 1} - stretch: 1 + rendering: + useSimpleLineRenderer: 1 + simpleLineWidth: 0.001 spawnPoints: - x: -0.90364826 y: -1.0066781 @@ -3491,11 +3483,9 @@ MonoBehaviour: isLoop: 1 material: {fileID: 2100000, guid: 189732c736fdb544f98524f96c34aff6, type: 2} shadowMode: 1 - customMesh: - mesh: {fileID: 0} - rotation: 90 - scale: {x: 1, y: 1, z: 1} - stretch: 0 + rendering: + useSimpleLineRenderer: 0 + simpleLineWidth: 0.02 spawnPoints: - x: 0 y: 0 @@ -3792,11 +3782,9 @@ MonoBehaviour: isLoop: 0 material: {fileID: 2100000, guid: 189732c736fdb544f98524f96c34aff6, type: 2} shadowMode: 1 - customMesh: - mesh: {fileID: 0} - rotation: 90 - scale: {x: 1, y: 1, z: 1} - stretch: 0 + rendering: + useSimpleLineRenderer: 0 + simpleLineWidth: 0.02 spawnPoints: - x: 0 y: 0 @@ -5224,11 +5212,9 @@ MonoBehaviour: isLoop: 0 material: {fileID: 2100000, guid: 189732c736fdb544f98524f96c34aff6, type: 2} shadowMode: 1 - customMesh: - mesh: {fileID: 0} - rotation: 90 - scale: {x: 1, y: 1, z: 1} - stretch: 0 + rendering: + useSimpleLineRenderer: 0 + simpleLineWidth: 0.02 spawnPoints: - x: 4 y: 0 diff --git a/Assets/Scripts/ThirdParty/Rope/Rope.cs b/Assets/Scripts/ThirdParty/Rope/Rope.cs index fbc46c6aa..f744e4667 100644 --- a/Assets/Scripts/ThirdParty/Rope/Rope.cs +++ b/Assets/Scripts/ThirdParty/Rope/Rope.cs @@ -75,6 +75,21 @@ namespace NBF [Tooltip("绳子使用的阴影投射模式")] public ShadowCastingMode shadowMode = ShadowCastingMode.On; + [System.Serializable] + public struct RenderingSettings + { + [Tooltip("使用简单线渲染而非完整网格渲染。适用于细绳如鱼线,性能更好。")] + public bool useSimpleLineRenderer; + + [Tooltip("简单线渲染的宽度。")] + public float simpleLineWidth; + } + + [Space] public RenderingSettings rendering = new RenderingSettings() + { + useSimpleLineRenderer = false, + simpleLineWidth = 0.02f + }; // public CustomMeshSettings customMesh = new CustomMeshSettings() // { @@ -142,6 +157,7 @@ namespace NBF solverIterations = 2, }; + [System.Serializable] public struct CollisionSettings { @@ -197,6 +213,8 @@ namespace NBF public ParticleTarget target; } + // 添加LineRenderer组件引用 + protected LineRenderer lineRenderer; protected bool initialized; protected bool computingSimulationFrame; protected bool simulationDisabledPrevFrame; @@ -268,6 +286,7 @@ namespace NBF { simulation.resolution = Mathf.Max(0.01f, simulation.resolution); simulation.massPerMeter = Mathf.Max(0.01f, simulation.massPerMeter); + rendering.simpleLineWidth = Mathf.Max(0.001f, rendering.simpleLineWidth); } /// @@ -774,6 +793,23 @@ namespace NBF return false; } + if (rendering.useSimpleLineRenderer) + { + lineRenderer = gameObject.GetComponent(); + if (lineRenderer == null) + { + lineRenderer = gameObject.AddComponent(); + } + + lineRenderer.useWorldSpace = true; + lineRenderer.loop = isLoop; + lineRenderer.widthMultiplier = rendering.simpleLineWidth; + lineRenderer.material = material; + lineRenderer.shadowCastingMode = shadowMode; + lineRenderer.receiveShadows = false; + lineRenderer.alignment = LineAlignment.View; + } + // 状态 ComputeRealCurve(Allocator.Persistent, out _measurements, out positions); @@ -1329,16 +1365,30 @@ namespace NBF } Profiler.BeginSample(nameof(SubmitToRenderer)); - - // 默认绳子圆柱体 - if (simulation.enabled) + if (rendering.useSimpleLineRenderer) { - mesh.SetVertices(vertices); - mesh.SetNormals(normals); - mesh.RecalculateBounds(); + // 使用简单线渲染 + if (lineRenderer != null && simulation.enabled) + { + lineRenderer.positionCount = positions.Length; + for (int i = 0; i < positions.Length; i++) + { + lineRenderer.SetPosition(i, positions[i]); + } + } } + else + { + // 默认绳子圆柱体 + if (simulation.enabled) + { + mesh.SetVertices(vertices); + mesh.SetNormals(normals); + mesh.RecalculateBounds(); + } - Graphics.DrawMesh(mesh, Matrix4x4.identity, material, gameObject.layer, null, 0, null, shadowMode); + Graphics.DrawMesh(mesh, Matrix4x4.identity, material, gameObject.layer, null, 0, null, shadowMode); + } Profiler.EndSample(); }