Esempio n. 1
0
        /// <summary>
        /// Loads all resources of this texture painter object.
        /// </summary>
        /// <param name="resources">The target resource dictionary.</param>
        internal void LoadResources(ResourceDictionary resources)
        {
            // Load material
            m_materialResource = resources.GetResourceAndEnsureLoaded <SpriteMaterialResource>(
                KEY_MATERIAL,
                () => new SpriteMaterialResource(m_texture));

            // Load geometry resource
            m_geometryResource = resources.GetResourceAndEnsureLoaded <GeometryResource>(
                KEY_GEOMETRY,
                () =>
            {
                VertexStructure structure = new VertexStructure();
                structure.FirstSurface.BuildRect4V(
                    new Vector3(-1f, -1f, 0f),
                    new Vector3(1f, -1f, 0f),
                    new Vector3(1f, 1f, 0f),
                    new Vector3(-1f, 1f, 0f),
                    Color4.Transparent);
                structure.FirstSurface.Material = KEY_MATERIAL;
                return(new GeometryResource(structure));
            });

            // Load the texture resource
            m_textureResource = resources.GetResourceAndEnsureLoaded <TextureResource>(m_texture);

            // Get default resources
            m_defaultResources = resources.GetResourceAndEnsureLoaded <DefaultResources>(
                DefaultResources.RESOURCE_KEY,
                () => new DefaultResources());

            m_renderParameters = resources.GetResourceAndEnsureLoaded <ObjectRenderParameters>(
                KEY_RENDER_PARAMETERS,
                () => new ObjectRenderParameters());
        }
Esempio n. 2
0
        /// <summary>
        /// Gets an array of StandardVertices from the given VertexStructure objects.
        /// </summary>
        public static                               StandardVertex[] FromVertexStructure(VertexStructure[] structures)
        {
            if (structures == null)
            {
                throw new ArgumentNullException("structures");
            }

            //Get total vertex count
            int vertexCount = 0;

            for (int loop = 0; loop < structures.Length; loop++)
            {
                vertexCount += structures[loop].CountVertices;
            }

            //create result array
            StandardVertex[] result = new StandardVertex[vertexCount];
            int actVertexPos        = 0;

            for (int loop = 0; loop < structures.Length; loop++)
            {
                VertexStructure actStructure         = structures[loop];
                int             structureVertexCount = actStructure.CountVertices;
                for (int innerLoop = 0; innerLoop < structureVertexCount; innerLoop++)
                {
                    result[actVertexPos] = new StandardVertex(actStructure.Vertices[innerLoop]);
                    actVertexPos++;
                }
            }

            return(result);
        }
        public override VertexStructure[] BuildStructure(StructureBuildOptions buildOptions)
        {
            float halfWidth = Constants.TILE_WIDTH / 2f;
            float halfHeight = Constants.TILE_HEIGHT / 2f;

            VertexStructure[] result = new VertexStructure[2];

            result[0] = new VertexStructure();
            result[0].BuildRect4V(
                new Vector3(-halfWidth, 0f, -halfHeight),
                new Vector3(halfWidth, 0f, -halfHeight),
                new Vector3(halfWidth, 0f, halfHeight),
                new Vector3(-halfWidth, 0f, halfHeight));
            result[0].Material = this.FrontMaterial;

            result[1] = new VertexStructure();
            result[1].BuildRect4V(
                new Vector3(-halfWidth, 0f, halfHeight),
                new Vector3(halfWidth, 0f, halfHeight),
                new Vector3(halfWidth, 0f, -halfHeight),
                new Vector3(-halfWidth, 0f, -halfHeight));
            result[1].Material = this.BackMaterial;

            return result;
        }
