Exemple #1
0
        public Light(H3DLight Light)
        {
            Matrix4 Transform =
                Matrix4.CreateScale(Light.TransformScale.ToVector3()) *
                Matrix4.CreateRotationX(Light.TransformRotation.X) *
                Matrix4.CreateRotationY(Light.TransformRotation.Y) *
                Matrix4.CreateRotationZ(Light.TransformRotation.Z) *
                Matrix4.CreateTranslation(Light.TransformTranslation.ToVector3());

            Position = Vector4.Transform(Transform, Vector4.UnitW).Xyz;

            Enabled = Light.IsEnabled;

            DistAttEnabled  = (Light.Flags & H3DLightFlags.HasDistanceAttenuation) != 0;
            TwoSidedDiffuse = (Light.Flags & H3DLightFlags.IsTwoSidedDiffuse) != 0;

            AngleLUTInput = (int)Light.LUTInput;
            AngleLUTScale = Light.LUTScale.ToSingle();

            if (Light.Content is H3DFragmentLight FragmentLight)
            {
                Direction = FragmentLight.Direction.ToVector3();

                Ambient   = FragmentLight.AmbientColor.ToColor4();
                Diffuse   = FragmentLight.DiffuseColor.ToColor4();
                Specular0 = FragmentLight.Specular0Color.ToColor4();
                Specular1 = FragmentLight.Specular1Color.ToColor4();

                float AttDiff =
                    FragmentLight.AttenuationEnd -
                    FragmentLight.AttenuationStart;

                AttDiff = Math.Max(AttDiff, 0.01f);

                AttenuationScale = 1f / AttDiff;
                AttenuationBias  = -FragmentLight.AttenuationStart / AttDiff;

                AngleLUTTableName   = FragmentLight.AngleLUTTableName;
                AngleLUTSamplerName = FragmentLight.AngleLUTSamplerName;

                DistanceLUTTableName   = FragmentLight.DistanceLUTTableName;
                DistanceLUTSamplerName = FragmentLight.DistanceLUTSamplerName;

                Directional = Light.Type == H3DLightType.FragmentDir;

                /*
                 * Note: Directional lights doesn't need a position, because they
                 * only specify the direction the light is pointing to.
                 * The Shader expects the direction to be on the Position vector
                 * on this case. The Direction vector is only used for the SpotLight
                 * direction on the Shader.
                 */
                if (Directional)
                {
                    Position = Direction;
                }
            }
        }
Exemple #2
0
        public H3DLight ToH3DLight()
        {
            H3DLight Output = new H3DLight()
            {
                Name = Name
            };

            Output.IsEnabled = IsEnabled;

            Output.TransformScale       = TransformScale;
            Output.TransformRotation    = TransformRotation;
            Output.TransformTranslation = TransformTranslation;

            if (this is GfxHemisphereLight HemisphereLight)
            {
                Output.Type = H3DLightType.Hemisphere;

                Output.Content = new H3DHemisphereLight()
                {
                    SkyColor    = HemisphereLight.SkyColor,
                    GroundColor = HemisphereLight.GroundColor,
                    Direction   = HemisphereLight.Direction,
                    LerpFactor  = HemisphereLight.LerpFactor
                };
            }
            else if (this is GfxAmbientLight AmbientLight)
            {
                Output.Type = H3DLightType.Ambient;

                Output.Content = new H3DAmbientLight()
                {
                    Color = AmbientLight.Color
                };
            }
            else if (this is GfxVertexLight VertexLight)
            {
                Output.Type = H3DLightType.Vertex | (H3DLightType)VertexLight.Type;

                Output.Content = new H3DVertexLight()
                {
                    AmbientColor         = VertexLight.AmbientColor,
                    DiffuseColor         = VertexLight.DiffuseColor,
                    Direction            = VertexLight.Direction,
                    AttenuationConstant  = VertexLight.AttenuationConstant,
                    AttenuationLinear    = VertexLight.AttenuationLinear,
                    AttenuationQuadratic = VertexLight.AttenuationQuadratic,
                    SpotExponent         = VertexLight.SpotExponent,
                    SpotCutOffAngle      = VertexLight.SpotCutOffAngle
                };
            }
            else if (this is GfxFragmentLight FragmentLight)
            {
                Output.Type = H3DLightType.Fragment | (H3DLightType)FragmentLight.Type;

                Output.LUTInput = FragmentLight.AngleSampler?.Input ?? 0;
                Output.LUTScale = FragmentLight.AngleSampler?.Scale ?? 0;

                Output.Content = new H3DFragmentLight()
                {
                    AmbientColor           = FragmentLight.AmbientColor,
                    DiffuseColor           = FragmentLight.DiffuseColor,
                    Specular0Color         = FragmentLight.Specular0Color,
                    Specular1Color         = FragmentLight.Specular1Color,
                    Direction              = FragmentLight.Direction,
                    AttenuationStart       = FragmentLight.AttenuationStart,
                    AttenuationEnd         = FragmentLight.AttenuationEnd,
                    DistanceLUTTableName   = FragmentLight.DistanceSampler?.TableName,
                    DistanceLUTSamplerName = FragmentLight.DistanceSampler?.SamplerName,
                    AngleLUTTableName      = FragmentLight.AngleSampler?.Sampler.TableName,
                    AngleLUTSamplerName    = FragmentLight.AngleSampler?.Sampler.SamplerName
                };
            }

            return(Output);
        }
