Exemple #1
0
        protected override void Viewport_UpdateBeforeOutput(Viewport viewport)
        {
            base.Viewport_UpdateBeforeOutput(viewport);

            if (ParticleSystem != null && needResultCompile)
            {
                ParticleSystem.ResultCompile();
                needResultCompile = false;
            }

            //highlight emitter shapes
            if (ParticleSystem != null && ParticleSystem.Result != null)
            {
                foreach (var emitter in ParticleSystem.GetComponents <Component_ParticleEmitter>())
                {
                    if (emitter.Enabled)
                    {
                        bool emitterSelected = SelectedObjectsSet.Contains(emitter);

                        var tr = Transform.Identity;
                        int verticesRendered = 0;

                        foreach (var shape in emitter.GetComponents <Component_ParticleEmitterShape>(false, false, false))
                        {
                            if (shape.Enabled)
                            {
                                ColorValue color;
                                if (emitterSelected || SelectedObjectsSet.Contains(shape))
                                {
                                    color = ProjectSettings.Get.SelectedColor;
                                }
                                else
                                {
                                    //!!!!add editor options
                                    color = new ColorValue(0, 0, 0.8);
                                }
                                viewport.Simple3DRenderer.SetColor(color, color * ProjectSettings.Get.HiddenByOtherObjectsColorMultiplier);
                                shape.PerformRender(viewport, tr, false, ref verticesRendered);
                            }
                        }
                    }
                }
            }
        }
 public bool IsObjectSelected(object obj)
 {
     return(SelectedObjectsSet.Contains(obj));
 }
        protected override void Viewport_UpdateBeforeOutput(Viewport viewport)
        {
            base.Viewport_UpdateBeforeOutput(viewport);

            if (Mesh != null && needResultCompile)
            {
                Mesh.ResultCompile();
                needResultCompile = false;
            }

            if (Mesh != null && Mesh.Result != null)
            {
                var meshBounds  = Mesh.Result.SpaceBounds;
                var selectedLOD = GetSelectedLOD();

                //center axes
                if (Mesh.EditorDisplayPivot)
                {
                    var sizeInPixels = 35 * DpiHelper.Default.DpiScaleFactor;
                    var size         = viewport.Simple3DRenderer.GetThicknessByPixelSize(Vector3.Zero, sizeInPixels);
                    //var size3 = meshBounds.CalculatedBoundingBox.GetSize();
                    //var size = Math.Max( size3.X, Math.Max( size3.Y, size3.Z ) ) / 20;

                    var thickness  = size / 20;
                    var headHeight = size / 3;
                    var headRadius = headHeight / 3;
                    var alpha      = 1.0;               // 0.5;

                    //viewport.Simple3DRenderer.SetColor( new ColorValue( 1, 0, 0, alpha ), false, true );
                    //viewport.Simple3DRenderer.AddLine( Vector3.Zero, Vector3.XAxis * size );
                    //viewport.Simple3DRenderer.SetColor( new ColorValue( 0, 1, 0, alpha ), false, true );
                    //viewport.Simple3DRenderer.AddLine( Vector3.Zero, Vector3.YAxis * size );
                    //viewport.Simple3DRenderer.SetColor( new ColorValue( 0, 0, 1, alpha ), false, true );
                    //viewport.Simple3DRenderer.AddLine( Vector3.Zero, Vector3.ZAxis * size );

                    viewport.Simple3DRenderer.SetColor(new ColorValue(1, 0, 0, alpha), false);                        //, true );
                    viewport.Simple3DRenderer.AddArrow(Vector3.Zero, Vector3.XAxis * size, headHeight, headRadius, true, thickness);
                    viewport.Simple3DRenderer.SetColor(new ColorValue(0, 1, 0, alpha), false);                        //, true );
                    viewport.Simple3DRenderer.AddArrow(Vector3.Zero, Vector3.YAxis * size, headHeight, headRadius, true, thickness);
                    viewport.Simple3DRenderer.SetColor(new ColorValue(0, 0, 1, alpha), false);                        //, true );
                    viewport.Simple3DRenderer.AddArrow(Vector3.Zero, Vector3.ZAxis * size, headHeight, headRadius, true, thickness);
                }

                //vertex color
                if (Mesh.EditorDisplayVertexColor)
                {
                    var vertices = new Simple3DRenderer.Vertex[selectedLOD.ExtractedVertices.Length];
                    for (int n = 0; n < vertices.Length; n++)
                    {
                        var sourceVertex = selectedLOD.ExtractedVertices[n];

                        var vertex = new Simple3DRenderer.Vertex();
                        vertex.position = sourceVertex.Position;

                        vertex.color = RenderingSystem.ConvertColorValue(ref sourceVertex.Color);
                        //vertex.color = sourceVertex.Color;

                        //vertex.colorInvisibleBehindObjects = sourceVertex.color;
                        vertices[n] = vertex;
                    }

                    viewport.Simple3DRenderer.AddTriangles(vertices, selectedLOD.ExtractedIndices, false, true);
                }

                //triangles
                if (Mesh.EditorDisplayTriangles)
                {
                    //!!!!цвета настраивать в опциях
                    viewport.Simple3DRenderer.SetColor(new ColorValue(0, 0, 1, 0.7));
                    //viewport.DebugGeometry.SetColor( new ColorValue( 0, 0, 1, 0.3 ) );
                    viewport.Simple3DRenderer.AddTriangles(selectedLOD.ExtractedVerticesPositions, selectedLOD.ExtractedIndices, true, false);
                }

                //vertices
                if (Mesh.EditorDisplayVertices)
                {
                    var size3 = meshBounds.CalculatedBoundingBox.GetSize();
                    var scale = (float)Math.Max(size3.X, Math.Max(size3.Y, size3.Z)) / 200;
                    viewport.Simple3DRenderer.SetColor(new ColorValue(1, 0, 0, 0.7));
                    //viewport.DebugGeometry.SetColor( new ColorValue( 0, 1, 0, 0.7 ) );
                    foreach (var vertex in selectedLOD.ExtractedVerticesPositions)
                    {
                        viewport.Simple3DRenderer.AddLineThin(vertex - new Vector3F(scale, 0, 0), vertex + new Vector3F(scale, 0, 0));
                        viewport.Simple3DRenderer.AddLineThin(vertex - new Vector3F(0, scale, 0), vertex + new Vector3F(0, scale, 0));
                        viewport.Simple3DRenderer.AddLineThin(vertex - new Vector3F(0, 0, scale), vertex + new Vector3F(0, 0, scale));
                    }
                }

                //normals
                if (Mesh.EditorDisplayNormals)
                {
                    var size3 = meshBounds.CalculatedBoundingBox.GetSize();
                    var scale = (float)Math.Max(size3.X, Math.Max(size3.Y, size3.Z)) / 30;
                    viewport.Simple3DRenderer.SetColor(new ColorValue(0, 1, 0, 0.7));
                    foreach (var vertex in selectedLOD.ExtractedVertices)
                    {
                        if (vertex.Normal != Vector3F.Zero)
                        {
                            viewport.Simple3DRenderer.AddLineThin(vertex.Position, vertex.Position + vertex.Normal * scale);
                        }
                    }
                }

                //tangents
                if (Mesh.EditorDisplayTangents)
                {
                    var size3 = meshBounds.CalculatedBoundingBox.GetSize();
                    var scale = (float)Math.Max(size3.X, Math.Max(size3.Y, size3.Z)) / 30;
                    viewport.Simple3DRenderer.SetColor(new ColorValue(1, 0, 0, 0.7));
                    foreach (var vertex in selectedLOD.ExtractedVertices)
                    {
                        if (vertex.Tangent != Vector4F.Zero)
                        {
                            viewport.Simple3DRenderer.AddLineThin(vertex.Position, vertex.Position + vertex.Tangent.ToVector3F() * scale);
                        }
                    }
                }

                //binormals
                if (Mesh.EditorDisplayBinormals)
                {
                    var size3 = meshBounds.CalculatedBoundingBox.GetSize();
                    var scale = (float)Math.Max(size3.X, Math.Max(size3.Y, size3.Z)) / 30;
                    viewport.Simple3DRenderer.SetColor(new ColorValue(0, 0, 1, 0.7));
                    foreach (var vertex in selectedLOD.ExtractedVertices)
                    {
                        if (vertex.Normal != Vector3F.Zero && vertex.Tangent != Vector4F.Zero)
                        {
                            var v = Vector3.Cross(vertex.Tangent.ToVector3F(), vertex.Normal) * vertex.Tangent.W;
                            viewport.Simple3DRenderer.AddLineThin(vertex.Position, vertex.Position + v * scale);
                        }
                    }
                }

                //collision
                if (Mesh.EditorDisplayCollision)
                {
                    var collision = Mesh.GetComponent("Collision Definition") as Component_RigidBody;
                    if (collision != null)
                    {
                        ColorValue color = new ColorValue(0, 0, 1, 0.7);
                        //if( MotionType.Value == MotionTypeEnum.Static )
                        //	color = ProjectSettings.Get.SceneShowPhysicsStaticColor;
                        //else if( rigidBody.IsActive )
                        //	color = ProjectSettings.Get.SceneShowPhysicsDynamicActiveColor;
                        //else
                        //	color = ProjectSettings.Get.SceneShowPhysicsDynamicInactiveColor;
                        viewport.Simple3DRenderer.SetColor(color, color * ProjectSettings.Get.HiddenByOtherObjectsColorMultiplier);

                        int verticesRendered = 0;
                        foreach (var shape in collision.GetComponents <Component_CollisionShape>(false, true, true))
                        {
                            shape.Render(viewport, Transform.Identity, false, ref verticesRendered);
                        }
                    }
                }

                //update PlayAnimation
                if (!string.IsNullOrEmpty(Mesh.EditorPlayAnimation))
                {
                    var animation = Mesh.ParentRoot.GetComponentByPath(Mesh.EditorPlayAnimation) as Component_Animation;
                    skeletonAnimationController.PlayAnimation = animation;
                    //viewport.CanvasRenderer.AddText( animation != null ? animation.ToString() : "null", new Vec2( .5, .5 ) );
                }
                else
                {
                    skeletonAnimationController.PlayAnimation = null;
                }

                //skeleton
                if (Mesh.EditorDisplaySkeleton)
                {
                    var skeleton = Mesh.Skeleton.Value;
                    if (skeleton != null)
                    {
                        var color = new ColorValue(0, 0.5, 1, 0.7);
                        viewport.Simple3DRenderer.SetColor(color, color * ProjectSettings.Get.HiddenByOtherObjectsColorMultiplier);

                        var skeletonArrows = skeletonAnimationController.GetCurrentAnimatedSkeletonArrows();
                        if (skeletonArrows != null)
                        {
                            foreach (var arrow in skeletonArrows)
                            {
                                viewport.Simple3DRenderer.AddArrow(arrow.Start, arrow.End);
                            }
                        }
                        else
                        {
                            foreach (var bone in skeleton.GetBones())
                            {
                                var pos    = bone.Transform.Value.Position;
                                var parent = bone.Parent as Component_SkeletonBone;
                                if (parent != null)
                                {
                                    var from = parent.Transform.Value.Position;

                                    viewport.Simple3DRenderer.AddArrow(from, pos);
                                }
                            }
                        }
                    }
                }

                //bounds
                if (Mesh.EditorDisplayBounds)
                {
                    viewport.Simple3DRenderer.SetColor(new ColorValue(1, 1, 0, 0.7));
                    //viewport.DebugGeometry.SetColor( new ColorValue( 1, 1, 0, 0.5 ) );
                    if (meshBounds.BoundingBox.HasValue)
                    {
                        viewport.Simple3DRenderer.AddBounds(meshBounds.BoundingBox.Value);
                    }
                    if (meshBounds.BoundingSphere.HasValue)
                    {
                        viewport.Simple3DRenderer.AddSphere(meshBounds.BoundingSphere.Value);
                    }
                }

                //select triangle
                int triangleIndex = -1;
                if (!viewport.MouseRelativeMode)
                {
                    var ray = viewport.CameraSettings.GetRayByScreenCoordinates(viewport.MousePosition);
                    if (selectedLOD.RayCast(ray, Component_Mesh.CompiledData.RayCastMode.BruteforceNoCache, false, out double scale2, out int triangleIndex2))
                    {
                        triangleIndex = triangleIndex2;
                    }
                }

                //selected triangle data
                if (triangleIndex != -1)
                {
                    var vertices = selectedLOD.ExtractedVertices;
                    var indices  = selectedLOD.ExtractedIndices;

                    int      index0       = indices[triangleIndex * 3 + 0];
                    int      index1       = indices[triangleIndex * 3 + 1];
                    int      index2       = indices[triangleIndex * 3 + 2];
                    var      showVertices = new int[] { index0, index1, index2 };
                    Vector3F p0           = vertices[index0].Position;
                    Vector3F p1           = vertices[index1].Position;
                    Vector3F p2           = vertices[index2].Position;

                    ////draw connected triangles
                    //{
                    //	ESet<Vector3I> triangles = new ESet<Vector3I>();

                    //	//find triangles with indexes
                    //	for( int nTriangle = 0; nTriangle < indices.Length / 3; nTriangle++ )
                    //	{
                    //		var i0 = indices[ nTriangle * 3 + 0 ];
                    //		var i1 = indices[ nTriangle * 3 + 1 ];
                    //		var i2 = indices[ nTriangle * 3 + 2 ];

                    //		int c = 0;
                    //		if( index0 == i0 || index1 == i0 || index2 == i0 )
                    //			c++;
                    //		if( index0 == i1 || index1 == i1 || index2 == i1 )
                    //			c++;
                    //		if( index0 == i2 || index1 == i2 || index2 == i2 )
                    //			c++;

                    //		//if( index0 == i0 || index0 == i1 || index0 == i2 ||
                    //		//	index1 == i0 || index1 == i1 || index1 == i2 ||
                    //		//	index2 == i0 || index2 == i1 || index2 == i2 )
                    //		if( c == 2 )
                    //		{
                    //			Vector3I triangle = new Vector3I( i0, i1, i2 );
                    //			if( triangle[ 1 ] < triangle[ 0 ] ) { var v = triangle[ 0 ]; triangle[ 0 ] = triangle[ 1 ]; triangle[ 1 ] = v; }
                    //			if( triangle[ 2 ] < triangle[ 0 ] ) { var v = triangle[ 0 ]; triangle[ 0 ] = triangle[ 2 ]; triangle[ 2 ] = v; }
                    //			if( triangle[ 2 ] < triangle[ 1 ] ) { var v = triangle[ 1 ]; triangle[ 1 ] = triangle[ 2 ]; triangle[ 2 ] = v; }

                    //			triangles.AddWithCheckAlreadyContained( triangle );
                    //		}
                    //	}

                    //	foreach( var triangle in triangles )
                    //	{
                    //		var v0 = vertices[ triangle[ 0 ] ].Position;
                    //		var v1 = vertices[ triangle[ 1 ] ].Position;
                    //		var v2 = vertices[ triangle[ 2 ] ].Position;

                    //		//viewport.Simple3DRenderer.SetColor( new ColorValue( 0, 0, 1, 0.3 ) );
                    //		//viewport.Simple3DRenderer.AddTriangles( new Vector3F[] { v0, v1, v2 }, Matrix4.Identity, false, false );
                    //		viewport.Simple3DRenderer.SetColor( new ColorValue( 0, 0, 1 ) );
                    //		viewport.Simple3DRenderer.AddLine( v0, v1 );
                    //		viewport.Simple3DRenderer.AddLine( v1, v2 );
                    //		viewport.Simple3DRenderer.AddLine( v2, v0 );
                    //	}
                    //}

                    //draw triangle
                    {
                        viewport.Simple3DRenderer.SetColor(new ColorValue(1, 1, 0, 0.3));
                        viewport.Simple3DRenderer.AddTriangles(new Vector3F[] { p0, p1, p2 }, false, false);
                        viewport.Simple3DRenderer.SetColor(new ColorValue(1, 1, 0));
                        viewport.Simple3DRenderer.AddLine(p0, p1);
                        viewport.Simple3DRenderer.AddLine(p1, p2);
                        viewport.Simple3DRenderer.AddLine(p2, p0);
                    }

                    float maxLength    = Math.Max((p0 - p1).Length(), Math.Max((p1 - p2).Length(), (p2 - p0).Length()));
                    float arrowLength  = maxLength / 5;
                    float vertexRadius = maxLength / 40;

                    //!!!!можно еще буквами подписать как в transform tool
                    //normals, tangents
                    foreach (int nVertex in showVertices)
                    {
                        var      v = vertices[nVertex];
                        Vector3F p = v.Position;

                        if (v.Normal != Vector3F.Zero)
                        {
                            viewport.Simple3DRenderer.SetColor(new ColorValue(0, 0, 1));
                            viewport.Simple3DRenderer.AddArrow(p, p + v.Normal * arrowLength, 0, 0, true);

                            if (v.Tangent != Vector4F.Zero)
                            {
                                viewport.Simple3DRenderer.SetColor(new ColorValue(1, 0, 0));
                                viewport.Simple3DRenderer.AddArrow(p, p + v.Tangent.ToVector3F() * arrowLength, 0, 0, true);

                                var bitangent = Vector3F.Cross(v.Tangent.ToVector3F(), v.Normal) * v.Tangent.W;
                                viewport.Simple3DRenderer.SetColor(new ColorValue(0, 1, 0));
                                viewport.Simple3DRenderer.AddArrow(p, p + bitangent * arrowLength, 0, 0, true);
                            }
                        }
                    }

                    //positions
                    {
                        viewport.Simple3DRenderer.SetColor(new ColorValue(1, 0, 0));
                        //SimpleMeshGenerator.GenerateSphere( vertexRadius, 10, 10, false, out Vec3F[] spherePositions, out int[] sphereIndices );

                        foreach (int nVertex in showVertices)
                        {
                            var      v = vertices[nVertex];
                            Vector3F p = v.Position;
                            viewport.Simple3DRenderer.AddSphere(new Sphere(p, vertexRadius), 10, true);
                            //viewport.DebugGeometry.AddTriangles( spherePositions, sphereIndices, Mat4.FromTranslate( p ), false, false );
                        }
                    }
                }

                //highlight selected mesh geometries, materials
                if (GetSelectedLODIndex() == 0)
                {
                    var geometriesToHighlight = new ESet <Component_MeshGeometry>();
                    foreach (var geometry in Mesh.GetComponents <Component_MeshGeometry>())                    // checkChildren: true ) )
                    {
                        if (SelectedObjectsSet.Contains(geometry))
                        {
                            geometriesToHighlight.AddWithCheckAlreadyContained(geometry);
                        }

                        var material = geometry.Material.Value;
                        if (material != null && SelectedObjectsSet.Contains(material))
                        {
                            geometriesToHighlight.AddWithCheckAlreadyContained(geometry);
                        }
                    }

                    foreach (var geometry in geometriesToHighlight)
                    {
                        var structure = geometry.VertexStructure.Value;
                        var vertices  = geometry.Vertices.Value;
                        var indices   = geometry.Indices.Value;

                        if (vertices != null & indices != null)
                        {
                            structure.GetInfo(out var vertexSize, out var holes);
                            var vertexCount = vertices.Length / vertexSize;

                            try
                            {
                                if (structure.GetElementBySemantic(VertexElementSemantic.Position, out var element) && element.Type == VertexElementType.Float3)
                                {
                                    if (vertices != null && indices != null)
                                    {
                                        //!!!!кешировать?

                                        var positions = ExtractChannel <Vector3F>(vertices, vertexSize, vertexCount, element.Offset);

                                        viewport.Simple3DRenderer.SetColor(ProjectSettings.Get.SelectedColor);
                                        viewport.Simple3DRenderer.AddTriangles(positions, indices, true, true);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Log.Warning(e.Message);
                            }
                        }
                    }
                }

                //update selected LOD
                if (Scene.RenderingPipeline.Value != null)
                {
                    Scene.RenderingPipeline.Value.LODRange = new RangeI(Mesh.EditorDisplayLOD, Mesh.EditorDisplayLOD);
                }
            }
        }
        protected override void Viewport_UpdateBeforeOutput(Viewport viewport)
        {
            base.Viewport_UpdateBeforeOutput(viewport);

            //highlight selected mesh geometries, materials
            {
                var geometriesToHighlight = new ESet <Component_MeshGeometry>();
                foreach (var geometry in Import.GetComponents <Component_MeshGeometry>(checkChildren: true))
                {
                    //skip when selected is LOD
                    if (geometry.ParentMesh != null && geometry.ParentMesh.FindParent <Component_Mesh>() == null)
                    {
                        //mesh selected. skip when available only one mesh
                        if (SelectedObjectsSet.Contains(geometry.ParentMesh) && Import.GetComponent("Mesh") == null)
                        {
                            geometriesToHighlight.AddWithCheckAlreadyContained(geometry);
                        }

                        if (SelectedObjectsSet.Contains(geometry))
                        {
                            geometriesToHighlight.AddWithCheckAlreadyContained(geometry);
                        }

                        var material = geometry.Material.Value;
                        if (material != null && SelectedObjectsSet.Contains(material))
                        {
                            geometriesToHighlight.AddWithCheckAlreadyContained(geometry);
                        }
                    }
                }

                foreach (var geometry in geometriesToHighlight)
                {
                    var mesh      = geometry.ParentMesh;
                    var structure = geometry.VertexStructure.Value;
                    var vertices  = geometry.Vertices.Value;
                    var indices   = geometry.Indices.Value;

                    if (structure != null)
                    {
                        structure.GetInfo(out var vertexSize, out var holes);
                        var vertexCount = vertices.Length / vertexSize;

                        try
                        {
                            if (structure.GetElementBySemantic(VertexElementSemantic.Position, out var element) && element.Type == VertexElementType.Float3)
                            {
                                if (vertices != null && indices != null)
                                {
                                    //!!!!кешировать?

                                    var positions = ExtractChannel <Vector3F>(vertices, vertexSize, vertexCount, element.Offset);

                                    Transform transform;
                                    if (!displayObjectTransformBySourceMesh.TryGetValue(mesh, out transform))
                                    {
                                        transform = Transform.Identity;
                                    }
                                    var transformMatrix = transform.ToMatrix4();

                                    viewport.Simple3DRenderer.SetColor(ProjectSettings.Get.SelectedColor);
                                    viewport.Simple3DRenderer.AddTriangles(positions, indices, ref transformMatrix, true, true);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Warning(e.Message);
                        }
                    }
                }
            }

            ////total bounds
            //var totalBounds = GetTotalBoundsOfObjectInSpace();
            //if( !totalBounds.IsCleared() )
            //{
            //	viewport.DebugGeometry.SetColor( new ColorValue( 1, 1, 0, 0.5 ) );
            //	viewport.DebugGeometry.AddBounds( totalBounds );
            //}

            //highlight selected objects in space
            {
                var selected = new ESet <Component_ObjectInSpace>();
                foreach (var obj in SelectedObjects)
                {
                    var objectInSpace = obj as Component_ObjectInSpace;
                    if (objectInSpace != null)
                    {
                        selected.Add(objectInSpace);
                    }
                }

                foreach (var obj in selected)
                {
                    var bounds = obj.SpaceBounds.CalculatedBoundingBox;

                    viewport.Simple3DRenderer.SetColor(new ColorValue(0, 1, 0, 0.5));
                    viewport.Simple3DRenderer.AddBounds(bounds);

                    //if( b.BoundingBox.HasValue )
                    //	viewport.DebugGeometry.AddBounds( b.BoundingBox.Value );
                    //if( b.BoundingSphere.HasValue )
                    //	viewport.DebugGeometry.AddSphere( b.BoundingSphere.Value );
                }
            }
        }