public override void From(Object component)
        {
            MeshRenderer meshRenderer = (MeshRenderer)component;

            meshFilter     = meshRenderer.GetComponent <MeshFilter>();
            castShadows    = meshRenderer.shadowCastingMode != UnityEngine.Rendering.ShadowCastingMode.Off;
            receiveShadows = meshRenderer.receiveShadows;
            material       = meshRenderer.sharedMaterial;
            instanceID     = meshRenderer.GetInstanceID();
        }
    public void AddMatrix(MeshRenderer renderer)
    {
        if (renderer == null /*|| sharedMaterials != renderer.sharedMaterials*/)
        {
            return;
        }

        int instanceId = renderer.GetInstanceID();

        if (matrixIdxMap != null && matrixIdxMap.ContainsKey(instanceId))
        {
            return;
        }

        Matrix4x4 mat;

        if (!GetRendererMatrix(renderer, out mat))
        {
            return;
        }



        if (matrixList == null)
        {
            matrixList = new List <Matrix4x4> ();
        }
        if (matrixInstanceIdList == null)
        {
            matrixInstanceIdList = new List <int> ();
        }
        //if (scaleList == null)
        //	scaleList = new List<Vector4> ();
        int idx = matrixList.Count;

        matrixList.Add(mat);
        matrixInstanceIdList.Add(instanceId);
        //Vector4 scale = renderer.transform.lossyScale;
        //scale.w = 1.0f;
        //scaleList.Add (scale);

        if (matrixIdxMap == null)
        {
            matrixIdxMap = new Dictionary <int, int> ();
        }
        matrixIdxMap [instanceId] = idx;

        //RefreshScalePropBlock ();

        InitCommandBuffer();
    }
        private void UpdateActiveGameObjectSelection()
        {
            MeshRenderer renderer = null;
            Terrain      terrain  = null;

            // if the selected active object (also active in the hierarchy) is a renderer or a terrain, we check its index etc.
            // otherwise bail
            if (Selection.activeGameObject == null || !Selection.activeGameObject.activeInHierarchy ||
                (!Selection.activeGameObject.TryGetComponent(out renderer) &&
                 !Selection.activeGameObject.TryGetComponent(out terrain)))
            {
                m_ActiveGameObjectLightmapIndex = -1;
                m_ActiveGameObjectInstanceId    = -1;
                m_ActiveGameObjectTextureHash   = new Hash128();
                return;
            }

            if (isRealtimeLightmap)
            {
                Hash128 inputSystemHash;
                if ((renderer != null && Lightmapping.GetInputSystemHash(renderer.GetInstanceID(), out inputSystemHash)) ||
                    (terrain != null && Lightmapping.GetInputSystemHash(terrain.GetInstanceID(), out inputSystemHash)))
                {
                    m_ActiveGameObjectTextureHash = inputSystemHash;
                }
                else
                {
                    m_ActiveGameObjectTextureHash = new Hash128();
                }
            }
            else
            {
                m_ActiveGameObjectLightmapIndex = renderer != null ? renderer.lightmapIndex : terrain.lightmapIndex;
                m_ActiveGameObjectInstanceId    = renderer != null?renderer.GetInstanceID() : terrain.GetInstanceID();
            }
        }
        /// <summary>
        /// Clones a mesh renderer object with properties for WIM
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private MeshRenderer CloneMeshRenderer(MeshRenderer obj)
        {
            MeshRenderer objClone = Instantiate(obj);

            objClone.name = WIM_OBJECT_NAME + obj.gameObject.name;
            if (layer != -1)
            {
                objClone.gameObject.layer = layer;
            }

            // append object to WIM
            objClone.transform.SetParent(Wim.transform, false);

            objClone.transform.localPosition = obj.transform.position;
            objClone.transform.localRotation = obj.transform.rotation;
            objClone.transform.localScale    = obj.transform.lossyScale; // global scale!

            if (obj.transform.parent)
            {
                objClone.transform.localScale = Vector3.Scale(objClone.transform.localScale, obj.transform.parent.localScale);
            }
            objClone.transform.position = obj.transform.position;

            objClone.gameObject.AddComponent <VRIL_WIMObject>();

            // set kinematic true for all WIM objects
            if (HasComponent(objClone.gameObject, out Rigidbody rigibody))
            {
                rigibody.isKinematic = true;
            }

            MappingCloneIdsToOriginals[objClone.GetInstanceID()] = obj;
            MappingOriginalIdsToClones[obj.GetInstanceID()]      = objClone;

            // remove children to avoid double existing objects in WIM
            ClearChildren(objClone.gameObject);

            return(objClone);
        }
