public override void OnSelect(Player player) { // Use correct preview mesh MeshResource shapeMesh = null; MaterialResource shapeMat = null; switch (player.CurrentDiggingShape) { case Player.DiggingShape.Box: shapeMesh = Resources.UseMesh("::Debug/Box", null); shapeMat = Resources.UseMaterial("Items/DigPreviewBox", UpvoidMiner.ModDomain); break; case Player.DiggingShape.Cylinder: shapeMesh = Resources.UseMesh("::Debug/Cylinder", null); shapeMat = Resources.UseMaterial("Items/DigPreviewCylinder", UpvoidMiner.ModDomain); break; case Player.DiggingShape.Sphere: shapeMesh = Resources.UseMesh("::Debug/Sphere", null); shapeMat = Resources.UseMaterial("Items/DigPreviewSphere", UpvoidMiner.ModDomain); break; default: throw new InvalidOperationException("Unknown digging shape"); } // Create a transparent sphere as 'fill-indicator'. previewShape = new MeshRenderJob(Renderer.Transparent.Mesh, shapeMat, shapeMesh, mat4.Scale(0f)); LocalScript.world.AddRenderJob(previewShape); // And a second one for indicating the center. previewShapeIndicator = new MeshRenderJob(Renderer.Transparent.Mesh, Resources.UseMaterial("Items/ResourcePreviewIndicator", UpvoidMiner.ModDomain), shapeMesh, mat4.Scale(0f)); LocalScript.world.AddRenderJob(previewShapeIndicator); }
void ValidateMeshSetShape(Shape shapeArg, Shape referenceArg, Dictionary <ulong, Resource> resources) { ShapeTestFramework.ValidateShape(shapeArg, referenceArg, resources); MeshSet shape = (MeshSet)shapeArg; MeshSet reference = (MeshSet)referenceArg; Assert.Equal(reference.PartCount, shape.PartCount); Net.ObjectAttributes attrRef = new Net.ObjectAttributes(); Net.ObjectAttributes attrMesh = new Net.ObjectAttributes(); for (int i = 0; i < shape.PartCount; ++i) { // Transforms may not be exactly equal. We need to convert to PRS components and compare approximate equality // there as this is what the transfer does. attrRef.SetFromTransform(reference.PartTransformAt(i)); attrMesh.SetFromTransform(shape.PartTransformAt(i)); AssertExt.Near(attrRef.X, attrMesh.X, 1e-3f); AssertExt.Near(attrRef.Y, attrMesh.Y, 1e-3f); AssertExt.Near(attrRef.Z, attrMesh.Z, 1e-3f); AssertExt.Near(attrRef.RotationX, attrMesh.RotationX, 1e-3f); AssertExt.Near(attrRef.RotationY, attrMesh.RotationY, 1e-3f); AssertExt.Near(attrRef.RotationZ, attrMesh.RotationZ, 1e-3f); AssertExt.Near(attrRef.RotationW, attrMesh.RotationW, 1e-3f); AssertExt.Near(attrRef.ScaleX, attrMesh.ScaleX, 1e-3f); AssertExt.Near(attrRef.ScaleY, attrMesh.ScaleY, 1e-3f); AssertExt.Near(attrRef.ScaleZ, attrMesh.ScaleZ, 1e-3f); // Shape will only have a placeholder resource. Lookup in resources. MeshResource mesh = (MeshResource)resources[shape.PartAt(i).UniqueKey()]; ValidateMesh(mesh, reference.PartAt(i)); } }
/// <summary> /// Override to write part details. /// </summary> /// <param name="packet">Packet buffer to write to.</param> /// <returns>true on success.</returns> public override bool WriteCreate(PacketBuffer packet) { if (!base.WriteCreate(packet)) { return(false); } packet.WriteBytes(BitConverter.GetBytes((ushort)_parts.Count), true); ObjectAttributes partAttributes = new ObjectAttributes(); for (int i = 0; i < _parts.Count; ++i) { MeshResource part = _parts[i]; Matrix4 transform = _transforms[i]; partAttributes.SetFromTransform(transform); partAttributes.Color = 0xffffffffu; packet.WriteBytes(BitConverter.GetBytes((uint)part.ID), true); partAttributes.Write(packet); } return(true); }
/// <summary> /// Create a new point cloud shape. /// </summary> /// <param name="cloud">The cloud mesh resource.</param> /// <param name="id">The shape ID. Zero for transient.</param> /// <param name="category">Category to which the shape belongs.</param> /// <param name="pointSize">Point size override. Zero for default.</param> public PointCloudShape(MeshResource cloud, uint id = 0, ushort category = 0, byte pointSize = 0) : base((ushort)Tes.Net.ShapeID.PointCloud, id, category) { PointCloud = cloud; PointSize = pointSize; IsComplex = true; }
Shape CreateCloud(uint id, MeshResource mesh) { PointCloudShape cloud = new PointCloudShape(mesh, id); cloud.ID = id; cloud.Position = new Tes.Maths.Vector3((float)id); return(cloud); }
public static void SwitchTo(int index) { LScene.Destroy(); LanguageNotification.Destroy(); MeshResource.Destroy(); SceneManager.LoadScene(index); }
Shape CreateMeshSet(uint id, MeshResource mesh) { MeshSet meshSet = new MeshSet(id, 0); meshSet.AddPart(mesh); meshSet.Position = new Tes.Maths.Vector3((float)id); return(meshSet); }
public static MeshResource GetInstance() { if (instance == null) { instance = new MeshResource(); } return(instance); }
MeshResource <MeshData> CreateMeshResource(int slice) { var meshData = new MeshData() { Randomness = FRandomizeIn[slice], Resolution = FResoIn[slice] }; return(MeshResource.Create(meshData, CreateMesh, UpdateMesh)); }
/// <summary> /// Add a part to the mesh set with colour tint. /// </summary> /// <param name="part">The mesh resource to add.</param> /// <param name="transform">The local transform for <paramref name="part"/>.</param> /// <param name="colour">The part colour tint for <paramref name="part"/>.</param> /// <returns>This</returns> public MeshSet AddPart(MeshResource part, Matrix4 transform, Colour colour) { _parts.Add(new Part { Resource = part, Transform = transform, Colour = colour }); return(this); }
private MeshResource <Metaball> CreateMeshResource(int slice) { var metaball = new Metaball() { Position = FPositionIn[slice], Mass = FMassIn[slice] }; return(MeshResource.Create(metaball, CreateMesh)); }
static public dynamic mesh(LispList args, LispEnvironment env) { IEnumerator <dynamic> arg = args.GetEnumerator(); arg.MoveNext(); var topology = arg.Current.Eval(env); arg.MoveNext(); var layout = arg.Current.Eval(env); var resource = new MeshResource(); resource.elementSize = layout.elementSize; resource.inputLayout = layout.elements; resource.primitiveTopology = topology; env.Add(new LispSymbol("vertexSize"), new LispInteger(resource.elementSize)); arg.MoveNext(); resource.vertexstream = arg.Current.Eval(env); resource.elementCount = (int)resource.vertexstream.Length / resource.elementSize; if (arg.MoveNext()) { resource.indexed = true; if (resource.elementCount <= ushort.MaxValue) { env.Add(new LispSymbol("indexes"), VertexFormats.R16UIntIndexes); resource.indexFormat = SlimDX.DXGI.Format.R16_UInt; } else { env.Add(new LispSymbol("indexes"), VertexFormats.R32UIntIndexes); resource.indexFormat = SlimDX.DXGI.Format.R32_UInt; } resource.indexstream = arg.Current.Eval(env); if (resource.indexFormat == SlimDX.DXGI.Format.R16_UInt) { resource.indexCount = (int)resource.indexstream.Length / 2; } else { resource.indexCount = (int)resource.indexstream.Length / 4; } } else { resource.indexed = false; resource.indexFormat = SlimDX.DXGI.Format.Unknown; } return(resource); }
private GameObject m_MeshModelInstance = null; //该模型的实例 public Mesh(string name, string MeshPath, string TexturePath, float size, float MaxSize, OrganType Type, TreeModel modelEntry) { m_strName = name; m_strMeshPath = MeshPath; m_strTexturePath = TexturePath; m_fSize = size; m_fMaxSize = MaxSize; m_ModelEntry = modelEntry; m_Type = Type; MeshResource.GetInstance().Add(m_strName); m_iNameValue = MeshResource.GetInstance().GetValueOf(m_strName); }
public override string ToString() { string result = ""; result += m_cSymbol; if (m_listParams.Count == 0) { return(result); } result += "("; if (m_cSymbol.Equals("%")) //存在Mesh名替换的情况 { for (int i = 0; i < m_listParams.Count; i++) { //判断是否为数字,如果为数字则判断时候需要替换 string MeshName = StringValidate.IsNumeric(m_listParams[i]) ? MeshResource.GetInstance().GetNameOf(Convert.ToInt32(m_listParams[i])) : m_listParams[i]; if (i != 0) { result += ","; } if (MeshName != null) { result += MeshName; } else { result += m_listParams[i]; } } } else { for (int i = 0; i < m_listParams.Count; i++) { if (i != 0) { result += ","; } result += m_listParams[i]; } } result += ")"; return(result); }
public void ValidatePointCloudShape(Shape shapeArg, Shape referenceArg, Dictionary <ulong, Resource> resources) { ShapeTestFramework.ValidateShape(shapeArg, referenceArg, resources); PointCloudShape shape = (PointCloudShape)shapeArg; PointCloudShape reference = (PointCloudShape)referenceArg; Assert.Equal(reference.PointScale, shape.PointScale); Assert.NotNull(reference.PointCloud); Assert.NotNull(shape.PointCloud); Assert.Equal(reference.PointCloud.ID, shape.PointCloud.ID); // Resolve the mesh resource. Resource resource; Assert.True(resources.TryGetValue(shape.PointCloud.UniqueKey(), out resource)); // Remember, resource will be a SimpleMesh, not a PointCloud. MeshResource cloud = (MeshResource)resource; ValidateMesh(cloud, reference.PointCloud); }
public void ValidateMesh(MeshResource mesh, MeshResource reference) { Assert.Equal(reference.ID, mesh.ID); Assert.Equal(reference.TypeID, mesh.TypeID); Assert.Equal(reference.Transform, mesh.Transform); Assert.Equal(reference.Tint, mesh.Tint); Assert.Equal(reference.DrawType, mesh.DrawType); Assert.Equal(reference.IndexSize, mesh.IndexSize); Assert.Equal(reference.VertexCount(), mesh.VertexCount()); Assert.Equal(reference.IndexCount(), mesh.IndexCount()); if (mesh.VertexCount() > 0) { Assert.NotNull(reference.Vertices()); Assert.NotNull(mesh.Vertices()); Vector3 refv, meshv; for (int i = 0; i < mesh.VertexCount(); ++i) { refv = reference.Vertices()[i]; meshv = mesh.Vertices()[i]; Assert.Equal(refv.X, meshv.X); Assert.Equal(refv.Y, meshv.Y); Assert.Equal(refv.Z, meshv.Z); } } if (mesh.IndexCount() > 0) { if (reference.IndexSize == 2) { Assert.NotNull(mesh.Indices2()); for (int i = 0; i < mesh.IndexCount(); ++i) { Assert.Equal(reference.Indices2()[i], mesh.Indices2()[i]); } } else { Assert.NotNull(reference.Indices4()); Assert.NotNull(mesh.Indices4()); for (int i = 0; i < mesh.IndexCount(); ++i) { Assert.Equal(reference.Indices4()[i], mesh.Indices4()[i]); } } } if (mesh.Normals() != null) { Assert.NotNull(mesh.Normals()); Vector3 refn, meshn; for (int i = 0; i < mesh.VertexCount(); ++i) { refn = reference.Normals()[i]; meshn = mesh.Normals()[i]; Assert.Equal(refn.X, meshn.X); Assert.Equal(refn.Y, meshn.Y); Assert.Equal(refn.Z, meshn.Z); } } if (mesh.Colours() != null) { Assert.NotNull(mesh.Colours()); for (int i = 0; i < mesh.VertexCount(); ++i) { Assert.Equal(reference.Colours()[i], mesh.Colours()[i]); } } if (mesh.UVs() != null) { Assert.NotNull(mesh.UVs()); Vector2 refuv, meshuv; for (int i = 0; i < mesh.VertexCount(); ++i) { refuv = reference.UVs()[i]; meshuv = mesh.UVs()[i]; Assert.Equal(refuv.X, meshuv.X); Assert.Equal(refuv.Y, meshuv.Y); } } }
/// <summary> /// Add a part to the mesh set. /// </summary> /// <param name="part">The mesh resource to add.</param> /// <param name="transform">The local transform for <paramref name="part"/>.</param> /// <returns>This</returns> public MeshSet AddPart(MeshResource part, Matrix4 transform) { _parts.Add(part); _transforms.Add(transform); return(this); }
private void render() { clearRenderTargets(); RenderObjects objs = extractor.Scene; extractor.ExtractNext = true; foreach (var obj in objs.Objs) { using (MeshResource mesh = (MeshResource)obj.Key.Acquire()) { device.InputAssembler.SetPrimitiveTopology(mesh.primitiveTopology); device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(mesh.vertexBuffer, mesh.elementSize, 0)); device.InputAssembler.SetVertexBuffers(1, new VertexBufferBinding(instanceBuffer, 16 * 4 + sizeof(float), 0)); if (mesh.indexed) { device.InputAssembler.SetIndexBuffer(mesh.indexBuffer, mesh.indexFormat, 0); } foreach (var matRes in obj.Value) { using (MaterialResource material = (MaterialResource)matRes.Key.Acquire()) { var vertData = from RenderObject mat in matRes.Value select new { model = mat.model * objs.Camera, mat.frame }; var matArray = vertData.ToArray(); /* * for(int i = 0; i < posArray.Length; ++i) * { * posArray[i].model = posArray[i].model * objs.Camera; * } * */ using (var effect = material.AcquireEffect()) { foreach (var constant in material.Constants) { dynamic value = constant.Value; var variable = effect.Value.GetVariableByName(constant.Key); if (value is float) { variable.AsScalar().Set(value); } else if (value is Vector2 || value is Vector3 || value is Vector4) { variable.AsVector().Set(value); } /* * switch ((string)constant.Value.GetType().Name) * { * default: * throw new NotSupportedException("Constant type: " + constant.Value.GetType() + ) * break; * } * .*/ } //effect.Value.GetVariableByName("frameDimensions").AsVector().Set(material.frameDimensions); using (var textures = material.AcquireTextures()) { foreach (var texture in textures) { effect.Value.GetVariableByName(texture.Key).AsResource().SetResource(texture.Value.texture); } EffectTechnique tech = effect.Value.GetTechniqueByName("Full"); for (int i = 0; i < tech.Description.PassCount; ++i) { EffectPass pass = tech.GetPassByIndex(i); List <InputElement> elems = new List <InputElement>(mesh.inputLayout); elems.AddRange(elem); using (InputLayout layout = new InputLayout(device, pass.Description.Signature, elems.ToArray())) { device.InputAssembler.SetInputLayout(layout); for (int j = 0; j < matArray.Length; j += InstanceCount) { int curInstanceCount; using (DataStream stream = instanceBuffer.Map(MapMode.WriteDiscard, SlimDX.Direct3D10.MapFlags.None)) { curInstanceCount = Math.Min(InstanceCount, matArray.Length - j); for (int k = 0; k < curInstanceCount; ++k) { stream.Write <Matrix>(matArray[j + k].model); stream.Write <float>(matArray[j + k].frame); } // stream.WriteRange<Matrix>(posArray, j, curInstanceCount); instanceBuffer.Unmap(); } pass.Apply(); if (mesh.indexed) { device.DrawIndexedInstanced(mesh.indexCount, curInstanceCount, 0, 0, 0); } else { device.DrawInstanced(mesh.elementCount, curInstanceCount, 0, 0); } } } } } } } } } } calcPerformanceMetrics(); renderDebugOutput(); swapChain.Present(0, PresentFlags.None); }
/// <summary> /// Add a part to the mesh set. /// </summary> /// <param name="part">The mesh resource to add.</param> /// <param name="transform">The local transform for <paramref name="part"/>.</param> /// <returns>This</returns> public MeshSet AddPart(MeshResource part, Matrix4 transform) { return(AddPart(part, transform, new Colour(255, 255, 255))); }
/// <summary> /// Add a part to the mesh set with an identity local transformation. /// </summary> /// <param name="part">The mesh resource to add.</param> /// <returns>This</returns> public MeshSet AddPart(MeshResource part) { return(AddPart(part, Matrix4.Identity, new Colour(255, 255, 255))); }
/// <summary> /// Add a part to the mesh set with an identity local transformation. /// </summary> /// <param name="part">The mesh resource to add.</param> /// <returns>This</returns> public MeshSet AddPart(MeshResource part) { return(AddPart(part, Matrix4.Identity)); }
bool ValidateMeshSet(Shape shape, Shape referenceShape, MessageHandler handler) { MeshSetHandler meshSetHandler = (MeshSetHandler)handler; MeshSetHandler.PartSet parts = meshSetHandler.ShapeCache.GetShapeData <MeshSetHandler.PartSet>(shape.ID); Tes.Handlers.MeshCache meshCache = (Tes.Handlers.MeshCache)_tes.GetHandler((ushort)RoutingID.Mesh); MeshSet meshSetReference = (MeshSet)referenceShape; bool ok = true; // Validate the number of parts. if (parts.MeshIDs.Length != meshSetReference.PartCount) { Debug.LogError("Part count mismatch."); return(false); } // Validate each part. for (int i = 0; i < meshSetReference.PartCount; ++i) { MeshResource referenceMesh = meshSetReference.PartResource(i); if (parts.MeshIDs[i] != referenceMesh.ID) { Debug.LogError($"Part resource mismatch. {parts.MeshIDs[i]} != {meshSetReference.PartResource(i).ID}"); ok = false; continue; } // Resolve the mesh resource from the cache. Tes.Handlers.MeshCache.MeshDetails meshDetails = meshCache.GetEntry(parts.MeshIDs[i]); if (meshDetails == null) { Debug.LogError($"Unable to resolve mesh resource {parts.MeshIDs[i]}"); ok = false; continue; } // Validate mesh content. ok = ValidateVectors("Vertex", meshDetails.Mesh.Vertices, referenceMesh.Vertices()) && ok; if (meshDetails.Mesh.HasNormals) { Vector3[] normals = meshDetails.Mesh.Normals; if (referenceMesh.Normals().Length == 1) { // Single uniform normal will have been expanded. Extract just the first normal. normals = new Vector3[] { meshDetails.Mesh.Normals[0] }; } ok = ValidateVectors("Normal", normals, referenceMesh.Normals()) && ok; } else { if (referenceMesh.Normals() != null && referenceMesh.Normals().Length > 0) { Debug.LogError("Missing normals."); ok = false; } } if (meshDetails.Mesh.IndexCount > 0) { ok = ValidateIndices("Index", meshDetails.Mesh.Indices, referenceMesh.Indices4()) && ok; } else { if (referenceMesh.Indices4() != null && referenceMesh.Indices4().Length > 0) { Debug.LogError("Missing indices."); ok = false; } } } return(ok); }
private void GetIndexesFrom(LLinkedListNode <LTerm> node, BranchIndex fromBranch, GameObjectInfo objectInfo, ref List <OrganIndex> indexList) { if (node == null) { throw new ArgumentNullException("No start node."); } LLinkedListNode <LTerm> headNode = m_RuleData.FinalList.First; BranchIndex curBranchIndex = fromBranch; do { if (node.Value != null) { switch (node.Value.Symbol[0]) //符号解析 { case 'F': objectInfo.Length = Convert.ToSingle(node.Value.Params[0]); BranchIndex branchIndex = GetBranchIndex(curBranchIndex, fromBranch, objectInfo); indexList.Add(branchIndex); //在器官索引列表中添加,后续绘制用,绘制完成后删除 AddBranchIndex(branchIndex); //在枝干索引列表中添加,不删除 curBranchIndex = branchIndex; //设置当前的枝干索引 break; case 'f': objectInfo.Position = MoveForward(objectInfo); break; case '!': objectInfo.Radius = Convert.ToSingle(node.Value.Params[0]); break; case '+': objectInfo.Rotation -= new Vector3(Convert.ToSingle(node.Value.Params[0]), 0, 0); break; case '-': objectInfo.Rotation += new Vector3(Convert.ToSingle(node.Value.Params[0]), 0, 0); break; case '&': objectInfo.Rotation -= new Vector3(0, Convert.ToSingle(node.Value.Params[0]), 0); break; case '^': objectInfo.Rotation += new Vector3(0, Convert.ToSingle(node.Value.Params[0]), 0); break; case '\\': objectInfo.Rotation += new Vector3(0, 0, Convert.ToSingle(node.Value.Params[0])); break; case '/': objectInfo.Rotation -= new Vector3(0, 0, Convert.ToSingle(node.Value.Params[0])); break; case '%': OrganIndex organIndex = GetOrganIndex(MeshResource.GetInstance().GetNameOf(Convert.ToInt32(node.Value.Params[0])) /*将标识码转换成对应的名字*/, curBranchIndex, objectInfo); if (organIndex != null) { indexList.Add(organIndex); AddOrganIndex(organIndex); } break; case '[': node = node.Next; GetIndexesFrom(node, curBranchIndex, objectInfo.Clone(), ref indexList); //入栈 node = _CurrentNode; //将节点设置成分支结束的节点 break; case ']': _CurrentNode = node; node = headNode.Previous; //出栈,中断该函数 break; } } node = node.Next; } while (node != headNode); }
public static void Destroy() { instance = null; }
bool CreateShapes(IServer server, List <Shape> shapes) { try { // Explicit instantiation for: // - MeshShape // - MeshSet // - PointCloudShape // - Text2D // - Text3D // ( Categories) Type[] explicitTypes = new Type[] { typeof(MeshShape), typeof(MeshSet), typeof(PointCloudShape), typeof(Text2D), typeof(Text3D) }; ConstructorInfo[] simpleConstructors = GenerateShapeConstructors(explicitTypes); Shape shape = null; ShapeDelegate postCreate = null; uint objId = 0; foreach (ConstructorInfo constructor in simpleConstructors) { object obj = constructor.Invoke(new object[] { ++objId, (ushort)0 }); shape = (Shape)obj;// constructor.Invoke(new object[] { ++objId, (ushort)0 }); shape.Position = new Tes.Maths.Vector3((float)objId); shape.Rotation = Tes.Maths.Quaternion.AxisAngle(new Tes.Maths.Vector3(1, 1, 0).Normalised, (objId * 24.0f) / 180.0f * Mathf.PI); shape.Scale = new Tes.Maths.Vector3(0.5f, 0.1f, 0.1f); shape.Colour = Tes.Maths.Colour.Cycle(objId).Value; if (_postCreationFunctions.TryGetValue(shape.GetType(), out postCreate)) { postCreate(shape); } server.Create(shape); shapes.Add(shape); } // Now make explicit instantiations. // Tessellate a sphere for mesh tests. List <Vector3> verts = new List <Vector3>(); List <Vector3> normals = new List <Vector3>(); List <int> indices = new List <int>(); Tes.Tessellate.Sphere.SubdivisionSphere(verts, normals, indices, Vector3.zero, 0.42f, 5); shape = CreateMesh(++objId, verts, normals, indices); shape.Position = new Tes.Maths.Vector3((float)objId); server.Create(shape); shapes.Add(shape); _sampleMesh = CreateMeshResource(1u, verts, normals, indices); shape = CreateMeshSet(++objId, _sampleMesh); server.Create(shape); shapes.Add(shape); shape = CreateCloud(++objId, _sampleMesh); server.Create(shape); shapes.Add(shape); shape = CreateText2D(++objId); server.Create(shape); shapes.Add(shape); shape = CreateText3D(++objId); server.Create(shape); shapes.Add(shape); } catch (Exception e) { Debug.LogException(e); return(false); } return(true); }