public void CreateRiver(string gameObjectName, Vector3 position) { #if RAM_2019 RiverSettings riverSettings = editor.extension.riverSettings; GameObject parentGameObject = editor.extension.transform.gameObject; // create new gameobject RamSpline spline = RamSpline.CreateSpline(AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat")); // ensure gameobject gets reparented if this was a context click (otherwise does nothing) GameObjectUtility.SetParentAndAlign(spline.gameObject, parentGameObject); // set position spline.transform.position = Vector3.zero; // keeping it unique for the gameobject; using the point position we'd have to adjust the river points // apply river settings spline.width = 100; spline.currentProfile = riverSettings.splineProfile; spline.width = Random.Range(riverSettings.widthMin, riverSettings.widthMax); // apply simulation settings spline.AddPoint(position); spline.simulatedRiverLength = Random.Range(riverSettings.simulatedRiverLengthMin, riverSettings.simulatedRiverLengthMax); spline.simulatedRiverPoints = Random.Range(riverSettings.simulatedRiverPointsMin, riverSettings.simulatedRiverPointsMax); spline.simulatedMinStepSize = Random.Range(riverSettings.simulatedMinStepSizeMin, riverSettings.simulatedMinStepSizeMax); spline.simulatedNoUp = riverSettings.simulatedNoUp; spline.simulatedBreakOnUp = riverSettings.simulatedBreakOnUp; spline.noiseWidth = riverSettings.noiseWidth; spline.simulatedMinStepSize = Random.Range(riverSettings.simulatedMinStepSizeMin, riverSettings.simulatedMinStepSizeMax); spline.simulatedMinStepSize = Random.Range(riverSettings.simulatedMinStepSizeMin, riverSettings.simulatedMinStepSizeMax); spline.noiseMultiplierWidth = Random.Range(riverSettings.noiseMultiplierWidthMin, riverSettings.noiseMultiplierWidthMax); spline.noiseSizeWidth = Random.Range(riverSettings.noiseSizeWidthMin, riverSettings.noiseSizeWidthMax); // simulate bool generate = riverSettings.automation == RiverSettings.Automation.Generate; spline.SimulateRiver(generate); // register the creation in the undo system Undo.RegisterCreatedObjectUndo(spline.gameObject, "Create " + spline.gameObject.name); #endif }
public static RamSpline CreateSpline(Material splineMaterial = null, List <Vector4> positions = null) { GameObject gameObject = new GameObject("RamSpline"); RamSpline ramSpline = gameObject.AddComponent <RamSpline>(); MeshRenderer meshRenderer = gameObject.AddComponent <MeshRenderer>(); meshRenderer.receiveShadows = false; meshRenderer.shadowCastingMode = ShadowCastingMode.Off; if (splineMaterial != null) { meshRenderer.sharedMaterial = splineMaterial; } if (positions != null) { for (int i = 0; i < positions.Count; i++) { ramSpline.AddPoint(positions[i]); } } return(ramSpline); }
public static RamSpline CreateSpline(Material splineMaterial = null, List <Vector4> positions = null) { GameObject gameObject = new GameObject(nameof(RamSpline)); RamSpline ramSpline = (RamSpline)gameObject.AddComponent <RamSpline>(); MeshRenderer meshRenderer = (MeshRenderer)gameObject.AddComponent <MeshRenderer>(); ((Renderer)meshRenderer).set_receiveShadows(false); ((Renderer)meshRenderer).set_shadowCastingMode((ShadowCastingMode)0); if (Object.op_Inequality((Object)splineMaterial, (Object)null)) { ((Renderer)meshRenderer).set_sharedMaterial(splineMaterial); } if (positions != null) { for (int index = 0; index < positions.Count; ++index) { ramSpline.AddPoint(positions[index]); } } return(ramSpline); }
private void GenerateMesh(ref Mesh mesh) { foreach (Transform transform in this.meshesPartTransforms) { if (transform != null) { UnityEngine.Object.DestroyImmediate(transform.gameObject); } } int num = this.points.Count - 1; int count = this.points.Count; int num2 = this.vertsInShape * count; List <int> list = new List <int>(); Vector3[] array = new Vector3[num2]; Vector3[] array2 = new Vector3[num2]; Vector2[] array3 = new Vector2[num2]; Vector2[] array4 = new Vector2[num2]; Vector2[] array5 = new Vector2[num2]; if (this.colors == null || this.colors.Length != num2) { this.colors = new Color[num2]; for (int i = 0; i < this.colors.Length; i++) { this.colors[i] = Color.black; } } if (this.colorsFlowMap.Count != num2) { this.colorsFlowMap.Clear(); } this.length = 0f; this.fulllength = 0f; if (this.beginningSpline != null) { this.length = this.beginningSpline.length; } this.minMaxWidth = 1f; this.uvWidth = 1f; this.uvBeginning = 0f; if (this.beginningSpline != null) { this.minMaxWidth = this.beginningMaxWidth - this.beginningMinWidth; this.uvWidth = this.minMaxWidth * this.beginningSpline.uvWidth; this.uvBeginning = this.beginningSpline.uvWidth * this.beginningMinWidth + this.beginningSpline.uvBeginning; } else if (this.endingSpline != null) { this.minMaxWidth = this.endingMaxWidth - this.endingMinWidth; this.uvWidth = this.minMaxWidth * this.endingSpline.uvWidth; this.uvBeginning = this.endingSpline.uvWidth * this.endingMinWidth + this.endingSpline.uvBeginning; } for (int j = 0; j < this.pointsDown.Count; j++) { float num3 = this.widths[j]; if (j > 0) { this.fulllength += this.uvWidth * Vector3.Distance(this.pointsDown[j], this.pointsDown[j - 1]) / (this.uvScale * num3); } } float num4 = Mathf.Round(this.fulllength); for (int k = 0; k < this.pointsDown.Count; k++) { float num5 = this.widths[k]; int num6 = k * this.vertsInShape; if (k > 0) { this.length += this.uvWidth * Vector3.Distance(this.pointsDown[k], this.pointsDown[k - 1]) / (this.uvScale * num5) / this.fulllength * num4; } float num7 = 0f; float num8 = 0f; for (int l = 0; l < this.vertsInShape; l++) { int num9 = num6 + l; float num10 = (float)l / (float)(this.vertsInShape - 1); if (num10 < 0.5f) { num10 *= this.minVal * 2f; } else { num10 = ((num10 - 0.5f) * (1f - this.maxVal) + 0.5f * this.maxVal) * 2f; } if (k == 0 && this.beginningSpline != null && this.beginningSpline.verticesEnding != null && this.beginningSpline.normalsEnding != null) { int num11 = (int)((float)this.beginningSpline.vertsInShape * this.beginningMinWidth); array[num9] = this.beginningSpline.verticesEnding[Mathf.Clamp(l + num11, 0, this.beginningSpline.verticesEnding.Count - 1)] + this.beginningSpline.transform.position - base.transform.position; } else if (k == this.pointsDown.Count - 1 && this.endingSpline != null && this.endingSpline.verticesBeginning != null && this.endingSpline.normalsBeginning != null) { int num12 = (int)((float)this.endingSpline.vertsInShape * this.endingMinWidth); array[num9] = this.endingSpline.verticesBeginning[Mathf.Clamp(l + num12, 0, this.endingSpline.verticesBeginning.Count - 1)] + this.endingSpline.transform.position - base.transform.position; } else { array[num9] = Vector3.Lerp(this.pointsDown[k], this.pointsUp[k], num10); RaycastHit raycastHit; if (Physics.Raycast(array[num9] + base.transform.position + Vector3.up * 5f, Vector3.down, out raycastHit, 1000f, this.snapMask.value)) { array[num9] = Vector3.Lerp(array[num9], raycastHit.point - base.transform.position + new Vector3(0f, 0.1f, 0f), (Mathf.Sin(3.14159274f * this.snaps[k] - 1.57079637f) + 1f) * 0.5f); } RaycastHit raycastHit2; if (this.normalFromRaycast && Physics.Raycast(this.points[k] + base.transform.position + Vector3.up * 5f, Vector3.down, out raycastHit2, 1000f, this.snapMask.value)) { array2[num9] = raycastHit2.normal; } Vector3[] array6 = array; int num13 = num9; array6[num13].y = array6[num13].y + Mathf.Lerp(this.controlPointsMeshCurves[Mathf.FloorToInt(this.lerpValues[k])].Evaluate(num10), this.controlPointsMeshCurves[Mathf.CeilToInt(this.lerpValues[k])].Evaluate(num10), this.lerpValues[k] - Mathf.Floor(this.lerpValues[k])); } if (k > 0 && k < 5 && this.beginningSpline != null && this.beginningSpline.verticesEnding != null) { array[num9].y = (array[num9].y + array[num9 - this.vertsInShape].y) * 0.5f; } if (k == this.pointsDown.Count - 1 && this.endingSpline != null && this.endingSpline.verticesBeginning != null) { for (int m = 1; m < 5; m++) { array[num9 - this.vertsInShape * m].y = (array[num9 - this.vertsInShape * (m - 1)].y + array[num9 - this.vertsInShape * m].y) * 0.5f; } } if (k == 0) { this.verticesBeginning.Add(array[num9]); } if (k == this.pointsDown.Count - 1) { this.verticesEnding.Add(array[num9]); } if (!this.normalFromRaycast) { array2[num9] = this.orientations[k] * Vector3.up; } if (k == 0) { this.normalsBeginning.Add(array2[num9]); } if (k == this.pointsDown.Count - 1) { this.normalsEnding.Add(array2[num9]); } if (l > 0) { num7 = num10 * this.uvWidth; num8 = num10; } if (this.beginningSpline != null || this.endingSpline != null) { num7 += this.uvBeginning; } num7 /= this.uvScale; float num14 = this.FlowCalculate(num8, array2[num9].y); int num15 = 10; if (this.beginnigChildSplines.Count > 0 && k <= num15) { float num16 = 0f; int n = 0; while (n < this.endingChildSplines.Count) { RamSpline ramSpline = this.endingChildSplines[n]; if (ramSpline == null) { this.endingChildSplines.RemoveAt(n); } else { n++; if (Mathf.CeilToInt(ramSpline.endingMaxWidth * (float)(this.vertsInShape - 1)) >= l && l >= Mathf.CeilToInt(ramSpline.endingMinWidth * (float)(this.vertsInShape - 1))) { num16 = (float)(l - Mathf.CeilToInt(ramSpline.endingMinWidth * (float)(this.vertsInShape - 1))) / (float)(Mathf.CeilToInt(ramSpline.endingMaxWidth * (float)(this.vertsInShape - 1)) - Mathf.CeilToInt(ramSpline.endingMinWidth * (float)(this.vertsInShape - 1))); num16 = this.FlowCalculate(num16, array2[num9].y); } } } if (k > 0) { num14 = Mathf.Lerp(num14, num16, 1f - (float)k / (float)num15); } else { num14 = num16; } } if (k >= this.pointsDown.Count - num15 - 1 && this.endingChildSplines.Count > 0) { float num17 = 0f; int num18 = 0; while (num18 < this.endingChildSplines.Count) { RamSpline ramSpline2 = this.endingChildSplines[num18]; if (ramSpline2 == null) { this.endingChildSplines.RemoveAt(num18); } else { num18++; if (Mathf.CeilToInt(ramSpline2.beginningMaxWidth * (float)(this.vertsInShape - 1)) >= l && l >= Mathf.CeilToInt(ramSpline2.beginningMinWidth * (float)(this.vertsInShape - 1))) { num17 = (float)(l - Mathf.CeilToInt(ramSpline2.beginningMinWidth * (float)(this.vertsInShape - 1))) / (float)(Mathf.CeilToInt(ramSpline2.beginningMaxWidth * (float)(this.vertsInShape - 1)) - Mathf.CeilToInt(ramSpline2.beginningMinWidth * (float)(this.vertsInShape - 1))); num17 = this.FlowCalculate(num17, array2[num9].y); } } } if (k < this.pointsDown.Count - 1) { num14 = Mathf.Lerp(num14, num17, (float)(k - (this.pointsDown.Count - num15 - 1)) / (float)num15); } else { num14 = num17; } } float num19 = -(num8 - 0.5f) * 0.01f; if (this.uvRotation) { if (!this.invertUVDirection) { array3[num9] = new Vector2(1f - this.length, num7); array4[num9] = new Vector2(1f - this.length / this.fulllength, num8); array5[num9] = new Vector2(num14, num19); } else { array3[num9] = new Vector2(1f + this.length, num7); array4[num9] = new Vector2(1f + this.length / this.fulllength, num8); array5[num9] = new Vector2(num14, num19); } } else if (!this.invertUVDirection) { array3[num9] = new Vector2(num7, 1f - this.length); array4[num9] = new Vector2(num8, 1f - this.length / this.fulllength); array5[num9] = new Vector2(num19, num14); } else { array3[num9] = new Vector2(num7, 1f + this.length); array4[num9] = new Vector2(num8, 1f + this.length / this.fulllength); array5[num9] = new Vector2(num19, num14); } if (this.colorsFlowMap.Count <= num9) { this.colorsFlowMap.Add(array5[num9]); } else if (!this.overrideFlowMap) { this.colorsFlowMap[num9] = array5[num9]; } } } for (int num20 = 0; num20 < num; num20++) { int num21 = num20 * this.vertsInShape; for (int num22 = 0; num22 < this.vertsInShape - 1; num22++) { int item = num21 + num22; int item2 = num21 + num22 + this.vertsInShape; int item3 = num21 + num22 + 1 + this.vertsInShape; int item4 = num21 + num22 + 1; list.Add(item); list.Add(item2); list.Add(item3); list.Add(item3); list.Add(item4); list.Add(item); } } mesh = new Mesh(); mesh.Clear(); mesh.vertices = array; mesh.normals = array2; mesh.uv = array3; mesh.uv3 = array4; mesh.uv4 = this.colorsFlowMap.ToArray(); mesh.triangles = list.ToArray(); mesh.colors = this.colors; mesh.RecalculateTangents(); this.meshfilter.mesh = mesh; base.GetComponent <MeshRenderer>().enabled = true; if (this.generateMeshParts) { this.GenerateMeshParts(mesh); } }
public override void OnInspectorGUI() { EditorGUILayout.Space(); logo = (Texture2D)Resources.Load("logoRAM"); Color baseCol = GUI.color; spline = (RamSpline)target; scrollPos = EditorGUILayout.BeginScrollView(scrollPos); { GUIContent btnTxt = new GUIContent(logo); var rt = GUILayoutUtility.GetRect(btnTxt, GUI.skin.label, GUILayout.ExpandWidth(false)); rt.center = new Vector2(EditorGUIUtility.currentViewWidth / 2, rt.center.y); GUI.Button(rt, btnTxt, GUI.skin.label); EditorGUILayout.HelpBox("Add Point - CTRL + Left Mouse Button Click", MessageType.Info); EditorGUI.indentLevel++; EditorGUILayout.BeginHorizontal(); showPositions = EditorGUILayout.Foldout(showPositions, "Points"); if (GUILayout.Button("Add point at end")) { Undo.RecordObject(spline, "Add point"); int i = spline.controlPoints.Count - 1; Vector4 position = Vector3.zero; position.w = spline.width; if (i < spline.controlPoints.Count - 1 && spline.controlPoints.Count > i + 1) { position = spline.controlPoints [i]; Vector4 positionSecond = spline.controlPoints [i + 1]; if (Vector3.Distance((Vector3)positionSecond, (Vector3)position) > 0) { position = (position + positionSecond) * 0.5f; } else { position.x += 1; } } else if (spline.controlPoints.Count > 1 && i == spline.controlPoints.Count - 1) { position = spline.controlPoints [i]; Vector4 positionSecond = spline.controlPoints [i - 1]; if (Vector3.Distance((Vector3)positionSecond, (Vector3)position) > 0) { position = position + (position - positionSecond); } else { position.x += 1; } } else if (spline.controlPoints.Count > 0) { position = spline.controlPoints [i]; position.x += 1; } spline.controlPoints.Add(position); spline.GenerateSpline(); } EditorGUILayout.EndHorizontal(); EditorGUI.BeginChangeCheck(); if (showPositions) { EditorGUI.indentLevel++; for (int i = 0; i < spline.controlPoints.Count; i++) { EditorGUILayout.BeginHorizontal(); spline.controlPoints [i] = EditorGUILayout.Vector4Field("", spline.controlPoints [i]); if (spline.controlPoints [i].w <= 0) { Vector4 vec4 = spline.controlPoints [i]; vec4.w = 1; spline.controlPoints [i] = vec4; } if (GUILayout.Button(new GUIContent("A", "Add point after this point"))) { Undo.RecordObject(spline, "Add point"); Vector4 position = spline.controlPoints [i]; if (i < spline.controlPoints.Count - 1 && spline.controlPoints.Count > i + 1) { Vector4 positionSecond = spline.controlPoints [i + 1]; if (Vector3.Distance((Vector3)positionSecond, (Vector3)position) > 0) { position = (position + positionSecond) * 0.5f; } else { position.x += 1; } } else if (spline.controlPoints.Count > 1 && i == spline.controlPoints.Count - 1) { Vector4 positionSecond = spline.controlPoints [i - 1]; if (Vector3.Distance((Vector3)positionSecond, (Vector3)position) > 0) { position = position + (position - positionSecond); } else { position.x += 1; } } else { position.x += 1; } spline.controlPoints.Insert(i + 1, position); spline.GenerateSpline(); } if (GUILayout.Button(new GUIContent("R", "Remove this Point"))) { Undo.RecordObject(spline, "Remove point"); spline.controlPoints.RemoveAt(i); spline.GenerateSpline(); } if (GUILayout.Toggle(selectedPosition == i, new GUIContent("S", "Select point"), "Button")) { selectedPosition = i; } else if (selectedPosition == i) { selectedPosition = -1; } EditorGUILayout.EndHorizontal(); } EditorGUI.indentLevel--; EditorGUILayout.Space(); } EditorGUI.indentLevel--; string meshResolution = "Triangles density"; if (spline.meshfilter != null && spline.meshfilter.sharedMesh != null) { float tris = spline.meshfilter.sharedMesh.triangles.Length; meshResolution += " (" + tris + " tris)"; } else if (spline.meshfilter != null && spline.meshfilter.sharedMesh == null) { spline.GenerateSpline(); } EditorGUILayout.LabelField(meshResolution); EditorGUI.indentLevel++; spline.traingleDensity = 1 / (float)EditorGUILayout.IntSlider("U", (int)(1 / (float)spline.traingleDensity), 1, 100); spline.vertsInShape = EditorGUILayout.IntSlider("V", spline.vertsInShape - 1, 1, 20) + 1; EditorGUI.indentLevel--; spline.uvScale = EditorGUILayout.FloatField("UV scale (texture tiling)", spline.uvScale); if (EditorGUI.EndChangeCheck()) { spline.GenerateSpline(); } EditorGUILayout.BeginHorizontal(); { spline.width = EditorGUILayout.FloatField("River width", spline.width); if (GUILayout.Button("Change width for whole river")) { if (spline.width > 0) { Undo.RecordObject(spline, "Add point"); for (int i = 0; i < spline.controlPoints.Count; i++) { Vector4 point = spline.controlPoints [i]; point.w = spline.width; spline.controlPoints [i] = point; } spline.GenerateSpline(); } } } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); if (GUILayout.Button("Set basic river material")) { try { string materialName = "RAM_River_Material_Gamma"; if (PlayerSettings.colorSpace == ColorSpace.Linear) { materialName = "RAM_River_Material_Linear"; } Material riverMat = (Material)Resources.Load(materialName); if (riverMat != null) { spline.GetComponent <MeshRenderer> ().sharedMaterial = riverMat; } } catch { } } if (GUILayout.Button("Set basic river material with tesselation")) { try { string materialName = "RAM_River_Material_Gamma_Tess"; if (PlayerSettings.colorSpace == ColorSpace.Linear) { materialName = "RAM_River_Material_Linear_Tess"; } Material riverMat = (Material)Resources.Load(materialName); if (riverMat != null) { spline.GetComponent <MeshRenderer> ().sharedMaterial = riverMat; } } catch { } } if (GUILayout.Button("Set basic river material - vertex color")) { try { string materialName = "RAM_River_Material_Gamma_Vertex_Color"; if (PlayerSettings.colorSpace == ColorSpace.Linear) { materialName = "RAM_River_Material_Linear_Vertex_Color"; } Material riverMat = (Material)Resources.Load(materialName); if (riverMat != null) { spline.GetComponent <MeshRenderer> ().sharedMaterial = riverMat; } } catch { } } if (GUILayout.Button("Set basic river material with tesselation - vertex color")) { try { string materialName = "RAM_River_Material_Gamma_Tess_Vertex_Color"; if (PlayerSettings.colorSpace == ColorSpace.Linear) { materialName = "RAM_River_Material_Linear_Tess_Vertex_Color"; } Material riverMat = (Material)Resources.Load(materialName); if (riverMat != null) { spline.GetComponent <MeshRenderer> ().sharedMaterial = riverMat; } } catch { } } EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUI.indentLevel++; spline.showAdditionalOptions = EditorGUILayout.Foldout(spline.showAdditionalOptions, "Additional options"); if (spline.showAdditionalOptions) { GUIStyle buttonEditingStyle = new GUIStyle(GUI.skin.GetStyle("Button")); buttonEditingStyle.fontStyle = FontStyle.Bold; buttonEditingStyle.fontSize = 15; Color guiBaseColor = GUI.color; GUI.color = Color.green; if (!spline.drawOnMesh && GUILayout.Button("Start drawing vertex color", buttonEditingStyle)) { spline.drawOnMesh = true; } GUI.color = Color.red; if (spline.drawOnMesh && GUILayout.Button("Stop drawing vertex color", buttonEditingStyle)) { spline.drawOnMesh = false; } GUI.color = guiBaseColor; if (spline.drawOnMesh) { EditorGUILayout.HelpBox("R - Slow Water G - Small Cascade B - Big Cascade A - Opacity", MessageType.Info); spline.drawColor = EditorGUILayout.ColorField("Draw color", spline.drawColor); spline.opacity = EditorGUILayout.FloatField("Opacity", spline.opacity); spline.drawSize = EditorGUILayout.FloatField("Size", spline.drawSize); if (spline.drawSize < 0) { spline.drawSize = 0; } } if (!spline.showVertexColors) { if (GUILayout.Button("Show vertex colors")) { spline.oldMaterial = spline.GetComponent <MeshRenderer> ().sharedMaterial; spline.GetComponent <MeshRenderer> ().sharedMaterial = new Material(Shader.Find("Debug/Vertex color")); spline.showVertexColors = true; } } else { if (GUILayout.Button("Hide vertex colors")) { spline.GetComponent <MeshRenderer> ().sharedMaterial = spline.oldMaterial; spline.showVertexColors = false; } } EditorGUILayout.Space(); EditorGUILayout.Space(); if (GUILayout.Button("Set object pivot to center")) { Vector3 center = spline.meshfilter.sharedMesh.bounds.center; ChangePivot(center); } EditorGUILayout.BeginHorizontal(); { if (GUILayout.Button("Set object pivot position")) { ChangePivot(pivotChange - spline.transform.position); } pivotChange = EditorGUILayout.Vector3Field("", pivotChange); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); if (GUILayout.Button(new GUIContent("Regenerate spline", "Racalculates whole mesh"))) { spline.GenerateSpline(); } EditorGUILayout.Space(); if (GUILayout.Button("Export as mesh")) { string path = EditorUtility.SaveFilePanelInProject("Save river mesh", "", "asset", "Save river mesh"); if (path.Length != 0 && spline.meshfilter.sharedMesh != null) { AssetDatabase.CreateAsset(spline.meshfilter.sharedMesh, path); AssetDatabase.Refresh(); spline.GenerateSpline(); } } } } EditorGUILayout.EndScrollView(); }