コード例 #1
0
        public UnityEngine.Material CreateMaterial(PbrMaterial vpxMaterial, TableAuthoring table, StringBuilder debug = null)
        {
            var unityMaterial = new UnityEngine.Material(GetShader())
            {
                name = vpxMaterial.Id
            };

            // apply some basic manipulations to the color. this just makes very
            // very white colors be clipped to 0.8204 aka 204/255 is 0.8
            // this is to give room to lighting values. so there is more modulation
            // of brighter colors when being lit without blow outs too soon.
            var col = vpxMaterial.Color.ToUnityColor();

            if (vpxMaterial.Color.IsGray() && col.grayscale > 0.8)
            {
                debug?.AppendLine("Color manipulation performed, brightness reduced.");
                col.r = col.g = col.b = 0.8f;
            }

            // alpha for color depending on blend mode
            ApplyBlendMode(unityMaterial, vpxMaterial.MapBlendMode);
            if (vpxMaterial.MapBlendMode == BlendMode.Translucent)
            {
                col.a = Mathf.Min(1, Mathf.Max(0, vpxMaterial.Opacity));
            }

            unityMaterial.SetColor(BaseColor, col);

            // validate IsMetal. if true, set the metallic value.
            // found VPX authors setting metallic as well as translucent at the
            // same time, which does not render correctly in unity so we have
            // to check if this value is true and also if opacity <= 1.
            if (vpxMaterial.IsMetal && (!vpxMaterial.IsOpacityActive || vpxMaterial.Opacity >= 1))
            {
                unityMaterial.SetFloat(Metallic, 1f);
                debug?.AppendLine("Metallic set to 1.");
            }

            // roughness / glossiness
            unityMaterial.SetFloat(Smoothness, vpxMaterial.Roughness);


            // map
            if (table != null && vpxMaterial.HasMap)
            {
                unityMaterial.SetTexture(BaseMap, table.GetTexture(vpxMaterial.Map.Name));
            }

            // normal map
            if (table != null && vpxMaterial.HasNormalMap)
            {
                unityMaterial.EnableKeyword("_NORMALMAP");

                unityMaterial.SetTexture(BumpMap, table.GetTexture(vpxMaterial.NormalMap.Name)
                                         );
            }

            return(unityMaterial);
        }
コード例 #2
0
        public void Init(TableAuthoring tableAuthoring)
        {
            _entityManager    = World.DefaultGameObjectInjectionWorld.EntityManager;
            _flipperDataQuery = _entityManager.CreateEntityQuery(
                ComponentType.ReadOnly <FlipperMovementData>(),
                ComponentType.ReadOnly <FlipperStaticData>(),
                ComponentType.ReadOnly <SolenoidStateData>()
                );

            _ballDataQuery = _entityManager.CreateEntityQuery(ComponentType.ReadOnly <BallData>());

            var visualPinballSimulationSystemGroup = _entityManager.World.GetOrCreateSystem <VisualPinballSimulationSystemGroup>();
            var simulateCycleSystemGroup           = _entityManager.World.GetOrCreateSystem <SimulateCycleSystemGroup>();

            visualPinballSimulationSystemGroup.Enabled = true;
            simulateCycleSystemGroup.PhysicsEngine     = this;         // needed for flipper status update we don't do in all engines

            _worldToLocal = tableAuthoring.gameObject.transform.worldToLocalMatrix;
        }
コード例 #3
0
        public static Material ToUnityMaterial(this PbrMaterial vpxMaterial, TableAuthoring table, StringBuilder debug = null)
        {
            if (table != null)
            {
                var existingMat = table.GetMaterial(vpxMaterial);
                if (existingMat != null)
                {
                    return(existingMat);
                }
            }

            var unityMaterial = MaterialConverter.CreateMaterial(vpxMaterial, table, debug);

            if (table != null)
            {
                table.AddMaterial(vpxMaterial, unityMaterial);
            }

            return(unityMaterial);
        }