Exemple #3
0
        public static GfxLight ConvertLight(H3DLight Src)
        {
            GfxLight Lt = null;

            switch (Src.Type)
            {
            case H3DLightType.Hemisphere:
                H3DHemisphereLight Hl = (H3DHemisphereLight)Src.Content;
                Lt = new GfxHemisphereLight()
                {
                    SkyColor    = Hl.SkyColor,
                    GroundColor = Hl.GroundColor,
                    Direction   = Hl.Direction,
                    LerpFactor  = Hl.LerpFactor,
                };
                break;

            case H3DLightType.Ambient:
                Lt = new GfxAmbientLight()
                {
                    Color = ((H3DAmbientLight)Src.Content).Color
                };
                break;

            case H3DLightType.Vertex:
            case H3DLightType.VertexDir:
            case H3DLightType.VertexPoint:
            case H3DLightType.VertexSpot:
                H3DVertexLight Vl = (H3DVertexLight)Src.Content;
                Lt = new GfxVertexLight()
                {
                    Type                 = (GfxLightType)((Src.Type & ~H3DLightType.Vertex) - H3DLightType.VertexDir),
                    AmbientColor         = Vl.AmbientColor,
                    DiffuseColor         = Vl.DiffuseColor,
                    Direction            = Vl.Direction,
                    AttenuationConstant  = Vl.AttenuationConstant,
                    AttenuationLinear    = Vl.AttenuationLinear,
                    AttenuationQuadratic = Vl.AttenuationQuadratic,
                    SpotExponent         = Vl.SpotExponent,
                    SpotCutOffAngle      = Vl.SpotCutOffAngle
                };
                break;

            case H3DLightType.Fragment:
            case H3DLightType.FragmentDir:
            case H3DLightType.FragmentPoint:
            case H3DLightType.FragmentSpot:
                H3DFragmentLight Fl = (H3DFragmentLight)Src.Content;
                Lt = new GfxFragmentLight()
                {
                    Type             = (GfxLightType)((Src.Type & ~H3DLightType.Fragment) - H3DLightType.FragmentDir),
                    AmbientColor     = Fl.AmbientColor,
                    DiffuseColor     = Fl.DiffuseColor,
                    Specular0Color   = Fl.Specular0Color,
                    Specular1Color   = Fl.Specular1Color,
                    Direction        = Fl.Direction,
                    AttenuationStart = Fl.AttenuationStart,
                    AttenuationEnd   = Fl.AttenuationEnd,
                    DistanceSampler  = (Fl.DistanceLUTTableName != null && Fl.DistanceLUTSamplerName != null) ? new GfxLUTReference(Fl.DistanceLUTSamplerName, Fl.DistanceLUTTableName) : null,
                    AngleSampler     = (Fl.AngleLUTTableName != null && Fl.AngleLUTSamplerName != null) ? new GfxFragLightLUT(new GfxLUTReference(Fl.AngleLUTSamplerName, Fl.AngleLUTTableName)) : null
                };
                break;
            }

            if (Lt != null)
            {
                Lt.Header               = new GfxRevHeader("CLGT");
                Lt.Name                 = Src.Name;
                Lt.IsEnabled            = Src.IsEnabled;
                Lt.TransformScale       = Src.TransformScale;
                Lt.TransformRotation    = Src.TransformRotation;
                Lt.TransformTranslation = Src.TransformTranslation;
            }
            return(Lt);
        }