예제 #1
0
    private void TranslucentRender()
    {
        GraphicsDeviceContext device = GraphicsDeviceContext.Instance;

        device.SetBlendState(Addition);
        device.SetRasterizerState(CullingOff);
        device.SetDepthStencilState(ZTestOnWriteOff);
        device.SetVertexLayout(VertexLayout);
        device.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip);
        device.SetVertexShader(VertexShader);
        device.SetPixelShader(PixelShader);

        device.SetVertexBuffer(VertexPositions, 0);
        device.SetVertexBuffer(VertexTextureCoords, 1);

        PixelShader.SetSamplerState(Wrap, 0);
        PixelShader.SetTexture(Texture, 0);

        InstanceColors.Lock(Accessibility.DynamicWriteOnly);
        InstanceWVPs.Lock(Accessibility.DynamicWriteOnly);

        var viewing    = Entity.Find("camera").Get <CameraComponent>().ViewingMatrix;
        var projection = Entity.Find("projector").Get <ProjectorComponent>().ProjectionMatrix;
        var eye        = Entity.Find("camera").Get <TransformComponent>().Position;

        int     k         = 0;
        Vector3 position2 = Nodes.First.Value.Position;

        foreach (var node in Nodes)
        {
            Vector3 position1 = node.Position;

            if (k > 0)
            {
                Vector3 vz = position2 - position1;
                vz.Normalize();

                Vector3 vy = Vector3.Outer(vz, position1 - eye);
                vy.Normalize();
                Vector3   vx     = Vector3.Outer(vy, vz);
                Matrix4x3 matrix = new Matrix4x3().Identity();
                matrix.M11 = vx.X; matrix.M12 = vx.Y; matrix.M13 = vx.Z;
                matrix.M21 = vy.X; matrix.M22 = vy.Y; matrix.M23 = vy.Z;
                matrix.M31 = vz.X; matrix.M32 = vz.Y; matrix.M33 = vz.Z;

                var world = new Matrix4x3().Identity();
                world.Translate(position1);
                world.Transform(matrix);
                world.Scale(1, 2, (position1 - position2).Magnitude);

                InstanceWVPs.Write(k, world * viewing * projection);

                //float alpha = clamp(exp(-square(node.Time - 0.5f) / 0.075f), 0, 1) * 0.75f;
                float alpha = clamp(sin((1 - node.Time) * PI) - 0.05f, 0, 1) * (1 - node.Time);
                //float alpha = 1.0f - node.Time;
                InstanceColors.Write(k, new Color(alpha, alpha, alpha));
            }
            k++;
            position2 = position1;
        }

        InstanceColors.Unlock();
        InstanceWVPs.Unlock();

        device.SetInstanceBuffer(InstanceColors, 2);
        device.SetInstanceBuffer(InstanceWVPs, 3);

        device.DrawInstanced(4, Nodes.Count - 1);
    }
예제 #2
0
            public void SetLighting(LightSettings lightSettings, CameraBase camera)
            {
                Matrix4x3 cameraM = Matrix4x3.Identity;

                if (lightSettings.IsAnyLightAffixedToCamera)
                {
                    // if a light is affixed to the camera, its position is considered to be in camera coordinates
                    // but here we need the light in world coordinates
                    // cameraM transforms from camera coordinates to world coordinates
                    cameraM = camera.InverseLookAtRHMatrix;
                }

                // first ambient light
                var al = lightSettings.AmbientLight;

                SetAmbientLight(al.ColorBelow.Color, al.ColorAbove.Color, al.LightAmplitude, al.IsAffixedToCamera ? cameraM.Transform(al.DirectionBelowToAbove) : al.DirectionBelowToAbove);

                for (int idx = 0; idx < 4; ++idx)
                {
                    var l = lightSettings.GetDiscreteLight(idx);
                    if (null == l)
                    {
                        ClearSingleLight(idx);
                    }
                    else if (l is DirectionalLight)
                    {
                        var dl = (DirectionalLight)l;
                        SetDirectionalLight(
                            idx,
                            dl.Color.Color,
                            dl.LightAmplitude,
                            dl.IsAffixedToCamera ? cameraM.Transform(dl.DirectionToLight) : dl.DirectionToLight
                            );
                    }
                    else if (l is PointLight)
                    {
                        var pl = (PointLight)l;
                        SetPointLight(
                            idx,
                            pl.Color.Color,
                            pl.LightAmplitude,
                            pl.IsAffixedToCamera ? cameraM.Transform(pl.Position) : pl.Position,
                            pl.Range
                            );
                    }
                    else if (l is SpotLight)
                    {
                        var sl = (SpotLight)l;

                        // calculation of SpotCosInnerConeRcp: it is in reality not 1/CosInnerConeAngle, but it is  1/(Cos(InnerConeAngle) - Cos(OuterConeAngle))
                        double diffCos             = Math.Cos(sl.InnerConeAngle) - Math.Cos(sl.OuterConeAngle);
                        double SpotCosInnerConeRcp = diffCos >= 1E-18 ? 1 / diffCos : 1E18;

                        SetSpotLight(
                            idx,
                            sl.Color.Color,
                            sl.LightAmplitude,
                            sl.IsAffixedToCamera ? cameraM.Transform(sl.Position) : sl.Position,
                            sl.IsAffixedToCamera ? cameraM.Transform(sl.DirectionToLight) : sl.DirectionToLight,
                            sl.Range,
                            Math.Cos(sl.OuterConeAngle),
                            SpotCosInnerConeRcp
                            );
                    }
                    else
                    {
                        throw new NotImplementedException(string.Format("The type of lighting ({0}) is not implemented here."));
                    }
                }

                AssembleLights();
            }
예제 #3
0
 public RectangleD3D GetBounds()
 {
     return(RectangleD3D.NewRectangleIncludingAllPoints(Points.Select(p => _transformation.Transform(p))));
 }
예제 #4
0
        /// <summary>
        /// Converts relative positions of the object (0..1, 0..1, 0..1) to coordinates in the parent's (layer) coordinate system.
        /// </summary>
        /// <param name="relativeObjectCoordinates">Relative coordinates of the rectangle (0,0 is the upper left corner, 1,1 is the lower right corner).</param>
        /// <returns>The absolute parent coordinates of this point (i.e. normally layer coordinates).</returns>
        public PointD3D RelativeLocalToAbsoluteParentCoordinates(VectorD3D relativeObjectCoordinates)
        {
            var bounds = Bounds;

            return(_transformation.Transform(bounds.Location + VectorD3D.MultiplicationElementwise(relativeObjectCoordinates, bounds.Size)));
        }