Beispiel #1
0
        public void BuildMesh()
        {
            List <Vector3> points = GenerateLineFromLSystem(m_currentLSystem, new Vector3(0.0f, 0.0f, 0.0f), m_angleValue, m_initialAngle);

            if (points.Count > 0)
            {
                m_renderer.material_ = new MaterialDX11("vDefault.cso", "pLsystem.cso", "gDefaultLine.cso");
                m_renderer.material_.SetMainColor(0.0f, 1.0f, 0.0f, 1.0f);
                m_renderer.material_.AddConstantBuffer <float>(1.0f);


                List <StandardVertex> linemesh = new List <StandardVertex>();


                for (int i = 0; i < points.Count; i++)
                {
                    StandardVertex va = new StandardVertex(points[i]);
                    va.Uv = new Vector2(( float )i / ( float )points.Count, 0.0f);

                    linemesh.Add(va);
                }


                m_renderer.Vertices = linemesh;
                m_renderer.UpdateRenderer();
            }
        }
Beispiel #2
0
        protected override void OnEnabledInSimulation()
        {
            //create a mesh
            mesh      = CreateComponent <Component_Mesh>(enabled: false);
            mesh.Name = "Mesh 1";

            //generate vertices. use StandardVertex to make it easier
            StandardVertex.StaticOneTexCoord[] vertices = new StandardVertex.StaticOneTexCoord[4];

            var v = new StandardVertex.StaticOneTexCoord();

            v.Position = new Vector3F(-0.4f, -0.4f, 0f);
            v.Normal   = new Vector3F(0, 0, 1);
            v.Tangent  = new Vector4F(1, 0, 0, 0);
            v.Color    = new ColorValue(1, 1, 1);
            //v.TexCoord0 = new Vector2F(-1, -1);

            vertices[0] = v;

            v.Position  = new Vector3F(0.4f, -0.4f, 0);
            vertices[1] = v;

            v.Position  = new Vector3F(0.4f, 0.4f, 0);
            vertices[2] = v;

            v.Position  = new Vector3F(-0.4f, 0.4f, 0);
            vertices[3] = v;

            //generate indices
            var indices = new int[] { 0, 1, 2, 2, 3, 0 };

            //create geometry of the mesh
            geometry = mesh.CreateComponent <Component_MeshGeometry>();
            geometry.VertexStructure = StandardVertex.MakeStructure(StandardVertex.Components.StaticOneTexCoord, true, out int vertexSize);
            geometry.Vertices        = ConvertVertices(vertices);
            geometry.Indices         = indices;


            //mesh has been created, now we can enable it
            mesh.Enabled = true;

            meshInSpace           = CreateComponent <Component_MeshInSpace>(enabled: false);
            meshInSpace.Transform = new Transform(new Vector3(1, 1, 1));

            //make reference to the mesh. 'Root' reference - global path from scene root.
            meshInSpace.Mesh = ReferenceUtility.MakeRootReference(mesh);

            meshInSpace.Color   = new ColorValue(1, 0, 0);
            meshInSpace.Enabled = true;
        }
        /////////////////////////////////////////

        void DrawUV(Viewport viewport)
        {
            var meshData = GetSelectedLOD();

            if (Mesh.EditorDisplayUV >= 0 && Mesh.EditorDisplayUV < 4)
            {
                StandardVertex.Components component = StandardVertex.Components.TexCoord0;
                if (Mesh.EditorDisplayUV == 0)
                {
                    component = StandardVertex.Components.TexCoord0;
                }
                else if (Mesh.EditorDisplayUV == 1)
                {
                    component = StandardVertex.Components.TexCoord1;
                }
                else if (Mesh.EditorDisplayUV == 2)
                {
                    component = StandardVertex.Components.TexCoord2;
                }
                else if (Mesh.EditorDisplayUV == 3)
                {
                    component = StandardVertex.Components.TexCoord3;
                }

                if ((meshData.ExtractedVerticesComponents & component) != 0)
                {
                    var uvArray = (Vector2F[])StandardVertex.ExtractOneComponentArray(meshData.ExtractedVertices, component);

                    //bool valid = Array.Exists( uvArray, item => item != Vec2F.Zero );
                    //if( valid )
                    //{

                    //check old cache
                    if (cachedDisplayUV_Triangles != null)
                    {
                        if (cachedDisplayUV != Mesh.EditorDisplayUV || cachedDisplayUV_MeshData != meshData)
                        {
                            cachedDisplayUV_Triangles = null;
                            cachedDisplayUV_Lines     = null;
                        }
                    }

                    Vector2F screenMultiplier = new Vector2F(viewport.CanvasRenderer.AspectRatioInv * .8f, 0.8f);
                    Vector2F screenOffset     = new Vector2F(1.0f - screenMultiplier.X - 0.01f, 1.0f - screenMultiplier.Y - 0.01f);

                    Vector2F Convert(Vector2F v)
                    {
                        return(v * screenMultiplier + screenOffset);
                    };

                    //update
                    if (cachedDisplayUV_Triangles == null)
                    {
                        cachedDisplayUV          = Mesh.EditorDisplayUV;
                        cachedDisplayUV_MeshData = meshData;

                        var indices = meshData.ExtractedIndices;

                        //!!!!показывать те, которые за границами 0-1.


                        var uvArrayConverted = new Vector2F[uvArray.Length];
                        for (int n = 0; n < uvArrayConverted.Length; n++)
                        {
                            uvArrayConverted[n] = Convert(uvArray[n]);
                        }

                        var trianglesColor = new ColorValue(0, 1, 0, 0.5);
                        var linesColor     = new ColorValue(1, 1, 1);

                        //triangles
                        {
                            var triangles = new List <CanvasRenderer.TriangleVertex>(indices.Length / 3);

                            for (int triangle = 0; triangle < indices.Length / 3; triangle++)
                            {
                                int index0 = indices[triangle * 3 + 0];
                                int index1 = indices[triangle * 3 + 1];
                                int index2 = indices[triangle * 3 + 2];

                                Vector2F v0 = uvArrayConverted[index0];
                                Vector2F v1 = uvArrayConverted[index1];
                                Vector2F v2 = uvArrayConverted[index2];

                                triangles.Add(new CanvasRenderer.TriangleVertex(v0, trianglesColor));
                                triangles.Add(new CanvasRenderer.TriangleVertex(v1, trianglesColor));
                                triangles.Add(new CanvasRenderer.TriangleVertex(v2, trianglesColor));
                            }

                            cachedDisplayUV_Triangles = triangles.ToArray();
                        }

                        //lines
                        {
                            int[] lineIndices = MathAlgorithms.TriangleListToLineList(indices);

                            var lines = new List <CanvasRenderer.LineItem>(lineIndices.Length / 2);
                            for (int nLine = 0; nLine < lineIndices.Length / 2; nLine++)
                            {
                                var v0 = uvArrayConverted[lineIndices[nLine * 2 + 0]];
                                var v1 = uvArrayConverted[lineIndices[nLine * 2 + 1]];
                                lines.Add(new CanvasRenderer.LineItem(v0, v1, linesColor));
                            }

                            var vertices = new CanvasRenderer.TriangleVertex[lines.Count * 2];
                            for (int n = 0; n < lines.Count; n++)
                            {
                                var line = lines[n];
                                vertices[n * 2 + 0] = new CanvasRenderer.TriangleVertex(line.start, line.color, Vector2F.Zero);
                                vertices[n * 2 + 1] = new CanvasRenderer.TriangleVertex(line.end, line.color, Vector2F.Zero);
                            }

                            cachedDisplayUV_Lines = vertices;
                        }
                    }

                    //draw
                    if (cachedDisplayUV_Triangles != null)
                    {
                        viewport.CanvasRenderer.AddQuad(new RectangleF(Convert(Vector2F.Zero), Convert(Vector2F.One)), new ColorValue(0, 0, 0, .5));
                        viewport.CanvasRenderer.AddTriangles(cachedDisplayUV_Triangles);
                        viewport.CanvasRenderer.AddLines(cachedDisplayUV_Lines);
                    }
                }
            }
        }
        /// <summary>
        /// Loads all resources of the object.
        /// </summary>
        /// <param name="device">Current graphics device.</param>
        /// <param name="resourceDictionary">Current resource dicionary.</param>
        public override void LoadResources(EngineDevice device, ResourceDictionary resourceDictionary)
        {
            // Define all vertices
            StandardVertex[] vertices = new StandardVertex[]
            {
                // Front side
                new StandardVertex(new Vector3(-1f, +1f, -1f), new Vector2(0f, 0f)),
                new StandardVertex(new Vector3(-1f, -1f, -1f), new Vector2(0f, 1f)),
                new StandardVertex(new Vector3(+1f, -1f, -1f), new Vector2(1f, 1f)),
                new StandardVertex(new Vector3(+1f, +1f, -1f), new Vector2(1f, 0f)),

                // Right side
                new StandardVertex(new Vector3(+1f, +1f, -1f), new Vector2(0f, 0f)),
                new StandardVertex(new Vector3(+1f, -1f, -1f), new Vector2(0f, 1f)),
                new StandardVertex(new Vector3(+1f, -1f, +1f), new Vector2(1f, 1f)),
                new StandardVertex(new Vector3(+1f, +1f, +1f), new Vector2(1f, 0f)),

                // Back side
                new StandardVertex(new Vector3(+1f, +1f, +1f), new Vector2(0f, 0f)),
                new StandardVertex(new Vector3(+1f, -1f, +1f), new Vector2(0f, 1f)),
                new StandardVertex(new Vector3(-1f, -1f, +1f), new Vector2(1f, 1f)),
                new StandardVertex(new Vector3(-1f, +1f, +1f), new Vector2(1f, 0f)),

                // Left side
                new StandardVertex(new Vector3(-1f, +1f, +1f), new Vector2(0f, 0f)),
                new StandardVertex(new Vector3(-1f, -1f, +1f), new Vector2(0f, 1f)),
                new StandardVertex(new Vector3(-1f, -1f, -1f), new Vector2(1f, 1f)),
                new StandardVertex(new Vector3(-1f, +1f, -1f), new Vector2(1f, 0f)),

                // Top side
                new StandardVertex(new Vector3(-1f, +1f, +1f), new Vector2(0f, 0f)),
                new StandardVertex(new Vector3(-1f, +1f, -1f), new Vector2(0f, 1f)),
                new StandardVertex(new Vector3(+1f, +1f, -1f), new Vector2(1f, 1f)),
                new StandardVertex(new Vector3(+1f, +1f, +1f), new Vector2(1f, 0f)),

                // Down side
                new StandardVertex(new Vector3(+1f, -1f, -1f), new Vector2(0f, 0f)),
                new StandardVertex(new Vector3(-1f, -1f, -1f), new Vector2(1f, 0f)),
                new StandardVertex(new Vector3(-1f, -1f, +1f), new Vector2(1f, 1f)),
                new StandardVertex(new Vector3(+1f, -1f, +1f), new Vector2(0f, 1f)),
            };

            // Define all indices
            int[] indices = new int[]
            {
                0, 1, 2, 2, 3, 0,        // Font side
                4, 5, 6, 6, 7, 4,        // Right side
                8, 9, 10, 10, 11, 8,     // Back side
                12, 13, 14, 14, 15, 12,  // Left side
                16, 17, 18, 18, 19, 16,  // Top side
                20, 21, 22, 22, 23, 20   // Down side
            };

            // Create and fill resource container object
            SkyboxLocalResources localResources = new SkyboxLocalResources();

            localResources.DefaultResources = resourceDictionary.DefaultResources;
            localResources.CubeTexture      = resourceDictionary.GetResourceAndEnsureLoaded <TextureResource>(m_cubeTextureKey);
            localResources.VertexBuffer     = GraphicsHelper.CreateImmutableVertexBuffer(device, vertices);
            localResources.IndexBuffer      = GraphicsHelper.CreateImmutableIndexBuffer(device, indices);
            localResources.VertexShader     = resourceDictionary.GetResourceAndEnsureLoaded(
                ResourceKeys.RES_VERTEX_SHADER_SKYBOX,
                () => GraphicsHelper.GetVertexShaderResource(device, "Skybox", "CommonVertexShader"));
            localResources.PixelShader = resourceDictionary.GetResourceAndEnsureLoaded(
                ResourceKeys.RES_PIXEL_SHADER_SKYBOX,
                () => GraphicsHelper.GetPixelShaderResource(device, "Skybox", "CommonPixelShader"));

            // Store resource container object
            m_localResources.AddObject(localResources, device.DeviceIndex);
        }
        /// <summary>
        /// Builds vertex and index buffers.
        /// </summary>
        /// <param name="device">The device on which to build all buffers.</param>
        /// <param name="geometries">All geometries to be loaded.</param>
        private static RenderingChunkTemplate[] BuildBuffers(EngineDevice device, Geometry[] geometries)
        {
            var resultTemplates = new List <RenderingChunkTemplate>(geometries.Length * 2);

            var cachedVertices = new List <StandardVertex[]>(2);
            var cachedIndices  = new List <int[]>(6);

            var geometriesCount = geometries.Length;
            var actVertexCount  = 0;
            var actIndexCount   = 0;
            var lastFinishedVertexBufferResultIndex = -1;
            var lastFinishedIndexBufferResultIndex  = -1;
            var vertexBufferId = 0;
            var indexBufferId  = 0;

            // Define the action which finishes current index buffer
            void FinishIndexBuffer()
            {
                if (actIndexCount == 0)
                {
                    return;
                }

                // Create the vertex buffer
                var indexBuffer = GraphicsHelper.Internals.CreateImmutableIndexBuffer(device, cachedIndices.ToArray());

                cachedIndices.Clear();
                actIndexCount = 0;
                indexBufferId++;

                // Do also create index buffer
                for (var loop = lastFinishedIndexBufferResultIndex + 1; loop < resultTemplates.Count; loop++)
                {
                    resultTemplates[loop].IndexBuffer = indexBuffer;
                }

                lastFinishedIndexBufferResultIndex = resultTemplates.Count - 1;
            }

            // Define the action which finishes current vertex buffer
            void FinishVertexBuffer()
            {
                // Create the vertex buffer
                var vertexBuffer = GraphicsHelper.Internals.CreateImmutableVertexBuffer(device, cachedVertices.ToArray());

                cachedVertices.Clear();
                actVertexCount = 0;
                vertexBufferId++;

                // Do also finish index buffers in this case
                FinishIndexBuffer();

                // Do also create index buffer
                for (var loop = lastFinishedVertexBufferResultIndex + 1; loop < resultTemplates.Count; loop++)
                {
                    resultTemplates[loop].VertexBuffer = vertexBuffer;
                }

                lastFinishedVertexBufferResultIndex = resultTemplates.Count - 1;
            }

            // Load all geometries into memory within a loop
            for (var loopGeometry = 0; loopGeometry < geometriesCount; loopGeometry++)
            {
                var actGeometry = geometries[loopGeometry];

                if (actGeometry.CountVertices == 0)
                {
                    continue;
                }
                if (actGeometry.CountSurfaces == 0)
                {
                    continue;
                }

                // Handle vertex data
                var vertexArray = StandardVertex.FromGeometry(actGeometry);
                if (actVertexCount + vertexArray.Length > MAX_VERTEX_COUNT_PER_BUFFER)
                {
                    FinishVertexBuffer();
                }

                cachedVertices.Add(vertexArray);
                var actBaseVertex = actVertexCount;
                actVertexCount += vertexArray.Length;

                // Generate one RenderingChunkTemplate per surface
                var surfaceList  = new List <GeometrySurface>(actGeometry.Surfaces);
                var surfaceCount = surfaceList.Count;
                for (var loopSurface = 0; loopSurface < surfaceCount; loopSurface++)
                {
                    var actSurface = surfaceList[loopSurface];
                    if (actSurface.CountTriangles == 0)
                    {
                        continue;
                    }

                    // Handle index data
                    var indexArray = actSurface.GetIndexArray();
                    if (actBaseVertex > 0)
                    {
                        for (var loopIndex = 0; loopIndex < indexArray.Length; loopIndex++)
                        {
                            indexArray[loopIndex] = indexArray[loopIndex] + actBaseVertex;
                        }
                    }
                    if (actIndexCount + indexArray.Length > MAX_INDEX_COUNT_PER_BUFFER)
                    {
                        FinishIndexBuffer();
                    }

                    cachedIndices.Add(indexArray);
                    actIndexCount += indexArray.Length;

                    // Create the rendering chunk
                    var newChunkTemplate = new RenderingChunkTemplate
                    {
                        VertexBufferId = vertexBufferId,
                        IndexBufferId  = indexBufferId,
                        SizePerVertex  = StandardVertex.Size,
                        Geometry       = actGeometry,
                        IndexCount     = indexArray.Length,
                        StartIndex     = actIndexCount - indexArray.Length,
                        VertexBuffer   = null,
                        IndexBuffer    = null,
                        InputElements  = StandardVertex.InputElements
                    };

                    resultTemplates.Add(newChunkTemplate);
                }
            }

            // Finish all remaining buffers finally
            if (cachedVertices.Count > 0)
            {
                FinishVertexBuffer();
            }
            if (cachedIndices.Count > 0)
            {
                FinishIndexBuffer();
            }

            return(resultTemplates.ToArray());
        }