public void UpdatePreviewPrefab(Vector3 position, Vector3 normal) { if (previewPrefab == null) { return; } PrefabTransform appliedTransform = CreateAppliedTransform(previewPrefab.prefabSettings, position, normal); previewPrefab.prefabInstance.transform.position = appliedTransform.position; previewPrefab.prefabInstance.transform.rotation = appliedTransform.rotation; previewPrefab.prefabInstance.transform.localScale = appliedTransform.scale; /* * // toggle visibility depending on shift key * bool visible = !(Event.current.shift && Event.current.control); * previewPrefab.prefabInstance.SetActive(visible); */ if (ApplicationSettings.useInstanceAsPreview) { // nothing to do } else { // hide the preview instance previewPrefab.prefabInstance.SetActive(false); // use DrawMesh to render the instance MeshUtils.RenderGameObject(previewPrefab.prefabInstance, 0); } }
private void AddNewPrefab(PrefabSettings prefabSettings, Vector3 position, Vector3 normal) { PrefabTransform appliedTransform = CreateAppliedTransform(prefabSettings, position, normal); // create instance and apply position / rotation / scale brushModuleEditor.PersistPrefab(prefabSettings, appliedTransform); }
public void PersistPrefab(PrefabSettings prefabSettings, PrefabTransform prefabTransform) { // spawn item to vs pro if (editorTarget.brushSettings.spawnToVSPro) { vegetationStudioProIntegration.AddNewPrefab(prefabSettings, prefabTransform.position, prefabTransform.rotation, prefabTransform.scale); } // spawn item to scene else { // new prefab GameObject instance = PrefabUtility.InstantiatePrefab(prefabSettings.prefab) as GameObject; instance.transform.position = prefabTransform.position; instance.transform.rotation = prefabTransform.rotation; instance.transform.localScale = prefabTransform.scale; // attach as child of container instance.transform.parent = editorTarget.container.transform; Undo.RegisterCreatedObjectUndo(instance, "Instantiate Prefab"); if (editorTarget.spawnSettings.autoSimulationType != SpawnSettings.AutoSimulationType.None) { autoPhysicsCollection.Add(instance); } } }
public void PersistPrefab(PrefabSettings prefabSettings, PrefabTransform prefabTransform) { switch (editorTarget.brushSettings.spawnTarget) { case BrushSettings.SpawnTarget.PrefabContainer: // new prefab GameObject instance = PrefabUtility.InstantiatePrefab(prefabSettings.prefab) as GameObject; instance.transform.position = prefabTransform.position; instance.transform.rotation = prefabTransform.rotation; instance.transform.localScale = prefabTransform.scale; // attach as child of container instance.transform.parent = editorTarget.container.transform; Undo.RegisterCreatedObjectUndo(instance, "Instantiate Prefab"); if (editorTarget.spawnSettings.autoSimulationType != SpawnSettings.AutoSimulationType.None) { autoPhysicsCollection.Add(instance); } break; case BrushSettings.SpawnTarget.TerrainTrees: unityTerrainTreesIntegration.AddNewPrefab(prefabSettings, prefabTransform.position, prefabTransform.rotation, prefabTransform.scale); break; case BrushSettings.SpawnTarget.TerrainDetails: Debug.LogError("Not implemented"); break; case BrushSettings.SpawnTarget.VegetationStudioPro: vegetationStudioProIntegration.AddNewPrefab(prefabSettings, prefabTransform.position, prefabTransform.rotation, prefabTransform.scale); break; } }
private PrefabTransform CreateAppliedTransform(PrefabSettings prefabSettings, Vector3 position, Vector3 normal) { /// /// Calculate position / rotation / scale /// // get new position Vector3 newPosition = position; // add offset of brush in up direction newPosition += CalculateBrushOffsetUp(); // add offset of prefab settings newPosition += prefabSettings.positionOffset; // auto physics height offset newPosition = ApplyAutoPhysicsHeightOffset(newPosition); Vector3 newLocalScale = prefabSettings.prefab.transform.localScale; // size // please note that the scale might be change later again (scale to brush size) // which should happen after the rotation if (prefabSettings.changeScale) { newLocalScale = Vector3.one * Random.Range(prefabSettings.scaleMin, prefabSettings.scaleMax); } // rotation Quaternion alignedRotation = Quaternion.identity; Quaternion objectRotation; if (this.editorTarget.brushSettings.alignToTerrain) { alignedRotation = Quaternion.FromToRotation(Vector3.up, normal); } if (prefabSettings.randomRotation) { objectRotation = prefabSettings.instanceRotation; } else { objectRotation = Quaternion.Euler(prefabSettings.rotationOffset); } // additionally consider brush rotation Quaternion brushRotation = Quaternion.Euler(0f, editorTarget.brushSettings.brushRotation, 0f); // combine terrain aligned rotation and object rotation Quaternion newRotation = alignedRotation * objectRotation * brushRotation; // scale to brush size // this uses world bounds and happens after the rotation if (editorTarget.brushSettings.distribution == BrushSettings.Distribution.Fluent) { GameObject prefab = prefabSettings.prefab; Quaternion prevRotation = prefab.transform.rotation; { // we need to rotate the gameobject now in order to calculate the world bounds prefab.transform.rotation = newRotation; float brushSize = editorTarget.brushSettings.brushSize; Bounds worldBounds = BoundsUtils.CalculateBounds(prefab); Vector3 prefabScale = prefab.transform.localScale; float scaleFactorX = brushSize / worldBounds.size.x; float scaleFactorY = brushSize / worldBounds.size.y; float scaleFactorZ = brushSize / worldBounds.size.z; float scaleFactorXYZ = Mathf.Min(scaleFactorX, scaleFactorY, scaleFactorZ); newLocalScale = prefabScale * scaleFactorXYZ; } prefab.transform.rotation = prevRotation; } PrefabTransform prefabTransform = new PrefabTransform(newPosition, newRotation, newLocalScale); return(prefabTransform); }