public void Draw(Shader shader, MeshMode mode) { foreach (var mesh in Meshes) { mesh.Draw(shader, mode); } }
//次のモードを調べる MeshMode NextMode(Vector3 pos, MeshMode nowMode) { if (nowMode == MeshMode.Wide) { if (Physics.Linecast(pos + (forward * 0.95f) + up, pos + (forward * 0.95f), blockMask)) { return(MeshMode.Wide); } else { return(MeshMode.Curve); } } else { if (Physics.Linecast(pos + up - (forward * 0.05f), pos - (forward * 0.05f), blockMask)) { return(MeshMode.Wide); } else { return(MeshMode.Height); } } }
public void Draw(Shader shader, MeshMode mode) { if ((mode & MeshMode.Solid) == MeshMode.Solid) { shader.SetBool("wireframe", 0); // set textures int diffuseNr = 1; int specularNr = 1; for (int i = 0; i < Textures.Length; i++) { GL.ActiveTexture(TextureUnit.Texture0 + i); //activate proper texture unit before binding // retrieve texture number string number = ""; string name = Textures[i].Type; if (name == "texture_diffuse") // TODO: fix names in shaders { number = diffuseNr++.ToString(); } else if (name == "texture_specular") { number = specularNr++.ToString(); } shader.SetInt("material." + name + number, i); GL.BindTexture(TextureTarget.Texture2D, Textures[i].ID); } GL.ActiveTexture(TextureUnit.Texture0); // set colors shader.SetVector3("material.ambientCol", new Vector3(Color.Ambient.R, Color.Ambient.G, Color.Ambient.B)); shader.SetVector3("material.diffuseCol", new Vector3(Color.Diffuse.R, Color.Diffuse.G, Color.Diffuse.B)); shader.SetVector3("material.specularCol", new Vector3(Color.Specular.R, Color.Specular.G, Color.Specular.B)); shader.SetFloat("material.shininess", Color.Shininess); // set drawing mode shader.SetBool("material.textured", (uint)(Textures.Length == 0 ? 0 : 1)); // draw mesh GL.BindVertexArray(VAO); GL.DrawElements(BeginMode.Triangles, Indices.Length, DrawElementsType.UnsignedInt, 0); GL.BindVertexArray(0); } if ((mode & MeshMode.Wireframe) == MeshMode.Wireframe) { shader.SetBool("wireframe", 1); // draw mesh GL.BindVertexArray(VAO); GL.DrawElements(BeginMode.LineLoop, Indices.Length, DrawElementsType.UnsignedInt, 0); GL.BindVertexArray(0); } if (mode == 0) { // TODO: maybe notify the user about drawing nothing? } }
public bool HasIndexBufferRange(MeshMode meshMode) { if (meshMode == MeshMode.NotSet) { return(false); } return(indexBufferRanges[(int)meshMode] != null); }
public TileMeshSettings (IVector2 tiles, int tileResolution, float tileSize, MeshMode meshMode, TextureFormat textureFormat) { Tiles = tiles; TileResolution = tileResolution; TileSize = tileSize; MeshMode = meshMode; TextureFormat = textureFormat; }
public TileMeshSettings(int tilesX, int tilesY, int tileResolution, float tileSize, MeshMode meshMode, TextureFormat textureFormat) { TilesX = tilesX; TilesY = tilesY; TileResolution = tileResolution; TileSize = tileSize; MeshMode = meshMode; TextureFormat = textureFormat; }
public static void Draw(SerializedObject serializedObject, MeshMode meshMode) { bool value = GUIFoldout.Draw("Mesh Mode", meshMode); if (value == false) { return; } EditorGUI.indentLevel++; SerializedProperty meshModeEnable = serializedObject.FindProperty("meshMode.enable"); SerializedProperty meshModeAlpha = serializedObject.FindProperty("meshMode.alpha"); SerializedProperty meshModeShader = serializedObject.FindProperty("meshMode.shader"); EditorGUILayout.PropertyField(meshModeEnable, new GUIContent("Enable")); meshModeAlpha.floatValue = EditorGUILayout.Slider("Alpha", meshModeAlpha.floatValue, 0, 1); EditorGUILayout.PropertyField(meshModeShader, new GUIContent("Shader")); if (meshModeShader.intValue == (int)MeshModeShader.Custom) { bool value2 = GUIFoldout.Draw("Materials", meshMode.materials); if (value2) { EditorGUI.indentLevel++; int count = meshMode.materials.Length; count = EditorGUILayout.IntSlider("Material Count", count, 0, 10); if (count != meshMode.materials.Length) { System.Array.Resize(ref meshMode.materials, count); } for (int id = 0; id < meshMode.materials.Length; id++) { Material material = meshMode.materials[id]; material = (Material)EditorGUILayout.ObjectField("Material", material, typeof(Material), true); meshMode.materials[id] = material; } EditorGUI.indentLevel--; } } GUISortingLayer.Draw(serializedObject, meshMode.sortingLayer, "meshMode."); EditorGUI.indentLevel--; }
Vector3 Size(Vector3 start, Vector3 end, MeshMode meshMode) { if (meshMode == MeshMode.Height) { return(new Vector3(1f, (start - end).magnitude, 1f)); } else { return(new Vector3(1f, 1f, (start - end).magnitude)); } }
public State(State old) { Camera = old.Camera; Viewport = old.Viewport; Mesh = old.Mesh; Material = old.Material; MeshMode = old.MeshMode; Program = old.Program; VertexBuffer = old.VertexBuffer; IndexBuffer = old.IndexBuffer; IndexBufferRange = old.IndexBufferRange; VertexStream = old.VertexStream; }
Vector3 Center(Vector3 start, Vector3 end, MeshMode meshMode) { float rangeZ = new Vector3(start.x - transform.position.x, 0f, start.z - transform.position.z).magnitude; float rangeY = start.y - transform.position.y; if (meshMode == MeshMode.Height) { return(new Vector3(0.5f, -(start - end).magnitude / 2 + rangeY, -0.5f + rangeZ)); } else { return(new Vector3(0.5f, 0.5f + rangeY, (start - end).magnitude / 2 + rangeZ)); } }
public State(State old) { Camera = old.Camera; Viewport = old.Viewport; Model = old.Model; Frame = old.Frame; Mesh = old.Mesh; Material = old.Material; Program = old.Program; MeshMode = old.MeshMode; VertexBuffer = old.VertexBuffer; IndexBuffer = old.IndexBuffer; AttributeBindings = old.AttributeBindings; }
private void OnEnable() { m_tileMap = (TileMapBehaviour)target; m_tileSheet = m_tileMap.TileSheet; var meshSettings = m_tileMap.MeshSettings; if (meshSettings != null) { m_tilesX = meshSettings.TilesX; m_tilesY = meshSettings.TilesY; m_tileResolution = meshSettings.TileResolution; m_tileSize = meshSettings.TileSize; m_meshMode = meshSettings.MeshMode; m_textureFormat = meshSettings.TextureFormat; } m_activeInEditMode = m_tileMap.ActiveInEditMode; }
public IBufferRange FindOrCreateIndexBufferRange( MeshMode meshMode, IBuffer buffer, BeginMode beginMode ) { if (HasIndexBufferRange(meshMode) == false) { var indexBufferRange = buffer.CreateIndexBufferRange(beginMode); indexBufferRanges[(int)meshMode] = indexBufferRange; return(indexBufferRange); } else { var indexBufferRange = indexBufferRanges[(int)meshMode]; return(indexBufferRanges[(int)meshMode]); } }
void OnGUI() { GUILayout.BeginVertical(GUI.skin.box); MakeRangeField("Num points", ref numPoints); meshMode = (MeshMode)EditorGUILayout.EnumPopup("Generation mode", meshMode); nRelaxIter = EditorGUILayout.IntField("Relax iters", nRelaxIter); stepAngle = EditorGUILayout.FloatField(new GUIContent("Step angle", "Starting maximum point angle change on single repel iteration"), stepAngle); stepReduction = EditorGUILayout.FloatField(new GUIContent("Step reduction", "Reduction factor of maximum angle change for each subsequent iteration"), stepReduction); folder = EditorGUILayout.TextField("Path to meshes", folder.Trim('/')); numMeshesToGenerate = EditorGUILayout.IntField("Num meshes", numMeshesToGenerate); bool generateButtonPressed = GUILayout.Button("Generate meshes"); GUILayout.EndVertical(); if (generateButtonPressed) { StartGeneratingMeshes(); } }
//次の座標の更新 Vector3 NextPosUpdate(Vector3 pos) { nowModeLog = nowMode; nowMode = NextMode(pos, nowMode); int i = 0; Vector3 move; if (nowMode == MeshMode.Wide) { move = forward; } else { move = down; } MeshMode log = nowMode; do { if (nowMode == MeshMode.Height && (pos + (down * (i - 1))).y < -6f) { break; } nowMode = NextMode(pos + (move * i), nowMode); i++; }while (nowMode == nowModeLog); nowMode = log; if (nowMode == MeshMode.Wide || nowMode == MeshMode.Curve) { return(pos + (forward * (i - 1))); } else { return(pos + (down * (i - 1))); } }
void RenderToScreen() { Mesh mesh = quadMesh; ProgramDeprecated program = texturedProgram; MeshMode meshMode = MeshMode.PolygonFill; GL.ClearColor(0.5f, 0.5f, 0.5f, 1.0f); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit); UseViewport(windowViewport); program.Use(); // We animate one uniform every frame, so we also need to upload uniforms // This covers changes to camera uniforms caused by resizes double time = (double)(System.Environment.TickCount); float t = (float)(System.Math.Sin(time * 0.01) * 0.5f + 0.5f); (parameters["t"] as Floats).Set(t); program.ApplyUniforms(); var attributeBindings = mesh.AttributeBindings(program, meshMode); SetupAttributeBindings(attributeBindings); EnsureUploaded(mesh); BufferRange vertexBufferRange = mesh.VertexBufferRange; BufferRange indexBufferRange = mesh.IndexBufferRange(MeshMode.PolygonFill); GL.DrawElementsBaseVertex( indexBufferRange.BeginMode, (int)indexBufferRange.Count, indexBufferRange.Buffer.DrawElementsType, (IntPtr)(indexBufferRange.OffsetBytes), vertexBufferRange.BaseVertex ); DisableAttributeBindings(attributeBindings); }
void GenerateMesh(string name) { ConvexPolyhedra.Generator gen = new ConvexPolyhedra.Generator(); int exactNumPoints = Random.Range(numPoints.Min, numPoints.Max); Vector3[] points = gen.GeneratePointsOnSphere(exactNumPoints); int nearest = points.Length - 1; points = gen.FindRelaxedConfigurationOfPointsOnSphere(points, nearest, ConvexPolyhedra.Generator.InverseLinearRepel, stepAngle, stepReduction, nRelaxIter); List <Vector3> vertices = new List <Vector3>(); List <int> tris = new List <int>(); MeshMode currentMode = meshMode; if (currentMode == MeshMode.Both) { currentMode = Random.value < 0.5 ? MeshMode.ConvexHull : MeshMode.PlaneCut; } if (currentMode == MeshMode.ConvexHull) { gen.GenerateConvexHullTriangles(points, vertices, tris); } else { gen.GeneratePolyTriangles(points, vertices, tris); } Mesh mesh = new Mesh(); mesh.SetVertices(vertices); mesh.SetTriangles(tris, 0); mesh.RecalculateNormals(); AssetDatabase.CreateAsset(mesh, "Assets/" + folder + "/" + name + ".asset"); AssetDatabase.SaveAssets(); }
void RenderToTexture() { Mesh mesh = sphereMesh; ProgramDeprecated program = diffuseProgram; MeshMode meshMode = MeshMode.PolygonFill; framebuffer.Begin(); GL.ClearColor(0.72f, 0.72f, 0.72f, 1.0f); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); UseViewport(framebuffer); program.Use(); // We do not change any uniforms for this program so no need to reapply them //bindings.Apply(program); var attributeBindings = mesh.AttributeBindings(program, meshMode); SetupAttributeBindings(attributeBindings); EnsureUploaded(mesh); BufferRange vertexBufferRange = mesh.VertexBufferRange; BufferRange indexBufferRange = mesh.IndexBufferRange(MeshMode.PolygonFill); GL.DrawElementsBaseVertex( indexBufferRange.BeginMode, (int)indexBufferRange.Count, indexBufferRange.Buffer.DrawElementsType, (IntPtr)(indexBufferRange.OffsetBytes), vertexBufferRange.BaseVertex ); DisableAttributeBindings(attributeBindings); framebuffer.End(); }
public Mesh MeshCreate(Vector3 start, Vector3 end, MeshMode mode, bool flag) { Mesh createMesh = new Mesh(); start -= transform.position; end -= transform.position; if (mode == MeshMode.Wide) { createMesh.vertices = VertexPosWide(start, end); createMesh.triangles = Triangle(createMesh.vertices); createMesh.uv = UvMapWideCurve(createMesh.vertices); } else if (mode == MeshMode.Height) { createMesh.vertices = VertexPosHight(start, end); createMesh.triangles = Triangle(createMesh.vertices); createMesh.uv = UvMapHight(createMesh.vertices); } else { if (flag) { createMesh.vertices = VertexPosCurve2(start, end); } else { createMesh.vertices = VertexPosCurve(start, end); } createMesh.triangles = Triangle(createMesh.vertices); createMesh.uv = UvMapWideCurve(createMesh.vertices); } createMesh.RecalculateNormals(); return(createMesh); }
public override void OnInspectorGUI() { // base.OnInspectorGUI(); m_showMapSettings = EditorGUILayout.Foldout(m_showMapSettings, "Map Settings"); if (m_showMapSettings) { m_activeInEditMode = EditorGUILayout.Toggle("Active In Edit Mode", m_activeInEditMode); if (m_tileMap.ActiveInEditMode != m_activeInEditMode) { m_tileMap.ActiveInEditMode = m_activeInEditMode; OnSceneGUI(); // force redraw map editor box } m_tilesX = EditorGUILayout.IntField( new GUIContent("Tiles X", "The number of tiles on the X axis"), m_tilesX); m_tilesY = EditorGUILayout.IntField( new GUIContent("Tiles Y", "The number of tiles on the Y axis"), m_tilesY); m_tileResolution = EditorGUILayout.IntField( new GUIContent("Tile Resolution", "The number of pixels along each axis on one tile"), m_tileResolution); m_tileSize = EditorGUILayout.FloatField( new GUIContent("Tile Size", "The size of one tile in Unity units"), m_tileSize); m_meshMode = (MeshMode)EditorGUILayout.EnumPopup("Mesh Mode", m_meshMode); // these settings only apply to the single quad mode mesh if (m_meshMode == MeshMode.SingleQuad) { m_textureFormat = (TextureFormat)EditorGUILayout.EnumPopup("Texture Format", m_textureFormat); m_textureFilterMode = (FilterMode)EditorGUILayout.EnumPopup("Filter Mode", m_textureFilterMode); } if (GUILayout.Button("Create/Recreate Mesh")) { m_tileMap.MeshSettings = new TileMeshSettings(m_tilesX, m_tilesY, m_tileResolution, m_tileSize, m_meshMode, m_textureFormat); // if settings didnt change the mesh wouldnt be created, force creation if (!m_tileMap.HasMesh) m_tileMap.CreateMesh(); } if (GUILayout.Button("Destroy Mesh (keep data)")) m_tileMap.DestroyMesh(); } m_showPickerSettings = EditorGUILayout.Foldout(m_showPickerSettings, "Tile Picker Settings"); if (m_showPickerSettings) { m_tilePickerXCount = Mathf.Clamp(EditorGUILayout.IntField( new GUIContent("Items Per Row", "The number of items to draw in a row in the Tile Picker Window."), m_tilePickerXCount), 1, int.MaxValue); m_tilePickerPosition.width = Mathf.Clamp(EditorGUILayout.FloatField( new GUIContent("Picker Width", "The width of the Tile Picker Window."), m_tilePickerPosition.width), 64f, float.MaxValue); m_tilePickerPosition.height = Mathf.Clamp(EditorGUILayout.FloatField( new GUIContent("Picker Height", "The height of the Tile Picker Window."), m_tilePickerPosition.height), 128f, float.MaxValue); } bool prominentImportArea = m_tileSheet.Ids.Count() == 0; m_showSprites = EditorGUILayout.Foldout(m_showSprites || prominentImportArea, "Sprites:"); if (m_showSprites || prominentImportArea) { ShowImportDropArea(); } if (m_showSprites && !prominentImportArea) { if (GUILayout.Button("Delete all")) { foreach (var id in m_tileMap.TileSheet.Ids) m_tileMap.TileSheet.Remove(id); m_thumbnailCache.Clear(); } if (GUILayout.Button("Refresh thumbnails")) m_thumbnailCache.Clear(); m_sortSpritesByName = GUILayout.Toggle(m_sortSpritesByName, "Sort sprites by name"); var ids = m_tileSheet.Ids.ToList(); ids.Sort(); for (int i = 0; i < ids.Count; i++) { var sprite = m_tileSheet.Get(ids[i]); ShowSprite(sprite); // add separators, except below the last one // could probably find a better looking one if (i < (ids.Count - 1)) GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1)); } } EditorUtility.SetDirty(this); }
public TileMeshSettings(int tilesX, int tilesY, int tileResolution, float tileSize, MeshMode meshMode) : this(tilesX, tilesY, tileResolution, tileSize, meshMode, TextureFormat.RGBA32) { }
void SetAttr() { _meshMode_ = _meshMode; _lineThickness_ = _lineThickness; _lineMaterial_ = _lineMaterial; _lineSubdivide_ = _lineSubdivide; }
public override void OnInspectorGUI() { // base.OnInspectorGUI(); m_showMapSettings = EditorGUILayout.Foldout(m_showMapSettings, "Map Settings"); if (m_showMapSettings) { m_activeInEditMode = EditorGUILayout.Toggle("Active In Edit Mode", m_activeInEditMode); m_tilesX = EditorGUILayout.IntField( new GUIContent("Tiles X", "The number of tiles on the X axis"), m_tilesX); m_tilesY = EditorGUILayout.IntField( new GUIContent("Tiles Y", "The number of tiles on the Y axis"), m_tilesY); m_tileResolution = EditorGUILayout.IntField( new GUIContent("Tile Resolution", "The number of pixels along each axis on one tile"), m_tileResolution); if (m_tileResolution != m_tileMap.MeshSettings.TileResolution) { EditorGUILayout.HelpBox("Changing tile resolution will clear the current tile setup.\n" + string.Format("Current tile resolution is {0}.", m_tileMap.MeshSettings.TileResolution), MessageType.Warning); } m_tileSize = EditorGUILayout.FloatField( new GUIContent("Tile Size", "The size of one tile in Unity units"), m_tileSize); m_meshMode = (MeshMode)EditorGUILayout.EnumPopup("Mesh Mode", m_meshMode); // these settings only apply to the single quad mode mesh if (m_meshMode == MeshMode.SingleQuad) { m_textureFormat = (TextureFormat)EditorGUILayout.EnumPopup("Texture Format", m_textureFormat); m_textureFilterMode = (FilterMode)EditorGUILayout.EnumPopup("Filter Mode", m_textureFilterMode); } if (GUILayout.Button("Create/Recreate Mesh")) { bool canDelete = true; if (m_tileResolution != m_tileMap.MeshSettings.TileResolution) { canDelete = ShowTileDeletionWarning(); } if (canDelete) { m_tileMap.MeshSettings = new TileMeshSettings(m_tilesX, m_tilesY, m_tileResolution, m_tileSize, m_meshMode, m_textureFormat); // if settings didnt change the mesh wouldnt be created, force creation if (!m_tileMap.HasMesh) { m_tileMap.CreateMesh(); } m_activeInEditMode = true; } } if (GUILayout.Button("Destroy Mesh (keep data)")) { m_tileMap.DestroyMesh(); m_activeInEditMode = false; } if (GUILayout.Button("Clear")) { if (ShowTileDeletionWarning()) { m_tileMap.DestroyMesh(); m_tileMap.ClearTiles(); m_activeInEditMode = false; } } if (m_tileMap.ActiveInEditMode != m_activeInEditMode) { m_tileMap.ActiveInEditMode = m_activeInEditMode; OnSceneGUI(); // force redraw map editor box } } m_showSortSettings = EditorGUILayout.Foldout(m_showSortSettings, "Sort Settings"); if (m_showSortSettings) { TileMeshBehaviour mesh = m_tileMap.GetComponentInChildren<TileMeshBehaviour>(); // When first creating tile map or after deleting the mesh, // it will not be accessible until it's created by user. if (mesh != null && mesh.GetComponent<Renderer>() != null) { EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); string[] sortingLayers = GetSortingLayerNames(); int currentLayer = 0; bool isLayerSet = mesh.GetComponent<Renderer>().sortingLayerName.Length > 0; if (! isLayerSet) { currentLayer = FindStringIndex(ref sortingLayers, "Default"); } else { currentLayer = FindStringIndex(ref sortingLayers, mesh.GetComponent<Renderer>().sortingLayerName); } int chosenLayer = EditorGUILayout.Popup("Sorting Layer Name", Mathf.Max(currentLayer, 0), sortingLayers); if (EditorGUI.EndChangeCheck() || ! isLayerSet) { mesh.GetComponent<Renderer>().sortingLayerName = sortingLayers[chosenLayer]; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); int order = EditorGUILayout.IntField("Sorting Order", mesh.GetComponent<Renderer>().sortingOrder); if (EditorGUI.EndChangeCheck()) { mesh.GetComponent<Renderer>().sortingOrder = order; } EditorGUILayout.EndHorizontal(); } else { EditorGUILayout.HelpBox("Mesh has not been created.\nPlease use 'Create/Recreate Mesh' button in the Map Settings.", MessageType.Info, true); } } m_showPickerSettings = EditorGUILayout.Foldout(m_showPickerSettings, "Tile Picker Settings"); if (m_showPickerSettings) { m_tilePickerXCount = Mathf.Clamp(EditorGUILayout.IntField( new GUIContent("Items Per Row", "The number of items to draw in a row in the Tile Picker Window."), m_tilePickerXCount), 1, int.MaxValue); m_tilePickerPosition.width = Mathf.Clamp(EditorGUILayout.FloatField( new GUIContent("Picker Width", "The width of the Tile Picker Window."), m_tilePickerPosition.width), 64f, float.MaxValue); m_tilePickerPosition.height = Mathf.Clamp(EditorGUILayout.FloatField( new GUIContent("Picker Height", "The height of the Tile Picker Window."), m_tilePickerPosition.height), 128f, float.MaxValue); } bool prominentImportArea = m_tileSheet.Ids.Count() == 0; m_showSprites = EditorGUILayout.Foldout(m_showSprites || prominentImportArea, "Sprites"); if (m_showSprites || prominentImportArea) { ShowImportDropArea(); } if (m_showSprites && !prominentImportArea) { if (GUILayout.Button("Delete all")) { foreach (var id in m_tileMap.TileSheet.Ids) m_tileMap.TileSheet.Remove(id); m_thumbnailCache.Clear(); } if (GUILayout.Button("Refresh thumbnails")) m_thumbnailCache.Clear(); m_sortSpritesByName = GUILayout.Toggle(m_sortSpritesByName, "Sort sprites by name"); var ids = m_tileSheet.Ids.ToList(); ids.Sort(); for (int i = 0; i < ids.Count; i++) { var sprite = m_tileSheet.Get(ids[i]); ShowSprite(sprite); // add separators, except below the last one // could probably find a better looking one if (i < (ids.Count - 1)) GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1)); } } EditorUtility.SetDirty(this); }
public One(Vector3 s, Vector3 e, MeshMode m) { meshMode = m; start = now = s; end = e; }
public void UpdateLightSprite(LightSprite2D id, MeshMode meshMode) { if (id.GetSprite() == null) { Free(); return; } if (meshModeMaterial != meshMode.materials) { meshModeMaterial = meshMode.materials; ClearMaterial(); } if (meshModeShader != meshMode.shader) { meshModeShader = meshMode.shader; ClearMaterial(); } Material[] material = GetMaterials(); if (material == null) { return; } float rotation = id.lightSpriteTransform.rotation; if (id.lightSpriteTransform.applyRotation) { rotation += id.transform.rotation.eulerAngles.z; } ////////////////////// Scale Vector2 scale = Vector2.zero; Sprite sprite = id.GetSprite(); Rect spriteRect = sprite.textureRect; scale.x = (float)sprite.texture.width / spriteRect.width; scale.y = (float)sprite.texture.height / spriteRect.height; Vector2 size = id.lightSpriteTransform.scale; size.x *= 2; size.y *= 2; size.x /= scale.x; size.y /= scale.y; size.x *= (float)sprite.texture.width / (sprite.pixelsPerUnit * 2); size.y *= (float)sprite.texture.height / (sprite.pixelsPerUnit * 2); if (id.spriteRenderer.flipX) { size.x = -size.x; } if (id.spriteRenderer.flipY) { size.y = -size.y; } ////////////////////// PIVOT Rect rect = spriteRect; Vector2 pivot = sprite.pivot; pivot.x /= spriteRect.width; pivot.y /= spriteRect.height; pivot.x -= 0.5f; pivot.y -= 0.5f; pivot.x *= size.x; pivot.y *= size.y; float pivotDist = Mathf.Sqrt(pivot.x * pivot.x + pivot.y * pivot.y); float pivotAngle = Mathf.Atan2(pivot.y, pivot.x); float rot = rotation * Mathf.Deg2Rad + Mathf.PI; Vector2 position = Vector2.zero; // Pivot Pushes Position position.x += Mathf.Cos(pivotAngle + rot) * pivotDist * id.transform.lossyScale.x; position.y += Mathf.Sin(pivotAngle + rot) * pivotDist * id.transform.lossyScale.y; position.x += id.transform.position.x; position.y += id.transform.position.y; position.x += id.lightSpriteTransform.position.x; position.y += id.lightSpriteTransform.position.y; Vector3 pos = position; pos.z = id.transform.position.z - 0.1f; transform.position = pos; Vector3 scale2 = id.transform.lossyScale; scale2.x *= size.x; scale2.y *= size.y; scale2.x /= 2; scale2.y /= 2; scale2.z = 1; transform.localScale = scale2; transform.rotation = Quaternion.Euler(0, 0, rotation); Rect uvRect = new Rect(); uvRect.x = rect.x / sprite.texture.width; uvRect.y = rect.y / sprite.texture.height; uvRect.width = rect.width / sprite.texture.width + uvRect.x; uvRect.height = rect.height / sprite.texture.height + uvRect.y; if (meshRenderer != null) { Color lightColor = id.color; lightColor.a = id.meshMode.alpha; for (int i = 0; i < materials.Length; i++) { if (materials[i] == null) { continue; } materials[i].SetColor("_TintColor", lightColor); materials[i].color = lightColor; materials[i].mainTexture = id.GetSprite().texture; } id.meshMode.sortingLayer.ApplyToMeshRenderer(meshRenderer); meshRenderer.sharedMaterials = materials; meshRenderer.enabled = true; Mesh mesh = GetMeshSprite(); Vector2[] uvs = mesh.uv; uvs[0].x = uvRect.x; uvs[0].y = uvRect.y; uvs[1].x = uvRect.width; uvs[1].y = uvRect.y; uvs[2].x = uvRect.width; uvs[2].y = uvRect.height; uvs[3].x = uvRect.x; uvs[3].y = uvRect.height; mesh.uv = uvs; meshFilter.mesh = mesh; } }
} // \todo use constructor to set VertexBuffer? public IBufferRange IndexBufferRange(MeshMode meshMode) { return(indexBufferRanges[(int)meshMode]); }
public void ReplaceMesh() { _meshMode = MeshMode.Unsupported; _particleSystem = GetComponent <ParticleSystem>(); if (_particleSystem != null) { _particles = new ParticleSystem.Particle[_particleSystem.maxParticles]; ParticleSystemRenderer particleRenderer = GetComponent <ParticleSystemRenderer>(); if (particleRenderer != null) { if (particleRenderer.renderMode == ParticleSystemRenderMode.Mesh) { if (!particleRenderer.mesh.isReadable) { Debug.LogWarning(name + ": 无法贴地表,因为Mesh数据不可读写!"); } else { _meshMode = MeshMode.ParticleRendered; _mesh = Instantiate(particleRenderer.mesh); _mesh.MarkDynamic(); _rawVertices = _mesh.vertices; _vertices = _mesh.vertices; particleRenderer.mesh = _mesh; } } } } else { MeshRenderer meshRenderer = GetComponent <MeshRenderer>(); if (meshRenderer != null && meshRenderer.enabled) { MeshFilter meshFilter = GetComponent <MeshFilter>(); if (meshFilter != null) { if (!meshFilter.sharedMesh.isReadable) { Debug.LogWarning(name + ": 无法贴地表,因为Mesh数据不可读写!"); } else { _meshMode = MeshMode.MeshRendered; _mesh = meshFilter.mesh; _mesh.MarkDynamic(); _rawVertices = _mesh.vertices; _vertices = _mesh.vertices; } } } } if (_meshMode == MeshMode.Unsupported) { enabled = false; } }
public override void OnInspectorGUI() { // base.OnInspectorGUI(); m_showMapSettings = EditorGUILayout.Foldout(m_showMapSettings, "Map Settings"); if (m_showMapSettings) { m_activeInEditMode = EditorGUILayout.Toggle("Active In Edit Mode", m_activeInEditMode); if (m_tileMap.ActiveInEditMode != m_activeInEditMode) { m_tileMap.ActiveInEditMode = m_activeInEditMode; OnSceneGUI(); // force redraw map editor box } m_tilesX = EditorGUILayout.IntField( new GUIContent("Tiles X", "The number of tiles on the X axis"), m_tilesX); m_tilesY = EditorGUILayout.IntField( new GUIContent("Tiles Y", "The number of tiles on the Y axis"), m_tilesY); m_tileResolution = EditorGUILayout.IntField( new GUIContent("Tile Resolution", "The number of pixels along each axis on one tile"), m_tileResolution); m_tileSize = EditorGUILayout.FloatField( new GUIContent("Tile Size", "The size of one tile in Unity units"), m_tileSize); m_meshMode = (MeshMode)EditorGUILayout.EnumPopup("Mesh Mode", m_meshMode); // these settings only apply to the single quad mode mesh if (m_meshMode == MeshMode.SingleQuad) { m_textureFormat = (TextureFormat)EditorGUILayout.EnumPopup("Texture Format", m_textureFormat); m_textureFilterMode = (FilterMode)EditorGUILayout.EnumPopup("Filter Mode", m_textureFilterMode); } if (GUILayout.Button("Create/Recreate Mesh")) { m_tileMap.MeshSettings = new TileMeshSettings(m_tilesX, m_tilesY, m_tileResolution, m_tileSize, m_meshMode, m_textureFormat); // if settings didnt change the mesh wouldnt be created, force creation if (!m_tileMap.HasMesh) { m_tileMap.CreateMesh(); } } if (GUILayout.Button("Destroy Mesh (keep data)")) { m_tileMap.DestroyMesh(); } } m_showPickerSettings = EditorGUILayout.Foldout(m_showPickerSettings, "Tile Picker Settings"); if (m_showPickerSettings) { m_tilePickerXCount = Mathf.Clamp(EditorGUILayout.IntField( new GUIContent("Items Per Row", "The number of items to draw in a row in the Tile Picker Window."), m_tilePickerXCount), 1, int.MaxValue); m_tilePickerPosition.width = Mathf.Clamp(EditorGUILayout.FloatField( new GUIContent("Picker Width", "The width of the Tile Picker Window."), m_tilePickerPosition.width), 64f, float.MaxValue); m_tilePickerPosition.height = Mathf.Clamp(EditorGUILayout.FloatField( new GUIContent("Picker Height", "The height of the Tile Picker Window."), m_tilePickerPosition.height), 128f, float.MaxValue); } bool prominentImportArea = m_tileSheet.Ids.Count() == 0; m_showSprites = EditorGUILayout.Foldout(m_showSprites || prominentImportArea, "Sprites:"); if (m_showSprites || prominentImportArea) { ShowImportDropArea(); } if (m_showSprites && !prominentImportArea) { if (GUILayout.Button("Delete all")) { foreach (var id in m_tileMap.TileSheet.Ids) { m_tileMap.TileSheet.Remove(id); } m_thumbnailCache.Clear(); } if (GUILayout.Button("Refresh thumbnails")) { m_thumbnailCache.Clear(); } m_sortSpritesByName = GUILayout.Toggle(m_sortSpritesByName, "Sort sprites by name"); var ids = m_tileSheet.Ids.ToList(); ids.Sort(); for (int i = 0; i < ids.Count; i++) { var sprite = m_tileSheet.Get(ids[i]); ShowSprite(sprite); // add separators, except below the last one // could probably find a better looking one if (i < (ids.Count - 1)) { GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1)); } } } EditorUtility.SetDirty(this); }
public TileMeshSettings (IVector2 tiles, int tileResolution, float tileSize, MeshMode meshMode) : this (tiles, tileResolution, tileSize, meshMode, TextureFormat.ARGB32) { }
public override void OnInspectorGUI() { // base.OnInspectorGUI(); m_showMapSettings = EditorGUILayout.Foldout(m_showMapSettings, "Map Settings"); if (m_showMapSettings) { m_activeInEditMode = EditorGUILayout.Toggle("Active In Edit Mode", m_activeInEditMode); m_tilesX = EditorGUILayout.IntField( new GUIContent("Tiles X", "The number of tiles on the X axis"), m_tilesX); m_tilesY = EditorGUILayout.IntField( new GUIContent("Tiles Y", "The number of tiles on the Y axis"), m_tilesY); m_tileResolution = EditorGUILayout.IntField( new GUIContent("Tile Resolution", "The number of pixels along each axis on one tile"), m_tileResolution); if (m_tileResolution != m_tileMap.MeshSettings.TileResolution) { EditorGUILayout.HelpBox("Changing tile resolution will clear the current tile setup.\n" + string.Format("Current tile resolution is {0}.", m_tileMap.MeshSettings.TileResolution), MessageType.Warning); } m_tileSize = EditorGUILayout.FloatField( new GUIContent("Tile Size", "The size of one tile in Unity units"), m_tileSize); m_meshMode = (MeshMode)EditorGUILayout.EnumPopup("Mesh Mode", m_meshMode); // these settings only apply to the single quad mode mesh if (m_meshMode == MeshMode.SingleQuad) { m_textureFormat = (TextureFormat)EditorGUILayout.EnumPopup("Texture Format", m_textureFormat); m_textureFilterMode = (FilterMode)EditorGUILayout.EnumPopup("Filter Mode", m_textureFilterMode); } if (GUILayout.Button("Create/Recreate Mesh")) { bool canDelete = true; if (m_tileResolution != m_tileMap.MeshSettings.TileResolution) { canDelete = ShowTileDeletionWarning(); } if (canDelete) { m_tileMap.MeshSettings = new TileMeshSettings(m_tilesX, m_tilesY, m_tileResolution, m_tileSize, m_meshMode, m_textureFormat); // if settings didnt change the mesh wouldnt be created, force creation if (!m_tileMap.HasMesh) { m_tileMap.CreateMesh(); } m_activeInEditMode = true; } } if (GUILayout.Button("Destroy Mesh (keep data)")) { m_tileMap.DestroyMesh(); m_activeInEditMode = false; } if (GUILayout.Button("Clear")) { if (ShowTileDeletionWarning()) { m_tileMap.DestroyMesh(); m_tileMap.ClearTiles(); m_activeInEditMode = false; } } if (m_tileMap.ActiveInEditMode != m_activeInEditMode) { m_tileMap.ActiveInEditMode = m_activeInEditMode; OnSceneGUI(); // force redraw map editor box } } m_showSortSettings = EditorGUILayout.Foldout(m_showSortSettings, "Sort Settings"); if (m_showSortSettings) { TileMeshBehaviour mesh = m_tileMap.GetComponentInChildren <TileMeshBehaviour>(); // When first creating tile map or after deleting the mesh, // it will not be accessible until it's created by user. if (mesh != null && mesh.renderer != null) { EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); string[] sortingLayers = GetSortingLayerNames(); int currentLayer = 0; bool isLayerSet = mesh.renderer.sortingLayerName.Length > 0; if (!isLayerSet) { currentLayer = FindStringIndex(ref sortingLayers, "Default"); } else { currentLayer = FindStringIndex(ref sortingLayers, mesh.renderer.sortingLayerName); } int chosenLayer = EditorGUILayout.Popup("Sorting Layer Name", Mathf.Max(currentLayer, 0), sortingLayers); if (EditorGUI.EndChangeCheck() || !isLayerSet) { mesh.renderer.sortingLayerName = sortingLayers[chosenLayer]; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); int order = EditorGUILayout.IntField("Sorting Order", mesh.renderer.sortingOrder); if (EditorGUI.EndChangeCheck()) { mesh.renderer.sortingOrder = order; } EditorGUILayout.EndHorizontal(); } else { EditorGUILayout.HelpBox("Mesh has not been created.\nPlease use 'Create/Recreate Mesh' button in the Map Settings.", MessageType.Info, true); } } m_showPickerSettings = EditorGUILayout.Foldout(m_showPickerSettings, "Tile Picker Settings"); if (m_showPickerSettings) { m_tilePickerXCount = Mathf.Clamp(EditorGUILayout.IntField( new GUIContent("Items Per Row", "The number of items to draw in a row in the Tile Picker Window."), m_tilePickerXCount), 1, int.MaxValue); m_tilePickerPosition.width = Mathf.Clamp(EditorGUILayout.FloatField( new GUIContent("Picker Width", "The width of the Tile Picker Window."), m_tilePickerPosition.width), 64f, float.MaxValue); m_tilePickerPosition.height = Mathf.Clamp(EditorGUILayout.FloatField( new GUIContent("Picker Height", "The height of the Tile Picker Window."), m_tilePickerPosition.height), 128f, float.MaxValue); } bool prominentImportArea = m_tileSheet.Ids.Count() == 0; m_showSprites = EditorGUILayout.Foldout(m_showSprites || prominentImportArea, "Sprites"); if (m_showSprites || prominentImportArea) { ShowImportDropArea(); } if (m_showSprites && !prominentImportArea) { if (GUILayout.Button("Delete all")) { foreach (var id in m_tileMap.TileSheet.Ids) { m_tileMap.TileSheet.Remove(id); } m_thumbnailCache.Clear(); } if (GUILayout.Button("Refresh thumbnails")) { m_thumbnailCache.Clear(); } m_sortSpritesByName = GUILayout.Toggle(m_sortSpritesByName, "Sort sprites by name"); var ids = m_tileSheet.Ids.ToList(); ids.Sort(); for (int i = 0; i < ids.Count; i++) { var sprite = m_tileSheet.Get(ids[i]); ShowSprite(sprite); // add separators, except below the last one // could probably find a better looking one if (i < (ids.Count - 1)) { GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1)); } } } EditorUtility.SetDirty(this); }
public void UpdateLight(Light2D id, MeshMode meshMode) // Camera { if (meshModeMaterial != meshMode.materials) { meshModeMaterial = meshMode.materials; ClearMaterial(); } if (meshModeShader != meshMode.shader) { meshModeShader = meshMode.shader; ClearMaterial(); } Material[] materials = GetMaterials(); if (materials == null) { return; } if (id.IsPixelPerfect()) { Camera camera = Camera.main; Vector2 cameraSize = LightingRender2D.GetSize(camera); Vector2 cameraPosition = LightingPosition.GetPosition2D(-camera.transform.position); transform.position = new Vector3(cameraPosition.x, cameraPosition.y, id.transform.position.z); transform.localScale = new Vector3(cameraSize.x, cameraSize.y, 1); } else { transform.position = id.transform.position; transform.localScale = new Vector3(id.size, id.size, 1); } transform.rotation = Quaternion.Euler(0, 0, 0); // transform.rotation = id.transform.rotation; // only if rotation enabled if (id.Buffer != null && meshRenderer != null) { Color lightColor = id.color; lightColor.a = id.meshMode.alpha; for (int i = 0; i < materials.Length; i++) { if (materials[i] == null) { continue; } materials[i].SetColor("_TintColor", lightColor); materials[i].color = lightColor; materials[i].mainTexture = id.Buffer.renderTexture.renderTexture; } id.meshMode.sortingLayer.ApplyToMeshRenderer(meshRenderer); meshRenderer.sharedMaterials = GetMaterials(); meshRenderer.enabled = true; meshFilter.mesh = GetMeshLight(); } }