protected virtual void RenderInstances(CameraContext cameraContext, CommandBuffer renderQueue, Mesh mesh, List <Matrix4x4> transforms, List <Matrix4x4> parentTransforms, List <CreateMessage> shapes, Material material) { CategoriesState categories = this.CategoriesState; // Handle instancing block size limits. for (int i = 0; i < transforms.Count; i += _instanceTransforms.Length) { MaterialPropertyBlock materialProperties = new MaterialPropertyBlock(); int itemCount = 0; _instanceColours.Clear(); for (int j = 0; j < _instanceTransforms.Length && j + i < transforms.Count; ++j) { if (categories == null || categories.IsActive(shapes[i + j].Category)) { _instanceTransforms[itemCount] = cameraContext.TesSceneToWorldTransform * parentTransforms[i + j] * transforms[i + j]; Maths.Colour colour = new Maths.Colour(shapes[i + j].Attributes.Colour); _instanceColours.Add(Maths.ColourExt.ToUnityVector4(colour)); ++itemCount; } } if (itemCount > 0) { materialProperties.SetVectorArray("_Color", _instanceColours); renderQueue.DrawMeshInstanced(mesh, 0, material, 0, _instanceTransforms, itemCount, materialProperties); } } }
protected override void RenderInstances(CameraContext cameraContext, CommandBuffer renderQueue, Mesh mesh, List <Matrix4x4> transforms, List <Matrix4x4> parentTransforms, List <CreateMessage> shapes, Material material) { // Work out which mesh set we are rendering from the parent call: solid or wireframe. We could also look at // the first CreateMessage flags. Mesh[] meshes = (shapes.Count > 0 && (shapes[0].Flags & (ushort)ObjectFlag.Wireframe) != 0) ? _wireframeMeshes : _solidMeshes; CategoriesState categories = this.CategoriesState; // Handle instancing block size limits. for (int i = 0; i < transforms.Count; i += _instanceTransforms.Length) { MaterialPropertyBlock materialProperties = new MaterialPropertyBlock(); int itemCount = 0; _instanceColours.Clear(); for (int j = 0; j < _instanceTransforms.Length && j + i < transforms.Count; ++j) { if (categories != null && !categories.IsActive(shapes[i + j].Category)) { continue; } // Build the end cap transforms. Matrix4x4 modelToSceneTransform = cameraContext.TesSceneToWorldTransform * parentTransforms[i + j]; Matrix4x4 transform = transforms[i + j]; // Work out the scaling. We only want to lengthen each line. Vector3 scale = Vector3.zero; scale.x = transform.GetColumn(0).magnitude; scale.y = transform.GetColumn(1).magnitude; scale.z = transform.GetColumn(2).magnitude; // Remove scaling. transform.SetColumn(0, transform.GetColumn(0) / scale.x); transform.SetColumn(1, transform.GetColumn(1) / scale.y); transform.SetColumn(2, transform.GetColumn(2) / scale.z); // Scale each mesh. Matrix4x4 transform2 = transform; transform2.SetColumn(0, transform2.GetColumn(0) * scale.x); _instanceTransforms[itemCount] = modelToSceneTransform * transform2; transform2 = transform; transform2.SetColumn(1, transform2.GetColumn(1) * scale.y); _axis1Transforms[itemCount] = modelToSceneTransform * transform2; transform2 = transform; transform2.SetColumn(2, transform2.GetColumn(2) * scale.z); _axis2Transforms[itemCount] = modelToSceneTransform * transform2; Maths.Colour colour = new Maths.Colour(shapes[i + j].Attributes.Colour); _instanceColours.Add(Maths.ColourExt.ToUnityVector4(colour)); ++itemCount; } if (itemCount > 0) { materialProperties.SetVectorArray("_Color", _instanceColours); // Render body. renderQueue.DrawMeshInstanced(meshes[0], 0, material, 0, _instanceTransforms, itemCount, materialProperties); // Render end caps. renderQueue.DrawMeshInstanced(meshes[1], 0, material, 0, _axis1Transforms, itemCount, materialProperties); renderQueue.DrawMeshInstanced(meshes[2], 0, material, 0, _axis2Transforms, itemCount, materialProperties); } } }
protected override void RenderInstances(CameraContext cameraContext, CommandBuffer renderQueue, Mesh mesh, List <Matrix4x4> transforms, List <Matrix4x4> parentTransforms, List <CreateMessage> shapes, Material material) { // Work out which mesh set we are rendering from the parent call: solid or wireframe. We could also look at // the first CreateMessage flags. Mesh[] meshes = (shapes.Count > 0 && (shapes[0].Flags & (ushort)ObjectFlag.Wireframe) != 0) ? _wireframeMeshes : _solidMeshes; CategoriesState categories = this.CategoriesState; // Handle instancing block size limits. for (int i = 0; i < transforms.Count; i += _instanceTransforms.Length) { MaterialPropertyBlock materialProperties = new MaterialPropertyBlock(); int itemCount = 0; _instanceColours.Clear(); for (int j = 0; j < _instanceTransforms.Length && j + i < transforms.Count; ++j) { if (categories != null && !categories.IsActive(shapes[i + j].Category)) { continue; } // Build the end cap transforms. Matrix4x4 modelToSceneTransform = cameraContext.TesSceneToWorldTransform * parentTransforms[i + j]; Matrix4x4 transform = transforms[i + j]; _instanceTransforms[itemCount] = modelToSceneTransform * transform; // Extract radius and length to position the end caps. float radius = transform.GetColumn(0).magnitude; Vector4 zAxis = transform.GetColumn(2); float length = zAxis.magnitude; zAxis *= 1.0f / (length != 0 ? length : 1.0f); // Scale the length axis to match the other two as the end caps are spheres. transform.SetColumn(2, zAxis * radius); // Adjust position for the first end cap. Vector4 tAxis = transform.GetColumn(3); tAxis += -0.5f * length * zAxis; transform.SetColumn(3, tAxis); _cap1Transforms[j] = modelToSceneTransform * transform; // Adjust position for the second end cap. tAxis += length * zAxis; transform.SetColumn(3, tAxis); _cap2Transforms[j] = modelToSceneTransform * transform; Maths.Colour colour = new Maths.Colour(shapes[i + j].Attributes.Colour); _instanceColours.Add(Maths.ColourExt.ToUnityVector4(colour)); ++itemCount; } if (itemCount > 0) { materialProperties.SetVectorArray("_Color", _instanceColours); // Render body. renderQueue.DrawMeshInstanced(meshes[0], 0, material, 0, _instanceTransforms, itemCount, materialProperties); // Render end caps. renderQueue.DrawMeshInstanced(meshes[1], 0, material, 0, _cap1Transforms, itemCount, materialProperties); renderQueue.DrawMeshInstanced(meshes[2], 0, material, 0, _cap2Transforms, itemCount, materialProperties); } } }