浏览代码

Implementing randomization.

/dr-test1
Jon Hogins 5 年前
当前提交
3bed86dd
共有 4 个文件被更改,包括 203 次插入9 次删除
  1. 67
      TestProjects/PerceptionURP/Assets/Scenes/SampleScene.unity
  2. 2
      TestProjects/PerceptionURP/ProjectSettings/QualitySettings.asset
  3. 107
      com.unity.perception/Editor/Randomization/RandomizerEditor.cs
  4. 36
      com.unity.perception/Runtime/Randomization/Randomizer.cs

67
TestProjects/PerceptionURP/Assets/Scenes/SampleScene.unity


- {fileID: 1640252283}
- {fileID: 464025709}
- {fileID: 411238281}
- {fileID: 1058419514}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

m_Script: {fileID: 11500000, guid: 8b33f0bc2b78db642a758f07826d0dd0, type: 3}
m_Name:
m_EditorClassIdentifier:
classes:
labels:
- Crate
--- !u!65 &411238278
BoxCollider:

m_Script: {fileID: 11500000, guid: 8b33f0bc2b78db642a758f07826d0dd0, type: 3}
m_Name:
m_EditorClassIdentifier:
classes:
labels:
- Cube
--- !u!65 &464025706
BoxCollider:

m_Type: 1
m_Shape: 0
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
m_Intensity: 3.1415927
m_Intensity: 0.84
m_Range: 10
m_SpotAngle: 30
m_InnerSpotAngle: 21.80208

m_RequiresDepthTextureOption: 2
m_RequiresOpaqueTextureOption: 2
m_CameraType: 0
m_CameraOutput: 0
m_Cameras: []
m_RendererIndex: -1
m_VolumeLayerMask:

m_AntialiasingQuality: 2
m_StopNaN: 0
m_Dithering: 0
m_ClearDepth: 1
m_RequiresDepthTexture: 0
m_RequiresColorTexture: 0
m_Version: 2

captureRgbImages: 1
produceSegmentationImages: 1
produceObjectCountAnnotations: 1
objectCountId: 51DA3C27-369D-4929-AEA6-D01614635CE2
produceBoundingBoxAnnotations: 1
boundingBoxId: F9F22E05-443F-4602-A422-EBE4EA9B55CB
produceRenderedObjectInfoMetric: 1
renderedObjectInfoId: 5BA92024-B3B7-41A7-9D3F-C03A6A8DDD01
boundingBoxOrigin: 0
--- !u!1 &1058419513
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1058419514}
- component: {fileID: 1058419515}
m_Layer: 0
m_Name: Randomizer
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1058419514
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1058419513}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 4662620}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1058419515
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1058419513}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e18db86d4c0f4b57b10daa3bac1f2c66, type: 3}
m_Name:
m_EditorClassIdentifier:
randomizationEntries:
- GameObject: {fileID: 705507993}
ComponentType: UnityEngine.Light
MemberName: intensity
TargetKind: 1
Min: 0
Max: 1
--- !u!1 &1640252278
GameObject:
m_ObjectHideFlags: 0

m_Script: {fileID: 11500000, guid: 8b33f0bc2b78db642a758f07826d0dd0, type: 3}
m_Name:
m_EditorClassIdentifier:
classes:
labels:
- Box
--- !u!65 &1640252280
BoxCollider:

2
TestProjects/PerceptionURP/ProjectSettings/QualitySettings.asset


skinWeights: 2
textureQuality: 0
anisotropicTextures: 1
antiAliasing: 2
antiAliasing: 0
softParticles: 0
softVegetation: 1
realtimeReflectionProbes: 1

107
com.unity.perception/Editor/Randomization/RandomizerEditor.cs