Exemple #5
0
    // Start is called before the first frame update
    void Start()
    {
        //1. GetComponent<T>() 获取当前游戏对象的制定组件,没有就返回null。不查找子物体组件
        rb = GetComponent <Rigidbody>();
        print(string.Format("使用GetComponent<T>()获取游戏对象{0}的RigidBody这个组件的InstanceID为{1}", rb.name, rb.GetInstanceID()));
        //2. GetComponents<T>() 获取当前游戏对象的所有指定不查找子物体的组件
        rbs = GetComponents <Rigidbody>();
        foreach (var rb in rbs)
        {
            print(string.Format("使用GetComponents<T>()获取游戏对象{0}的RigidBody这个组件的InstanceID为{1}", rb.name, rb.GetInstanceID()));
        }

        //3. GetComponentInChildren<T>() 先查找父物体有没有制定组件,没有的话再查找子物体,最后返回第一个指定组件(由上往下查找指定组件)
        mr = GetComponentInChildren <MeshRenderer>(); //因为Shpere本身就具有MeshRenderer组件,即使子物体也包含这个组件,也只返回本身这个物体的Sphere
        print(string.Format("使用GetComponentInChildren<T>()获取游戏对象{0}的MeshRenderer这个组件的InstanceID为{1}", mr.name, mr.GetInstanceID()));
        //4. GetComponentsInChildren<T>() 获取当前游戏对象及子对象的所有指定组件
        mrs = GetComponentsInChildren <MeshRenderer>();
        foreach (var mr in mrs)
        {
            print(string.Format("使用GetComponentsInChildren<T>()获取游戏对象{0}的MeshRenderer这个组件的InstanceID为{1}", mr.name, mr.GetInstanceID()));
        }

        //5. GetComponentInParent<T>() 由下往上查找指定组件
        t = GetComponentInParent <Transform>();
        print(string.Format("使用GetComponentInChildren<T>()获取游戏对象{0}的Transform这个组件的InstanceID为{1}", t.name, t.GetInstanceID()));
        //6. GetComponentInParent<T>() 获取当前游戏对象及父对象的所有指定组件
        ts = GetComponentsInParent <Transform>();
        foreach (var t in ts)
        {
            print(string.Format("使用GetComponentsInParent<T>()获取游戏对象{0}的Transform这个组件的InstanceID为{1}", t.name, t.GetInstanceID()));
        }
    }
    /*
     * void RefreshScalePropBlock()
     * {
     *      if (scaleList == null)
     *              return;
     *
     *      if (propBlock == null)
     *              propBlock = new MaterialPropertyBlock ();
     *      propBlock.SetVectorArray (_globalScaleNameID, scaleList);
     * }
     */
    public void RemoveMatrix(MeshRenderer renderer)
    {
        if (renderer == null)
        {
            return;
        }

        if (matrixList == null || matrixList.Count <= 0 || matrixIdxMap == null || matrixIdxMap.Count <= 0 || matrixInstanceIdList == null || matrixInstanceIdList.Count <= 0)
        {
            return;
        }

        int instanceId = renderer.GetInstanceID();
        int idx;

        if (!matrixIdxMap.TryGetValue(instanceId, out idx))
        {
            return;
        }
        matrixIdxMap.Remove(instanceId);
        if (idx < 0 || idx >= matrixList.Count || idx >= matrixInstanceIdList.Count)
        {
            return;
        }

        if (matrixList.Count <= 1)
        {
            matrixList.Clear();
            matrixIdxMap.Clear();
            matrixInstanceIdList.Clear();
            //scaleList.Clear ();
        }
        else
        {
            int removeIdx = matrixList.Count - 1;
            if (idx != removeIdx)
            {
                Matrix4x4 mat = matrixList[removeIdx];
                matrixList[idx] = mat;
                matrixList.RemoveAt(removeIdx);
                int moveInstanceId = matrixInstanceIdList[removeIdx];
                matrixInstanceIdList[idx] = moveInstanceId;
                matrixInstanceIdList.RemoveAt(removeIdx);
                matrixIdxMap[moveInstanceId] = idx;

                //	Vector4 moveScale = scaleList [removeIdx];
                //	scaleList [idx] = moveScale;
                //	scaleList.RemoveAt (removeIdx);
            }
            else
            {
                matrixList.RemoveAt(removeIdx);
                matrixInstanceIdList.RemoveAt(removeIdx);
                //	scaleList.RemoveAt (removeIdx);
            }
        }

        //RefreshScalePropBlock ();

        InitCommandBuffer();
    }
    public override void OnInspectorGUI()
    {
        this.DrawDefaultInspector();

        EditorGUI.indentLevel = 0;
        foldout = EditorGUILayout.Foldout(foldout, "MeshRenderer AABB");
        if (foldout)
        {
            EditorGUI.indentLevel = 2;
            bool newIsAABBenabled = EditorGUILayout.Toggle(STR_ENABLE_MESH_AABB, isAABBenabled);
            if (newIsAABBenabled != isAABBenabled)
            {
                isAABBenabled = newIsAABBenabled;
                EditorUtility.SetDirty(this.target);
            }

            // Update status of the "Keep AABB visible" checkbox (optimize this by doing the search in storedSelections dictionary only when the current selection changes)
            if (currentSelection != this.target)
            {
                currentSelection = this.target as MeshRenderer;

                // check if this MeshRenderer is already in the stored selections
                isKeepAABBVisible = storedSelections.ContainsKey(currentSelection.GetInstanceID());
                //Debug.Log("isKeepAABBVisible current status: " + isKeepAABBVisible);
            }

            bool newIsKeepAABBVisible = EditorGUILayout.Toggle(STR_KEEP_VISIBLE, isKeepAABBVisible);
            if (newIsKeepAABBVisible != isKeepAABBVisible)
            {
                if (newIsKeepAABBVisible)
                {
                    Transform[] multiSelection = Selection.transforms;

                    // check for multi selection
                    if (multiSelection.Length > 1)
                    {
                        for (int i = 0; i < multiSelection.Length; i++)
                        {
                            if (multiSelection[i].renderer && !storedSelections.ContainsKey((multiSelection[i].renderer as MeshRenderer).GetInstanceID()))
                            {
                                storedSelections.Add((multiSelection[i].renderer as MeshRenderer).GetInstanceID(), multiSelection[i].renderer as MeshRenderer);
                            }
                        }
                    }
                    else
                    {
                        // add current mesh renderer to the stored selection dictionary to have the AABB always highlighted
                        storedSelections.Add(currentSelection.GetInstanceID(), currentSelection);
                    }
                }
                else
                {
                    Transform[] multiSelection = Selection.transforms;

                    // check for multi-selection
                    if (multiSelection.Length > 1)
                    {
                        for (int i = 0; i < multiSelection.Length; i++)
                        {
                            if (multiSelection[i].renderer && storedSelections.ContainsKey((multiSelection[i].renderer as MeshRenderer).GetInstanceID()))
                            {
                                storedSelections.Remove((multiSelection[i].renderer as MeshRenderer).GetInstanceID());
                            }
                        }
                    }
                    else
                    {
                        // remove current MeshRenderer from the storeSelections dictionary
                        storedSelections.Remove(currentSelection.GetInstanceID());
                    }
                }
                EditorUtility.SetDirty(this.target);
                // force refresh of the "Keep AABB visible" checkbox status
                currentSelection = null;
            }

            // Display current AABB bounds size
            if (currentSelection != null)
            {
                EditorGUILayout.SelectableLabel("Bounds Size:" + currentSelection.bounds.size.ToString());
            }

            EditorGUILayout.Separator();
            GUILayout.BeginVertical();
            if (GUILayout.Button(STR_CLEAR_ALL_VISIBLE, GUILayout.MaxWidth(200)))
            {
                storedSelections.Clear();
                cleanupList.Clear();
                EditorUtility.SetDirty(this.target);
            }
            GUILayout.EndVertical();
        }
    }