Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore.coffee/Utils.coffee
4626 views
1
###
2
Various useful methods
3
4
@class bkcore.Utils
5
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
6
###
7
class Utils
8
9
###
10
Creates a bkcore.threejs.Shaders["normalV"|"normal"] material
11
with given parameters
12
###
13
@createNormalMaterial = (opts)->
14
15
opts ?= {}
16
opts.ambient ?= 0x444444
17
opts.normalScale ?= 1.0
18
opts.reflectivity ?= 0.9
19
opts.shininess ?= 42
20
opts.metal ?= false
21
22
shadername = if opts.perPixel then "normalV" else "normal"
23
shader = bkcore.threejs.Shaders[shadername]
24
uniforms = THREE.UniformsUtils.clone(shader.uniforms)
25
26
uniforms["enableDiffuse"].value = true
27
uniforms["enableSpecular"].value = true
28
uniforms["enableReflection"].value = !!opts.cube
29
uniforms["tNormal"].texture = opts.normal
30
uniforms["tDiffuse"].texture = opts.diffuse
31
uniforms["tSpecular"].texture = opts.specular
32
uniforms["uAmbientColor"].value.setHex(opts.ambient)
33
uniforms["uAmbientColor"].value.convertGammaToLinear()
34
uniforms["uNormalScale"].value = opts.normalScale
35
36
if opts.cube?
37
uniforms["tCube"].texture = opts.cube
38
uniforms["uReflectivity"].value = opts.reflectivity
39
40
parameters = {
41
fragmentShader: shader.fragmentShader
42
vertexShader: shader.vertexShader
43
uniforms: uniforms
44
lights: true
45
fog: false
46
}
47
48
material = new THREE.ShaderMaterial(parameters)
49
material.perPixel = true
50
material.metal = opts.metal
51
52
return material
53
54
###
55
Projects an object origin vector to screen using given camera
56
@param THREE.Object3D object The object which origin you want to project
57
@param THREE.Camera camera The camera of the projection
58
@return THEE.Vector3 Projected verctor
59
###
60
@projectOnScreen: (object, camera)->
61
62
mat = new THREE.Matrix4()
63
mat.multiply(camera.matrixWorldInverse, object.matrixWorld)
64
mat.multiply(camera.projectionMatrix , mat)
65
66
c = mat.n44
67
lPos = new THREE.Vector3(mat.n14/c, mat.n24/c, mat.n34/c)
68
return lPos.multiplyScalar(0.5).addScalar(0.5)
69
70
###
71
Get an url parameter
72
@param String name Parameter slug
73
@return Mixed
74
###
75
@URLParameters: null
76
@getURLParameter: (name)->
77
78
if !@URLParameters?
79
@URLParameters = {}
80
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, (m, key, val)=>
81
@URLParameters[key] = val
82
)
83
84
return @URLParameters[name]
85
86
###
87
Get top offset of an element
88
@param obj HTMLElement
89
###
90
@getOffsetTop: (obj)->
91
92
curtop = obj.offsetTop
93
94
if obj.offsetParent
95
while obj = obj.offsetParent
96
curtop += obj.offsetTop
97
98
return curtop
99
100
###
101
Scrolls page to given element id
102
@param string id The ID of the element
103
###
104
@scrollTo: (id)->
105
106
window.scroll(0, @getOffsetTop(document.getElementById(id)))
107
108
###
109
Add or remove a class from an element
110
@param string id [description]
111
@param string cssclass [description]
112
@param bool active [description]
113
###
114
@updateClass: (id, cssclass, active)->
115
116
e = document.getElementById(id)
117
return unless e?
118
119
if active
120
e.classList.add(cssclass)
121
else
122
e.classList.remove(cssclass)
123
124
###
125
Performs an XMLHttpRequest
126
@param string url [description]
127
@param bool postData true = POST, false = GET
128
@param {Function} callback [description]
129
@param {Object} data [description]
130
###
131
@request: (url, postData, callback, data)->
132
133
XMLHttpFactories = [
134
()-> return new XMLHttpRequest()
135
()-> return new ActiveXObject("Msxml2.XMLHTTP")
136
()-> return new ActiveXObject("Msxml3.XMLHTTP")
137
()-> return new ActiveXObject("Microsoft.XMLHTTP")
138
]
139
140
createXMLHTTPObject = () ->
141
142
xmlhttp = false
143
144
for i in [0..XMLHttpFactories.length]
145
try
146
xmlhttp = XMLHttpFactories[i]()
147
catch e
148
continue
149
break
150
151
return xmlhttp
152
153
req = createXMLHTTPObject()
154
return unless req?
155
156
method = if postData? then "POST" else "GET"
157
158
qdata = "o=bk"
159
if data?
160
for i, val of data
161
qdata += "&"+i+"="+val
162
url += "?"+qdata if postData?
163
164
req.open(method,url,true)
165
166
if postData?
167
req.setRequestHeader('Content-type','application/x-www-form-urlencoded')
168
169
req.onreadystatechange = () ->
170
return unless req.readyState is 4
171
return unless req.status is 200 or req.status is 304
172
callback?(req)
173
174
req.send(qdata)
175
176
return req
177
178
###
179
Checks whether the device supports Touch input
180
###
181
@isTouchDevice: ()->
182
183
return ('ontouchstart' of window) or
184
(navigator.MaxTouchPoints > 0) or
185
(navigator.msMaxTouchPoints > 0)
186
187
###
188
Exports
189
@package bkcore
190
###
191
exports = exports ? @
192
exports.bkcore ||= {}
193
exports.bkcore.Utils = Utils
194