Пример #1
0
        private void mMeshListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mStateListView.Items.Clear();
            SceneMesh m;

            if (e.AddedItems.Count > 0)
            {
                m             = (SceneMesh)e.AddedItems[0];
                mSelectedMesh = m;
            }
            else
            {
                mSelectedMesh = null;
            }
            m = mSelectedMesh;
            GeostatesPanel.Visibility = m == null || m.States.Count() == 0 ? Visibility.Collapsed : Visibility.Visible;
            if (m != null)
            {
                mStateListView.Items.Add(new SceneGeostate(m, null, null));
                foreach (SceneGeostate s in m.States)
                {
                    mStateListView.Items.Add(s);
                }
                mStateListView.SelectedIndex = 0;
            }
            else
            {
                GeostatesPanel.Visibility = Visibility.Collapsed;
            }
            UpdateMaterials();
        }
Пример #2
0
        public async System.Threading.Tasks.Task <bool> SetMaterialInstanceAsync(CRenderContext RHICtx, UInt32 index,
                                                                                 Graphics.CGfxMaterialInstance material, CGfxShadingEnv[] envs, bool preUseEffect = false)
        {
            var ret = SetMaterialInstance(RHICtx, index, material, envs, preUseEffect);
            await SceneMesh.AwaitEffects();

            return(ret);
        }
Пример #3
0
 public bool SetMaterialInstance(CRenderContext RHICtx, UInt32 index, Graphics.CGfxMaterialInstance material, CGfxShadingEnv[] envs, bool preUseEffect = false)
 {
     if (index >= mSceneMesh.MtlMeshArray.Length)
     {
         return(false);
     }
     SceneMesh.SetMaterialInstance(RHICtx, index, material, envs, preUseEffect);
     return(true);
 }
Пример #4
0
        public async System.Threading.Tasks.Task <bool> SetMaterialInstance(CRenderContext RHICtx, UInt32 index, RName materialName, CGfxShadingEnv[] envs)
        {
            var mtl = await CEngine.Instance.MaterialInstanceManager.GetMaterialInstanceAsync(RHICtx, materialName);

            if (mtl == null)
            {
                return(false);
            }
            var ret = SetMaterialInstance(RHICtx, index, mtl, envs);
            await SceneMesh.AwaitEffects();

            return(ret);
        }
        /// <summary>
        /// <para xml:lang="en">Internal method, do not call directly.</para>
        /// <para xml:lang="zh">内部方法,不可直接调用。</para>
        /// </summary>
        internal void UpdateData(BlockInfo info, SceneMesh easyarMesh)
        {
            Info = info;

            if (Info.numOfVertex == 0)
            {
                vertices = null;
                normals  = null;
                indexes  = null;
                gameObject.SetActive(false);
                return;
            }

            gameObject.SetActive(true);
            CopyMeshData(easyarMesh);
        }
        private void CopyMeshData(SceneMesh easyarMesh)
        {
            using (var verticesBuffer = easyarMesh.getVerticesIncremental())
                using (var normalBuffer = easyarMesh.getNormalsIncremental())
                    using (var indicesBuffer = easyarMesh.getIndicesIncremental())
                    {
                        using (var vb = verticesBuffer.partition(Info.startPointOfVertex * 12, Info.numOfVertex * 12))
                        {
                            var vbData = new float[Info.numOfVertex * 3];
                            Marshal.Copy(vb.data(), vbData, 0, vbData.Length);

                            vertices = new Vector3[Info.numOfVertex];
                            for (int i = 0; i < Info.numOfVertex; ++i)
                            {
                                var idx = i * 3;
                                vertices[i] = new Vector3(vbData[idx], vbData[idx + 1], -vbData[idx + 2]);
                            }
                        }
                        using (var nb = easyarMesh.getNormalsIncremental().partition(Info.startPointOfVertex * 12, Info.numOfVertex * 12))
                        {
                            var nbData = new float[Info.numOfVertex * 3];
                            Marshal.Copy(nb.data(), nbData, 0, nbData.Length);

                            normals = new Vector3[Info.numOfVertex];
                            for (int i = 0; i < Info.numOfVertex; ++i)
                            {
                                var idx = i * 3;
                                normals[i] = new Vector3(nbData[idx], nbData[idx + 1], -nbData[idx + 2]);
                            }
                        }
                        using (var ib = easyarMesh.getIndicesIncremental().partition(Info.startPointOfIndex * 4, Info.numOfIndex * 4))
                        {
                            indexes = new int[Info.numOfIndex - Info.numOfIndex % 3];
                            Marshal.Copy(ib.data(), indexes, 0, indexes.Length);

                            for (int i = 2; i < Info.numOfIndex; i += 3)
                            {
                                var tmp = indexes[i];
                                indexes[i]     = indexes[i - 1];
                                indexes[i - 1] = tmp;
                            }
                        }
                    }
        }
        public void SyncValues(MaterialInstanceEditProperty showvalue)
        {
            foreach (var varvalue in showvalue.VarRNameValues)
            {
                var varkey   = varvalue.Key;
                var objvalue = varvalue.Value;

                var ShowName = SceneMesh.MtlMeshArray[MtlIndex].MtlInst.GetVarName((uint)varkey, true);
                var index    = SceneMesh.McFindVar((uint)MtlIndex, ShowName);

                if (objvalue.GetType().Equals(typeof(float)))
                {
                    SceneMesh.McSetVarFloat((uint)MtlIndex, index, (float)objvalue, 0);
                    //elem.VarObject.mMaterialInstance.SetVarValue(elem.VarObject.Index, 0, ref value);
                }
                else if (objvalue.GetType().Equals(typeof(Vector2)))
                {
                    SceneMesh.McSetVarVector2((uint)MtlIndex, index, (Vector2)objvalue, 0);
                }
                else if (objvalue.GetType().Equals(typeof(Vector3)))
                {
                    SceneMesh.McSetVarVector3((uint)MtlIndex, index, (Vector3)objvalue, 0);
                }
                else if (objvalue.GetType().Equals(typeof(Vector4)))
                {
                    SceneMesh.McSetVarColor4((uint)MtlIndex, index, (Vector4)objvalue, 0);
                }
                else if (objvalue.GetType().Equals(typeof(EngineNS.Color)))
                {
                    SceneMesh.McSetVarColor4((uint)MtlIndex, index, (EngineNS.Color)objvalue, 0);
                }

                AddVarValue(varkey, objvalue);
            }

            foreach (var srvvalue in showvalue.SRVRNameValues)
            {
                var varkey   = srvvalue.Key;
                var objvalue = srvvalue.Value;
                SceneMesh.McSetTexture((uint)MtlIndex, varkey, objvalue as RName);

                AddSRVValue(varkey, objvalue);
            }
        }