Esempio n. 4
0
        /// <summary>
        /// Creates the default resource for the given type name..
        /// </summary>
        /// <typeparam name="T">The type for which the generic resource should be created.</typeparam>
        internal static T CreateDefaultResource <T>()
            where T : Resource
        {
            Type resourceType = typeof(T);
            T    result       = null;

            // Try to create default resources
            if (resourceType == typeof(MaterialResource))
            {
                result = new SimpleColoredMaterialResource() as T;
            }
            else if (resourceType == typeof(TextureResource))
            {
#if DESKTOP
                result = new LinearGradientTextureResource(
                    Color4.White,
                    Color4.LightGray,
                    GradientDirection.Directional,
                    32, 32) as T;
#else
                result = new StandardTextureResource(
                    new AssemblyResourceLink(
                        typeof(ResourceDictionary),
                        "SeeingSharp.Multimedia.Resources.Textures.Blank_16x16.png")) as T;
#endif
            }
            else if (resourceType == typeof(GeometryResource))
            {
                VertexStructure dummyStructure = new VertexStructure();
                dummyStructure.FirstSurface.BuildCube24V(
                    Vector3.Zero,
                    new Vector3(1f, 1f, 1f),
                    Color4.White);
                result = new GeometryResource(dummyStructure) as T;
            }

            //Try to create the resource using the standard constructor
            if (result == null)
            {
#if DESKTOP
                ConstructorInfo standardConstructor = resourceType.GetConstructor(
                    BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                    null, Type.EmptyTypes, null);
#else
                ConstructorInfo standardConstructor =
                    resourceType.GetTypeInfo().DeclaredConstructors
                    .FirstOrDefault((actConstructor) => actConstructor.GetParameters().Length <= 0);
#endif
                if (standardConstructor != null)
                {
                    result = Activator.CreateInstance(resourceType) as T;
                }
            }

            if (result == null)
            {
                throw new SeeingSharpGraphicsException("Unable to create default resource for resource type " + resourceType.FullName);
            }
            return(result);
        }
        /// <summary>
        /// Adds a new text geometry with the given text.
        /// </summary>
        internal static GeometryResource AddTextGeometry(this ResourceDictionary resourceDiciontary, NamedOrGenericKey resourceKey, string textToAdd, TextGeometryOptions textGeometryOptions)
        {
            VertexStructure newStructure = new VertexStructure();

            newStructure.FirstSurface.BuildTextGeometry(textToAdd, textGeometryOptions);
            newStructure.FirstSurface.Material = textGeometryOptions.SurfaceMaterial;
            return(resourceDiciontary.AddGeometry(resourceKey, newStructure));
        }
Esempio n. 6
0
 /// <summary>
 /// Gets all edges defined by this triangle.
 /// </summary>
 /// <param name="sourceStructure">The source structure.</param>
 public     Line[] GetEdges(VertexStructure sourceStructure)
 {
     return(new Line[]
     {
         new Line(
             sourceStructure.Vertices[this.Index1].Position,
             sourceStructure.Vertices[this.Index2].Position),
         new Line(
             sourceStructure.Vertices[this.Index2].Position,
             sourceStructure.Vertices[this.Index3].Position),
         new Line(
             sourceStructure.Vertices[this.Index3].Position,
             sourceStructure.Vertices[this.Index1].Position)
     });
 }
        /// <summary>
        /// Adds a text object displaying the given text.
        /// </summary>
        /// <param name="textToDisplay">The text to be displayed.</param>
        /// <param name="textOptions">All options regarding the text geometry generator.</param>
        /// <param name="realignToCenter">Moves all vertices so that the center is 0.</param>
        /// <param name="layer">The layer on which to add the object.</param>
        public GenericObject Add3DText(string textToDisplay, TextGeometryOptions textOptions, bool realignToCenter = false, string layer = Scene.DEFAULT_LAYER_NAME)
        {
            VertexStructure newStructure = new VertexStructure();

            newStructure.FirstSurface.BuildTextGeometry(
                textToDisplay,
                textOptions);

            if (realignToCenter)
            {
                newStructure.RealignToCenter();
            }

            var resTextGeometry = this.AddResource(() => new GeometryResource(newStructure));

            return(this.AddGeneric(resTextGeometry, layer));
        }
Esempio n. 8
0
        private static void AppendWallObjectToScene(SceneManipulator manipulator, int sideLength)
        {
            // Define wall object (define geometry and create object for the scene).
            var resWallTexture = manipulator.AddTexture(
                new AssemblyResourceLink(
                    typeof(SeeingSharpSampleResources),
                    "Textures.Wall.png"));
            var resWallMaterial = manipulator.AddSimpleColoredMaterial(resWallTexture);

            VertexStructure wallStructure = new VertexStructure();

            wallStructure.FirstSurface.EnableTextureTileMode(new Vector2(2f, 2f));
            wallStructure.FirstSurface.BuildCube24V(
                new Vector3(-sideLength * SPACE_X / 2f - 10f, 0f, -sideLength * SPACE_Z / 2f),
                new Vector3(0.2f, sideLength * SPACE_Y, sideLength * SPACE_Z),
                Color4.Gray);
            wallStructure.FirstSurface.Material = resWallMaterial;
            var           resWallGeometry = manipulator.AddGeometry(wallStructure);
            GenericObject wallObject      = manipulator.AddGeneric(resWallGeometry, new Vector3(6.3f, 0f, 0f));
        }
