static public Batch Create(BatchPool parentPool, Transform rParent, Bounds rBounds) { var brush = BrushCatalog.m_Instance.GetBrush(parentPool.m_BrushGuid); string name = string.Format("Batch_{0}_{1}", parentPool.m_Batches.Count, brush.m_Guid); GameObject newObj = new GameObject(name); Transform t = newObj.transform; t.parent = rParent; t.localPosition = Vector3.zero; t.localRotation = Quaternion.identity; t.localScale = Vector3.one; newObj.AddComponent <MeshFilter>(); Renderer renderer = newObj.AddComponent <MeshRenderer>(); renderer.material = brush.Material; var propertyBlock = new MaterialPropertyBlock(); renderer.GetPropertyBlock(propertyBlock); ushort batchId = GpuIntersector.GetNextBatchId(); propertyBlock.SetFloat("_BatchID", batchId); renderer.SetPropertyBlock(propertyBlock); Batch batch = newObj.AddComponent <Batch>(); batch.Init(parentPool, rBounds, batchId); // This forces instantiation, but we can detect and clean it up in Destroy() batch.m_InstantiatedMaterial = renderer.material; return(batch); }
override protected void Awake() { base.Awake(); // Normalize for size. // Use transform.localScale.x because prefabs have scales != Vector3.one. m_Size = transform.localScale.x / Coords.CanvasPose.scale; // Manually apply Canvas scale because Awake() is called before the transform is parented. var sizeRange = GetWidgetSizeRange(); if (m_Size < sizeRange.x) { m_Size = sizeRange.x; transform.localScale = m_Size * Vector3.one * Coords.CanvasPose.scale; } if (m_Size > sizeRange.y) { m_Size = sizeRange.y; transform.localScale = m_Size * Vector3.one * Coords.CanvasPose.scale; } m_Collider = GetComponentInChildren <Collider>(); InitSnapGhost(m_Collider.transform, transform); // Pull tintable meshes from collider and reuse them for the highlight meshes. m_HighlightMeshFilters = m_TintableMeshes.Select(x => x.GetComponent <MeshFilter>()).ToArray(); // Custom pin scalar for stencils. m_PinScalar = 0.5f; // Set a new batchId on this image so it can be picked up in GPU intersections. m_BatchId = GpuIntersector.GetNextBatchId(); WidgetManager.m_Instance.AddWidgetToBatchMap(this, m_BatchId); HierarchyUtils.RecursivelySetMaterialBatchID(transform, m_BatchId); RestoreGameObjectLayer(App.Scene.MainCanvas.gameObject.layer); }
void LoadModel() { // Clean up existing model if (m_ModelInstance != null) { GameObject.Destroy(m_ModelInstance.gameObject); } // Early out if we don't have a model to clone. // This can happen if model loading is deferred. if (m_Model == null || m_Model.m_ModelParent == null) { return; } m_ModelInstance = Instantiate(m_Model.m_ModelParent); m_ModelInstance.gameObject.SetActive(true); m_ModelInstance.parent = this.transform; Coords.AsLocal[m_ModelInstance] = TrTransform.identity; float maxExtent = 2 * Mathf.Max(m_Model.m_MeshBounds.extents.x, Mathf.Max(m_Model.m_MeshBounds.extents.y, m_Model.m_MeshBounds.extents.z)); float size; if (maxExtent == 0.0f) { // If we created a widget with a model that doesn't have geo, we won't have calculated a // bounds worth much. In that case, give us a default size. size = 1.0f; } else { size = kInitialSizeMeters_RS * App.METERS_TO_UNITS / maxExtent; } m_InitSize_CS = size / Coords.CanvasPose.scale; // Models are created in the main canvas. Cache model layer in case it's overridden later. HierarchyUtils.RecursivelySetLayer(transform, App.Scene.MainCanvas.gameObject.layer); m_BackupLayer = m_ModelInstance.gameObject.layer; // Set a new batchId on this model so it can be picked up in GPU intersections. m_BatchId = GpuIntersector.GetNextBatchId(); HierarchyUtils.RecursivelySetMaterialBatchID(m_ModelInstance, m_BatchId); WidgetManager.m_Instance.AddWidgetToBatchMap(this, m_BatchId); Vector3 ratios = GetBoundsRatios(m_Model.m_MeshBounds); m_ContainerBloat.x = Mathf.Max(0, m_MinContainerRatio - ratios.x); m_ContainerBloat.y = Mathf.Max(0, m_MinContainerRatio - ratios.y); m_ContainerBloat.z = Mathf.Max(0, m_MinContainerRatio - ratios.z); m_ContainerBloat /= m_MinContainerRatio; // Normalize for the min ratio. m_ContainerBloat *= m_MaxBloat / App.Scene.Pose.scale; // Apply bloat to appropriate axes. m_BoxCollider.size = m_Model.m_MeshBounds.size + m_ContainerBloat; m_BoxCollider.transform.localPosition = m_Model.m_MeshBounds.center; InitSnapGhost(m_Model.m_ModelParent, m_ModelInstance); // Remove previous model vertex recording. WidgetManager.m_Instance.AdjustModelVertCount(-m_NumVertsTrackedByWidgetManager); m_NumVertsTrackedByWidgetManager = 0; m_ObjModelScript = GetComponentInChildren <ObjModelScript>(); m_ObjModelScript.Init(); if (m_ObjModelScript.NumMeshes == 0) { OutputWindowScript.Error("No usable geometry in model"); } else { m_NumVertsTrackedByWidgetManager = m_ObjModelScript.GetNumVertsInMeshes(); WidgetManager.m_Instance.AdjustModelVertCount(m_NumVertsTrackedByWidgetManager); } if (m_Model.IsCached()) { m_Model.RefreshCache(); } }