コード例 #4
0
        private void MakeSerializable(GameObject go, Table table)
        {
            // add table component (plus other data)
            _tableAuthoring = go.AddComponent <TableAuthoring>();
            _tableAuthoring.SetItem(table);

            var sidecar = _tableAuthoring.GetOrCreateSidecar();

            foreach (var key in table.TableInfo.Keys)
            {
                sidecar.tableInfo[key] = table.TableInfo[key];
            }

            // copy each texture ref into the sidecar's serialized storage
            sidecar.textures.AddRange(table.Textures);
            // and tell the engine's table to now use the sidecar as its container so we can all operate on the same underlying container
            table.SetTextureContainer(sidecar.textures);

            // copy each sound ref into the sidecar's serialized storage
            sidecar.sounds.AddRange(table.Sounds);
            // and tell the engine's table to now use the sidecar as its container so we can all operate on the same underlying container
            table.SetSoundContainer(sidecar.sounds);

            sidecar.customInfoTags = table.CustomInfoTags;
            sidecar.collections    = table.Collections.Values.Select(c => c.Data).ToArray();
            sidecar.decals         = table.GetAllData <Decal, DecalData>();
            sidecar.dispReels      = table.GetAllData <DispReel, DispReelData>();
            sidecar.flashers       = table.GetAllData <Flasher, FlasherData>();
            sidecar.lightSeqs      = table.GetAllData <LightSeq, LightSeqData>();
            sidecar.plungers       = table.GetAllData <Plunger, PlungerData>();
            sidecar.textBoxes      = table.GetAllData <TextBox, TextBoxData>();
            sidecar.timers         = table.GetAllData <Timer, TimerData>();

            Logger.Info("Collections saved: [ {0} ] [ {1} ]",
                        string.Join(", ", table.Collections.Keys),
                        string.Join(", ", sidecar.collections.Select(c => c.Name))
                        );
        }
コード例 #5
0
        public UnityEngine.Material CreateMaterial(PbrMaterial vpxMaterial, TableAuthoring table, StringBuilder debug = null)
        {
            var unityMaterial = new UnityEngine.Material(GetShader())
            {
                name = vpxMaterial.Id
            };

            // apply some basic manipulations to the color. this just makes very
            // very white colors be clipped to 0.8204 aka 204/255 is 0.8
            // this is to give room to lighting values. so there is more modulation
            // of brighter colors when being lit without blow outs too soon.
            var col = vpxMaterial.Color.ToUnityColor();

            if (vpxMaterial.Color.IsGray() && col.grayscale > 0.8)
            {
                debug?.AppendLine("Color manipulation performed, brightness reduced.");
                col.r = col.g = col.b = 0.8f;
            }

            // alpha for color depending on blend mode
            ApplyBlendMode(unityMaterial, vpxMaterial.MapBlendMode);
            if (vpxMaterial.MapBlendMode == Engine.VPT.BlendMode.Translucent)
            {
                col.a = Mathf.Min(1, Mathf.Max(0, vpxMaterial.Opacity));
            }
            unityMaterial.SetColor(BaseColor, col);

            // validate IsMetal. if true, set the metallic value.
            // found VPX authors setting metallic as well as translucent at the
            // same time, which does not render correctly in unity so we have
            // to check if this value is true and also if opacity <= 1.
            if (vpxMaterial.IsMetal && (!vpxMaterial.IsOpacityActive || vpxMaterial.Opacity >= 1))
            {
                unityMaterial.SetFloat(Metallic, 1f);
                debug?.AppendLine("Metallic set to 1.");
            }

            // roughness / glossiness
            unityMaterial.SetFloat(Smoothness, vpxMaterial.Roughness);

            // map
            if (table != null && vpxMaterial.HasMap)
            {
                unityMaterial.SetTexture(BaseColorMap, table.GetTexture(vpxMaterial.Map.Name));
            }

            // normal map
            if (table != null && vpxMaterial.HasNormalMap)
            {
                unityMaterial.EnableKeyword("_NORMALMAP");
                unityMaterial.EnableKeyword("_NORMALMAP_TANGENT_SPACE");

                unityMaterial.SetInt(NormalMapSpace, 0);                 // 0 = TangentSpace, 1 = ObjectSpace
                unityMaterial.SetFloat(NormalScale, 0f);                 // TODO FIXME: setting the scale to 0 for now. anything above 0 makes the entire unity editor window become black which is more likely a unity bug

                unityMaterial.SetTexture(NormalMap, table.GetTexture(vpxMaterial.NormalMap.Name));
            }

            // GI hack. This is a necessary step, see respective code in BaseUnlitGUI.cs of the HDRP source
            SetupMainTexForAlphaTestGI(unityMaterial, "_BaseColorMap", "_BaseColor");

            return(unityMaterial);
        }
