/// <summary> /// 动态添加鼠标的MehcanicalObject组件至root节点,生成其信息。添加StiffSpringForceField组件,连接目标物体和鼠标MechanicalObject /// </summary> private unsafe void MouseDragAdd() { if (!SofaPhysics.unityMechanicalObjectInfoDic.ContainsKey(mechanicalObjectName) && !SofaPhysics.sofaMechanicalObjectInfoDic.ContainsKey(mechanicalObjectName)) { //加入mechanicalObject组件及相关info double[] dou = new double[3] { draggingMousePosition.x, draggingMousePosition.y, draggingMousePosition.z }; SofaPhysicsAPI.AddMechanicalObjectArray(SofaPhysics.ToChar("root"), SofaPhysics.ToChar(mechanicalObjectName), dou, 1); for (int i = 0; i < SofaPhysics.nodes.Length; i++) { if (SofaPhysics.nodes[i].gameObject.name == "root") { UnityMechanicalObjectInfo unityMechanicalObjectInfo = new UnityMechanicalObjectInfo(SofaPhysics.nodes[i], mechanicalObjectName); SofaPhysics.unityMechanicalObjectInfoDic.Add(unityMechanicalObjectInfo.componentName, unityMechanicalObjectInfo); } } foreach (string componentName in SofaPhysics.unityMechanicalObjectInfoDic.Keys) { if (mechanicalObjectName == componentName) { SofaMechanicalObjectInfo sofaMechanicalObjectInfo = new SofaMechanicalObjectInfo(componentName); SofaPhysics.sofaMechanicalObjectInfoDic.Add(sofaMechanicalObjectInfo.componentName, sofaMechanicalObjectInfo); } } //加入stiffSpringForceField组件及其Spring SofaPhysicsAPI.AddStiffSpringForceField(SofaPhysics.ToChar("root"), SofaPhysics.ToChar(stiffSpringForceFieldName), SofaPhysics.ToChar("@./"), SofaPhysics.ToChar(GetSrcPathToRoot()), dragSpringKs, 0.1, 1); SofaPhysicsAPI.SpringAddToStiffSpringForceField(SofaPhysics.ToChar(stiffSpringForceFieldName), 0, draggingMechanicalObjectPointIndex, dragSpringKs, dragSpringKd, 0); } }
/// <summary> /// 初始化SofaMechanicalObject信息 /// </summary> private void SofaMechanicalObjectInfoInit() { foreach (string componentName in unityMechanicalObjectInfoDic.Keys) { SofaMechanicalObjectInfo sofaMechanicalObjectInfo = new SofaMechanicalObjectInfo(componentName); sofaMechanicalObjectInfoDic.Add(sofaMechanicalObjectInfo.componentName, sofaMechanicalObjectInfo); } }
public void Update(SofaMechanicalObjectInfo sofaMechanicalObjectInfo) { if (componentName == sofaMechanicalObjectInfo.componentName) { verticesPosition = new Vector3[sofaMechanicalObjectInfo.verticesAmount]; for (int i = 0; i < sofaMechanicalObjectInfo.verticesAmount; i++) { verticesPosition[i].x = sofaMechanicalObjectInfo.verticesPosition[i * 3 + 0]; verticesPosition[i].y = sofaMechanicalObjectInfo.verticesPosition[i * 3 + 1]; verticesPosition[i].z = sofaMechanicalObjectInfo.verticesPosition[i * 3 + 2]; } triangleTopology = new int[sofaMechanicalObjectInfo.triangleAmount * 3]; for (int i = 0; i < sofaMechanicalObjectInfo.triangleAmount; i++) { //1triangletopology = 3 vertices //1triangletopology = 1 triangles //由topology生成vertice! triangleTopology[i * 3 + 0] = sofaMechanicalObjectInfo.triangleTopology[i * 3 + 0]; triangleTopology[i * 3 + 1] = sofaMechanicalObjectInfo.triangleTopology[i * 3 + 1]; triangleTopology[i * 3 + 2] = sofaMechanicalObjectInfo.triangleTopology[i * 3 + 2]; } quadTopology = new int[sofaMechanicalObjectInfo.quadAmount * 6]; for (int i = 0; i < sofaMechanicalObjectInfo.quadAmount; i++) { //1quadtopology = 6 vertices //1quadtopology = 2 triangles //由topology生成vertice! quadTopology[i * 6 + 0] = sofaMechanicalObjectInfo.quadTopology[i * 4 + 0]; quadTopology[i * 6 + 1] = sofaMechanicalObjectInfo.quadTopology[i * 4 + 1]; quadTopology[i * 6 + 2] = sofaMechanicalObjectInfo.quadTopology[i * 4 + 2]; quadTopology[i * 6 + 3] = sofaMechanicalObjectInfo.quadTopology[i * 4 + 2]; quadTopology[i * 6 + 4] = sofaMechanicalObjectInfo.quadTopology[i * 4 + 3]; quadTopology[i * 6 + 5] = sofaMechanicalObjectInfo.quadTopology[i * 4 + 0]; } tetrahedraTopology = new int[sofaMechanicalObjectInfo.tetrahedraAmount * 12]; for (int i = 0; i < sofaMechanicalObjectInfo.tetrahedraAmount; i++) { //1tetrahedratopology = 12 vertices //1tetrahedratopology = 4 triangles //由topology生成vertice! tetrahedraTopology[i * 12 + 0] = sofaMechanicalObjectInfo.tetrahedraTopology[i * 4 + 0]; tetrahedraTopology[i * 12 + 1] = sofaMechanicalObjectInfo.tetrahedraTopology[i * 4 + 1]; tetrahedraTopology[i * 12 + 2] = sofaMechanicalObjectInfo.tetrahedraTopology[i * 4 + 2]; tetrahedraTopology[i * 12 + 3] = sofaMechanicalObjectInfo.tetrahedraTopology[i * 4 + 0]; tetrahedraTopology[i * 12 + 4] = sofaMechanicalObjectInfo.tetrahedraTopology[i * 4 + 2]; tetrahedraTopology[i * 12 + 5] = sofaMechanicalObjectInfo.tetrahedraTopology[i * 4 + 3]; tetrahedraTopology[i * 12 + 6] = sofaMechanicalObjectInfo.tetrahedraTopology[i * 4 + 0]; tetrahedraTopology[i * 12 + 7] = sofaMechanicalObjectInfo.tetrahedraTopology[i * 4 + 3]; tetrahedraTopology[i * 12 + 8] = sofaMechanicalObjectInfo.tetrahedraTopology[i * 4 + 1]; tetrahedraTopology[i * 12 + 9] = sofaMechanicalObjectInfo.tetrahedraTopology[i * 4 + 1]; tetrahedraTopology[i * 12 + 10] = sofaMechanicalObjectInfo.tetrahedraTopology[i * 4 + 3]; tetrahedraTopology[i * 12 + 11] = sofaMechanicalObjectInfo.tetrahedraTopology[i * 4 + 2]; } if (triangleDynamicMesh != null) { triangleDynamicMesh.vertices = verticesPosition; triangleDynamicMesh.triangles = triangleTopology; triangleDynamicMesh.RecalculateNormals(); triangleMeshCollider.sharedMesh = triangleDynamicMesh; triangleMeshRenderer.enabled = node.GetComponent <Sofa_MechanicalObject>().isRenderingTriangle; triangleMeshCollider.enabled = node.GetComponent <Sofa_MechanicalObject>().isTriangleCastingRay; } if (quadDynamicMesh != null) { quadDynamicMesh.vertices = verticesPosition; quadDynamicMesh.triangles = quadTopology; quadDynamicMesh.RecalculateNormals(); quadMeshCollider.sharedMesh = quadDynamicMesh; quadMeshRenderer.enabled = node.GetComponent <Sofa_MechanicalObject>().isRenderingQuad; quadMeshCollider.enabled = node.GetComponent <Sofa_MechanicalObject>().isQuadCastingRay; } if (tetrahedraDynamicMesh != null) { tetrahedraDynamicMesh.vertices = verticesPosition; tetrahedraDynamicMesh.triangles = tetrahedraTopology; tetrahedraDynamicMesh.RecalculateNormals(); tetrahedraMeshCollider.sharedMesh = tetrahedraDynamicMesh; tetrahedraMeshRenderer.enabled = node.GetComponent <Sofa_MechanicalObject>().isRenderingTetrahedra; tetrahedraMeshCollider.enabled = node.GetComponent <Sofa_MechanicalObject>().isTetrahedraCastingRay; } } else { Debug.Log("Unity MechanicalObject Info Update Failed : Soaf MechanicalObject Info Name Not Match."); } }