Example #1
0
        public void CreateBabylonMaterial(BabylonScene scene)
        {
            var babylonMaterial = new BabylonMaterial();
            scene.MaterialsList.Add(babylonMaterial);

            // Guid
            babylonMaterial.id = ID.ToString();

            // Name
            babylonMaterial.name = Name;

            // Data
            babylonMaterial.backFaceCulling = BackFaceCulling;
            babylonMaterial.diffuse = Diffuse.ToArray();
            babylonMaterial.emissive = Emissive.ToArray();
            babylonMaterial.specular = Specular.ToArray();
            babylonMaterial.specularPower = SpecularPower;
            babylonMaterial.alpha = Alpha;

            if (string.IsNullOrEmpty(DiffuseTexture))
            {
                babylonMaterial.diffuseTexture = null;
                return;
            }

            babylonMaterial.diffuseTexture = new BabylonTexture();
            babylonMaterial.diffuseTexture.name = Path.GetFileName(DiffuseTexture);

            scene.AddTexture(DiffuseTexture);
        }
        void DumpStandardMaterial(NovaStandardMaterial stdMaterial, BabylonScene babylonScene)
        {
            var babylonMaterial = new BabylonMaterial();
            babylonScene.MaterialsList.Add(babylonMaterial);

            babylonMaterial.name = stdMaterial.Name;
            babylonMaterial.id = stdMaterial.ID.ToString();
            babylonMaterial.alpha = stdMaterial.Alpha;
            babylonMaterial.ambient = (stdMaterial.Ambient * (1.0f - stdMaterial.SelfIllumination)).ToArray();
            babylonMaterial.diffuse = (stdMaterial.Diffuse * (1.0f - stdMaterial.SelfIllumination)).ToArray();
            babylonMaterial.emissive = (stdMaterial.Emissive * stdMaterial.SelfIllumination).ToArray();
            babylonMaterial.specular = stdMaterial.Specular.ToArray();
            babylonMaterial.specularPower = stdMaterial.SpecularSharpness;
            babylonMaterial.backFaceCulling = (stdMaterial.CullMode == NovaCull.CounterClockwise);

            if (stdMaterial.DiffuseTexture != null && stdMaterial.DiffuseTexture.HasTextureData && stdMaterial.OpacityTexture != null && stdMaterial.OpacityTexture.HasTextureData)
            {
                if (stdMaterial.DiffuseTexture.LoadedTexture == stdMaterial.OpacityTexture.LoadedTexture)
                {
                    stdMaterial.DiffuseTexture = stdMaterial.OpacityTexture;
                    stdMaterial.OpacityTexture = null;
                }
            }

            if (stdMaterial.DiffuseTexture != null && stdMaterial.DiffuseTexture.HasTextureData && !stdMaterial.DiffuseTexture.IsVideo && !stdMaterial.DiffuseTexture.IsCubeRender)
            {
                babylonMaterial.diffuseTexture = new BabylonTexture();
                DumpTexture(stdMaterial.DiffuseTexture, babylonMaterial.diffuseTexture, babylonScene);
            }

            if (stdMaterial.AmbientTexture != null && stdMaterial.AmbientTexture.HasTextureData && !stdMaterial.AmbientTexture.IsVideo && !stdMaterial.AmbientTexture.IsCubeRender)
            {
                babylonMaterial.ambientTexture = new BabylonTexture();
                DumpTexture(stdMaterial.AmbientTexture, babylonMaterial.ambientTexture, babylonScene);
            }

            if (stdMaterial.OpacityTexture != null && stdMaterial.OpacityTexture.HasTextureData && !stdMaterial.OpacityTexture.IsVideo && !stdMaterial.OpacityTexture.IsCubeRender)
            {
                babylonMaterial.opacityTexture = new BabylonTexture();
                DumpTexture(stdMaterial.OpacityTexture, babylonMaterial.opacityTexture, babylonScene);
            }

            if (mirrorsMaterials.ContainsKey(stdMaterial))
            {
                babylonMaterial.reflectionTexture = new BabylonTexture();
                var novaObject = mirrorsMaterials[stdMaterial];
                DumpRenderTargetTexture(stdMaterial.ReflectionTexture, babylonMaterial.reflectionTexture, novaObject.MirrorMapSize, novaObject.MirrorLevel, novaObject.MirroredObjects);

                AttachMirrorPlane(babylonMaterial.reflectionTexture, novaObject);
            }
            else if (stdMaterial.ReflectionTexture != null && stdMaterial.ReflectionTexture.HasTextureData && !stdMaterial.ReflectionTexture.IsVideo && !stdMaterial.ReflectionTexture.IsCubeRender)
            {
                babylonMaterial.reflectionTexture = new BabylonTexture();
                DumpTexture(stdMaterial.ReflectionTexture, babylonMaterial.reflectionTexture, babylonScene);
            }

            if (stdMaterial.EmissiveTexture != null && stdMaterial.EmissiveTexture.HasTextureData && !stdMaterial.EmissiveTexture.IsVideo && !stdMaterial.EmissiveTexture.IsCubeRender)
            {
                babylonMaterial.emissiveTexture = new BabylonTexture();
                DumpTexture(stdMaterial.EmissiveTexture, babylonMaterial.emissiveTexture, babylonScene);
            }

            if (stdMaterial.SpecularTexture != null && stdMaterial.SpecularTexture.HasTextureData && !stdMaterial.SpecularTexture.IsVideo && !stdMaterial.SpecularTexture.IsCubeRender)
            {
                babylonMaterial.specularTexture = new BabylonTexture();
                DumpTexture(stdMaterial.SpecularTexture, babylonMaterial.specularTexture, babylonScene);
            }

            if (stdMaterial.BumpTexture != null && stdMaterial.BumpTexture.HasTextureData && !stdMaterial.BumpTexture.IsVideo && !stdMaterial.BumpTexture.IsCubeRender)
            {
                babylonMaterial.bumpTexture = new BabylonTexture();
                DumpTexture(stdMaterial.BumpTexture, babylonMaterial.bumpTexture, babylonScene);
                babylonMaterial.bumpTexture.level /= 2.0f;
            }
        }
        void DumpPerPixelShader(NovaPerPixelShader ppShader, BabylonScene babylonScene)
        {
            var babylonMaterial = new BabylonMaterial();
            babylonScene.MaterialsList.Add(babylonMaterial);

            babylonMaterial.name = ppShader.Name;
            babylonMaterial.id = ppShader.ID.ToString();
            babylonMaterial.alpha = ppShader.Alpha;
            babylonMaterial.ambient = (ppShader.Ambient * (1.0f - ppShader.SelfIllumination)).ToArray();
            babylonMaterial.diffuse = (ppShader.Diffuse * (1.0f - ppShader.SelfIllumination)).ToArray();
            babylonMaterial.emissive = (ppShader.Emissive * ppShader.SelfIllumination).ToArray();
            babylonMaterial.specular = ppShader.Specular.ToArray();
            babylonMaterial.specularPower = ppShader.SpecularSharpness;
            babylonMaterial.backFaceCulling = (ppShader.CullMode == NovaCull.CounterClockwise);

            if (ppShader.DiffuseTexture != null && ppShader.DiffuseTexture.HasTextureData && ppShader.OpacityTexture != null && ppShader.OpacityTexture.HasTextureData)
            {
                if (ppShader.DiffuseTexture.LoadedTexture == ppShader.OpacityTexture.LoadedTexture)
                {
                    ppShader.DiffuseTexture = ppShader.OpacityTexture;
                    ppShader.OpacityTexture = null;
                }
            }

            if (ppShader.DiffuseTexture != null && ppShader.DiffuseTexture.HasTextureData && !ppShader.DiffuseTexture.IsVideo && !ppShader.DiffuseTexture.IsCubeRender)
            {
                babylonMaterial.diffuseTexture = new BabylonTexture();
                DumpTexture(ppShader.DiffuseTexture, babylonMaterial.diffuseTexture, babylonScene);
            }

            if (ppShader.AmbientTexture != null && ppShader.AmbientTexture.HasTextureData && !ppShader.AmbientTexture.IsVideo && !ppShader.AmbientTexture.IsCubeRender)
            {
                babylonMaterial.ambientTexture = new BabylonTexture();
                DumpTexture(ppShader.AmbientTexture, babylonMaterial.ambientTexture, babylonScene);
            }

            if (ppShader.OpacityTexture != null && ppShader.OpacityTexture.HasTextureData && !ppShader.OpacityTexture.IsVideo && !ppShader.OpacityTexture.IsCubeRender)
            {
                babylonMaterial.opacityTexture = new BabylonTexture();
                DumpTexture(ppShader.OpacityTexture, babylonMaterial.opacityTexture, babylonScene);
            }

            if (mirrorsMaterials.ContainsKey(ppShader))
            {
                babylonMaterial.reflectionTexture = new BabylonTexture();
                var novaObject = mirrorsMaterials[ppShader];
                DumpRenderTargetTexture(ppShader.ReflectionTexture, babylonMaterial.reflectionTexture, novaObject.MirrorMapSize, novaObject.MirrorLevel, novaObject.MirroredObjects);

                AttachMirrorPlane(babylonMaterial.reflectionTexture, novaObject);
            }
            else if (ppShader.ReflectionTexture != null && ppShader.ReflectionTexture.HasTextureData && !ppShader.ReflectionTexture.IsVideo && !ppShader.ReflectionTexture.IsCubeRender)
            {
                babylonMaterial.reflectionTexture = new BabylonTexture();
                DumpTexture(ppShader.ReflectionTexture, babylonMaterial.reflectionTexture, babylonScene);
            }

            // Refraction
            if (ppShader.RefractionLevel > 0)
            {
                babylonMaterial.alpha = 1.0f - ppShader.RefractionLevel;
            }

            if (ppShader.EmissiveTexture != null && ppShader.EmissiveTexture.HasTextureData && !ppShader.EmissiveTexture.IsVideo && !ppShader.EmissiveTexture.IsCubeRender)
            {
                babylonMaterial.emissiveTexture = new BabylonTexture();
                DumpTexture(ppShader.EmissiveTexture, babylonMaterial.emissiveTexture, babylonScene);
            }

            if (ppShader.SpecularTexture != null && ppShader.SpecularTexture.HasTextureData && !ppShader.SpecularTexture.IsVideo && !ppShader.SpecularTexture.IsCubeRender)
            {
                babylonMaterial.specularTexture = new BabylonTexture();
                DumpTexture(ppShader.SpecularTexture, babylonMaterial.specularTexture, babylonScene);
            }

            if (ppShader.BumpTexture != null && ppShader.BumpTexture.HasTextureData && !ppShader.BumpTexture.IsVideo && !ppShader.BumpTexture.IsCubeRender)
            {
                babylonMaterial.bumpTexture = new BabylonTexture();
                DumpTexture(ppShader.BumpTexture, babylonMaterial.bumpTexture, babylonScene);
                babylonMaterial.bumpTexture.level /= 2.0f;
            }
        }