Пример #1
0
        internal void doRender(GraphicsDevice device)
        {
            if (targetChanged)
            {
                CreateRenderTarget();
            }
            if (target != null)
            {
                device.SetRenderTarget(target);
                device.Clear(Microsoft.Xna.Framework.Color.Transparent);
            }
            Clear(device);

#if DEBUG
            if (logRenderCalls)
            {
                if (target == null)
                {
                    Debug.Log("**** Camera begin : ", name, "****");
                }
                else
                {
                    Debug.Log("**** Camera begin to texture : ", name, "****");
                }
            }
#endif
            RenderStats.Clear();

            if (CulledRenderQueue.Count > 0)
            {
                int rqCount = CulledRenderQueue.Count;
                for (int i = 0; i < rqCount; i++)
                {
                    CulledRenderQueue[i].Render(device, this);
                }
            }
            frustum.Matrix = view * projectionMatrix;

            int q     = 0;
            int count = oldRenderQueue.Count;

            if (count > 0)
            {
                BasicEffect.View       = view;
                BasicEffect.Projection = projectionMatrix;

                if (Light.HasLights)
                {
                    Light.EnableLighting(BasicEffect, 0);
                }
            }

            for (int i = 0; i < count; i++)
            {
                Renderer r = oldRenderQueue[i];

                if (r.gameObject == null)
                {
                    // This will happen if the game object has been destroyed in update.
                    // It is acceptable behaviour.
                    continue;
                }

                if (r.material == null)
                {
                    // We have no material, so we will skip rendering
                    continue;
                }

                if (r.isPartOfStaticBatch)
                {
                    // The object is statically batched, so we will skip it
                    continue;
                }

                if (r.material.renderQueue != q)
                {
                    if (q > 0)
                    {
                        dynamicBatchRenderer.DoDraw(device, this);
                    }
                    q = r.material.renderQueue;
                }

                if (r.gameObject.active && r.enabled)
                {
                    r.Draw(device, this);
                }
            }

            dynamicBatchRenderer.DoDraw(device, this);
        }
Пример #2
0
        public void Render(GraphicsDevice device, Camera cam)
        {
            bool devicePrepared = false;
            int  id             = cam.GetInstanceID();

            if (!CameraCullingInfo.ContainsKey(id))
            {
                return;
            }
            PooledPriorityQueue cullingInfo = CameraCullingInfo[id];

            for (int i = 0; i < cullingInfo.Count; i++)
            {
                Transform t = Application.Find <Transform>(cullingInfo[i]);
                if (t == null)
                {
                    continue;
                }
                if (!t.renderer.enabled || !t.gameObject.active)
                {
                    continue;
                }
#if DEBUG
                if (Camera.logRenderCalls)
                {
                    Debug.LogFormat("Render: {0} for {1} on {2}", this, t.gameObject, cam.gameObject);
                }
#endif
                Effect e = Material.shader.effect;
                if (!devicePrepared)
                {
                    devicePrepared = true;
                    device.SetVertexBuffer(VertexBuffer);
                    device.Indices = IndexBuffer;

                    Material.shader.ApplyPreRenderSettings(Material, UseVertexColor);
                    Material.SetBlendState(device);

                    IEffectMatrices ems = e as IEffectMatrices;
                    if (ems != null)
                    {
                        ems.World      = t.world;
                        ems.View       = cam.view;
                        ems.Projection = cam.projectionMatrix;
                    }
                }
                else
                {
                    IEffectMatrices ems = e as IEffectMatrices;
                    if (ems != null)
                    {
                        ems.World = t.world;
                    }
                }
                foreach (EffectPass pass in e.CurrentTechnique.Passes)
                {
                    pass.Apply();
                    device.DrawIndexedPrimitives(
                        PrimitiveType.TriangleList,
                        0,
                        0,
                        VertexBuffer.VertexCount,
                        0,
                        IndexBuffer.IndexCount / 3
                        );
                }
                RenderStats.AddDrawCall(batches, VertexBuffer.VertexCount, IndexBuffer.IndexCount / 3);
            }
        }
Пример #3
0
        public override void Draw(GraphicsDevice device, Camera cam)
        {
            UInt32 tiles = tilesV * tilesU;

            for (UInt32 i = 0; i < tiles; ++i)
            {
                quadTreeTiles[i].visible = !cam.DoFrustumCulling(ref quadTreeTiles[i].boundingBox);
#if DEBUG
                if (Camera.logRenderCalls)
                {
                    if (!quadTreeTiles[i].visible)
                    {
                        Debug.LogFormat("VP Cull Static batch: Tile {0} on {1} on {2}", i, gameObject, cam.gameObject);
                    }
                }
#endif
            }

            for (int i = 0; i < _sharedMaterials.Length; i++)
            {
                Material mat    = _sharedMaterials[i];
                Effect   effect = cam.BasicEffect;
                if (useLightMap)
                {
                    effect = dtEffect;
                }
#if DEBUG
                if (Camera.logRenderCalls)
                {
                    Debug.LogFormat("Static batch RQ({6}): Tile {0} go {1} cam {2}. Using {3} and material {4} and shader {5}", i, gameObject, cam.gameObject, effect.GetType().Name, mat, mat.shaderName, mat.finalRenderQueue);
                }
#endif

                (effect as IEffectMatrices).World      = Matrix.Identity;
                (effect as IEffectMatrices).View       = cam.view;
                (effect as IEffectMatrices).Projection = cam.projectionMatrix;

                if (effect is IEffectLights)
                {
                    (effect as IEffectLights).LightingEnabled = Light.HasLights;
                }

                if (effect is DualTextureEffect)
                {
                    (effect as DualTextureEffect).DiffuseColor = new Microsoft.Xna.Framework.Vector3(mat.color.r / 2, mat.color.g / 2, mat.color.b / 2);
                    (effect as DualTextureEffect).Alpha        = mat.color.a;
                    (effect as DualTextureEffect).Texture      = mat.mainTexture;
                    (effect as DualTextureEffect).Texture2     = LightmapSettings.lightmaps[lightmapIndex].lightmapFar;
                }
                if (effect is BasicEffect)
                {
                    mat.SetTextureState(effect as BasicEffect);
                }
                mat.SetBlendState(device);

                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Apply();

                    for (UInt32 j = 0; j < tiles; ++j)
                    {
                        if (!quadTreeTiles[j].visible ||
                            quadTreeTiles[j].vertexBuffer == null ||
                            quadTreeTiles[j].indexBuffer == null)
                        {
                            continue;
                        }

                        device.SetVertexBuffer(quadTreeTiles[j].vertexBuffer);
                        device.Indices = quadTreeTiles[j].indexBuffer[i];

                        device.DrawIndexedPrimitives(
                            PrimitiveType.TriangleList,
                            0,
                            0,
                            quadTreeTiles[j].vertexBuffer.VertexCount,
                            0,
                            quadTreeTiles[j].indexBuffer[i].IndexCount / 3
                            );
                        RenderStats.AddDrawCall(quadTreeTiles[j].batches, quadTreeTiles[j].vertexBuffer.VertexCount, quadTreeTiles[j].indexBuffer[i].IndexCount / 3);
                    }
                }
            }
        }