public void FitBoxToTargets() { if (NumTargetParents == 0) { _boxSize = Vector3.zero; return; } if (ExtrudeSpace == GizmoSpace.Global) { AABB worldAABB = AABB.GetInvalid(); foreach (var parent in _targetParents) { AABB aabb = ObjectBounds.CalcHierarchyWorldAABB(parent, _boundsQConfig); if (aabb.IsValid) { if (worldAABB.IsValid) { worldAABB.Encapsulate(aabb); } else { worldAABB = aabb; } } } SetAABB(worldAABB); UpdateSnapSteps(); } else if (ExtrudeSpace == GizmoSpace.Local) { OBB worldOBB = ObjectBounds.CalcHierarchyWorldOBB(_targetParents[0], _boundsQConfig); for (int parentIndex = 1; parentIndex < NumTargetParents; ++parentIndex) { GameObject parent = _targetParents[parentIndex]; OBB obb = ObjectBounds.CalcHierarchyWorldOBB(parent, _boundsQConfig); if (obb.IsValid) { if (worldOBB.IsValid) { worldOBB.Encapsulate(obb); } else { worldOBB = obb; } } } SetOBB(worldOBB); UpdateSnapSteps(); } }
public static GameObject SpawnInFrontOfCamera(GameObject sourceObject, Camera camera, Config config) { float halfSize = config.ObjectSize * 0.5f; var boundsQConfig = new ObjectBounds.QueryConfig(); boundsQConfig.ObjectTypes = GameObjectTypeHelper.AllCombined; boundsQConfig.NoVolumeSize = Vector3Ex.FromValue(1.0f); Transform cameraTransform = camera.transform; AABB aabb = ObjectBounds.CalcHierarchyWorldAABB(sourceObject, boundsQConfig); if (!aabb.IsValid) { return(null); } Sphere sphere = new Sphere(aabb); Vector3 fromCenterToPos = sourceObject.transform.position - sphere.Center; float zOffset = Mathf.Max(camera.nearClipPlane + sphere.Radius, sphere.Radius / halfSize); Vector3 spherePos = cameraTransform.position + cameraTransform.forward * zOffset; GameObject spawned = GameObject.Instantiate(sourceObject, spherePos + fromCenterToPos, sourceObject.transform.rotation) as GameObject; spawned.SetActive(true); //VRCTS spawned.name = sourceObject.name; OBB spawnedOBB = ObjectBounds.CalcHierarchyWorldOBB(spawned, boundsQConfig); Ray ray = new Ray(camera.transform.position, (spawnedOBB.Center - camera.transform.position).normalized); var raycastFilter = new SceneRaycastFilter(); raycastFilter.AllowedObjectTypes.Add(GameObjectType.Mesh); raycastFilter.AllowedObjectTypes.Add(GameObjectType.Terrain); raycastFilter.AllowedObjectTypes.Add(GameObjectType.Sprite); var rayHit = RTScene.Get.Raycast(ray, SceneRaycastPrecision.BestFit, raycastFilter); if (rayHit.WasAnObjectHit) { Vector3 oldCenter = spawnedOBB.Center; spawnedOBB.Center = rayHit.ObjectHit.HitPoint; Vector3 offsetVector = spawnedOBB.Center - oldCenter; offsetVector += ObjectSurfaceSnap.CalculateSitOnSurfaceOffset(spawnedOBB, rayHit.ObjectHit.HitPlane, 0.0f); spawned.transform.position += offsetVector; } return(spawned); }
public void FitBoxToTargets() { if (NumTargetParents == 0) { _boxSize = Vector3.zero; return; } if (ExtrudeSpace == GizmoSpace.Global) { AABB worldAABB = AABB.GetInvalid(); foreach (var parent in _targetParents) { if (_ignoredParentObjects.Contains(parent)) { continue; } AABB aabb = ObjectBounds.CalcHierarchyWorldAABB(parent, _boundsQConfig); if (aabb.IsValid) { if (worldAABB.IsValid) { worldAABB.Encapsulate(aabb); } else { worldAABB = aabb; } } } SetAABB(worldAABB); UpdateSnapSteps(); } else if (ExtrudeSpace == GizmoSpace.Local) { int firstParentIndex = 0; while (firstParentIndex < NumTargetParents) { if (_ignoredParentObjects.Contains(_targetParents[firstParentIndex])) { ++firstParentIndex; } else { break; } } if (firstParentIndex == NumTargetParents) { SetOBB(OBB.GetInvalid()); UpdateSnapSteps(); return; } OBB worldOBB = ObjectBounds.CalcHierarchyWorldOBB(_targetParents[firstParentIndex], _boundsQConfig); for (int parentIndex = firstParentIndex; parentIndex < NumTargetParents; ++parentIndex) { GameObject parent = _targetParents[parentIndex]; if (_ignoredParentObjects.Contains(parent)) { continue; } OBB obb = ObjectBounds.CalcHierarchyWorldOBB(parent, _boundsQConfig); if (obb.IsValid) { if (worldOBB.IsValid) { worldOBB.Encapsulate(obb); } else { worldOBB = obb; } } } SetOBB(worldOBB); UpdateSnapSteps(); } }
public Texture2D Generate(GameObject unityPrefab) { if (!_isGenSessionActive || _renderCamera.targetTexture == null) { return(null); } RenderTexture oldRenderTexture = UnityEngine.RenderTexture.active; RenderTexture.active = _renderCamera.targetTexture; GL.Clear(true, true, _previewLookAndFeel.BkColor); bool hasMesh = unityPrefab.HierarchyHasMesh(); bool hasSprite = unityPrefab.HierarchyHasSprite(); PreviewObjectType previewObjectType = PreviewObjectType.Mesh; if (!hasMesh && hasSprite) { previewObjectType = PreviewObjectType.Sprite; } else if (!hasMesh && !hasSprite) { if (unityPrefab.HierarchyHasObjectsOfType(GameObjectType.Light)) { previewObjectType = PreviewObjectType.Light; } else if (unityPrefab.HierarchyHasObjectsOfType(GameObjectType.ParticleSystem)) { previewObjectType = PreviewObjectType.ParticleSystem; } else { previewObjectType = PreviewObjectType.Other; } } GameObject previewObject = null; if (previewObjectType == PreviewObjectType.Mesh || previewObjectType == PreviewObjectType.Sprite) { previewObject = GameObject.Instantiate(unityPrefab); } else { previewObject = _nonMeshPreviewObject; } Transform previewObjectTransform = previewObject.transform; previewObjectTransform.position = Vector3.zero; previewObjectTransform.rotation = Quaternion.identity; previewObjectTransform.localScale = unityPrefab.transform.lossyScale; AABB sceneAABB = RTScene.Get.CalculateBounds(); Sphere sceneSphere = new Sphere(sceneAABB); AABB previewAABB = new AABB(); previewAABB = ObjectBounds.CalcHierarchyWorldAABB(previewObject, _boundsQConfig); Sphere previewSphere = new Sphere(previewAABB); Vector3 previewSphereCenter = sceneSphere.Center - Vector3.right * (sceneSphere.Radius + previewSphere.Radius + 90.0f); previewObjectTransform.position += (previewSphereCenter - previewSphere.Center); previewAABB = ObjectBounds.CalcHierarchyWorldAABB(previewObject, _boundsQConfig); previewSphere.Center = previewSphereCenter; Transform camTransform = _renderCamera.transform; if (previewObjectType == PreviewObjectType.Mesh || previewObjectType == PreviewObjectType.Sprite) { camTransform.rotation = Quaternion.identity; if (previewObjectType != PreviewObjectType.Sprite) { camTransform.rotation = Quaternion.AngleAxis(-45.0f, Vector3.up) * Quaternion.AngleAxis(35.0f, camTransform.right); } camTransform.position = previewSphere.Center - camTransform.forward * (previewSphere.Radius * 2.0f + _renderCamera.nearClipPlane); } else { camTransform.rotation = previewObjectTransform.rotation; camTransform.position = previewSphere.Center - camTransform.forward * (previewSphere.Radius * 2.0f + _renderCamera.nearClipPlane); Texture2D previewIcon = previewObjectType == PreviewObjectType.Light ? RTScene.Get.LookAndFeel.LightIcon : RTScene.Get.LookAndFeel.ParticleSystemIcon; _nonMeshPreviewObject.GetComponent <MeshRenderer>().sharedMaterial.SetTexture("_MainTex", previewIcon); } _previewLight.transform.forward = camTransform.forward; _renderCamera.Render(); if (previewObject != _nonMeshPreviewObject) { GameObject.DestroyImmediate(previewObject); } Texture2D previewTexture = new Texture2D(_previewLookAndFeel.PreviewWidth, _previewLookAndFeel.PreviewHeight, TextureFormat.ARGB32, true, true); previewTexture.ReadPixels(new Rect(0, 0, _previewLookAndFeel.PreviewWidth, _previewLookAndFeel.PreviewHeight), 0, 0); previewTexture.Apply(); UnityEngine.RenderTexture.active = oldRenderTexture; return(previewTexture); }