Esempio n. 9
0
        /// <summary>
        /// Performs an intersection test using given picking ray and picking options.
        /// </summary>
        /// <param name="pickingRay">The given picking ray.</param>
        /// <param name="pickingOptions">The picking options.</param>
        /// <param name="distance">The distance from origin to the picking point.</param>
        public bool Intersects(Ray pickingRay, PickingOptions pickingOptions, out float distance)
        {
            distance = float.MaxValue;
            bool result = false;

            for (int loop = 0; loop < m_loadedStructures.Length; loop++)
            {
                VertexStructure actLoadedStructure = m_loadedStructures[loop].VertexStructure;

                float currentDistance = float.NaN;
                if (actLoadedStructure.Intersects(pickingRay, pickingOptions, out currentDistance))
                {
                    result = true;
                    if (currentDistance < distance)
                    {
                        distance = currentDistance;
                    }
                }
            }

            return(result);
        }
Esempio n. 10
0
        /// <summary>
        /// Loads the resource.
        /// </summary>
        protected override void LoadResourceInternal(EngineDevice device, ResourceDictionary resources)
        {
            // Build structures
            VertexStructure[] structures = new VertexStructure[]
            {
                m_objectType.BuildStructure(new StructureBuildOptions(device.SupportedDetailLevel))
            };

            // Build BoundingBox around all vertices
            List <Vector3> vertexLocations = new List <Vector3>();

            for (int loop = 0; loop < structures.Length; loop++)
            {
                foreach (Vertex actVertex in structures[loop].Vertices)
                {
                    vertexLocations.Add(actVertex.Position);
                }
            }
            m_boundingBox = new BoundingBox(vertexLocations);

            // Build geometry
            m_loadedStructures = BuildBuffers(device, structures, resources);
        }
 /// <summary>
 /// Adds a new geometry resource.
 /// </summary>
 internal static GeometryResource AddGeometry(this ResourceDictionary resourceDiciontary, VertexStructure structure)
 {
     return(resourceDiciontary.AddResource(new GeometryResource(structure)));
 }
Esempio n. 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GeometryResource"/> class.
 /// </summary>
 /// <param name="vertexStructure">The vertex structures.</param>
 public GeometryResource(VertexStructure vertexStructure)
     : this(new GenericObjectType(vertexStructure))
 {
 }
