Skip to content

Instantly share code, notes, and snippets.

@pofulu
pofulu / RandomNext.js
Created April 22, 2019 09:57
隨機從陣列中選取一個元素
function RandomNext(array) {
return array[Math.random() * array.length | 0];
}
@pofulu
pofulu / Open Unity 16-9.scpt
Created April 30, 2019 03:07 — forked from walterpalladino/Open Unity 16-9.scpt
How to Open the Unity Window on Mac OS X on a 16:9 relationship (1280x720)
tell application "Unity" to activate -- needs to be in front
tell application "System Events" to tell application process "Unity"
try
get properties of window 1
set size of window 1 to {1280, 720}
on error errmess
log errmess
-- no window open
end try
end tell
@pofulu
pofulu / alphaMask.js
Last active January 9, 2020 17:06
Alpha mask shader for Spark AR
const Scene = require('Scene');
const Reactive = require('Reactive');
const Textures = require('Textures');
const output = alphaMask(Textures.get('colorTexture'), Textures.get('alphaTexture'))
const plane1 = Scene.root.find('plane1');
plane1.material.setTexture(output, { textureSlotName: 'DIFFUSE' })
function alphaMask(diffuse, alpha, invert) {
/*
const Scene = require('Scene');
const Diagnostics = require('Diagnostics');
const plane0 = Scene.root.find('plane0');
plane0.transform.x = 1;
Diagnostics.log(plane0.transform.x.pinLastValue()); // 0
onSetSignal(plane0.transform.x, Diagnostics.log); // 1
@pofulu
pofulu / KonamiCode.cs
Last active March 4, 2020 11:04
KonamiCode implementation based on UniRx.
// http://www.timegamers.com/threads/friday-update-detecting-the-konami-code-with-unirx.1723/
using UnityEngine;
using UniRx;
using System.Linq;
using System;
public class KonamiCode : MonoBehaviour
{
void Start()
@pofulu
pofulu / WindowsTouchKeyboard.cs
Last active September 11, 2020 02:26
Windows touch keyboard invoker. It works in Unity.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;
public class WindowsTouchKeyboard
{
[DllImport("user32")] static extern IntPtr FindWindow(String sClassName, String sAppName);
[DllImport("user32")] static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
@pofulu
pofulu / audio2spark
Created August 2, 2020 09:22
Audio to Spark AR (AppleScript - JXA)
const FFMPEG_PATH = 'usr/local/bin/ffmpeg';
//––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
const app = Application.currentApplication()
app.includeStandardAdditions = true
function openDocuments(droppedItems) {
const changeExtension = (file, newExtension) => file.substr(0, file.lastIndexOf(".")) + `.${newExtension}`;
@pofulu
pofulu / LocaleModuleExample.js
Last active December 18, 2020 18:01
Spark AR API example: Locale Module
const Locale = require('Locale');
const Diagnostics = require('Diagnostics');
(async function () {
const [language, locale, region] = await Promise.all([
pinInitialValueAsync(Locale.language),
pinInitialValueAsync(Locale.locale),
pinInitialValueAsync(Locale.region),
]);
@pofulu
pofulu / smoothDamp.swift
Created January 25, 2021 10:41 — forked from devindazzle/smoothDamp.swift
The smoothDamp function in Unity implemented using Swift
/**
* Gradually changes a value towards a desired goal over time - implemented from Unity C#
*/
public func smoothDamp(current c: CGFloat, target t: CGFloat, currentVelocity: inout CGFloat, smoothTime time: CGFloat, maxSpeed: CGFloat = CGFloat.infinity, deltaTime: CGFloat) -> CGFloat {
let smoothTime = max(0.0001, time)
let num = 2 / smoothTime
let num2 = num * deltaTime
let num3 = 1 / (1 + num2 + 0.48 * num2 * num2 + 0.235 * num2 * num2 * num2)
var num4 = c - t
let num5 = t
@pofulu
pofulu / IsQuestionMark.cs
Created April 21, 2021 03:13
Unity gist, used to check if the texture is question mark
public static bool IsQuestionMark(Texture2D texture)
{
if (texture.width == 8 && texture.height == 8)
{
using (var md5Provider = new System.Security.Cryptography.MD5CryptoServiceProvider())
{
var raw = texture.GetRawTextureData();
var md5 = md5Provider.ComputeHash(raw);
return System.BitConverter.ToString(md5) == "07-6B-BF-9B-9D-80-03-1E-80-AD-2C-A7-DB-D1-AD-B5";
}