using UnityEditor;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.Perception.Randomization;
[CustomEditor(typeof(Randomizer))]
struct RandomizationTarget
{
public GameObject GameObject;
public Type ComponentType;
public string FloatFieldName;
public TargetKind TargetKind;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (EditorGUILayout.DropdownButton(new GUIContent("Add parameter"), FocusType.Keyboard))
{
var lastRect = GUILayoutUtility.GetLastRect();
var dropdownOptions = GatherOptions().ToArray();
var menu = new GenericMenu();
foreach (var option in dropdownOptions)
{
var localOption = option;
menu.AddItem(new GUIContent($"{option.GameObject.name}.{option.ComponentType.Name}.{option.FloatFieldName}"),
false,
() => AddParameter(localOption));
}
menu.DropDown(lastRect);
}
}
void AddParameter(RandomizationTarget localOption)
{
var entriesProp = this.serializedObject.FindProperty(nameof(Randomizer.randomizationEntries));
var newIndex = entriesProp.arraySize;
entriesProp.InsertArrayElementAtIndex(newIndex);
var element = entriesProp.GetArrayElementAtIndex(newIndex);
element.FindPropertyRelative(nameof(RandomizationEntry.GameObject)).objectReferenceValue = localOption.GameObject;
element.FindPropertyRelative(nameof(RandomizationEntry.ComponentType)).stringValue =
$"{localOption.ComponentType.FullName}, {localOption.ComponentType.Assembly.FullName}";
element.FindPropertyRelative(nameof(RandomizationEntry.MemberName)).stringValue = localOption.FloatFieldName;
var targetKindProp = element.FindPropertyRelative(nameof(RandomizationEntry.TargetKind));
targetKindProp.enumValueIndex = (int)localOption.TargetKind;
element.FindPropertyRelative(nameof(RandomizationEntry.Min)).floatValue = 0f;
element.FindPropertyRelative(nameof(RandomizationEntry.Max)).floatValue = 1f;
serializedObject.ApplyModifiedProperties();
}
IEnumerable<RandomizationTarget> GatherOptions()
{
var randomizer = (Randomizer)this.target;
var targetScene = randomizer.gameObject.scene;
var rootGameObjects = targetScene.GetRootGameObjects();
foreach (var rootGameObject in rootGameObjects)
{
foreach (var randomizationTarget in GatherOptions(rootGameObject.transform))
{
yield return randomizationTarget;
}
}
}
IEnumerable<RandomizationTarget> GatherOptions(Transform parent)
{
foreach (var component in parent.GetComponents<Component>())
{
if (component == null)
continue;
var componentType = component.GetType();
var fieldInfos = componentType.GetFields();
foreach (var fieldInfo in fieldInfos)
{
if (fieldInfo.FieldType == typeof(float))
yield return new RandomizationTarget()
{
ComponentType = componentType,
FloatFieldName = fieldInfo.Name,
TargetKind = TargetKind.Field,
GameObject = parent.gameObject
};
}
var propertyInfos = componentType.GetProperties();
foreach (var propertyInfo in propertyInfos)
{
if (propertyInfo.PropertyType == typeof(float))
yield return new RandomizationTarget()
{
ComponentType = componentType,
FloatFieldName = propertyInfo.Name,
TargetKind = TargetKind.Property,
GameObject = parent.gameObject
};
}
}
for (int i = 0; i < parent.transform.childCount; i++)
{
foreach (var childOption in GatherOptions(parent.transform.GetChild(i)))
{
yield return childOption;
}
}
}
}
}

36
com.unity.perception/Runtime/Randomization/Randomizer.cs


using System;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
namespace UnityEngine.Perception.Randomization

[SerializeField]
public List<RandomizationEntry> randomizationEntries = new List<RandomizationEntry>();
Unity.Mathematics.Random m_Random = new Unity.Mathematics.Random(1);
randomizationEntries
foreach (var randomizationEntry in randomizationEntries)
{
Randomize(randomizationEntry);
}
}
void Randomize(RandomizationEntry randomizationEntry)
{
var value = m_Random.NextFloat(randomizationEntry.Min, randomizationEntry.Max);
var type = Type.GetType(randomizationEntry.ComponentType);
var component = randomizationEntry.GameObject.GetComponent(type);
switch (randomizationEntry.TargetKind)
{
case TargetKind.Field:
var fieldInfo = type.GetField(randomizationEntry.MemberName);
fieldInfo.SetValue(component, value);
break;
case TargetKind.Property:
var propertyInfo = type.GetProperty(randomizationEntry.MemberName);
propertyInfo.SetValue(component, value);
break;
}
public enum TargetKind
{
Field,
Property
}
[Serializable]
public Type ComponentType;
public string ComponentType;
public TargetKind TargetKind;
public float Min;
public float Max;
}
正在加载...
取消
保存