/// <summary> /// Convert the given <see cref="Renderer"/> into a single Hybrid Rendered entity that uses /// the first <see cref="Material"/> configured in the <see cref="Renderer"/>. /// </summary> public void ConvertToSingleEntity( EntityManager dstEntityManager, GameObjectConversionSystem conversionSystem, RenderMeshDescription renderMeshDescription, Renderer renderer) { var entity = conversionSystem.GetPrimaryEntity(renderer); var lightmappedMaterial = ConfigureHybridStaticLighting( entity, dstEntityManager, renderer, renderMeshDescription.RenderMesh.material); if (lightmappedMaterial != null) { renderMeshDescription.RenderMesh.material = lightmappedMaterial; } RenderMeshUtility.AddComponents( entity, dstEntityManager, renderMeshDescription); conversionSystem.ConfigureEditorRenderData(entity, renderer.gameObject, true); }
private static void AddComponentsToEntity( RenderMesh renderMesh, Entity entity, EntityManager dstEntityManager, GameObjectConversionSystem conversionSystem, Renderer meshRenderer, Mesh mesh, List <Material> materials, bool flipWinding, int id) { renderMesh.material = materials[id]; renderMesh.subMesh = id; dstEntityManager.AddSharedComponentData(entity, renderMesh); dstEntityManager.AddComponentData(entity, new PerInstanceCullingTag()); dstEntityManager.AddComponentData(entity, new RenderBounds { Value = mesh.bounds.ToAABB() }); if (flipWinding) { dstEntityManager.AddComponent(entity, ComponentType.ReadWrite <RenderMeshFlippedWindingTag>()); } conversionSystem.ConfigureEditorRenderData(entity, meshRenderer.gameObject, true); #if ENABLE_HYBRID_RENDERER_V2 dstEntityManager.AddComponent(entity, ComponentType.ReadOnly <WorldToLocal_Tag>()); #if HDRP_9_0_0_OR_NEWER // HDRP previous frame matrices (for motion vectors) if (renderMesh.needMotionVectorPass) { dstEntityManager.AddComponent(entity, ComponentType.ReadWrite <BuiltinMaterialPropertyUnity_MatrixPreviousM>()); dstEntityManager.AddComponent(entity, ComponentType.ReadWrite <BuiltinMaterialPropertyUnity_MatrixPreviousMI_Tag>()); } dstEntityManager.AddComponentData(entity, CreateMotionVectorsParams(ref renderMesh, ref meshRenderer)); #endif dstEntityManager.AddComponentData(entity, new BuiltinMaterialPropertyUnity_RenderingLayer { Value = new uint4(meshRenderer.renderingLayerMask, 0, 0, 0) }); dstEntityManager.AddComponentData(entity, new BuiltinMaterialPropertyUnity_WorldTransformParams { Value = flipWinding ? new float4(0, 0, 0, -1) : new float4(0, 0, 0, 1) }); #if URP_9_0_0_OR_NEWER // Default initialized light data for URP dstEntityManager.AddComponentData(entity, new BuiltinMaterialPropertyUnity_LightData { Value = new float4(0, 0, 1, 0) }); #endif #endif }
/// <summary> /// Convert the given <see cref="Renderer"/> into a multiple Hybrid Rendered entities such that every /// <see cref="Material"/> in the <see cref="Renderer"/> is converted into one entity that uses that <see cref="Material"/> /// with the corresponding sub-mesh from <see cref="RenderMesh.mesh"/>. All created entities will be parented /// to the entity created from <see cref="root"/>. /// </summary> public void ConvertToMultipleEntities( EntityManager dstEntityManager, GameObjectConversionSystem conversionSystem, RenderMeshDescription renderMeshDescription, Renderer renderer, List <Material> sharedMaterials, Transform root) { int materialCount = sharedMaterials.Count; float4x4 localToWorld = root.localToWorldMatrix; var rootEntity = conversionSystem.GetPrimaryEntity(root); for (var m = 0; m != materialCount; m++) { var meshEntity = conversionSystem.CreateAdditionalEntity(renderer); // required for incremental conversion to work without a dependency on the transform itself. dstEntityManager.AddComponent <CopyTransformFromPrimaryEntityTag>(meshEntity); dstEntityManager.AddComponentData(meshEntity, new LocalToWorld { Value = localToWorld }); if (!dstEntityManager.HasComponent <Static>(meshEntity)) { dstEntityManager.AddComponentData(meshEntity, new Parent { Value = rootEntity }); dstEntityManager.AddComponentData(meshEntity, new LocalToParent { Value = float4x4.identity }); } var material = sharedMaterials[m]; var lightmappedMaterial = ConfigureHybridLightMapping( meshEntity, dstEntityManager, renderer, material); if (lightmappedMaterial != null) { material = lightmappedMaterial; } renderMeshDescription.RenderMesh.subMesh = m; renderMeshDescription.RenderMesh.material = material; RenderMeshUtility.AddComponents( meshEntity, dstEntityManager, renderMeshDescription); RemoveLightProbeComponentsFromLightMappedEntities(meshEntity, dstEntityManager); conversionSystem.ConfigureEditorRenderData(meshEntity, renderer.gameObject, true); } }
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { if (dstManager.TryGetComponent <Parent>(entity, out Parent parent) && dstManager.HasComponent <MapData>(parent.Value)) { //EntityQuery unitManagerQuery = dstManager.CreateEntityQuery(typeof(UnitManagerData)); //var unitManagerArray = unitManagerQuery.ToEntityArray(Allocator.TempJob); //there should only be one... maybe two actually. for right now this is fine. dstManager.AddComponentData(entity, new Core.Map.MapBody { point = position, anchor = new Anchor(0, 1, 0) }); dstManager.AddComponent <MapCollidableData>(entity); dstManager.AddComponentData(entity, new MapElement { value = parent.Value }); //dstManager.RemoveComponent<Translation>(entity); conversionSystem.ConfigureEditorRenderData(entity, gameObject, false); } }
public static void Convert( Entity entity, EntityManager dstEntityManager, GameObjectConversionSystem conversionSystem, Renderer meshRenderer, Mesh mesh, List <Material> materials) { var materialCount = materials.Count; // Don't add RenderMesh (and other required components) unless both mesh and material assigned. if ((mesh != null) && (materialCount > 0)) { var renderMesh = new RenderMesh { mesh = mesh, castShadows = meshRenderer.shadowCastingMode, receiveShadows = meshRenderer.receiveShadows, layer = meshRenderer.gameObject.layer }; //@TODO: Transform system should handle RenderMeshFlippedWindingTag automatically. This should not be the responsibility of the conversion system. float4x4 localToWorld = meshRenderer.transform.localToWorldMatrix; var flipWinding = math.determinant(localToWorld) < 0.0; if (materialCount == 1 && AttachToPrimaryEntityForSingleMaterial) { renderMesh.material = materials[0]; renderMesh.subMesh = 0; dstEntityManager.AddSharedComponentData(entity, renderMesh); dstEntityManager.AddComponentData(entity, new PerInstanceCullingTag()); dstEntityManager.AddComponentData(entity, new RenderBounds { Value = mesh.bounds.ToAABB() }); if (flipWinding) { dstEntityManager.AddComponent(entity, ComponentType.ReadWrite <RenderMeshFlippedWindingTag>()); } conversionSystem.ConfigureEditorRenderData(entity, meshRenderer.gameObject, true); } else { for (var m = 0; m != materialCount; m++) { var meshEntity = conversionSystem.CreateAdditionalEntity(meshRenderer); renderMesh.material = materials[m]; renderMesh.subMesh = m; dstEntityManager.AddSharedComponentData(meshEntity, renderMesh); dstEntityManager.AddComponentData(meshEntity, new PerInstanceCullingTag()); dstEntityManager.AddComponentData(meshEntity, new RenderBounds { Value = mesh.bounds.ToAABB() }); dstEntityManager.AddComponentData(meshEntity, new LocalToWorld { Value = localToWorld }); if (!dstEntityManager.HasComponent <Static>(meshEntity)) { dstEntityManager.AddComponentData(meshEntity, new Parent { Value = entity }); dstEntityManager.AddComponentData(meshEntity, new LocalToParent { Value = float4x4.identity }); } if (flipWinding) { dstEntityManager.AddComponent(meshEntity, ComponentType.ReadWrite <RenderMeshFlippedWindingTag>()); } conversionSystem.ConfigureEditorRenderData(meshEntity, meshRenderer.gameObject, true); } } } }
//TODO: Editor Rendering not working for some reason. public async void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { if (map != null) { var mapSystem = dstManager.World.GetOrCreateSystem <MapSystemGroup>(); dstManager.AppendArchetype(entity, mapSystem.Archetypes.Map); var mesh = map.CreateMesh(); mesh.RecalculateBounds(); dstManager.AddComponentData(entity, new MapData(map.CreateBlob())); dstManager.AddComponentData(entity, new MapHighlightState { dirty = 0, states = new Unity.Collections.LowLevel.Unsafe.UnsafeMultiHashMap <ushort, Point>(16, Allocator.Persistent) }); dstManager.AddComponentData(entity, new MapCollisionState { value = new Unity.Collections.LowLevel.Unsafe.UnsafeHashMap <Point, Entity>(16, Allocator.Persistent) }); dstManager.AddComponentData(entity, new MapRenderInfo { baseIndexCount = mesh.GetIndexCount(0), tileSize = 1f, elevationStep = 0.25f }); var layerEntities = new NativeArray <Entity>(MapLayers.Count, Allocator.TempJob); for (int i = 0; i < MapLayers.Count; i++) { layerEntities[i] = conversionSystem.CreateAdditionalEntity(this); dstManager.AppendArchetype(layerEntities[i], mapSystem.Archetypes.MapRenderLayer); } var layers = (MapLayer[])Enum.GetValues(typeof(MapLayer)); Material material = mapMaterial; for (int i = 0; i < layers.Length; i++) { #if UNITY_EDITOR dstManager.SetName(layerEntities[i], $"{this.name} ({layers[i]} Layer)"); #endif dstManager.SetSharedComponentData(layerEntities[i], new RenderMesh { mesh = mesh, material = material, subMesh = i, castShadows = ShadowCastingMode.Off, receiveShadows = false, layer = transform.gameObject.layer }); dstManager.SetComponentData(layerEntities[i], new MapElement { value = entity }); dstManager.SetComponentData(layerEntities[i], new MaterialColor { Value = layerColors[layers[i]].ToFloat4() }); dstManager.SetComponentData(layerEntities[i], new RenderBounds { Value = mesh.bounds.ToAABB() }); /* dstManager.AddComponentData(layerEntities[i], new Translation { Value = new float3(0, 0, 0) }); * dstManager.AddComponentData(layerEntities[i], new Rotation { Value = quaternion.identity }); * dstManager.AddComponentData(layerEntities[i], new CompositeScale { Value = float4x4.Scale(1) }); * dstManager.AddComponentData(layerEntities[i], new PerInstanceCullingTag()); * dstManager.AddComponentData(layerEntities[i], new BuiltinMaterialPropertyUnity_RenderingLayer { Value = 1 }); */ //Overlapping submeshes can't overlap anymore so offset position slightly upward for each layer. dstManager.SetComponentData(layerEntities[i], new LocalToWorld { Value = float4x4.Translate(new float3(0, transform.position.y + (LAYER_OFFSET * i), 0)) }); dstManager.GetBuffer <MapLayerRenderer>(entity).Add(new MapLayerRenderer { entity = layerEntities[i], layer = layers[i] }); //dstManager.AddBuffer<MapTileEffect>(entity); dstManager.AddComponentData(layerEntities[i], new Parent { Value = entity }); conversionSystem.ConfigureEditorRenderData(layerEntities[i], this.gameObject, true); } var children = dstManager.AddBuffer <Child>(entity); children.AddRange(layerEntities.Reinterpret <Child>()); conversionSystem.DeclareLinkedEntityGroup(this.gameObject); conversionSystem.ConfigureEditorRenderData(entity, this.gameObject, true); layerEntities.Dispose(); } }
private static void AddComponentsToEntity( Entity entity, EntityManager dstEntityManager, GameObjectConversionSystem conversionSystem, Renderer meshRenderer, Mesh mesh, List <Material> materials, Dictionary <MaterialLookupKey, Material> createdMaterials, bool flipWinding, int id, LightMaps lightMaps, int lightmapIndex, AABB localBounds) { var needMotionVectorPass = (meshRenderer.motionVectorGenerationMode == MotionVectorGenerationMode.Object) || (meshRenderer.motionVectorGenerationMode == MotionVectorGenerationMode.ForceNoMotion); var renderMesh = new RenderMesh { mesh = mesh, castShadows = meshRenderer.shadowCastingMode, receiveShadows = meshRenderer.receiveShadows, layer = meshRenderer.gameObject.layer, subMesh = id, needMotionVectorPass = needMotionVectorPass }; var staticLightingMode = StaticLightingMode.None; if (meshRenderer.lightmapIndex >= 65534 || meshRenderer.lightmapIndex < 0) { staticLightingMode = StaticLightingMode.LightProbes; } else if (meshRenderer.lightmapIndex >= 0) { staticLightingMode = StaticLightingMode.Lightmapped; } dstEntityManager.AddComponentData(entity, new PerInstanceCullingTag()); dstEntityManager.AddComponentData(entity, new RenderBounds { Value = localBounds }); var material = materials[id]; if (staticLightingMode == StaticLightingMode.Lightmapped && lightMaps.isValid) { conversionSystem.DeclareAssetDependency(meshRenderer.gameObject, material); var localFlags = LightMappingFlags.Lightmapped; if (lightMaps.hasDirections) { localFlags |= LightMappingFlags.Directional; } if (lightMaps.hasShadowMask) { localFlags |= LightMappingFlags.ShadowMask; } var key = new MaterialLookupKey { BaseMaterial = materials[id], lightmaps = lightMaps, Flags = localFlags }; var lookUp = createdMaterials ?? new Dictionary <MaterialLookupKey, Material>(); if (lookUp.TryGetValue(key, out Material result)) { material = result; } else { material = new Material(materials[id]); material.name = $"{material.name}_Lightmapped_"; material.EnableKeyword("LIGHTMAP_ON"); material.SetTexture("unity_Lightmaps", lightMaps.colors); material.SetTexture("unity_LightmapsInd", lightMaps.directions); material.SetTexture("unity_ShadowMasks", lightMaps.shadowMasks); if (lightMaps.hasDirections) { material.name = material.name + "_DIRLIGHTMAP"; material.EnableKeyword("DIRLIGHTMAP_COMBINED"); } if (lightMaps.hasShadowMask) { material.name = material.name + "_SHADOW_MASK"; } lookUp[key] = material; } dstEntityManager.AddComponentData(entity, new BuiltinMaterialPropertyUnity_LightmapST() { Value = meshRenderer.lightmapScaleOffset }); dstEntityManager.AddComponentData(entity, new BuiltinMaterialPropertyUnity_LightmapIndex() { Value = lightmapIndex }); dstEntityManager.AddSharedComponentData(entity, lightMaps); } else if (staticLightingMode == StaticLightingMode.LightProbes) { if (meshRenderer.lightProbeUsage == LightProbeUsage.CustomProvided) { dstEntityManager.AddComponent <CustomProbeTag>(entity); } else if (meshRenderer.lightProbeUsage == LightProbeUsage.BlendProbes && LightmapSettings.lightProbes != null && LightmapSettings.lightProbes.count > 0) { dstEntityManager.AddComponent <BlendProbeTag>(entity); } else { dstEntityManager.AddComponent <AmbientProbeTag>(entity); } } renderMesh.material = material; dstEntityManager.AddSharedComponentData(entity, renderMesh); if (flipWinding) { dstEntityManager.AddComponent(entity, ComponentType.ReadWrite <RenderMeshFlippedWindingTag>()); } conversionSystem.ConfigureEditorRenderData(entity, meshRenderer.gameObject, true); #if ENABLE_HYBRID_RENDERER_V2 dstEntityManager.AddComponent(entity, ComponentType.ReadOnly <WorldToLocal_Tag>()); #if HDRP_9_0_0_OR_NEWER // HDRP previous frame matrices (for motion vectors) if (needMotionVectorPass) { dstEntityManager.AddComponent(entity, ComponentType.ReadWrite <BuiltinMaterialPropertyUnity_MatrixPreviousM>()); dstEntityManager.AddComponent(entity, ComponentType.ReadWrite <BuiltinMaterialPropertyUnity_MatrixPreviousMI_Tag>()); } dstEntityManager.AddComponentData(entity, CreateMotionVectorsParams(ref renderMesh, ref meshRenderer)); #endif dstEntityManager.AddComponentData(entity, new BuiltinMaterialPropertyUnity_RenderingLayer { Value = new uint4(meshRenderer.renderingLayerMask, 0, 0, 0) }); dstEntityManager.AddComponentData(entity, new BuiltinMaterialPropertyUnity_WorldTransformParams { Value = flipWinding ? new float4(0, 0, 0, -1) : new float4(0, 0, 0, 1) }); #if URP_9_0_0_OR_NEWER // Default initialized light data for URP dstEntityManager.AddComponentData(entity, new BuiltinMaterialPropertyUnity_LightData { Value = new float4(0, 0, 1, 0) }); #endif #endif }
private static void AddComponentsToEntity( Entity entity, EntityManager dstEntityManager, GameObjectConversionSystem conversionSystem, Renderer meshRenderer, Mesh mesh, Material material, bool flipWinding, int id, AABB localBounds) { var needMotionVectorPass = (meshRenderer.motionVectorGenerationMode == MotionVectorGenerationMode.Object) || (meshRenderer.motionVectorGenerationMode == MotionVectorGenerationMode.ForceNoMotion); var renderMesh = new RenderMesh { mesh = mesh, material = material, castShadows = meshRenderer.shadowCastingMode, receiveShadows = meshRenderer.receiveShadows, layer = meshRenderer.gameObject.layer, subMesh = id, needMotionVectorPass = needMotionVectorPass }; dstEntityManager.AddSharedComponentData(entity, renderMesh); dstEntityManager.AddComponentData(entity, new PerInstanceCullingTag()); dstEntityManager.AddComponentData(entity, new RenderBounds { Value = localBounds }); if (flipWinding) { dstEntityManager.AddComponent(entity, ComponentType.ReadWrite <RenderMeshFlippedWindingTag>()); } conversionSystem.ConfigureEditorRenderData(entity, meshRenderer.gameObject, true); #if ENABLE_HYBRID_RENDERER_V2 dstEntityManager.AddComponent(entity, ComponentType.ReadOnly <WorldToLocal_Tag>()); #if HDRP_9_0_0_OR_NEWER // HDRP previous frame matrices (for motion vectors) if (needMotionVectorPass) { dstEntityManager.AddComponent(entity, ComponentType.ReadWrite <BuiltinMaterialPropertyUnity_MatrixPreviousM>()); dstEntityManager.AddComponent(entity, ComponentType.ReadWrite <BuiltinMaterialPropertyUnity_MatrixPreviousMI_Tag>()); } dstEntityManager.AddComponentData(entity, CreateMotionVectorsParams(ref renderMesh, ref meshRenderer)); #endif dstEntityManager.AddComponentData(entity, new BuiltinMaterialPropertyUnity_RenderingLayer { Value = new uint4(meshRenderer.renderingLayerMask, 0, 0, 0) }); dstEntityManager.AddComponentData(entity, new BuiltinMaterialPropertyUnity_WorldTransformParams { Value = flipWinding ? new float4(0, 0, 0, -1) : new float4(0, 0, 0, 1) }); #if URP_9_0_0_OR_NEWER // Default initialized light data for URP dstEntityManager.AddComponentData(entity, new BuiltinMaterialPropertyUnity_LightData { Value = new float4(0, 0, 1, 0) }); if (meshRenderer.lightProbeUsage == LightProbeUsage.CustomProvided) { dstEntityManager.AddComponent <CustomProbeTag>(entity); } else { dstEntityManager.AddComponent <AmbientProbeTag>(entity); } #endif #endif }