void ExecuteMeshDeformationJob(JellyBody _jellyBody) { NativeArray <Vector3> initialVertsAccess = new NativeArray <Vector3> (_jellyBody.InitialVerts, Allocator.TempJob); NativeArray <Vector3> displacedVertsAccess = new NativeArray <Vector3> (_jellyBody.DisplacedVerts, Allocator.TempJob); NativeArray <Vector3> vertVelocitiesAccess = new NativeArray <Vector3> (_jellyBody.VertVelocities, Allocator.TempJob); MeshDeformationJob meshDeformationJob = new MeshDeformationJob { initialVerts = initialVertsAccess, displacedVerts = displacedVertsAccess, vertVelocities = vertVelocitiesAccess, springforce = (useManualSettings) ? _jellyBody.Stiffness : stiffness, dampening = (useManualSettings) ? _jellyBody.Attenuation : attenuation, unitformScale = _jellyBody.UnitformScale, time = Time.deltaTime }; JobHandle meshDeformationJobHandle = meshDeformationJob.Schedule (_jellyBody.DisplacedVerts.Length, _jellyBody.DisplacedVerts.Length); meshDeformationJobHandle.Complete(); initialVertsAccess.CopyTo(_jellyBody.InitialVerts); initialVertsAccess.Dispose(); displacedVertsAccess.CopyTo(_jellyBody.DisplacedVerts); displacedVertsAccess.Dispose(); vertVelocitiesAccess.CopyTo(_jellyBody.VertVelocities); vertVelocitiesAccess.Dispose(); _jellyBody.JellyMesh.vertices = _jellyBody.DisplacedVerts; }
void Update() { for (int i = 0; i < jelliedBodies.Count; i++) { JellyBody currentJellyBody = jelliedBodies[i].GetComponent <JellyBody>(); UpdateVerts(currentJellyBody); } }
void Update() { for (int i = 0; i < jelliedBodies.Count; i++) { JellyBody currentJellybody = jelliedBodies[i].GetComponent <JellyBody>(); if (currentJellybody.DisplacedVerts.Length != 0) { ExecuteMeshDeformationJob(currentJellybody); } } }
private void CheckForMouseInput() { if (Input.GetMouseButton(0)) { ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hitInfo)) { jellyBody = hitInfo.collider.gameObject.GetComponent <JellyBody>(); if (jellyBody != null) { inputPoint = hitInfo.point; inputPoint += hitInfo.normal * 0.1f; jellyBody.AddPointForce(pressureForce, inputPoint); } } } }
void UpdateVerts(JellyBody _jellyBody) { for (int i = 0; i < _jellyBody.DisplacedVerts.Length; i++) { Vector3 velocity = _jellyBody.VertVelocities[i]; Vector3 displacement = _jellyBody.DisplacedVerts[i] - _jellyBody.InitialVerts[i]; float springforce = (useManualSettings) ? _jellyBody.Stiffness : stiffness; float dampening = (useManualSettings) ? _jellyBody.Attenuation : attenuation; displacement *= _jellyBody.UnitformScale; velocity -= displacement * springforce * Time.deltaTime; velocity *= _jellyBody.UnitformScale - dampening * Time.deltaTime; _jellyBody.VertVelocities[i] = velocity; _jellyBody.DisplacedVerts[i] += velocity * (Time.deltaTime / _jellyBody.UnitformScale); } _jellyBody.JellyMesh.vertices = _jellyBody.DisplacedVerts; _jellyBody.JellyMesh.RecalculateNormals(); }
public void CheckForTouch() { if (Input.GetMouseButton(0)) { ray = battleCamera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hitInfo)) { jellyBody = hitInfo.collider.gameObject.GetComponent <JellyBody>(); if (jellyBody != null) { inputPoint = hitInfo.point; inputPoint += hitInfo.normal * 0.1f; jellyBody.AddPointForce(pressureForce, inputPoint); //どのエネルギーにタッチしたか、そのIDを返す //IDは個々にpublicで設定 jellyBodyStatus = JellyBody.Status.touched; energyID = jellyBody.id; } } } }
public void AddForceToVerts(Vector3[] _contactPoints, float _force, GameObject _jelliesObject, JellyBody _jellyBody) { Vector3 currentPoint; for (int i = 0; i < _contactPoints.Length; i++) { currentPoint = _jelliesObject.transform.InverseTransformPoint(_contactPoints[i]); for (int j = 0; j < _jellyBody.DisplacedVerts.Length; j++) { Vector3 pointToVert = (_jellyBody.DisplacedVerts[j] - currentPoint) * _jellyBody.UnitformScale; if (!useManualSettings) { float attenuatedForce = (_force / ((_jellyBody.Attenuation) / attenuatedForceParam_1)) / (1f + pointToVert.sqrMagnitude); float velocity = attenuatedForce * Time.deltaTime; _jellyBody.VertVelocities[j] += pointToVert.normalized * velocity; } else { float attenuatedForce = (_force / ((attenuation) / attenuatedForceParam_1)) / (1f + pointToVert.sqrMagnitude); float velocity = attenuatedForce * Time.deltaTime; _jellyBody.VertVelocities[j] += pointToVert.normalized * velocity; } } } }
void OnGUI() { GUILayout.Label("itsKristin's Jellyfier", EditorStyles.boldLabel); GUILayout.Space(10); GUILayout.Label("Browse through the prefabs in your Project!", EditorStyles.label); GUILayout.Label("Decide weather or not you want them to be jellyfied.", EditorStyles.label); GUILayout.Space(15); int previousPadding = GUI.skin.window.padding.bottom; GUI.skin.window.padding.bottom = -20; Rect windowRect = GUILayoutUtility.GetRect(1f, 17f); windowRect.x += 4f; windowRect.width -= 7f; editorMode = GUI.SelectionGrid(windowRect, editorMode, editorModes, 2, "window"); GUI.skin.window.padding.bottom = previousPadding; GetPrefabs(); switch (editorMode) { case 0: if (usedPrefabs.Count == 0 && possiblePrefabs.Count == 0) { GUILayout.Label("Your project doesn't seem to have any suitable Prefabs yet!", EditorStyles.boldLabel); } else if (usedPrefabs.Count == 0 && possiblePrefabs.Count != 0) { GUILayout.Label("There are no jellied prefabs yet.", EditorStyles.boldLabel); } else { GUILayout.Label("Preview:", EditorStyles.boldLabel); if (previewGameObject != null) { if (previewGameObject.GetComponent <JellyBody>()) { previewJelly = previewGameObject.GetComponent <JellyBody>(); } else if (previewGameObject.GetComponentInChildren <JellyBody>()) { previewJelly = previewGameObject.GetComponentInChildren <JellyBody>(); } if (previewEditor == null) { previewEditor = Editor.CreateEditor(previewGameObject); } previewEditor.OnInteractivePreviewGUI(GUILayoutUtility.GetRect(256f, 256f), GUIStyle.none); } GUILayout.Label("Jelly Settings:", EditorStyles.boldLabel); using (new EditorGUI.DisabledScope(!previewGameObject)) { GUILayout.Label("Jelly Consistency:", EditorStyles.boldLabel); GUILayout.Label("REMEBER: If you toggle automatic setup all personalized settings will be ignored!", EditorStyles.label); setupType = (JellyManagement.UseManualSettings) ? 0 : 1; setupType = GUILayout.SelectionGrid(setupType, setupTypes, 2, "Toggle"); JellyManagement.UseManualSettings = (setupType == 0) ? false : true; using (new EditorGUI.DisabledScope(setupType == 0)) { if (previewJelly) { GUILayout.Label("Physics Settings:", EditorStyles.boldLabel); previewJelly.ReactToGravity = EditorGUILayout.Toggle("React to Gravity", previewJelly.ReactToGravity); previewJelly.AllowRotation = EditorGUILayout.Toggle("Allow Rotation", previewJelly.AllowRotation); GUILayout.Space(10); previewJelly.Stiffness = EditorGUILayout.FloatField("Stiffness", previewJelly.Stiffness); previewJelly.Attenuation = EditorGUILayout.FloatField("Attenuation", previewJelly.Attenuation); } } } GUILayout.Space(10); GUILayout.Label("Jellyfied Prefabs:", EditorStyles.boldLabel); usedPrefabsScroll = GUILayout.BeginScrollView(usedPrefabsScroll); for (int i = 0; i < usedPrefabs.Count; i++) { GUILayout.BeginVertical(); GUILayout.Label(PrefabName(usedPrefabs[i]), EditorStyles.miniBoldLabel); GUILayout.Label(usedPrefabs[i], GUILayout.Width(position.width / 2)); GUILayout.BeginHorizontal(); if (GUILayout.Button("Select", GUILayout.Width(position.width / 2 - 10))) { Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(usedPrefabs[i]); previewGameObject = (GameObject)AssetDatabase.LoadMainAssetAtPath(usedPrefabs[i]); previewEditor = Editor.CreateEditor(previewGameObject); previewEditor.OnInteractivePreviewGUI(GUILayoutUtility.GetRect(256, 256), GUIStyle.none); } if (GUILayout.Button("Unjellify", GUILayout.Width(position.width / 2 - 10))) { Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(usedPrefabs[i]); previewGameObject = (GameObject)AssetDatabase.LoadMainAssetAtPath(usedPrefabs[i]); Unjellify(previewGameObject); GetPrefabs(); Repaint(); previewGameObject = null; } GUILayout.EndHorizontal(); GUILayout.EndVertical(); } GUILayout.EndScrollView(); } break; case 1: if (usedPrefabs.Count == 0 && possiblePrefabs.Count == 0) { GUILayout.Label("Your project doesn't seem to have any suitable Prefabs yet!", EditorStyles.boldLabel); } else if (usedPrefabs.Count != 0 && possiblePrefabs.Count == 0) { GUILayout.Label("There are no suitable prefabs that aren't jellyfied.", EditorStyles.boldLabel); } else { GUILayout.Label("Preview:", EditorStyles.boldLabel); if (previewGameObject != null) { if (previewEditor == null) { previewEditor = Editor.CreateEditor(previewGameObject); } previewEditor.OnInteractivePreviewGUI(GUILayoutUtility.GetRect(256f, 256f), GUIStyle.none); } GUILayout.Space(10); GUILayout.Label("Suitable Prefabs:", EditorStyles.boldLabel); usedPrefabsScroll = GUILayout.BeginScrollView(possiblePrefabsScroll); for (int i = 0; i < possiblePrefabs.Count; i++) { GUILayout.BeginVertical(); GUILayout.Label(PrefabName(possiblePrefabs[i]), EditorStyles.miniBoldLabel); GUILayout.Label(possiblePrefabs[i], GUILayout.Width(position.width / 2)); GUILayout.BeginHorizontal(); if (GUILayout.Button("Select", GUILayout.Width(position.width / 2 - 10))) { Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(possiblePrefabs[i]); previewGameObject = (GameObject)AssetDatabase.LoadMainAssetAtPath(possiblePrefabs[i]); previewEditor = Editor.CreateEditor(previewGameObject); previewEditor.OnInteractivePreviewGUI(GUILayoutUtility.GetRect(256, 256), GUIStyle.none); } if (GUILayout.Button("Jellify", GUILayout.Width(position.width / 2 - 10))) { Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(possiblePrefabs[i]); previewGameObject = (GameObject)AssetDatabase.LoadMainAssetAtPath(possiblePrefabs[i]); Jellify(previewGameObject); GetPrefabs(); Repaint(); previewGameObject = null; } GUILayout.EndHorizontal(); GUILayout.EndVertical(); } GUILayout.EndScrollView(); } break; } }