コード例 #6
0
        public static IEnumerable <Tuple <GameObject, RenderObject> > ConvertRenderObjects(IRenderable item, RenderObjectGroup rog, GameObject parent, TableAuthoring tb, out GameObject obj)
        {
            obj = new GameObject(rog.Name);
            obj.transform.parent = parent.transform;

            var createdObjs = new Tuple <GameObject, RenderObject> [0];

            if (rog.HasOnlyChild && !rog.ForceChild)
            {
                ConvertRenderObject(rog.RenderObjects[0], obj, tb);
                createdObjs = new[] { new Tuple <GameObject, RenderObject>(obj, rog.RenderObjects[0]) };
            }
            else if (rog.HasChildren)
            {
                createdObjs = new Tuple <GameObject, RenderObject> [rog.RenderObjects.Length];
                var i = 0;
                foreach (var ro in rog.RenderObjects)
                {
                    var subObj = new GameObject(ro.Name);
                    subObj.transform.SetParent(obj.transform, false);
                    subObj.layer = ChildObjectsLayer;
                    ConvertRenderObject(ro, subObj, tb);
                    createdObjs[i++] = new Tuple <GameObject, RenderObject>(subObj, ro);
                }
            }

            // apply transformation
            obj.transform.SetFromMatrix(rog.TransformationMatrix.ToUnityMatrix());

            // add unity component
            MonoBehaviour ic = null;

            switch (item)
            {
            case Bumper bumper:                                     ic = bumper.SetupGameObject(obj, rog); break;

            case Flipper flipper:                           ic = flipper.SetupGameObject(obj, rog); break;

            case Gate gate:                                         ic = gate.SetupGameObject(obj, rog); break;

            case HitTarget hitTarget:                       ic = hitTarget.SetupGameObject(obj, rog); break;

            case Kicker kicker:                                     ic = kicker.SetupGameObject(obj, rog); break;

            case Engine.VPT.Light.Light lt:         ic = lt.SetupGameObject(obj, rog); break;

            case Plunger plunger:                           ic = plunger.SetupGameObject(obj, rog); break;

            case Primitive primitive:                       ic = primitive.SetupGameObject(obj, rog); break;

            case Ramp ramp:                                         ic = ramp.SetupGameObject(obj, rog); break;

            case Rubber rubber:                                     ic = rubber.SetupGameObject(obj, rog); break;

            case Spinner spinner:                           ic = spinner.SetupGameObject(obj, rog); break;

            case Surface surface:                           ic = surface.SetupGameObject(obj, rog); break;

            case Table table:                                       ic = table.SetupGameObject(obj, rog); break;

            case Trigger trigger:                           ic = trigger.SetupGameObject(obj, rog); break;
            }
            return(createdObjs);
        }
コード例 #7
0
        public static GameObject ConvertRenderObject(RenderObject ro, GameObject obj, TableAuthoring ta)
        {
            if (ro.Mesh == null)
            {
                Logger.Warn($"No mesh for object {obj.name}, skipping.");
                return(null);
            }

            var mesh = ro.Mesh.ToUnityMesh($"{obj.name}_mesh");

            // apply mesh to game object
            var mf = obj.AddComponent <MeshFilter>();

            mf.sharedMesh = mesh;

            // apply material
            if (ro.Mesh.AnimationFrames.Count > 0)
            {
                var smr = obj.AddComponent <SkinnedMeshRenderer>();
                smr.sharedMaterial = ro.Material.ToUnityMaterial(ta);
                smr.sharedMesh     = mesh;
                smr.enabled        = ro.IsVisible;
            }
            else
            {
                var mr = obj.AddComponent <MeshRenderer>();
                mr.sharedMaterial = ro.Material.ToUnityMaterial(ta);
                mr.enabled        = ro.IsVisible;
            }

            return(obj);
        }