Esempio n. 13
0
        /// <summary>
        /// Builds vertex and index buffers.
        /// </summary>
        /// <param name="device">The device on which to build all buffers.</param>
        /// <param name="structures">All structure to be loaded.</param>
        /// <param name="resources">The current resource dictionary</param>
        protected virtual LoadedStructureInfo[] BuildBuffers(EngineDevice device, VertexStructure[] structures, ResourceDictionary resources)
        {
            List <LoadedStructureInfo> result         = new List <LoadedStructureInfo>(structures.Length * 2);
            List <StandardVertex[]>    cachedVertices = new List <StandardVertex[]>(2);
            List <int[]> cachedIndices = new List <int[]>(6);

            int structuresCount = structures.Length;
            int actVertexCount  = 0;
            int actIndexCount   = 0;
            int lastFinishedVertexBufferResultIndex = -1;
            int lastFinishedIndexBufferResultIndex  = -1;
            int vertexBufferID = 0;
            int indexBufferID  = 0;

            // Define the action which finishes current index buffer
            Action actionFinishIndexBuffer = () =>
            {
                // Create the vertex buffer
                D3D11.Buffer indexBuffer = GraphicsHelper.CreateImmutableIndexBuffer(
                    device,
                    cachedIndices.ToArray());
                cachedIndices.Clear();
                actIndexCount = 0;
                indexBufferID++;

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

            // Define the action which finishes current vertex buffer
            Action actionFinishVertexBuffer = () =>
            {
                // Create the vertex buffer
                D3D11.Buffer vertexBuffer = GraphicsHelper.CreateImmutableVertexBuffer(
                    device,
                    cachedVertices.ToArray());
                cachedVertices.Clear();
                actVertexCount = 0;
                vertexBufferID++;

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

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

            // Load all structures into memory within a loop
            for (int loopStruct = 0; loopStruct < structuresCount; loopStruct++)
            {
                VertexStructure actStructure = structures[loopStruct];
                if (actStructure.CountVertices == 0)
                {
                    continue;
                }
                if (actStructure.CountSurfaces == 0)
                {
                    continue;
                }

                // Handle vertex data
                StandardVertex[] vertexArray = StandardVertex.FromVertexStructure(actStructure);
                if (actVertexCount + vertexArray.Length > MAX_VERTEX_COUNT_PER_BUFFER)
                {
                    actionFinishVertexBuffer();
                }
                cachedVertices.Add(vertexArray);
                int actBaseVertex = actVertexCount;
                actVertexCount += vertexArray.Length;

                // Sort all surfaces by material
                List <VertexStructureSurface> surfaceList = new List <VertexStructureSurface>(actStructure.Surfaces);
                surfaceList.Sort((left, right) => left.MaterialProperties.GetHashCode().CompareTo(right.MaterialProperties.GetHashCode()));

                int surfaceCount = surfaceList.Count;
                for (int loopSurface = 0; loopSurface < surfaceCount; loopSurface++)
                {
                    VertexStructureSurface actSurface = surfaceList[loopSurface];
                    if (actSurface.CountTriangles == 0)
                    {
                        continue;
                    }

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

                    // Get or create the material
                    LoadedStructureInfo lastStructureInfo = result.Count > 0 ? result[result.Count - 1] : null;
                    if ((lastStructureInfo != null) &&
                        (lastStructureInfo.IndexBuffer == null) &&
                        (actSurface.MaterialProperties.Equals(lastStructureInfo.MaterialProperties)))
                    {
                        LoadedStructureInfo actStructureInfo = result[result.Count - 1];
                        actStructureInfo.IndexCount = actStructureInfo.IndexCount + indexArray.Length;
                    }
                    else
                    {
                        MaterialResource actMaterialResource = resources.GetOrCreateMaterialResourceAndEnsureLoaded(actSurface);

                        // Create some information about the loaded structures
                        LoadedStructureInfo newStructureInfo = new LoadedStructureInfo();
                        newStructureInfo.VertexBufferID     = vertexBufferID;
                        newStructureInfo.IndexBufferID      = indexBufferID;
                        newStructureInfo.SizePerVertex      = StandardVertex.Size;
                        newStructureInfo.VertexStructure    = actStructure;
                        newStructureInfo.IndexCount         = indexArray.Length;
                        newStructureInfo.StartIndex         = actIndexCount - indexArray.Length;
                        newStructureInfo.Material           = actMaterialResource;
                        newStructureInfo.MaterialProperties = actSurface.MaterialProperties;
                        newStructureInfo.VertexBuffer       = null;
                        newStructureInfo.IndexBuffer        = null;
                        newStructureInfo.InputLayout        = newStructureInfo.Material.GenerateInputLayout(
                            device, StandardVertex.InputElements, MaterialApplyInstancingMode.SingleObject);
                        result.Add(newStructureInfo);
                    }
                }
            }

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

            return(result.ToArray());
        }
Esempio n. 14
0
 /// <summary>
 /// Redefines the content of this geometry resource.
 /// </summary>
 public void Redefine(ResourceDictionary resources, VertexStructure vertexStructures)
 {
     this.Redefine(resources, new GenericObjectType(vertexStructures));
 }
Esempio n. 15
0
        /// <summary>
        /// Gets an array of StandardVertices from the given VertexStructure object.
        /// </summary>
        /// <param name="source">The VertexStructure object.</param>
        public static                               StandardVertex[] FromVertexStructure(VertexStructure source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            int vertexCount = source.CountVertices;

            //Create result array
            StandardVertex[] result = new StandardVertex[vertexCount];
            for (int loop = 0; loop < source.CountVertices; loop++)
            {
                Vertex vertex = source.Vertices[loop];
                result[loop] = new StandardVertex(vertex);
            }

            return(result);
        }
 /// <summary>
 /// Adds a new geometry resource.
 /// </summary>
 internal static GeometryResource AddGeometry(this ResourceDictionary resourceDiciontary, NamedOrGenericKey resourceKey, VertexStructure structure)
 {
     return(resourceDiciontary.AddResource(resourceKey, new GeometryResource(structure)));
 }
Esempio n. 17
0
 /// <summary>
 /// Adds a new geometry resource to the scene.
 /// </summary>
 /// <param name="sceneManipulator">The manipulator of the scene.</param>
 /// <param name="vertexStructure">The structures which define the geometry.</param>
 public static NamedOrGenericKey AddGeometry(this SceneManipulator sceneManipulator, VertexStructure vertexStructure)
 {
     return(sceneManipulator.AddResource <GeometryResource>(() => new GeometryResource(vertexStructure)));
 }