Пример #8
0
 public SceneGeostate(SceneMesh owner, MLOD.GeometryState state, GeometryModel3D model)
 {
     Owner = owner;
     State = state;
     Model = model;
 }
Пример #9
0
        private void CreateVisuals()
        {
            var compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;

            var mesh = SceneMesh.Create(compositor);
        }
Пример #10
0
		public List<MeshFilter> GetSceneMeshes () {
			
			
			MeshFilter[] filters = GameObject.FindObjectsOfType(typeof(MeshFilter)) as MeshFilter[];
			
			List<MeshFilter> filteredFilters = new List<MeshFilter> (filters.Length/3);
			
			for (int i=0;i<filters.Length;i++) {
				MeshFilter filter = filters[i];
				if (filter.renderer != null && filter.renderer.enabled && (((1 << filter.gameObject.layer) & mask) == (1 << filter.gameObject.layer) || tagMask.Contains (filter.tag))) {
					filteredFilters.Add (filter);
				}
			}
			
			List<SceneMesh> meshes = new List<SceneMesh>();
			
#if UNITY_4
			bool containedStatic = false;
#else
			HashSet<Mesh> staticMeshes = new HashSet<Mesh>();
#endif
			
			foreach (MeshFilter filter in filteredFilters) {
				
				//Workaround for statically batched meshes
				if (filter.renderer.isPartOfStaticBatch) {
#if UNITY_4
					containedStatic = true;
#else
					Mesh mesh = filter.sharedMesh;
					if (!staticMeshes.Contains (mesh)) {
						staticMeshes.Add (mesh);
						SceneMesh smesh = new SceneMesh();
						smesh.mesh = mesh;
						//This is not the real bounds, it will be expanded later as more renderers are found
						smesh.bounds = filter.renderer.bounds;
						smesh.matrix = Matrix4x4.identity;
						meshes.Add (smesh);
					} else {
						SceneMesh smesh;
						for (int i=0;i<meshes.Count;i++) {
							if (meshes[i].mesh == mesh) { smesh = meshes[i]; break; }
						}
						smesh.bounds.Encapsulate (filter.renderer.bounds);
					}
#endif
				} else {
					//Only include it if it intersects with the graph
					if (filter.renderer.bounds.Intersects (forcedBounds)) {
						Mesh mesh = filter.sharedMesh;
						SceneMesh smesh = new SceneMesh();
						smesh.matrix = filter.renderer.localToWorldMatrix;
						smesh.mesh = mesh;
						smesh.bounds = filter.renderer.bounds;
						
						meshes.Add (smesh);
					}
				}
				
#if UNITY_4
				if (containedStatic)
					Debug.LogWarning ("Some meshes were statically batched. These meshes can not be used for navmesh calculation" +
						" due to technical constraints.");
#endif
			}
			
#if ASTARDEBUG
			int y = 0;
			foreach (SceneMesh smesh in meshes) {
				y++;
				Vector3[] vecs = smesh.mesh.vertices;
				int[] tris = smesh.mesh.triangles;
				
				for (int i=0;i<tris.Length;i+=3) {
					Vector3 p1 = smesh.matrix.MultiplyPoint3x4(vecs[tris[i+0]]);
					Vector3 p2 = smesh.matrix.MultiplyPoint3x4(vecs[tris[i+1]]);
					Vector3 p3 = smesh.matrix.MultiplyPoint3x4(vecs[tris[i+2]]);
					
					Debug.DrawLine (p1,p2,Color.red,1);
					Debug.DrawLine (p2,p3,Color.red,1);
					Debug.DrawLine (p3,p1,Color.red,1);
				}
				
			}
#endif
			
			return filteredFilters;
		}
