Skip to content

Instantly share code, notes, and snippets.

@JohannesMP
JohannesMP / FileStreamReadLineExtension.cs
Last active November 7, 2019 12:40
A ReadLine extension for FileStream objects
using System.IO;
using System.Collections.Generic;
public static class FileStreamReadLineExtension
{
public static string ReadLine(this FileStream fs)
{
int curChar;
// EOF - return null
if ((curChar = fs.ReadByte()) == -1)
@JohannesMP
JohannesMP / Texture2DTGAExtension.cs
Last active March 26, 2024 05:11
[Unity3d] Add TGA Encoding to Texture2D
using UnityEngine;
// References:
// Quick and Dirty Writeable Bitmap as TGA image: http://nokola.com/blog/post/2010/01/21/Quick-and-Dirty-Output-of-WriteableBitmap-as-TGA-Image.aspx
// TGLoader: https://gist.github.com/mikezila/10557162
// Truevision TGA Specification on Wikipedia: https://en.wikipedia.org/wiki/Truevision_TGA#Technical_details
public static class Texture2DTGAExtension
{
public enum Channel { R, G, B, A, White, Black, Gray }
@JohannesMP
JohannesMP / ExtractMeshToFBX.cs
Last active January 6, 2024 07:22
Using the Unity FBX SDK to save a single Mesh as an FBX. Uses Unity FBX SDK from: https://assetstore.unity.com/packages/essentials/101408
using System.IO;
using UnityEngine;
using UnityEditor;
using Unity.FbxSdk;
using FbxExporters.Editor;
// Place in 'Editor' folder
public static class ExtractMeshToFBX
{
// true: fbx file is easy-to-debug ascii, false: fbx file is binary.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Testing
{
/// <summary>
@JohannesMP
JohannesMP / ClosestPointOnEllipse.cs
Last active March 28, 2023 10:13
[C#, Unity3D] Quickly find the closest point on an ellipse centered on the origin.
using UnityEngine;
using Math = System.Math;
public static class NearestPointTo
{
/// <summary>
/// Find the closest point on an ellipse centered on the origin.
/// </summary>
/// Credit:
/// 2015-09 Original Paper by Luc. Maisonobe https://www.spaceroots.org/documents/distance/distance-to-ellipse.pdf
@JohannesMP
JohannesMP / AttributeDemo.cs
Last active July 16, 2018 17:52
Conveniently extracting data of members on an object that were tagged with a given Attribute, with Reflection caching.
using UnityEngine;
using UnityEditor;
using Utils.Reflection;
using System.Linq;
public class AttributeDemo
{
/// <summary>
/// A quick demo for grabing fields on an object that were tagged with a given Attribute.
/// In this case an ‘Order’ attribute is used to display the tagged fields in a specific order.
@JohannesMP
JohannesMP / 2DLineRectIntersection.md
Last active April 18, 2024 00:53
[C#, Unity3D] An optimized solver for intersection between a Line Segment and a Rect in 2D space with spacial partitioning.

Overview

For a 2D Unity Project, I needed a really efficient way to get the intersection points between a line segment (as defined by two Vector2) and an axis-aligned Rect in 2D space, where the intersection points are returned as a parametric fraction of the length of the original line segment:

Note how when the line intersects the rect, the green line represents the portion inside the rect, and the parametric representation of the point of intersection along the line is displayed

In my use case the vast majority of lines are either completely inside or completely outside the rect, and so I needed an approach that was very efficient in cases where there is guaranteed no intersection and avoids unecessary raycasts when possible.

@JohannesMP
JohannesMP / FlyCamera.cs
Last active February 17, 2023 14:10 — forked from RyanBreaker/FlyCamera.cs
Unity Script to give camera WASD + mouse control
using UnityEngine;
using System.Collections;
public class FlyCamera : MonoBehaviour
{
/*
Writen by Windexglow 11-13-10. Use it, edit it, steal it I don't care.
Converted to C# 27-02-13 - no credit wanted.
Reformatted and cleaned by Ryan Breaker 23-6-18
Added Right click for camera by JohannesMP 6-7-18
// 2018-02-22
#define METHOD 3
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target)
{
int n = nums.size();
@JohannesMP
JohannesMP / file1.txt
Last active April 8, 2018 17:21
Multifile Testing
1 contents