Example #1
0
 public static void AddInanimateModel(string modelName, ModelRenderer model)
 {
     if (mAssetLibrary[InanimateModelKey].ContainsKey(modelName))
     {
         throw new Exception("Duplicate model key: " + modelName);
     }
     mAssetLibrary[InanimateModelKey].Add(modelName, model);
 }
Example #2
0
        /// <summary>
        /// Renders to texture a centered image of a given renderer.
        /// </summary>
        public static Texture2D RenderPreviewImage(ModelRenderer renderer)
        {
            RenderTarget2D previewTexture = new RenderTarget2D(mDevice, 256, 256, false, SurfaceFormat.Color, DepthFormat.Depth24);

            mDevice.SetRenderTarget(previewTexture);
            mDevice.Clear(Color.DimGray);

            mDevice.SamplerStates[0] = SamplerState.PointClamp;
            mDevice.SamplerStates[1] = SamplerState.PointClamp;

            RasterizerState cull = new RasterizerState();
            cull.CullMode = CullMode.None;
            mDevice.RasterizerState = cull;
            mDevice.BlendState = BlendState.AlphaBlend;
            mDevice.DepthStencilState = DepthStencilState.Default;

            AnimateModelRenderer.AnimateModelParameters parameters = new AnimateModelRenderer.AnimateModelParameters();
            parameters.SkinTransforms = new Matrix[] { Matrix.Identity };
            parameters.World = Matrix.Identity;

            Vector3 cameraOffset = new Vector3(1.7f * renderer.BoundingSphere.Radius, 1.0f * renderer.BoundingSphere.Radius, 1.7f * renderer.BoundingSphere.Radius);
            Vector3 cameraPosition = renderer.BoundingSphere.Center + cameraOffset;

            float offsetLength = cameraOffset.Length();
            float nearDistance = offsetLength - renderer.BoundingSphere.Radius;
            float farDistance  = offsetLength + renderer.BoundingSphere.Radius;

            renderer.ClearAllInstances();
            renderer.AddInstance(parameters);

            if (mCamera != null)
            {
                renderer.RenderAllInstancesWithoutShadows(
                    Matrix.CreateLookAt(cameraPosition, renderer.BoundingSphere.Center, Vector3.Up),
                    Matrix.CreatePerspectiveFieldOfView(mCamera.FieldOfView, 1.0f, nearDistance, farDistance),
                    new Light(Vector3.Up, -Vector3.Up, Color.White.ToVector3(), Color.White.ToVector3(), Color.White.ToVector3()));
            }

            mDevice.SetRenderTarget(null);

            return previewTexture;
        }