Пример #11
0
        private SpatialAwarenessSceneObject ConvertSceneObject(SceneObject sceneObject)
        {
            int quadCount = sceneObject.Quads.Count;
            int meshCount = sceneObject.Meshes.Count;

            List <SpatialAwarenessSceneObject.Quad>     quads  = new List <SpatialAwarenessSceneObject.Quad>(quadCount);
            List <SpatialAwarenessSceneObject.MeshData> meshes = new List <SpatialAwarenessSceneObject.MeshData>(meshCount);

            if (RequestPlaneData)
            {
                SceneQuad sceneQuad = null;

                for (int i = 0; i < quadCount; ++i)
                {
                    sceneQuad = sceneObject.Quads[i];

                    var quadIdKey = sceneQuad.Id;

                    byte[] occlusionMaskBytes = null;

                    if (RequestOcclusionMask)
                    {
                        occlusionMaskBytes = new byte[OcclusionMaskResolution.x * OcclusionMaskResolution.y];
                        sceneQuad.GetSurfaceMask((ushort)OcclusionMaskResolution.x, (ushort)OcclusionMaskResolution.y, occlusionMaskBytes);
                    }

                    var extents = new UnityEngine.Vector2(sceneQuad.Extents.X, sceneQuad.Extents.Y);

                    var quad = new SpatialAwarenessSceneObject.Quad(quadIdKey, extents, occlusionMaskBytes);

                    quads.Add(quad);

                    // Store a cache so we can retrieve best position on plane later.

                    if (!cachedSceneQuads.ContainsKey(quadIdKey))
                    {
                        cachedSceneQuads.Add(quadIdKey, new Tuple <SceneQuad, SceneObject>(sceneQuad, sceneObject));
                    }
                }
            }

            if (RequestMeshData)
            {
                SceneMesh sceneMesh = null;

                for (int i = 0; i < meshCount; ++i)
                {
                    sceneMesh = sceneObject.Meshes[i];
                    var meshData = MeshData(sceneMesh);
                    meshes.Add(meshData);
                }
            }

            // World space conversion

            System.Numerics.Matrix4x4 worldXformSystem = sceneObject.GetLocationAsMatrix() * sceneToWorldXformSystem * correctOrientation;

            System.Numerics.Vector3    worldTranslationSystem;
            System.Numerics.Quaternion worldRotationSytem;
            System.Numerics.Vector3    localScale;

            System.Numerics.Matrix4x4.Decompose(worldXformSystem, out localScale, out worldRotationSytem, out worldTranslationSystem);

            var result = new SpatialAwarenessSceneObject(
                sceneObject.Id,
                SpatialAwarenessSurfaceType(sceneObject.Kind),
                worldTranslationSystem.ToUnityVector3(),
                worldRotationSytem.ToUnityQuaternion(),
                quads,
                meshes);

            return(result);
        }
Пример #12
0
        private SpatialAwarenessSceneObject.MeshData MeshData(SceneMesh sceneMesh)
        {
            // Indices

            var indices = new int[sceneMesh.TriangleIndexCount];

            var meshIndices = new uint[sceneMesh.TriangleIndexCount];

            sceneMesh.GetTriangleIndices(meshIndices);

            var ilength = meshIndices.Length;

            for (int i = 0; i < ilength; ++i)
            {
                indices[i] = (int)meshIndices[i];
            }

            // Vertices

            var vertices = new UnityEngine.Vector3[sceneMesh.VertexCount];
            var uvs      = new UnityEngine.Vector2[sceneMesh.VertexCount];

            var meshVertices = new System.Numerics.Vector3[sceneMesh.VertexCount];

            sceneMesh.GetVertexPositions(meshVertices);

            var vlength = meshVertices.Length;

            var   minx = meshVertices[0].X;
            var   miny = meshVertices[0].Y;
            var   maxx = minx;
            var   maxy = miny;
            float x    = minx;
            float y    = miny;

            for (int i = 0; i < vlength; ++i)
            {
                x = meshVertices[i].X;
                y = meshVertices[i].Y;

                vertices[i] = new UnityEngine.Vector3(x, y, -meshVertices[i].Z);
                minx        = Math.Min(minx, x);
                miny        = Math.Min(miny, y);
                maxx        = Math.Max(maxx, x);
                maxy        = Math.Max(maxy, y);
            }

            // UVs - planar square projection

            float smallestDimension = Math.Min(minx, miny);
            float biggestDimension  = Math.Max(maxx, maxy);

            for (int i = 0; i < vlength; ++i)
            {
                uvs[i] = new Vector2(
                    Mathf.InverseLerp(smallestDimension, biggestDimension, vertices[i].x),
                    Mathf.InverseLerp(smallestDimension, biggestDimension, vertices[i].y));
            }

            var result = new SpatialAwarenessSceneObject.MeshData
            {
                indices  = indices,
                vertices = vertices,
                guid     = sceneMesh.Id,
                uvs      = uvs
            };

            return(result);
        }
Пример #13
0
    /// <summary>
    /// Create a list of Unity GameObjects that represent all the Meshes/Geometry in a Scene
    /// Understanding Object
    /// </summary>
    /// <param name="suObject">The Scene Understanding Object to generate in Unity</param>
    private List <GameObject> CreateSUObjectInUnity(SceneObject suObject)
    {
        // Each SU object has a specific type, query for its correspoding color
        // according to its type
        Color?color = GetColor(suObject.Kind);
        int   layer = GetLayer(suObject.Kind);

        // QUADS
        List <GameObject> listOfGeometryGameObjToReturn = new List <GameObject>();

        if (SceneObjectRequestMode == RenderMode.Quad || SceneObjectRequestMode == RenderMode.QuadWithMask)
        {
            // If the Request Settings are requesting quads, create a gameobject in unity for
            // each quad in the Scene Object
            foreach (SceneQuad quad in suObject.Quads)
            {
                Mesh unityMesh = GenerateUnityMeshFromSceneObjectQuad(quad);

                Material tempMaterial = GetMaterial(suObject.Kind, SceneObjectRequestMode);

                GameObject gameObjectToReturn = new GameObject(suObject.Kind.ToString());
                gameObjectToReturn.layer = layer;
                AddMeshToUnityObject(gameObjectToReturn, unityMesh, color, tempMaterial);

                if (SceneObjectRequestMode == RenderMode.QuadWithMask)
                {
                    ApplyQuadRegionMask(quad, gameObjectToReturn, color.Value);
                }

                if (AddColliders)
                {
                    gameObjectToReturn.AddComponent <BoxCollider>();
                }

                // Add to list
                listOfGeometryGameObjToReturn.Add(gameObjectToReturn);
            }
        }
        else // MESH OR WUREFRAME
        {
            // If the Request Settings are requesting Meshes or WireFrame, create a gameobject in unity for
            // each Mesh, and apply either the default material or the wireframe material
            for (int i = 0; i < suObject.Meshes.Count; i++)
            {
                SceneMesh suGeometryMesh = suObject.Meshes[i];
                SceneMesh suColliderMesh = suObject.ColliderMeshes[i];

                // Generate the unity mesh for the Scene Understanding mesh.
                Mesh unityMesh = GenerateUnityMeshFromSceneObjectMeshes(new List <SceneMesh> {
                    suGeometryMesh
                });
                GameObject gameObjectToReturn = new GameObject(suObject.Kind.ToString() + "Mesh");
                gameObjectToReturn.layer = layer;

                Material tempMaterial = GetMaterial(suObject.Kind, SceneObjectRequestMode);

                // Add the created Mesh into the Unity Object
                AddMeshToUnityObject(gameObjectToReturn, unityMesh, color, tempMaterial);

                if (AddColliders)
                {
                    // Generate a unity mesh for physics
                    Mesh unityColliderMesh = GenerateUnityMeshFromSceneObjectMeshes(new List <SceneMesh> {
                        suColliderMesh
                    });

                    MeshCollider col = gameObjectToReturn.AddComponent <MeshCollider>();
                    col.sharedMesh = unityColliderMesh;
                }

                listOfGeometryGameObjToReturn.Add(gameObjectToReturn);
            }
        }

        // Return all the Geometry GameObjects that represent a Scene
        // Understanding Object
        return(listOfGeometryGameObjToReturn);
    }